SHOW:
|
|
- or go back to the newest paste.
1 | if(!class_exists('Refactord_add_rewrite_rules')): | |
2 | class Refactord_add_rewrite_rules { | |
3 | ||
4 | var $query_vars = array(); | |
5 | var $rules = array(); | |
6 | - | var $show_rules = true; //used for debugging rewrite rules |
6 | + | var $show_rules = false; //used for debugging rewrite rules |
7 | var $show_query_vars = false; //used for debugging query vars | |
8 | ||
9 | function __construct($options = NULL){ | |
10 | if(!is_null($options)){ | |
11 | $this->init($options); | |
12 | } | |
13 | } | |
14 | ||
15 | function init($options){ | |
16 | foreach($options as $key => $value){ | |
17 | $this->$key = $value; | |
18 | } | |
19 | ||
20 | if(!empty($this->rules)){ | |
21 | add_action('wp_head', array(&$this, 'flush_rules')); | |
22 | add_action('generate_rewrite_rules', array(&$this, 'add_rules')); | |
23 | } | |
24 | ||
25 | if(!empty($this->query_vars)){ | |
26 | add_filter('query_vars', array(&$this, 'add_query_vars')); | |
27 | } | |
28 | ||
29 | if($this->show_rules){ | |
30 | add_action('wp_footer', array(&$this, 'show_rules'), 1); | |
31 | } | |
32 | ||
33 | if($this->show_query_vars){ | |
34 | add_action('wp_footer', array(&$this, 'show_query_vars'), 1); | |
35 | } | |
36 | } | |
37 | ||
38 | function add_query_vars($query_vars){ | |
39 | foreach($this->query_vars as $var){ | |
40 | $query_vars[] = $var; | |
41 | } | |
42 | return $query_vars; | |
43 | } | |
44 | ||
45 | function add_rules(){ | |
46 | global $wp_rewrite; | |
47 | $wp_rewrite->rules = $this->rules + $wp_rewrite->rules; | |
48 | } | |
49 | ||
50 | function rules_exist(){ | |
51 | global $wp_rewrite; | |
52 | ||
53 | foreach($this->rules as $key => $rule){ | |
54 | if(!in_array($rule, $wp_rewrite->rules) || !key_exists($key, $wp_rewrite->rules)){ | |
55 | return FALSE; | |
56 | } | |
57 | } | |
58 | return TRUE; | |
59 | } | |
60 | ||
61 | function flush_rules(){ | |
62 | global $wp_rewrite; | |
63 | if(!$this->rules_exist()){ | |
64 | $wp_rewrite->flush_rules(); | |
65 | } | |
66 | } | |
67 | ||
68 | function show_rules(){ | |
69 | global $wp_rewrite; | |
70 | ||
71 | echo "<pre>"; | |
72 | print_r($wp_rewrite->rules); | |
73 | echo "</pre>"; | |
74 | } | |
75 | ||
76 | function show_query_vars(){ | |
77 | global $wp_query; | |
78 | ||
79 | echo "<pre>"; | |
80 | print_r($wp_query->query_vars); | |
81 | echo "</pre>"; | |
82 | } | |
83 | } | |
84 | endif; | |
85 | ||
86 | ||
87 | ||
88 | $options = array( | |
89 | 'rules' => array( | |
90 | 'blah/?$' => 'index.php?blah=blah'), | |
91 | 'query_vars' => array('blah') | |
92 | ); | |
93 | $add_rewrite_rules = new Refactord_add_rewrite_rules($options); | |
94 | ||
95 | ||
96 | add_action('template_include', 'load_blah_template'); | |
97 | function load_blah_template($template) { | |
98 | $blah_query_var = get_query_var('blah'); | |
99 | if($blah_query_var) { | |
100 | $template = locate_template(array('blah.php' )); | |
101 | } | |
102 | return $template; | |
103 | } |