author | Anthony Ly <anthonyly.com@gmail.com> |
Wed, 19 Dec 2012 17:50:20 -0800 | |
changeset 205 | a4f7897e21a9 |
parent 204 | 09a1c134465b |
permissions | -rw-r--r-- |
136 | 1 |
<?php |
2 |
/** |
|
3 |
* WordPress Rewrite API |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage Rewrite |
|
7 |
*/ |
|
8 |
||
9 |
/** |
|
10 |
* Add a straight rewrite rule. |
|
11 |
* |
|
12 |
* @see WP_Rewrite::add_rule() for long description. |
|
13 |
* @since 2.1.0 |
|
14 |
* |
|
15 |
* @param string $regex Regular Expression to match request against. |
|
16 |
* @param string $redirect Page to redirect to. |
|
17 |
* @param string $after Optional, default is 'bottom'. Where to add rule, can also be 'top'. |
|
18 |
*/ |
|
19 |
function add_rewrite_rule($regex, $redirect, $after = 'bottom') { |
|
20 |
global $wp_rewrite; |
|
21 |
$wp_rewrite->add_rule($regex, $redirect, $after); |
|
22 |
} |
|
23 |
||
24 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
25 |
* Add a new rewrite tag (like %postname%). |
136 | 26 |
* |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
27 |
* The $query parameter is optional. If it is omitted you must ensure that |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
28 |
* you call this on, or before, the 'init' hook. This is because $query defaults |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
29 |
* to "$tag=", and for this to work a new query var has to be added. |
136 | 30 |
* |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
31 |
* @see WP_Rewrite::add_rewrite_tag() |
136 | 32 |
* @since 2.1.0 |
33 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
34 |
* @param string $tag Name of the new rewrite tag. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
35 |
* @param string $regex Regular expression to substitute the tag for in rewrite rules. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
36 |
* @param string $query String to append to the rewritten query. Must end in '='. Optional. |
136 | 37 |
*/ |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
38 |
function add_rewrite_tag( $tag, $regex, $query = '' ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
39 |
// validate the tag's name |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
40 |
if ( strlen( $tag ) < 3 || $tag[0] != '%' || $tag[ strlen($tag) - 1 ] != '%' ) |
136 | 41 |
return; |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
42 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
43 |
global $wp_rewrite, $wp; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
44 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
45 |
if ( empty( $query ) ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
46 |
$qv = trim( $tag, '%' ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
47 |
$wp->add_query_var( $qv ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
48 |
$query = $qv . '='; |
136 | 49 |
} |
50 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
51 |
$wp_rewrite->add_rewrite_tag( $tag, $regex, $query ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
52 |
} |
136 | 53 |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
54 |
/** |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
55 |
* Add permalink structure. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
56 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
57 |
* @see WP_Rewrite::add_permastruct() |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
58 |
* @since 3.0.0 |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
59 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
60 |
* @param string $name Name for permalink structure. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
61 |
* @param string $struct Permalink structure. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
62 |
* @param array $args Optional configuration for building the rules from the permalink structure, |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
63 |
* see {@link WP_Rewrite::add_permastruct()} for full details. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
64 |
*/ |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
65 |
function add_permastruct( $name, $struct, $args = array() ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
66 |
global $wp_rewrite; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
67 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
68 |
// backwards compatibility for the old parameters: $with_front and $ep_mask |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
69 |
if ( ! is_array( $args ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
70 |
$args = array( 'with_front' => $args ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
71 |
if ( func_num_args() == 4 ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
72 |
$args['ep_mask'] = func_get_arg( 3 ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
73 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
74 |
return $wp_rewrite->add_permastruct( $name, $struct, $args ); |
136 | 75 |
} |
76 |
||
77 |
/** |
|
78 |
* Add a new feed type like /atom1/. |
|
79 |
* |
|
80 |
* @since 2.1.0 |
|
81 |
* |
|
82 |
* @param string $feedname |
|
83 |
* @param callback $function Callback to run on feed display. |
|
84 |
* @return string Feed action name. |
|
85 |
*/ |
|
86 |
function add_feed($feedname, $function) { |
|
87 |
global $wp_rewrite; |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
88 |
if ( ! in_array($feedname, $wp_rewrite->feeds) ) //override the file if it is |
136 | 89 |
$wp_rewrite->feeds[] = $feedname; |
90 |
$hook = 'do_feed_' . $feedname; |
|
91 |
// Remove default function hook |
|
92 |
remove_action($hook, $hook, 10, 1); |
|
93 |
add_action($hook, $function, 10, 1); |
|
94 |
return $hook; |
|
95 |
} |
|
96 |
||
97 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
98 |
* Remove rewrite rules and then recreate rewrite rules. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
99 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
100 |
* @see WP_Rewrite::flush_rules() |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
101 |
* @since 3.0.0 |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
102 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
103 |
* @param bool $hard Whether to update .htaccess (hard flush) or just update |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
104 |
* rewrite_rules transient (soft flush). Default is true (hard). |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
105 |
*/ |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
106 |
function flush_rewrite_rules( $hard = true ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
107 |
global $wp_rewrite; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
108 |
$wp_rewrite->flush_rules( $hard ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
109 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
110 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
111 |
/** |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
112 |
* Endpoint Mask for default, which is nothing. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
113 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
114 |
* @since 2.1.0 |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
115 |
*/ |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
116 |
define('EP_NONE', 0); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
117 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
118 |
/** |
136 | 119 |
* Endpoint Mask for Permalink. |
120 |
* |
|
121 |
* @since 2.1.0 |
|
122 |
*/ |
|
123 |
define('EP_PERMALINK', 1); |
|
124 |
||
125 |
/** |
|
126 |
* Endpoint Mask for Attachment. |
|
127 |
* |
|
128 |
* @since 2.1.0 |
|
129 |
*/ |
|
130 |
define('EP_ATTACHMENT', 2); |
|
131 |
||
132 |
/** |
|
133 |
* Endpoint Mask for date. |
|
134 |
* |
|
135 |
* @since 2.1.0 |
|
136 |
*/ |
|
137 |
define('EP_DATE', 4); |
|
138 |
||
139 |
/** |
|
140 |
* Endpoint Mask for year |
|
141 |
* |
|
142 |
* @since 2.1.0 |
|
143 |
*/ |
|
144 |
define('EP_YEAR', 8); |
|
145 |
||
146 |
/** |
|
147 |
* Endpoint Mask for month. |
|
148 |
* |
|
149 |
* @since 2.1.0 |
|
150 |
*/ |
|
151 |
define('EP_MONTH', 16); |
|
152 |
||
153 |
/** |
|
154 |
* Endpoint Mask for day. |
|
155 |
* |
|
156 |
* @since 2.1.0 |
|
157 |
*/ |
|
158 |
define('EP_DAY', 32); |
|
159 |
||
160 |
/** |
|
161 |
* Endpoint Mask for root. |
|
162 |
* |
|
163 |
* @since 2.1.0 |
|
164 |
*/ |
|
165 |
define('EP_ROOT', 64); |
|
166 |
||
167 |
/** |
|
168 |
* Endpoint Mask for comments. |
|
169 |
* |
|
170 |
* @since 2.1.0 |
|
171 |
*/ |
|
172 |
define('EP_COMMENTS', 128); |
|
173 |
||
174 |
/** |
|
175 |
* Endpoint Mask for searches. |
|
176 |
* |
|
177 |
* @since 2.1.0 |
|
178 |
*/ |
|
179 |
define('EP_SEARCH', 256); |
|
180 |
||
181 |
/** |
|
182 |
* Endpoint Mask for categories. |
|
183 |
* |
|
184 |
* @since 2.1.0 |
|
185 |
*/ |
|
186 |
define('EP_CATEGORIES', 512); |
|
187 |
||
188 |
/** |
|
189 |
* Endpoint Mask for tags. |
|
190 |
* |
|
191 |
* @since 2.3.0 |
|
192 |
*/ |
|
193 |
define('EP_TAGS', 1024); |
|
194 |
||
195 |
/** |
|
196 |
* Endpoint Mask for authors. |
|
197 |
* |
|
198 |
* @since 2.1.0 |
|
199 |
*/ |
|
200 |
define('EP_AUTHORS', 2048); |
|
201 |
||
202 |
/** |
|
203 |
* Endpoint Mask for pages. |
|
204 |
* |
|
205 |
* @since 2.1.0 |
|
206 |
*/ |
|
207 |
define('EP_PAGES', 4096); |
|
208 |
||
209 |
/** |
|
210 |
* Endpoint Mask for everything. |
|
211 |
* |
|
212 |
* @since 2.1.0 |
|
213 |
*/ |
|
214 |
define('EP_ALL', 8191); |
|
215 |
||
216 |
/** |
|
217 |
* Add an endpoint, like /trackback/. |
|
218 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
219 |
* Adding an endpoint creates extra rewrite rules for each of the matching |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
220 |
* places specified by the provided bitmask. For example: |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
221 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
222 |
* <code> |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
223 |
* add_rewrite_endpoint( 'json', EP_PERMALINK | EP_PAGES ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
224 |
* </code> |
136 | 225 |
* |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
226 |
* will add a new rewrite rule ending with "json(/(.*))?/?$" for every permastruct |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
227 |
* that describes a permalink (post) or page. This is rewritten to "json=$match" |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
228 |
* where $match is the part of the URL matched by the endpoint regex (e.g. "foo" in |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
229 |
* "<permalink>/json/foo/"). |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
230 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
231 |
* A new query var with the same name as the endpoint will also be created. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
232 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
233 |
* When specifying $places ensure that you are using the EP_* constants (or a |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
234 |
* combination of them using the bitwise OR operator) as their values are not |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
235 |
* guaranteed to remain static (especially EP_ALL). |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
236 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
237 |
* Be sure to flush the rewrite rules - flush_rewrite_rules() - when your plugin gets |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
238 |
* activated and deactivated. |
136 | 239 |
* |
240 |
* @since 2.1.0 |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
241 |
* @see WP_Rewrite::add_endpoint() |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
242 |
* @global object $wp_rewrite |
136 | 243 |
* |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
244 |
* @param string $name Name of the endpoint. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
245 |
* @param int $places Endpoint mask describing the places the endpoint should be added. |
136 | 246 |
*/ |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
247 |
function add_rewrite_endpoint( $name, $places ) { |
136 | 248 |
global $wp_rewrite; |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
249 |
$wp_rewrite->add_endpoint( $name, $places ); |
136 | 250 |
} |
251 |
||
252 |
/** |
|
253 |
* Filter the URL base for taxonomies. |
|
254 |
* |
|
255 |
* To remove any manually prepended /index.php/. |
|
256 |
* |
|
257 |
* @access private |
|
258 |
* @since 2.6.0 |
|
259 |
* |
|
260 |
* @param string $base The taxonomy base that we're going to filter |
|
261 |
* @return string |
|
262 |
*/ |
|
263 |
function _wp_filter_taxonomy_base( $base ) { |
|
264 |
if ( !empty( $base ) ) { |
|
265 |
$base = preg_replace( '|^/index\.php/|', '', $base ); |
|
266 |
$base = trim( $base, '/' ); |
|
267 |
} |
|
268 |
return $base; |
|
269 |
} |
|
270 |
||
271 |
/** |
|
272 |
* Examine a url and try to determine the post ID it represents. |
|
273 |
* |
|
274 |
* Checks are supposedly from the hosted site blog. |
|
275 |
* |
|
276 |
* @since 1.0.0 |
|
277 |
* |
|
278 |
* @param string $url Permalink to check. |
|
279 |
* @return int Post ID, or 0 on failure. |
|
280 |
*/ |
|
281 |
function url_to_postid($url) { |
|
282 |
global $wp_rewrite; |
|
283 |
||
284 |
$url = apply_filters('url_to_postid', $url); |
|
285 |
||
286 |
// First, check to see if there is a 'p=N' or 'page_id=N' to match against |
|
287 |
if ( preg_match('#[?&](p|page_id|attachment_id)=(\d+)#', $url, $values) ) { |
|
288 |
$id = absint($values[2]); |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
289 |
if ( $id ) |
136 | 290 |
return $id; |
291 |
} |
|
292 |
||
293 |
// Check to see if we are using rewrite rules |
|
294 |
$rewrite = $wp_rewrite->wp_rewrite_rules(); |
|
295 |
||
296 |
// Not using rewrite rules, and 'p=N' and 'page_id=N' methods failed, so we're out of options |
|
297 |
if ( empty($rewrite) ) |
|
298 |
return 0; |
|
299 |
||
300 |
// Get rid of the #anchor |
|
301 |
$url_split = explode('#', $url); |
|
302 |
$url = $url_split[0]; |
|
303 |
||
304 |
// Get rid of URL ?query=string |
|
305 |
$url_split = explode('?', $url); |
|
306 |
$url = $url_split[0]; |
|
307 |
||
308 |
// Add 'www.' if it is absent and should be there |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
309 |
if ( false !== strpos(home_url(), '://www.') && false === strpos($url, '://www.') ) |
136 | 310 |
$url = str_replace('://', '://www.', $url); |
311 |
||
312 |
// Strip 'www.' if it is present and shouldn't be |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
313 |
if ( false === strpos(home_url(), '://www.') ) |
136 | 314 |
$url = str_replace('://www.', '://', $url); |
315 |
||
316 |
// Strip 'index.php/' if we're not using path info permalinks |
|
317 |
if ( !$wp_rewrite->using_index_permalinks() ) |
|
318 |
$url = str_replace('index.php/', '', $url); |
|
319 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
320 |
if ( false !== strpos($url, home_url()) ) { |
136 | 321 |
// Chop off http://domain.com |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
322 |
$url = str_replace(home_url(), '', $url); |
136 | 323 |
} else { |
324 |
// Chop off /path/to/blog |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
325 |
$home_path = parse_url(home_url()); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
326 |
$home_path = isset( $home_path['path'] ) ? $home_path['path'] : '' ; |
136 | 327 |
$url = str_replace($home_path, '', $url); |
328 |
} |
|
329 |
||
330 |
// Trim leading and lagging slashes |
|
331 |
$url = trim($url, '/'); |
|
332 |
||
333 |
$request = $url; |
|
334 |
||
335 |
// Look for matches. |
|
336 |
$request_match = $request; |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
337 |
foreach ( (array)$rewrite as $match => $query) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
338 |
|
136 | 339 |
// If the requesting file is the anchor of the match, prepend it |
340 |
// to the path info. |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
341 |
if ( !empty($url) && ($url != $request) && (strpos($match, $url) === 0) ) |
136 | 342 |
$request_match = $url . '/' . $request; |
343 |
||
344 |
if ( preg_match("!^$match!", $request_match, $matches) ) { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
345 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
346 |
if ( $wp_rewrite->use_verbose_page_rules && preg_match( '/pagename=\$matches\[([0-9]+)\]/', $query, $varmatch ) ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
347 |
// this is a verbose page match, lets check to be sure about it |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
348 |
if ( ! get_page_by_path( $matches[ $varmatch[1] ] ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
349 |
continue; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
350 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
351 |
|
136 | 352 |
// Got a match. |
353 |
// Trim the query of everything up to the '?'. |
|
354 |
$query = preg_replace("!^.+\?!", '', $query); |
|
355 |
||
356 |
// Substitute the substring matches into the query. |
|
357 |
$query = addslashes(WP_MatchesMapRegex::apply($query, $matches)); |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
358 |
|
136 | 359 |
// Filter out non-public query vars |
360 |
global $wp; |
|
361 |
parse_str($query, $query_vars); |
|
362 |
$query = array(); |
|
363 |
foreach ( (array) $query_vars as $key => $value ) { |
|
364 |
if ( in_array($key, $wp->public_query_vars) ) |
|
365 |
$query[$key] = $value; |
|
366 |
} |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
367 |
|
136 | 368 |
// Do the query |
369 |
$query = new WP_Query($query); |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
370 |
if ( !empty($query->posts) && $query->is_singular ) |
136 | 371 |
return $query->post->ID; |
372 |
else |
|
373 |
return 0; |
|
374 |
} |
|
375 |
} |
|
376 |
return 0; |
|
377 |
} |
|
378 |
||
379 |
/** |
|
380 |
* WordPress Rewrite Component. |
|
381 |
* |
|
382 |
* The WordPress Rewrite class writes the rewrite module rules to the .htaccess |
|
383 |
* file. It also handles parsing the request to get the correct setup for the |
|
384 |
* WordPress Query class. |
|
385 |
* |
|
386 |
* The Rewrite along with WP class function as a front controller for WordPress. |
|
387 |
* You can add rules to trigger your page view and processing using this |
|
388 |
* component. The full functionality of a front controller does not exist, |
|
389 |
* meaning you can't define how the template files load based on the rewrite |
|
390 |
* rules. |
|
391 |
* |
|
392 |
* @since 1.5.0 |
|
393 |
*/ |
|
394 |
class WP_Rewrite { |
|
395 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
396 |
* Permalink structure for posts. |
136 | 397 |
* |
398 |
* @since 1.5.0 |
|
399 |
* @access private |
|
400 |
* @var string |
|
401 |
*/ |
|
402 |
var $permalink_structure; |
|
403 |
||
404 |
/** |
|
405 |
* Whether to add trailing slashes. |
|
406 |
* |
|
407 |
* @since 2.2.0 |
|
408 |
* @access private |
|
409 |
* @var bool |
|
410 |
*/ |
|
411 |
var $use_trailing_slashes; |
|
412 |
||
413 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
414 |
* Base for the author permalink structure (example.com/$author_base/authorname). |
136 | 415 |
* |
416 |
* @since 1.5.0 |
|
417 |
* @access private |
|
418 |
* @var string |
|
419 |
*/ |
|
420 |
var $author_base = 'author'; |
|
421 |
||
422 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
423 |
* Permalink structure for author archives. |
136 | 424 |
* |
425 |
* @since 1.5.0 |
|
426 |
* @access private |
|
427 |
* @var string |
|
428 |
*/ |
|
429 |
var $author_structure; |
|
430 |
||
431 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
432 |
* Permalink structure for date archives. |
136 | 433 |
* |
434 |
* @since 1.5.0 |
|
435 |
* @access private |
|
436 |
* @var string |
|
437 |
*/ |
|
438 |
var $date_structure; |
|
439 |
||
440 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
441 |
* Permalink structure for pages. |
136 | 442 |
* |
443 |
* @since 1.5.0 |
|
444 |
* @access private |
|
445 |
* @var string |
|
446 |
*/ |
|
447 |
var $page_structure; |
|
448 |
||
449 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
450 |
* Base of the search permalink structure (example.com/$search_base/query). |
136 | 451 |
* |
452 |
* @since 1.5.0 |
|
453 |
* @access private |
|
454 |
* @var string |
|
455 |
*/ |
|
456 |
var $search_base = 'search'; |
|
457 |
||
458 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
459 |
* Permalink structure for searches. |
136 | 460 |
* |
461 |
* @since 1.5.0 |
|
462 |
* @access private |
|
463 |
* @var string |
|
464 |
*/ |
|
465 |
var $search_structure; |
|
466 |
||
467 |
/** |
|
468 |
* Comments permalink base. |
|
469 |
* |
|
470 |
* @since 1.5.0 |
|
471 |
* @access private |
|
472 |
* @var string |
|
473 |
*/ |
|
474 |
var $comments_base = 'comments'; |
|
475 |
||
476 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
477 |
* Pagination permalink base. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
478 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
479 |
* @since 3.1.0 |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
480 |
* @access private |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
481 |
* @var string |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
482 |
*/ |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
483 |
var $pagination_base = 'page'; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
484 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
485 |
/** |
136 | 486 |
* Feed permalink base. |
487 |
* |
|
488 |
* @since 1.5.0 |
|
489 |
* @access private |
|
490 |
* @var string |
|
491 |
*/ |
|
492 |
var $feed_base = 'feed'; |
|
493 |
||
494 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
495 |
* Comments feed permalink structure. |
136 | 496 |
* |
497 |
* @since 1.5.0 |
|
498 |
* @access private |
|
499 |
* @var string |
|
500 |
*/ |
|
501 |
var $comments_feed_structure; |
|
502 |
||
503 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
504 |
* Feed request permalink structure. |
136 | 505 |
* |
506 |
* @since 1.5.0 |
|
507 |
* @access private |
|
508 |
* @var string |
|
509 |
*/ |
|
510 |
var $feed_structure; |
|
511 |
||
512 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
513 |
* The static portion of the post permalink structure. |
136 | 514 |
* |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
515 |
* If the permalink structure is "/archive/%post_id%" then the front |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
516 |
* is "/archive/". If the permalink structure is "/%year%/%postname%/" |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
517 |
* then the front is "/". |
136 | 518 |
* |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
519 |
* @see WP_Rewrite::init() |
136 | 520 |
* @since 1.5.0 |
521 |
* @access private |
|
522 |
* @var string |
|
523 |
*/ |
|
524 |
var $front; |
|
525 |
||
526 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
527 |
* The prefix for all permalink structures. |
136 | 528 |
* |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
529 |
* If PATHINFO/index permalinks are in use then the root is the value of |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
530 |
* {@link WP_Rewrite::$index} with a trailing slash appended. Otherwise |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
531 |
* the root will be empty. |
136 | 532 |
* |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
533 |
* @see WP_Rewrite::init() |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
534 |
* @see WP_Rewrite::using_index_permalinks() |
136 | 535 |
* @since 1.5.0 |
536 |
* @access private |
|
537 |
* @var string |
|
538 |
*/ |
|
539 |
var $root = ''; |
|
540 |
||
541 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
542 |
* The name of the index file which is the entry point to all requests. |
136 | 543 |
* |
544 |
* @since 1.5.0 |
|
545 |
* @access public |
|
546 |
* @var string |
|
547 |
*/ |
|
548 |
var $index = 'index.php'; |
|
549 |
||
550 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
551 |
* Variable name to use for regex matches in the rewritten query. |
136 | 552 |
* |
553 |
* @since 1.5.0 |
|
554 |
* @access private |
|
555 |
* @var string |
|
556 |
*/ |
|
557 |
var $matches = ''; |
|
558 |
||
559 |
/** |
|
560 |
* Rewrite rules to match against the request to find the redirect or query. |
|
561 |
* |
|
562 |
* @since 1.5.0 |
|
563 |
* @access private |
|
564 |
* @var array |
|
565 |
*/ |
|
566 |
var $rules; |
|
567 |
||
568 |
/** |
|
569 |
* Additional rules added external to the rewrite class. |
|
570 |
* |
|
571 |
* Those not generated by the class, see add_rewrite_rule(). |
|
572 |
* |
|
573 |
* @since 2.1.0 |
|
574 |
* @access private |
|
575 |
* @var array |
|
576 |
*/ |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
577 |
var $extra_rules = array(); |
136 | 578 |
|
579 |
/** |
|
580 |
* Additional rules that belong at the beginning to match first. |
|
581 |
* |
|
582 |
* Those not generated by the class, see add_rewrite_rule(). |
|
583 |
* |
|
584 |
* @since 2.3.0 |
|
585 |
* @access private |
|
586 |
* @var array |
|
587 |
*/ |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
588 |
var $extra_rules_top = array(); |
136 | 589 |
|
590 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
591 |
* Rules that don't redirect to WordPress' index.php. |
136 | 592 |
* |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
593 |
* These rules are written to the mod_rewrite portion of the .htaccess, |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
594 |
* and are added by {@link add_external_rule()}. |
136 | 595 |
* |
596 |
* @since 2.1.0 |
|
597 |
* @access private |
|
598 |
* @var array |
|
599 |
*/ |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
600 |
var $non_wp_rules = array(); |
136 | 601 |
|
602 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
603 |
* Extra permalink structures, e.g. categories, added by {@link add_permastruct()}. |
136 | 604 |
* |
605 |
* @since 2.1.0 |
|
606 |
* @access private |
|
607 |
* @var array |
|
608 |
*/ |
|
609 |
var $extra_permastructs = array(); |
|
610 |
||
611 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
612 |
* Endpoints (like /trackback/) added by {@link add_rewrite_endpoint()}. |
136 | 613 |
* |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
614 |
* @since 2.1.0 |
136 | 615 |
* @access private |
616 |
* @var array |
|
617 |
*/ |
|
618 |
var $endpoints; |
|
619 |
||
620 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
621 |
* Whether to write every mod_rewrite rule for WordPress into the .htaccess file. |
136 | 622 |
* |
623 |
* This is off by default, turning it on might print a lot of rewrite rules |
|
624 |
* to the .htaccess file. |
|
625 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
626 |
* @see WP_Rewrite::mod_rewrite_rules() |
136 | 627 |
* @since 2.0.0 |
628 |
* @access public |
|
629 |
* @var bool |
|
630 |
*/ |
|
631 |
var $use_verbose_rules = false; |
|
632 |
||
633 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
634 |
* Could post permalinks be confused with those of pages? |
136 | 635 |
* |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
636 |
* If the first rewrite tag in the post permalink structure is one that could |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
637 |
* also match a page name (e.g. %postname% or %author%) then this flag is |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
638 |
* set to true. Prior to WordPress 3.3 this flag indicated that every page |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
639 |
* would have a set of rules added to the top of the rewrite rules array. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
640 |
* Now it tells {@link WP::parse_request()} to check if a URL matching the |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
641 |
* page permastruct is actually a page before accepting it. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
642 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
643 |
* @link http://core.trac.wordpress.org/ticket/16687 |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
644 |
* @see WP_Rewrite::init() |
136 | 645 |
* @since 2.5.0 |
646 |
* @access public |
|
647 |
* @var bool |
|
648 |
*/ |
|
649 |
var $use_verbose_page_rules = true; |
|
650 |
||
651 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
652 |
* Rewrite tags that can be used in permalink structures. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
653 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
654 |
* These are translated into the regular expressions stored in |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
655 |
* {@link WP_Rewrite::$rewritereplace} and are rewritten to the |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
656 |
* query variables listed in {@link WP_Rewrite::$queryreplace}. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
657 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
658 |
* Additional tags can be added with {@link add_rewrite_tag()}. |
136 | 659 |
* |
660 |
* @since 1.5.0 |
|
661 |
* @access private |
|
662 |
* @var array |
|
663 |
*/ |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
664 |
var $rewritecode = array( |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
665 |
'%year%', |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
666 |
'%monthnum%', |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
667 |
'%day%', |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
668 |
'%hour%', |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
669 |
'%minute%', |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
670 |
'%second%', |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
671 |
'%postname%', |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
672 |
'%post_id%', |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
673 |
'%author%', |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
674 |
'%pagename%', |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
675 |
'%search%' |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
676 |
); |
136 | 677 |
|
678 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
679 |
* Regular expressions to be substituted into rewrite rules in place |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
680 |
* of rewrite tags, see {@link WP_Rewrite::$rewritecode}. |
136 | 681 |
* |
682 |
* @since 1.5.0 |
|
683 |
* @access private |
|
684 |
* @var array |
|
685 |
*/ |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
686 |
var $rewritereplace = array( |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
687 |
'([0-9]{4})', |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
688 |
'([0-9]{1,2})', |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
689 |
'([0-9]{1,2})', |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
690 |
'([0-9]{1,2})', |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
691 |
'([0-9]{1,2})', |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
692 |
'([0-9]{1,2})', |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
693 |
'([^/]+)', |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
694 |
'([0-9]+)', |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
695 |
'([^/]+)', |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
696 |
'([^/]+?)', |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
697 |
'(.+)' |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
698 |
); |
136 | 699 |
|
700 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
701 |
* Query variables that rewrite tags map to, see {@link WP_Rewrite::$rewritecode}. |
136 | 702 |
* |
703 |
* @since 1.5.0 |
|
704 |
* @access private |
|
705 |
* @var array |
|
706 |
*/ |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
707 |
var $queryreplace = array( |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
708 |
'year=', |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
709 |
'monthnum=', |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
710 |
'day=', |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
711 |
'hour=', |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
712 |
'minute=', |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
713 |
'second=', |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
714 |
'name=', |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
715 |
'p=', |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
716 |
'author_name=', |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
717 |
'pagename=', |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
718 |
's=' |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
719 |
); |
136 | 720 |
|
721 |
/** |
|
722 |
* Supported default feeds. |
|
723 |
* |
|
724 |
* @since 1.5.0 |
|
725 |
* @access private |
|
726 |
* @var array |
|
727 |
*/ |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
728 |
var $feeds = array( 'feed', 'rdf', 'rss', 'rss2', 'atom' ); |
136 | 729 |
|
730 |
/** |
|
731 |
* Whether permalinks are being used. |
|
732 |
* |
|
733 |
* This can be either rewrite module or permalink in the HTTP query string. |
|
734 |
* |
|
735 |
* @since 1.5.0 |
|
736 |
* @access public |
|
737 |
* |
|
738 |
* @return bool True, if permalinks are enabled. |
|
739 |
*/ |
|
740 |
function using_permalinks() { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
741 |
return ! empty($this->permalink_structure); |
136 | 742 |
} |
743 |
||
744 |
/** |
|
745 |
* Whether permalinks are being used and rewrite module is not enabled. |
|
746 |
* |
|
747 |
* Means that permalink links are enabled and index.php is in the URL. |
|
748 |
* |
|
749 |
* @since 1.5.0 |
|
750 |
* @access public |
|
751 |
* |
|
752 |
* @return bool |
|
753 |
*/ |
|
754 |
function using_index_permalinks() { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
755 |
if ( empty($this->permalink_structure) ) |
136 | 756 |
return false; |
757 |
||
758 |
// If the index is not in the permalink, we're using mod_rewrite. |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
759 |
if ( preg_match('#^/*' . $this->index . '#', $this->permalink_structure) ) |
136 | 760 |
return true; |
761 |
||
762 |
return false; |
|
763 |
} |
|
764 |
||
765 |
/** |
|
766 |
* Whether permalinks are being used and rewrite module is enabled. |
|
767 |
* |
|
768 |
* Using permalinks and index.php is not in the URL. |
|
769 |
* |
|
770 |
* @since 1.5.0 |
|
771 |
* @access public |
|
772 |
* |
|
773 |
* @return bool |
|
774 |
*/ |
|
775 |
function using_mod_rewrite_permalinks() { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
776 |
if ( $this->using_permalinks() && ! $this->using_index_permalinks() ) |
136 | 777 |
return true; |
778 |
else |
|
779 |
return false; |
|
780 |
} |
|
781 |
||
782 |
/** |
|
783 |
* Index for matches for usage in preg_*() functions. |
|
784 |
* |
|
785 |
* The format of the string is, with empty matches property value, '$NUM'. |
|
786 |
* The 'NUM' will be replaced with the value in the $number parameter. With |
|
787 |
* the matches property not empty, the value of the returned string will |
|
788 |
* contain that value of the matches property. The format then will be |
|
789 |
* '$MATCHES[NUM]', with MATCHES as the value in the property and NUM the |
|
790 |
* value of the $number parameter. |
|
791 |
* |
|
792 |
* @since 1.5.0 |
|
793 |
* @access public |
|
794 |
* |
|
795 |
* @param int $number Index number. |
|
796 |
* @return string |
|
797 |
*/ |
|
798 |
function preg_index($number) { |
|
799 |
$match_prefix = '$'; |
|
800 |
$match_suffix = ''; |
|
801 |
||
802 |
if ( ! empty($this->matches) ) { |
|
803 |
$match_prefix = '$' . $this->matches . '['; |
|
804 |
$match_suffix = ']'; |
|
805 |
} |
|
806 |
||
807 |
return "$match_prefix$number$match_suffix"; |
|
808 |
} |
|
809 |
||
810 |
/** |
|
811 |
* Retrieve all page and attachments for pages URIs. |
|
812 |
* |
|
813 |
* The attachments are for those that have pages as parents and will be |
|
814 |
* retrieved. |
|
815 |
* |
|
816 |
* @since 2.5.0 |
|
817 |
* @access public |
|
818 |
* |
|
819 |
* @return array Array of page URIs as first element and attachment URIs as second element. |
|
820 |
*/ |
|
821 |
function page_uri_index() { |
|
822 |
global $wpdb; |
|
823 |
||
824 |
//get pages in order of hierarchy, i.e. children after parents |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
825 |
$posts = get_page_hierarchy( $wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'page' AND post_status != 'auto-draft'") ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
826 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
827 |
// If we have no pages get out quick |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
828 |
if ( !$posts ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
829 |
return array( array(), array() ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
830 |
|
136 | 831 |
//now reverse it, because we need parents after children for rewrite rules to work properly |
832 |
$posts = array_reverse($posts, true); |
|
833 |
||
834 |
$page_uris = array(); |
|
835 |
$page_attachment_uris = array(); |
|
836 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
837 |
foreach ( $posts as $id => $post ) { |
136 | 838 |
// URL => page name |
839 |
$uri = get_page_uri($id); |
|
840 |
$attachments = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'attachment' AND post_parent = %d", $id )); |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
841 |
if ( !empty($attachments) ) { |
136 | 842 |
foreach ( $attachments as $attachment ) { |
843 |
$attach_uri = get_page_uri($attachment->ID); |
|
844 |
$page_attachment_uris[$attach_uri] = $attachment->ID; |
|
845 |
} |
|
846 |
} |
|
847 |
||
848 |
$page_uris[$uri] = $id; |
|
849 |
} |
|
850 |
||
851 |
return array( $page_uris, $page_attachment_uris ); |
|
852 |
} |
|
853 |
||
854 |
/** |
|
855 |
* Retrieve all of the rewrite rules for pages. |
|
856 |
* |
|
857 |
* @since 1.5.0 |
|
858 |
* @access public |
|
859 |
* |
|
860 |
* @return array |
|
861 |
*/ |
|
862 |
function page_rewrite_rules() { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
863 |
// the extra .? at the beginning prevents clashes with other regular expressions in the rules array |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
864 |
$this->add_rewrite_tag( '%pagename%', '(.?.+?)', 'pagename=' ); |
136 | 865 |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
866 |
return $this->generate_rewrite_rules( $this->get_page_permastruct(), EP_PAGES, true, true, false, false ); |
136 | 867 |
} |
868 |
||
869 |
/** |
|
870 |
* Retrieve date permalink structure, with year, month, and day. |
|
871 |
* |
|
872 |
* The permalink structure for the date, if not set already depends on the |
|
873 |
* permalink structure. It can be one of three formats. The first is year, |
|
874 |
* month, day; the second is day, month, year; and the last format is month, |
|
875 |
* day, year. These are matched against the permalink structure for which |
|
876 |
* one is used. If none matches, then the default will be used, which is |
|
877 |
* year, month, day. |
|
878 |
* |
|
879 |
* Prevents post ID and date permalinks from overlapping. In the case of |
|
880 |
* post_id, the date permalink will be prepended with front permalink with |
|
881 |
* 'date/' before the actual permalink to form the complete date permalink |
|
882 |
* structure. |
|
883 |
* |
|
884 |
* @since 1.5.0 |
|
885 |
* @access public |
|
886 |
* |
|
887 |
* @return bool|string False on no permalink structure. Date permalink structure. |
|
888 |
*/ |
|
889 |
function get_date_permastruct() { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
890 |
if ( isset($this->date_structure) ) |
136 | 891 |
return $this->date_structure; |
892 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
893 |
if ( empty($this->permalink_structure) ) { |
136 | 894 |
$this->date_structure = ''; |
895 |
return false; |
|
896 |
} |
|
897 |
||
898 |
// The date permalink must have year, month, and day separated by slashes. |
|
899 |
$endians = array('%year%/%monthnum%/%day%', '%day%/%monthnum%/%year%', '%monthnum%/%day%/%year%'); |
|
900 |
||
901 |
$this->date_structure = ''; |
|
902 |
$date_endian = ''; |
|
903 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
904 |
foreach ( $endians as $endian ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
905 |
if ( false !== strpos($this->permalink_structure, $endian) ) { |
136 | 906 |
$date_endian= $endian; |
907 |
break; |
|
908 |
} |
|
909 |
} |
|
910 |
||
911 |
if ( empty($date_endian) ) |
|
912 |
$date_endian = '%year%/%monthnum%/%day%'; |
|
913 |
||
914 |
// Do not allow the date tags and %post_id% to overlap in the permalink |
|
915 |
// structure. If they do, move the date tags to $front/date/. |
|
916 |
$front = $this->front; |
|
917 |
preg_match_all('/%.+?%/', $this->permalink_structure, $tokens); |
|
918 |
$tok_index = 1; |
|
919 |
foreach ( (array) $tokens[0] as $token) { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
920 |
if ( '%post_id%' == $token && ($tok_index <= 3) ) { |
136 | 921 |
$front = $front . 'date/'; |
922 |
break; |
|
923 |
} |
|
924 |
$tok_index++; |
|
925 |
} |
|
926 |
||
927 |
$this->date_structure = $front . $date_endian; |
|
928 |
||
929 |
return $this->date_structure; |
|
930 |
} |
|
931 |
||
932 |
/** |
|
933 |
* Retrieve the year permalink structure without month and day. |
|
934 |
* |
|
935 |
* Gets the date permalink structure and strips out the month and day |
|
936 |
* permalink structures. |
|
937 |
* |
|
938 |
* @since 1.5.0 |
|
939 |
* @access public |
|
940 |
* |
|
941 |
* @return bool|string False on failure. Year structure on success. |
|
942 |
*/ |
|
943 |
function get_year_permastruct() { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
944 |
$structure = $this->get_date_permastruct(); |
136 | 945 |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
946 |
if ( empty($structure) ) |
136 | 947 |
return false; |
948 |
||
949 |
$structure = str_replace('%monthnum%', '', $structure); |
|
950 |
$structure = str_replace('%day%', '', $structure); |
|
951 |
||
952 |
$structure = preg_replace('#/+#', '/', $structure); |
|
953 |
||
954 |
return $structure; |
|
955 |
} |
|
956 |
||
957 |
/** |
|
958 |
* Retrieve the month permalink structure without day and with year. |
|
959 |
* |
|
960 |
* Gets the date permalink structure and strips out the day permalink |
|
961 |
* structures. Keeps the year permalink structure. |
|
962 |
* |
|
963 |
* @since 1.5.0 |
|
964 |
* @access public |
|
965 |
* |
|
966 |
* @return bool|string False on failure. Year/Month structure on success. |
|
967 |
*/ |
|
968 |
function get_month_permastruct() { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
969 |
$structure = $this->get_date_permastruct(); |
136 | 970 |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
971 |
if ( empty($structure) ) |
136 | 972 |
return false; |
973 |
||
974 |
$structure = str_replace('%day%', '', $structure); |
|
975 |
||
976 |
$structure = preg_replace('#/+#', '/', $structure); |
|
977 |
||
978 |
return $structure; |
|
979 |
} |
|
980 |
||
981 |
/** |
|
982 |
* Retrieve the day permalink structure with month and year. |
|
983 |
* |
|
984 |
* Keeps date permalink structure with all year, month, and day. |
|
985 |
* |
|
986 |
* @since 1.5.0 |
|
987 |
* @access public |
|
988 |
* |
|
989 |
* @return bool|string False on failure. Year/Month/Day structure on success. |
|
990 |
*/ |
|
991 |
function get_day_permastruct() { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
992 |
return $this->get_date_permastruct(); |
136 | 993 |
} |
994 |
||
995 |
/** |
|
996 |
* Retrieve the permalink structure for categories. |
|
997 |
* |
|
998 |
* If the category_base property has no value, then the category structure |
|
999 |
* will have the front property value, followed by 'category', and finally |
|
1000 |
* '%category%'. If it does, then the root property will be used, along with |
|
1001 |
* the category_base property value. |
|
1002 |
* |
|
1003 |
* @since 1.5.0 |
|
1004 |
* @access public |
|
1005 |
* |
|
1006 |
* @return bool|string False on failure. Category permalink structure. |
|
1007 |
*/ |
|
1008 |
function get_category_permastruct() { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1009 |
return $this->get_extra_permastruct('category'); |
136 | 1010 |
} |
1011 |
||
1012 |
/** |
|
1013 |
* Retrieve the permalink structure for tags. |
|
1014 |
* |
|
1015 |
* If the tag_base property has no value, then the tag structure will have |
|
1016 |
* the front property value, followed by 'tag', and finally '%tag%'. If it |
|
1017 |
* does, then the root property will be used, along with the tag_base |
|
1018 |
* property value. |
|
1019 |
* |
|
1020 |
* @since 2.3.0 |
|
1021 |
* @access public |
|
1022 |
* |
|
1023 |
* @return bool|string False on failure. Tag permalink structure. |
|
1024 |
*/ |
|
1025 |
function get_tag_permastruct() { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1026 |
return $this->get_extra_permastruct('post_tag'); |
136 | 1027 |
} |
1028 |
||
1029 |
/** |
|
1030 |
* Retrieve extra permalink structure by name. |
|
1031 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1032 |
* @since 2.5.0 |
136 | 1033 |
* @access public |
1034 |
* |
|
1035 |
* @param string $name Permalink structure name. |
|
1036 |
* @return string|bool False if not found. Permalink structure string. |
|
1037 |
*/ |
|
1038 |
function get_extra_permastruct($name) { |
|
1039 |
if ( empty($this->permalink_structure) ) |
|
1040 |
return false; |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1041 |
|
136 | 1042 |
if ( isset($this->extra_permastructs[$name]) ) |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1043 |
return $this->extra_permastructs[$name]['struct']; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1044 |
|
136 | 1045 |
return false; |
1046 |
} |
|
1047 |
||
1048 |
/** |
|
1049 |
* Retrieve the author permalink structure. |
|
1050 |
* |
|
1051 |
* The permalink structure is front property, author base, and finally |
|
1052 |
* '/%author%'. Will set the author_structure property and then return it |
|
1053 |
* without attempting to set the value again. |
|
1054 |
* |
|
1055 |
* @since 1.5.0 |
|
1056 |
* @access public |
|
1057 |
* |
|
1058 |
* @return string|bool False if not found. Permalink structure string. |
|
1059 |
*/ |
|
1060 |
function get_author_permastruct() { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1061 |
if ( isset($this->author_structure) ) |
136 | 1062 |
return $this->author_structure; |
1063 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1064 |
if ( empty($this->permalink_structure) ) { |
136 | 1065 |
$this->author_structure = ''; |
1066 |
return false; |
|
1067 |
} |
|
1068 |
||
1069 |
$this->author_structure = $this->front . $this->author_base . '/%author%'; |
|
1070 |
||
1071 |
return $this->author_structure; |
|
1072 |
} |
|
1073 |
||
1074 |
/** |
|
1075 |
* Retrieve the search permalink structure. |
|
1076 |
* |
|
1077 |
* The permalink structure is root property, search base, and finally |
|
1078 |
* '/%search%'. Will set the search_structure property and then return it |
|
1079 |
* without attempting to set the value again. |
|
1080 |
* |
|
1081 |
* @since 1.5.0 |
|
1082 |
* @access public |
|
1083 |
* |
|
1084 |
* @return string|bool False if not found. Permalink structure string. |
|
1085 |
*/ |
|
1086 |
function get_search_permastruct() { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1087 |
if ( isset($this->search_structure) ) |
136 | 1088 |
return $this->search_structure; |
1089 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1090 |
if ( empty($this->permalink_structure) ) { |
136 | 1091 |
$this->search_structure = ''; |
1092 |
return false; |
|
1093 |
} |
|
1094 |
||
1095 |
$this->search_structure = $this->root . $this->search_base . '/%search%'; |
|
1096 |
||
1097 |
return $this->search_structure; |
|
1098 |
} |
|
1099 |
||
1100 |
/** |
|
1101 |
* Retrieve the page permalink structure. |
|
1102 |
* |
|
1103 |
* The permalink structure is root property, and '%pagename%'. Will set the |
|
1104 |
* page_structure property and then return it without attempting to set the |
|
1105 |
* value again. |
|
1106 |
* |
|
1107 |
* @since 1.5.0 |
|
1108 |
* @access public |
|
1109 |
* |
|
1110 |
* @return string|bool False if not found. Permalink structure string. |
|
1111 |
*/ |
|
1112 |
function get_page_permastruct() { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1113 |
if ( isset($this->page_structure) ) |
136 | 1114 |
return $this->page_structure; |
1115 |
||
1116 |
if (empty($this->permalink_structure)) { |
|
1117 |
$this->page_structure = ''; |
|
1118 |
return false; |
|
1119 |
} |
|
1120 |
||
1121 |
$this->page_structure = $this->root . '%pagename%'; |
|
1122 |
||
1123 |
return $this->page_structure; |
|
1124 |
} |
|
1125 |
||
1126 |
/** |
|
1127 |
* Retrieve the feed permalink structure. |
|
1128 |
* |
|
1129 |
* The permalink structure is root property, feed base, and finally |
|
1130 |
* '/%feed%'. Will set the feed_structure property and then return it |
|
1131 |
* without attempting to set the value again. |
|
1132 |
* |
|
1133 |
* @since 1.5.0 |
|
1134 |
* @access public |
|
1135 |
* |
|
1136 |
* @return string|bool False if not found. Permalink structure string. |
|
1137 |
*/ |
|
1138 |
function get_feed_permastruct() { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1139 |
if ( isset($this->feed_structure) ) |
136 | 1140 |
return $this->feed_structure; |
1141 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1142 |
if ( empty($this->permalink_structure) ) { |
136 | 1143 |
$this->feed_structure = ''; |
1144 |
return false; |
|
1145 |
} |
|
1146 |
||
1147 |
$this->feed_structure = $this->root . $this->feed_base . '/%feed%'; |
|
1148 |
||
1149 |
return $this->feed_structure; |
|
1150 |
} |
|
1151 |
||
1152 |
/** |
|
1153 |
* Retrieve the comment feed permalink structure. |
|
1154 |
* |
|
1155 |
* The permalink structure is root property, comment base property, feed |
|
1156 |
* base and finally '/%feed%'. Will set the comment_feed_structure property |
|
1157 |
* and then return it without attempting to set the value again. |
|
1158 |
* |
|
1159 |
* @since 1.5.0 |
|
1160 |
* @access public |
|
1161 |
* |
|
1162 |
* @return string|bool False if not found. Permalink structure string. |
|
1163 |
*/ |
|
1164 |
function get_comment_feed_permastruct() { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1165 |
if ( isset($this->comment_feed_structure) ) |
136 | 1166 |
return $this->comment_feed_structure; |
1167 |
||
1168 |
if (empty($this->permalink_structure)) { |
|
1169 |
$this->comment_feed_structure = ''; |
|
1170 |
return false; |
|
1171 |
} |
|
1172 |
||
1173 |
$this->comment_feed_structure = $this->root . $this->comments_base . '/' . $this->feed_base . '/%feed%'; |
|
1174 |
||
1175 |
return $this->comment_feed_structure; |
|
1176 |
} |
|
1177 |
||
1178 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1179 |
* Add or update existing rewrite tags (e.g. %postname%). |
136 | 1180 |
* |
1181 |
* If the tag already exists, replace the existing pattern and query for |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1182 |
* that tag, otherwise add the new tag. |
136 | 1183 |
* |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1184 |
* @see WP_Rewrite::$rewritecode |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1185 |
* @see WP_Rewrite::$rewritereplace |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1186 |
* @see WP_Rewrite::$queryreplace |
136 | 1187 |
* @since 1.5.0 |
1188 |
* @access public |
|
1189 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1190 |
* @param string $tag Name of the rewrite tag to add or update. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1191 |
* @param string $regex Regular expression to substitute the tag for in rewrite rules. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1192 |
* @param string $query String to append to the rewritten query. Must end in '='. |
136 | 1193 |
*/ |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1194 |
function add_rewrite_tag( $tag, $regex, $query ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1195 |
$position = array_search( $tag, $this->rewritecode ); |
136 | 1196 |
if ( false !== $position && null !== $position ) { |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1197 |
$this->rewritereplace[ $position ] = $regex; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1198 |
$this->queryreplace[ $position ] = $query; |
136 | 1199 |
} else { |
1200 |
$this->rewritecode[] = $tag; |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1201 |
$this->rewritereplace[] = $regex; |
136 | 1202 |
$this->queryreplace[] = $query; |
1203 |
} |
|
1204 |
} |
|
1205 |
||
1206 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1207 |
* Generate rewrite rules from a permalink structure. |
136 | 1208 |
* |
1209 |
* The main WP_Rewrite function for building the rewrite rule list. The |
|
1210 |
* contents of the function is a mix of black magic and regular expressions, |
|
1211 |
* so best just ignore the contents and move to the parameters. |
|
1212 |
* |
|
1213 |
* @since 1.5.0 |
|
1214 |
* @access public |
|
1215 |
* |
|
1216 |
* @param string $permalink_structure The permalink structure. |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1217 |
* @param int $ep_mask Endpoint mask defining what endpoints are added to the structure. Default is EP_NONE. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1218 |
* @param bool $paged Should archive pagination rules be added for the structure? Default is true. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1219 |
* @param bool $feed Should feed rewrite rules be added for the structure? Default is true. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1220 |
* @param bool $forcomments Should the feed rules be a query for a comments feed? Default is false. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1221 |
* @param bool $walk_dirs Should the 'directories' making up the structure be walked over and rewrite rules |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1222 |
* built for each in turn? Default is true. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1223 |
* @param bool $endpoints Should endpoints be applied to the generated rewrite rules? Default is true. |
136 | 1224 |
* @return array Rewrite rule list. |
1225 |
*/ |
|
1226 |
function generate_rewrite_rules($permalink_structure, $ep_mask = EP_NONE, $paged = true, $feed = true, $forcomments = false, $walk_dirs = true, $endpoints = true) { |
|
1227 |
//build a regex to match the feed section of URLs, something like (feed|atom|rss|rss2)/? |
|
1228 |
$feedregex2 = ''; |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1229 |
foreach ( (array) $this->feeds as $feed_name) |
136 | 1230 |
$feedregex2 .= $feed_name . '|'; |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1231 |
$feedregex2 = '(' . trim($feedregex2, '|') . ')/?$'; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1232 |
|
136 | 1233 |
//$feedregex is identical but with /feed/ added on as well, so URLs like <permalink>/feed/atom |
1234 |
//and <permalink>/atom are both possible |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1235 |
$feedregex = $this->feed_base . '/' . $feedregex2; |
136 | 1236 |
|
1237 |
//build a regex to match the trackback and page/xx parts of URLs |
|
1238 |
$trackbackregex = 'trackback/?$'; |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1239 |
$pageregex = $this->pagination_base . '/?([0-9]{1,})/?$'; |
136 | 1240 |
$commentregex = 'comment-page-([0-9]{1,})/?$'; |
1241 |
||
1242 |
//build up an array of endpoint regexes to append => queries to append |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1243 |
if ( $endpoints ) { |
136 | 1244 |
$ep_query_append = array (); |
1245 |
foreach ( (array) $this->endpoints as $endpoint) { |
|
1246 |
//match everything after the endpoint name, but allow for nothing to appear there |
|
1247 |
$epmatch = $endpoint[1] . '(/(.*))?/?$'; |
|
1248 |
//this will be appended on to the rest of the query for each dir |
|
1249 |
$epquery = '&' . $endpoint[1] . '='; |
|
1250 |
$ep_query_append[$epmatch] = array ( $endpoint[0], $epquery ); |
|
1251 |
} |
|
1252 |
} |
|
1253 |
||
1254 |
//get everything up to the first rewrite tag |
|
1255 |
$front = substr($permalink_structure, 0, strpos($permalink_structure, '%')); |
|
1256 |
//build an array of the tags (note that said array ends up being in $tokens[0]) |
|
1257 |
preg_match_all('/%.+?%/', $permalink_structure, $tokens); |
|
1258 |
||
1259 |
$num_tokens = count($tokens[0]); |
|
1260 |
||
1261 |
$index = $this->index; //probably 'index.php' |
|
1262 |
$feedindex = $index; |
|
1263 |
$trackbackindex = $index; |
|
1264 |
//build a list from the rewritecode and queryreplace arrays, that will look something like |
|
1265 |
//tagname=$matches[i] where i is the current $i |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1266 |
for ( $i = 0; $i < $num_tokens; ++$i ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1267 |
if ( 0 < $i ) |
136 | 1268 |
$queries[$i] = $queries[$i - 1] . '&'; |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1269 |
else |
136 | 1270 |
$queries[$i] = ''; |
1271 |
||
1272 |
$query_token = str_replace($this->rewritecode, $this->queryreplace, $tokens[0][$i]) . $this->preg_index($i+1); |
|
1273 |
$queries[$i] .= $query_token; |
|
1274 |
} |
|
1275 |
||
1276 |
//get the structure, minus any cruft (stuff that isn't tags) at the front |
|
1277 |
$structure = $permalink_structure; |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1278 |
if ( $front != '/' ) |
136 | 1279 |
$structure = str_replace($front, '', $structure); |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1280 |
|
136 | 1281 |
//create a list of dirs to walk over, making rewrite rules for each level |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1282 |
//so for example, a $structure of /%year%/%monthnum%/%postname% would create |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1283 |
//rewrite rules for /%year%/, /%year%/%monthnum%/ and /%year%/%monthnum%/%postname% |
136 | 1284 |
$structure = trim($structure, '/'); |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1285 |
$dirs = $walk_dirs ? explode('/', $structure) : array( $structure ); |
136 | 1286 |
$num_dirs = count($dirs); |
1287 |
||
1288 |
//strip slashes from the front of $front |
|
1289 |
$front = preg_replace('|^/+|', '', $front); |
|
1290 |
||
1291 |
//the main workhorse loop |
|
1292 |
$post_rewrite = array(); |
|
1293 |
$struct = $front; |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1294 |
for ( $j = 0; $j < $num_dirs; ++$j ) { |
136 | 1295 |
//get the struct for this dir, and trim slashes off the front |
1296 |
$struct .= $dirs[$j] . '/'; //accumulate. see comment near explode('/', $structure) above |
|
1297 |
$struct = ltrim($struct, '/'); |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1298 |
|
136 | 1299 |
//replace tags with regexes |
1300 |
$match = str_replace($this->rewritecode, $this->rewritereplace, $struct); |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1301 |
|
136 | 1302 |
//make a list of tags, and store how many there are in $num_toks |
1303 |
$num_toks = preg_match_all('/%.+?%/', $struct, $toks); |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1304 |
|
136 | 1305 |
//get the 'tagname=$matches[i]' |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1306 |
$query = ( isset($queries) && is_array($queries) && !empty($num_toks) ) ? $queries[$num_toks - 1] : ''; |
136 | 1307 |
|
1308 |
//set up $ep_mask_specific which is used to match more specific URL types |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1309 |
switch ( $dirs[$j] ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1310 |
case '%year%': |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1311 |
$ep_mask_specific = EP_YEAR; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1312 |
break; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1313 |
case '%monthnum%': |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1314 |
$ep_mask_specific = EP_MONTH; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1315 |
break; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1316 |
case '%day%': |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1317 |
$ep_mask_specific = EP_DAY; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1318 |
break; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1319 |
default: |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1320 |
$ep_mask_specific = EP_NONE; |
136 | 1321 |
} |
1322 |
||
1323 |
//create query for /page/xx |
|
1324 |
$pagematch = $match . $pageregex; |
|
1325 |
$pagequery = $index . '?' . $query . '&paged=' . $this->preg_index($num_toks + 1); |
|
1326 |
||
1327 |
//create query for /comment-page-xx |
|
1328 |
$commentmatch = $match . $commentregex; |
|
1329 |
$commentquery = $index . '?' . $query . '&cpage=' . $this->preg_index($num_toks + 1); |
|
1330 |
||
1331 |
if ( get_option('page_on_front') ) { |
|
1332 |
//create query for Root /comment-page-xx |
|
1333 |
$rootcommentmatch = $match . $commentregex; |
|
1334 |
$rootcommentquery = $index . '?' . $query . '&page_id=' . get_option('page_on_front') . '&cpage=' . $this->preg_index($num_toks + 1); |
|
1335 |
} |
|
1336 |
||
1337 |
//create query for /feed/(feed|atom|rss|rss2|rdf) |
|
1338 |
$feedmatch = $match . $feedregex; |
|
1339 |
$feedquery = $feedindex . '?' . $query . '&feed=' . $this->preg_index($num_toks + 1); |
|
1340 |
||
1341 |
//create query for /(feed|atom|rss|rss2|rdf) (see comment near creation of $feedregex) |
|
1342 |
$feedmatch2 = $match . $feedregex2; |
|
1343 |
$feedquery2 = $feedindex . '?' . $query . '&feed=' . $this->preg_index($num_toks + 1); |
|
1344 |
||
1345 |
//if asked to, turn the feed queries into comment feed ones |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1346 |
if ( $forcomments ) { |
136 | 1347 |
$feedquery .= '&withcomments=1'; |
1348 |
$feedquery2 .= '&withcomments=1'; |
|
1349 |
} |
|
1350 |
||
1351 |
//start creating the array of rewrites for this dir |
|
1352 |
$rewrite = array(); |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1353 |
if ( $feed ) //...adding on /feed/ regexes => queries |
136 | 1354 |
$rewrite = array($feedmatch => $feedquery, $feedmatch2 => $feedquery2); |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1355 |
if ( $paged ) //...and /page/xx ones |
136 | 1356 |
$rewrite = array_merge($rewrite, array($pagematch => $pagequery)); |
1357 |
||
1358 |
//only on pages with comments add ../comment-page-xx/ |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1359 |
if ( EP_PAGES & $ep_mask || EP_PERMALINK & $ep_mask ) |
136 | 1360 |
$rewrite = array_merge($rewrite, array($commentmatch => $commentquery)); |
1361 |
else if ( EP_ROOT & $ep_mask && get_option('page_on_front') ) |
|
1362 |
$rewrite = array_merge($rewrite, array($rootcommentmatch => $rootcommentquery)); |
|
1363 |
||
1364 |
//do endpoints |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1365 |
if ( $endpoints ) { |
136 | 1366 |
foreach ( (array) $ep_query_append as $regex => $ep) { |
1367 |
//add the endpoints on if the mask fits |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1368 |
if ( $ep[0] & $ep_mask || $ep[0] & $ep_mask_specific ) |
136 | 1369 |
$rewrite[$match . $regex] = $index . '?' . $query . $ep[1] . $this->preg_index($num_toks + 2); |
1370 |
} |
|
1371 |
} |
|
1372 |
||
1373 |
//if we've got some tags in this dir |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1374 |
if ( $num_toks ) { |
136 | 1375 |
$post = false; |
1376 |
$page = false; |
|
1377 |
||
1378 |
//check to see if this dir is permalink-level: i.e. the structure specifies an |
|
1379 |
//individual post. Do this by checking it contains at least one of 1) post name, |
|
1380 |
//2) post ID, 3) page name, 4) timestamp (year, month, day, hour, second and |
|
1381 |
//minute all present). Set these flags now as we need them for the endpoints. |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1382 |
if ( strpos($struct, '%postname%') !== false |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1383 |
|| strpos($struct, '%post_id%') !== false |
136 | 1384 |
|| strpos($struct, '%pagename%') !== false |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1385 |
|| (strpos($struct, '%year%') !== false && strpos($struct, '%monthnum%') !== false && strpos($struct, '%day%') !== false && strpos($struct, '%hour%') !== false && strpos($struct, '%minute%') !== false && strpos($struct, '%second%') !== false) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1386 |
) { |
136 | 1387 |
$post = true; |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1388 |
if ( strpos($struct, '%pagename%') !== false ) |
136 | 1389 |
$page = true; |
1390 |
} |
|
1391 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1392 |
if ( ! $post ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1393 |
// For custom post types, we need to add on endpoints as well. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1394 |
foreach ( get_post_types( array('_builtin' => false ) ) as $ptype ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1395 |
if ( strpos($struct, "%$ptype%") !== false ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1396 |
$post = true; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1397 |
$page = is_post_type_hierarchical( $ptype ); // This is for page style attachment url's |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1398 |
break; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1399 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1400 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1401 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1402 |
|
136 | 1403 |
//if we're creating rules for a permalink, do all the endpoints like attachments etc |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1404 |
if ( $post ) { |
136 | 1405 |
//create query and regex for trackback |
1406 |
$trackbackmatch = $match . $trackbackregex; |
|
1407 |
$trackbackquery = $trackbackindex . '?' . $query . '&tb=1'; |
|
1408 |
//trim slashes from the end of the regex for this dir |
|
1409 |
$match = rtrim($match, '/'); |
|
1410 |
//get rid of brackets |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1411 |
$submatchbase = str_replace( array('(', ')'), '', $match); |
136 | 1412 |
|
1413 |
//add a rule for at attachments, which take the form of <permalink>/some-text |
|
1414 |
$sub1 = $submatchbase . '/([^/]+)/'; |
|
1415 |
$sub1tb = $sub1 . $trackbackregex; //add trackback regex <permalink>/trackback/... |
|
1416 |
$sub1feed = $sub1 . $feedregex; //and <permalink>/feed/(atom|...) |
|
1417 |
$sub1feed2 = $sub1 . $feedregex2; //and <permalink>/(feed|atom...) |
|
1418 |
$sub1comment = $sub1 . $commentregex; //and <permalink>/comment-page-xx |
|
1419 |
||
1420 |
//add another rule to match attachments in the explicit form: |
|
1421 |
//<permalink>/attachment/some-text |
|
1422 |
$sub2 = $submatchbase . '/attachment/([^/]+)/'; |
|
1423 |
$sub2tb = $sub2 . $trackbackregex; //and add trackbacks <permalink>/attachment/trackback |
|
1424 |
$sub2feed = $sub2 . $feedregex; //feeds, <permalink>/attachment/feed/(atom|...) |
|
1425 |
$sub2feed2 = $sub2 . $feedregex2; //and feeds again on to this <permalink>/attachment/(feed|atom...) |
|
1426 |
$sub2comment = $sub2 . $commentregex; //and <permalink>/comment-page-xx |
|
1427 |
||
1428 |
//create queries for these extra tag-ons we've just dealt with |
|
1429 |
$subquery = $index . '?attachment=' . $this->preg_index(1); |
|
1430 |
$subtbquery = $subquery . '&tb=1'; |
|
1431 |
$subfeedquery = $subquery . '&feed=' . $this->preg_index(2); |
|
1432 |
$subcommentquery = $subquery . '&cpage=' . $this->preg_index(2); |
|
1433 |
||
1434 |
//do endpoints for attachments |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1435 |
if ( !empty($endpoints) ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1436 |
foreach ( (array) $ep_query_append as $regex => $ep ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1437 |
if ( $ep[0] & EP_ATTACHMENT ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1438 |
$rewrite[$sub1 . $regex] = $subquery . $ep[1] . $this->preg_index(2); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1439 |
$rewrite[$sub2 . $regex] = $subquery . $ep[1] . $this->preg_index(2); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1440 |
} |
136 | 1441 |
} |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1442 |
} |
136 | 1443 |
|
1444 |
//now we've finished with endpoints, finish off the $sub1 and $sub2 matches |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1445 |
//add a ? as we don't have to match that last slash, and finally a $ so we |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1446 |
//match to the end of the URL |
136 | 1447 |
$sub1 .= '?$'; |
1448 |
$sub2 .= '?$'; |
|
1449 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1450 |
//post pagination, e.g. <permalink>/2/ |
136 | 1451 |
$match = $match . '(/[0-9]+)?/?$'; |
1452 |
$query = $index . '?' . $query . '&page=' . $this->preg_index($num_toks + 1); |
|
1453 |
} else { //not matching a permalink so this is a lot simpler |
|
1454 |
//close the match and finalise the query |
|
1455 |
$match .= '?$'; |
|
1456 |
$query = $index . '?' . $query; |
|
1457 |
} |
|
1458 |
||
1459 |
//create the final array for this dir by joining the $rewrite array (which currently |
|
1460 |
//only contains rules/queries for trackback, pages etc) to the main regex/query for |
|
1461 |
//this dir |
|
1462 |
$rewrite = array_merge($rewrite, array($match => $query)); |
|
1463 |
||
1464 |
//if we're matching a permalink, add those extras (attachments etc) on |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1465 |
if ( $post ) { |
136 | 1466 |
//add trackback |
1467 |
$rewrite = array_merge(array($trackbackmatch => $trackbackquery), $rewrite); |
|
1468 |
||
1469 |
//add regexes/queries for attachments, attachment trackbacks and so on |
|
1470 |
if ( ! $page ) //require <permalink>/attachment/stuff form for pages because of confusion with subpages |
|
1471 |
$rewrite = array_merge($rewrite, array($sub1 => $subquery, $sub1tb => $subtbquery, $sub1feed => $subfeedquery, $sub1feed2 => $subfeedquery, $sub1comment => $subcommentquery)); |
|
1472 |
$rewrite = array_merge(array($sub2 => $subquery, $sub2tb => $subtbquery, $sub2feed => $subfeedquery, $sub2feed2 => $subfeedquery, $sub2comment => $subcommentquery), $rewrite); |
|
1473 |
} |
|
1474 |
} //if($num_toks) |
|
1475 |
//add the rules for this dir to the accumulating $post_rewrite |
|
1476 |
$post_rewrite = array_merge($rewrite, $post_rewrite); |
|
1477 |
} //foreach ($dir) |
|
1478 |
return $post_rewrite; //the finished rules. phew! |
|
1479 |
} |
|
1480 |
||
1481 |
/** |
|
1482 |
* Generate Rewrite rules with permalink structure and walking directory only. |
|
1483 |
* |
|
1484 |
* Shorten version of {@link WP_Rewrite::generate_rewrite_rules()} that |
|
1485 |
* allows for shorter list of parameters. See the method for longer |
|
1486 |
* description of what generating rewrite rules does. |
|
1487 |
* |
|
1488 |
* @uses WP_Rewrite::generate_rewrite_rules() See for long description and rest of parameters. |
|
1489 |
* @since 1.5.0 |
|
1490 |
* @access public |
|
1491 |
* |
|
1492 |
* @param string $permalink_structure The permalink structure to generate rules. |
|
1493 |
* @param bool $walk_dirs Optional, default is false. Whether to create list of directories to walk over. |
|
1494 |
* @return array |
|
1495 |
*/ |
|
1496 |
function generate_rewrite_rule($permalink_structure, $walk_dirs = false) { |
|
1497 |
return $this->generate_rewrite_rules($permalink_structure, EP_NONE, false, false, false, $walk_dirs); |
|
1498 |
} |
|
1499 |
||
1500 |
/** |
|
1501 |
* Construct rewrite matches and queries from permalink structure. |
|
1502 |
* |
|
1503 |
* Runs the action 'generate_rewrite_rules' with the parameter that is an |
|
1504 |
* reference to the current WP_Rewrite instance to further manipulate the |
|
1505 |
* permalink structures and rewrite rules. Runs the 'rewrite_rules_array' |
|
1506 |
* filter on the full rewrite rule array. |
|
1507 |
* |
|
1508 |
* There are two ways to manipulate the rewrite rules, one by hooking into |
|
1509 |
* the 'generate_rewrite_rules' action and gaining full control of the |
|
1510 |
* object or just manipulating the rewrite rule array before it is passed |
|
1511 |
* from the function. |
|
1512 |
* |
|
1513 |
* @since 1.5.0 |
|
1514 |
* @access public |
|
1515 |
* |
|
1516 |
* @return array An associate array of matches and queries. |
|
1517 |
*/ |
|
1518 |
function rewrite_rules() { |
|
1519 |
$rewrite = array(); |
|
1520 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1521 |
if ( empty($this->permalink_structure) ) |
136 | 1522 |
return $rewrite; |
1523 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1524 |
// robots.txt -only if installed at the root |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1525 |
$home_path = parse_url( home_url() ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1526 |
$robots_rewrite = ( empty( $home_path['path'] ) || '/' == $home_path['path'] ) ? array( 'robots\.txt$' => $this->index . '?robots=1' ) : array(); |
136 | 1527 |
|
204
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
1528 |
// Old feed and service files |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
1529 |
$deprecated_files = array( |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
1530 |
'.*wp-(atom|rdf|rss|rss2|feed|commentsrss2)\.php$' => $this->index . '?feed=old', |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
1531 |
'.*wp-app\.php(/.*)?$' => $this->index . '?error=403', |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
1532 |
); |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1533 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1534 |
// Registration rules |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1535 |
$registration_pages = array(); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1536 |
if ( is_multisite() && is_main_site() ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1537 |
$registration_pages['.*wp-signup.php$'] = $this->index . '?signup=true'; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1538 |
$registration_pages['.*wp-activate.php$'] = $this->index . '?activate=true'; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1539 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1540 |
$registration_pages['.*wp-register.php$'] = $this->index . '?register=true'; // Deprecated |
136 | 1541 |
|
1542 |
// Post |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1543 |
$post_rewrite = $this->generate_rewrite_rules( $this->permalink_structure, EP_PERMALINK ); |
136 | 1544 |
$post_rewrite = apply_filters('post_rewrite_rules', $post_rewrite); |
1545 |
||
1546 |
// Date |
|
1547 |
$date_rewrite = $this->generate_rewrite_rules($this->get_date_permastruct(), EP_DATE); |
|
1548 |
$date_rewrite = apply_filters('date_rewrite_rules', $date_rewrite); |
|
1549 |
||
1550 |
// Root |
|
1551 |
$root_rewrite = $this->generate_rewrite_rules($this->root . '/', EP_ROOT); |
|
1552 |
$root_rewrite = apply_filters('root_rewrite_rules', $root_rewrite); |
|
1553 |
||
1554 |
// Comments |
|
1555 |
$comments_rewrite = $this->generate_rewrite_rules($this->root . $this->comments_base, EP_COMMENTS, true, true, true, false); |
|
1556 |
$comments_rewrite = apply_filters('comments_rewrite_rules', $comments_rewrite); |
|
1557 |
||
1558 |
// Search |
|
1559 |
$search_structure = $this->get_search_permastruct(); |
|
1560 |
$search_rewrite = $this->generate_rewrite_rules($search_structure, EP_SEARCH); |
|
1561 |
$search_rewrite = apply_filters('search_rewrite_rules', $search_rewrite); |
|
1562 |
||
1563 |
// Authors |
|
1564 |
$author_rewrite = $this->generate_rewrite_rules($this->get_author_permastruct(), EP_AUTHORS); |
|
1565 |
$author_rewrite = apply_filters('author_rewrite_rules', $author_rewrite); |
|
1566 |
||
1567 |
// Pages |
|
1568 |
$page_rewrite = $this->page_rewrite_rules(); |
|
1569 |
$page_rewrite = apply_filters('page_rewrite_rules', $page_rewrite); |
|
1570 |
||
1571 |
// Extra permastructs |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1572 |
foreach ( $this->extra_permastructs as $permastructname => $struct ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1573 |
if ( is_array( $struct ) ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1574 |
if ( count( $struct ) == 2 ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1575 |
$rules = $this->generate_rewrite_rules( $struct[0], $struct[1] ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1576 |
else |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1577 |
$rules = $this->generate_rewrite_rules( $struct['struct'], $struct['ep_mask'], $struct['paged'], $struct['feed'], $struct['forcomments'], $struct['walk_dirs'], $struct['endpoints'] ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1578 |
} else { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1579 |
$rules = $this->generate_rewrite_rules( $struct ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1580 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1581 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1582 |
$rules = apply_filters($permastructname . '_rewrite_rules', $rules); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1583 |
if ( 'post_tag' == $permastructname ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1584 |
$rules = apply_filters('tag_rewrite_rules', $rules); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1585 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1586 |
$this->extra_rules_top = array_merge($this->extra_rules_top, $rules); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1587 |
} |
136 | 1588 |
|
1589 |
// Put them together. |
|
1590 |
if ( $this->use_verbose_page_rules ) |
|
204
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
1591 |
$this->rules = array_merge($this->extra_rules_top, $robots_rewrite, $deprecated_files, $registration_pages, $root_rewrite, $comments_rewrite, $search_rewrite, $author_rewrite, $date_rewrite, $page_rewrite, $post_rewrite, $this->extra_rules); |
136 | 1592 |
else |
204
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
1593 |
$this->rules = array_merge($this->extra_rules_top, $robots_rewrite, $deprecated_files, $registration_pages, $root_rewrite, $comments_rewrite, $search_rewrite, $author_rewrite, $date_rewrite, $post_rewrite, $page_rewrite, $this->extra_rules); |
136 | 1594 |
|
1595 |
do_action_ref_array('generate_rewrite_rules', array(&$this)); |
|
1596 |
$this->rules = apply_filters('rewrite_rules_array', $this->rules); |
|
1597 |
||
1598 |
return $this->rules; |
|
1599 |
} |
|
1600 |
||
1601 |
/** |
|
1602 |
* Retrieve the rewrite rules. |
|
1603 |
* |
|
1604 |
* The difference between this method and {@link |
|
1605 |
* WP_Rewrite::rewrite_rules()} is that this method stores the rewrite rules |
|
1606 |
* in the 'rewrite_rules' option and retrieves it. This prevents having to |
|
1607 |
* process all of the permalinks to get the rewrite rules in the form of |
|
1608 |
* caching. |
|
1609 |
* |
|
1610 |
* @since 1.5.0 |
|
1611 |
* @access public |
|
1612 |
* |
|
1613 |
* @return array Rewrite rules. |
|
1614 |
*/ |
|
1615 |
function wp_rewrite_rules() { |
|
1616 |
$this->rules = get_option('rewrite_rules'); |
|
1617 |
if ( empty($this->rules) ) { |
|
1618 |
$this->matches = 'matches'; |
|
1619 |
$this->rewrite_rules(); |
|
1620 |
update_option('rewrite_rules', $this->rules); |
|
1621 |
} |
|
1622 |
||
1623 |
return $this->rules; |
|
1624 |
} |
|
1625 |
||
1626 |
/** |
|
1627 |
* Retrieve mod_rewrite formatted rewrite rules to write to .htaccess. |
|
1628 |
* |
|
1629 |
* Does not actually write to the .htaccess file, but creates the rules for |
|
1630 |
* the process that will. |
|
1631 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1632 |
* Will add the non_wp_rules property rules to the .htaccess file before |
136 | 1633 |
* the WordPress rewrite rules one. |
1634 |
* |
|
1635 |
* @since 1.5.0 |
|
1636 |
* @access public |
|
1637 |
* |
|
1638 |
* @return string |
|
1639 |
*/ |
|
1640 |
function mod_rewrite_rules() { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1641 |
if ( ! $this->using_permalinks() ) |
136 | 1642 |
return ''; |
1643 |
||
204
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
1644 |
$site_root = parse_url( site_url() ); |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1645 |
if ( isset( $site_root['path'] ) ) |
136 | 1646 |
$site_root = trailingslashit($site_root['path']); |
1647 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1648 |
$home_root = parse_url(home_url()); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1649 |
if ( isset( $home_root['path'] ) ) |
136 | 1650 |
$home_root = trailingslashit($home_root['path']); |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1651 |
else |
136 | 1652 |
$home_root = '/'; |
1653 |
||
1654 |
$rules = "<IfModule mod_rewrite.c>\n"; |
|
1655 |
$rules .= "RewriteEngine On\n"; |
|
1656 |
$rules .= "RewriteBase $home_root\n"; |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1657 |
$rules .= "RewriteRule ^index\.php$ - [L]\n"; // Prevent -f checks on index.php. |
136 | 1658 |
|
1659 |
//add in the rules that don't redirect to WP's index.php (and thus shouldn't be handled by WP at all) |
|
1660 |
foreach ( (array) $this->non_wp_rules as $match => $query) { |
|
1661 |
// Apache 1.3 does not support the reluctant (non-greedy) modifier. |
|
1662 |
$match = str_replace('.+?', '.+', $match); |
|
1663 |
||
1664 |
// If the match is unanchored and greedy, prepend rewrite conditions |
|
1665 |
// to avoid infinite redirects and eclipsing of real files. |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1666 |
//if ($match == '(.+)/?$' || $match == '([^/]+)/?$' ) { |
136 | 1667 |
//nada. |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1668 |
//} |
136 | 1669 |
|
1670 |
$rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA,L]\n"; |
|
1671 |
} |
|
1672 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1673 |
if ( $this->use_verbose_rules ) { |
136 | 1674 |
$this->matches = ''; |
1675 |
$rewrite = $this->rewrite_rules(); |
|
1676 |
$num_rules = count($rewrite); |
|
1677 |
$rules .= "RewriteCond %{REQUEST_FILENAME} -f [OR]\n" . |
|
1678 |
"RewriteCond %{REQUEST_FILENAME} -d\n" . |
|
1679 |
"RewriteRule ^.*$ - [S=$num_rules]\n"; |
|
1680 |
||
1681 |
foreach ( (array) $rewrite as $match => $query) { |
|
1682 |
// Apache 1.3 does not support the reluctant (non-greedy) modifier. |
|
1683 |
$match = str_replace('.+?', '.+', $match); |
|
1684 |
||
1685 |
// If the match is unanchored and greedy, prepend rewrite conditions |
|
1686 |
// to avoid infinite redirects and eclipsing of real files. |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1687 |
//if ($match == '(.+)/?$' || $match == '([^/]+)/?$' ) { |
136 | 1688 |
//nada. |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1689 |
//} |
136 | 1690 |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1691 |
if ( strpos($query, $this->index) !== false ) |
136 | 1692 |
$rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA,L]\n"; |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1693 |
else |
136 | 1694 |
$rules .= 'RewriteRule ^' . $match . ' ' . $site_root . $query . " [QSA,L]\n"; |
1695 |
} |
|
1696 |
} else { |
|
1697 |
$rules .= "RewriteCond %{REQUEST_FILENAME} !-f\n" . |
|
1698 |
"RewriteCond %{REQUEST_FILENAME} !-d\n" . |
|
1699 |
"RewriteRule . {$home_root}{$this->index} [L]\n"; |
|
1700 |
} |
|
1701 |
||
1702 |
$rules .= "</IfModule>\n"; |
|
1703 |
||
1704 |
$rules = apply_filters('mod_rewrite_rules', $rules); |
|
1705 |
$rules = apply_filters('rewrite_rules', $rules); // Deprecated |
|
1706 |
||
1707 |
return $rules; |
|
1708 |
} |
|
1709 |
||
1710 |
/** |
|
1711 |
* Retrieve IIS7 URL Rewrite formatted rewrite rules to write to web.config file. |
|
1712 |
* |
|
1713 |
* Does not actually write to the web.config file, but creates the rules for |
|
1714 |
* the process that will. |
|
1715 |
* |
|
1716 |
* @since 2.8.0 |
|
1717 |
* @access public |
|
1718 |
* |
|
1719 |
* @return string |
|
1720 |
*/ |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1721 |
function iis7_url_rewrite_rules( $add_parent_tags = false ) { |
136 | 1722 |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1723 |
if ( ! $this->using_permalinks() ) |
136 | 1724 |
return ''; |
1725 |
$rules = ''; |
|
1726 |
if ( $add_parent_tags ) { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1727 |
$rules .= '<configuration> |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1728 |
<system.webServer> |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1729 |
<rewrite> |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1730 |
<rules>'; |
136 | 1731 |
} |
204
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
1732 |
|
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
1733 |
$rules .= ' |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
1734 |
<rule name="wordpress" patternSyntax="Wildcard"> |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
1735 |
<match url="*" /> |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
1736 |
<conditions> |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
1737 |
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
1738 |
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1739 |
</conditions> |
204
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
1740 |
<action type="Rewrite" url="index.php" /> |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
1741 |
</rule>'; |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
1742 |
|
136 | 1743 |
if ( $add_parent_tags ) { |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1744 |
$rules .= ' |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1745 |
</rules> |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1746 |
</rewrite> |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1747 |
</system.webServer> |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1748 |
</configuration>'; |
136 | 1749 |
} |
1750 |
||
1751 |
$rules = apply_filters('iis7_url_rewrite_rules', $rules); |
|
1752 |
||
1753 |
return $rules; |
|
1754 |
} |
|
1755 |
||
1756 |
/** |
|
1757 |
* Add a straight rewrite rule. |
|
1758 |
* |
|
1759 |
* Any value in the $after parameter that isn't 'bottom' will be placed at |
|
1760 |
* the top of the rules. |
|
1761 |
* |
|
1762 |
* @since 2.1.0 |
|
1763 |
* @access public |
|
1764 |
* |
|
1765 |
* @param string $regex Regular expression to match against request. |
|
1766 |
* @param string $redirect URL regex redirects to when regex matches request. |
|
1767 |
* @param string $after Optional, default is bottom. Location to place rule. |
|
1768 |
*/ |
|
1769 |
function add_rule($regex, $redirect, $after = 'bottom') { |
|
1770 |
//get everything up to the first ? |
|
1771 |
$index = (strpos($redirect, '?') == false ? strlen($redirect) : strpos($redirect, '?')); |
|
1772 |
$front = substr($redirect, 0, $index); |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1773 |
if ( $front != $this->index ) { //it doesn't redirect to WP's index.php |
136 | 1774 |
$this->add_external_rule($regex, $redirect); |
1775 |
} else { |
|
1776 |
if ( 'bottom' == $after) |
|
1777 |
$this->extra_rules = array_merge($this->extra_rules, array($regex => $redirect)); |
|
1778 |
else |
|
1779 |
$this->extra_rules_top = array_merge($this->extra_rules_top, array($regex => $redirect)); |
|
1780 |
//$this->extra_rules[$regex] = $redirect; |
|
1781 |
} |
|
1782 |
} |
|
1783 |
||
1784 |
/** |
|
1785 |
* Add a rule that doesn't redirect to index.php. |
|
1786 |
* |
|
1787 |
* Can redirect to any place. |
|
1788 |
* |
|
1789 |
* @since 2.1.0 |
|
1790 |
* @access public |
|
1791 |
* |
|
1792 |
* @param string $regex Regular expression to match against request. |
|
1793 |
* @param string $redirect URL regex redirects to when regex matches request. |
|
1794 |
*/ |
|
1795 |
function add_external_rule($regex, $redirect) { |
|
1796 |
$this->non_wp_rules[$regex] = $redirect; |
|
1797 |
} |
|
1798 |
||
1799 |
/** |
|
1800 |
* Add an endpoint, like /trackback/. |
|
1801 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1802 |
* See {@link add_rewrite_endpoint()} for full documentation. |
136 | 1803 |
* |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1804 |
* @see add_rewrite_endpoint() |
136 | 1805 |
* @since 2.1.0 |
1806 |
* @access public |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1807 |
* @uses WP::add_query_var() |
136 | 1808 |
* |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1809 |
* @param string $name Name of the endpoint. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1810 |
* @param int $places Endpoint mask describing the places the endpoint should be added. |
136 | 1811 |
*/ |
1812 |
function add_endpoint($name, $places) { |
|
1813 |
global $wp; |
|
1814 |
$this->endpoints[] = array ( $places, $name ); |
|
1815 |
$wp->add_query_var($name); |
|
1816 |
} |
|
1817 |
||
1818 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1819 |
* Add a new permalink structure. |
136 | 1820 |
* |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1821 |
* A permalink structure (permastruct) is an abstract definition of a set of rewrite rules; it |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1822 |
* is an easy way of expressing a set of regular expressions that rewrite to a set of query strings. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1823 |
* The new permastruct is added to the {@link WP_Rewrite::$extra_permastructs} array. When the |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1824 |
* rewrite rules are built by {@link WP_Rewrite::rewrite_rules()} all of these extra permastructs |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1825 |
* are passed to {@link WP_Rewrite::generate_rewrite_rules()} which transforms them into the |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1826 |
* regular expressions that many love to hate. |
136 | 1827 |
* |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1828 |
* The $args parameter gives you control over how {@link WP_Rewrite::generate_rewrite_rules()} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1829 |
* works on the new permastruct. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1830 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1831 |
* @since 2.5.0 |
136 | 1832 |
* @access public |
1833 |
* |
|
1834 |
* @param string $name Name for permalink structure. |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1835 |
* @param string $struct Permalink structure (e.g. category/%category%) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1836 |
* @param array $args Optional configuration for building the rules from the permalink structure: |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1837 |
* - with_front (bool) - Should the structure be prepended with WP_Rewrite::$front? Default is true. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1838 |
* - ep_mask (int) - Endpoint mask defining what endpoints are added to the structure. Default is EP_NONE. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1839 |
* - paged (bool) - Should archive pagination rules be added for the structure? Default is true. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1840 |
* - feed (bool) - Should feed rewrite rules be added for the structure? Default is true. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1841 |
* - forcomments (bool) - Should the feed rules be a query for a comments feed? Default is false. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1842 |
* - walk_dirs (bool) - Should the 'directories' making up the structure be walked over and rewrite |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1843 |
* rules built for each in turn? Default is true. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1844 |
* - endpoints (bool) - Should endpoints be applied to the generated rewrite rules? Default is true. |
136 | 1845 |
*/ |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1846 |
function add_permastruct( $name, $struct, $args = array() ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1847 |
// backwards compatibility for the old parameters: $with_front and $ep_mask |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1848 |
if ( ! is_array( $args ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1849 |
$args = array( 'with_front' => $args ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1850 |
if ( func_num_args() == 4 ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1851 |
$args['ep_mask'] = func_get_arg( 3 ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1852 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1853 |
$defaults = array( |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1854 |
'with_front' => true, |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1855 |
'ep_mask' => EP_NONE, |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1856 |
'paged' => true, |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1857 |
'feed' => true, |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1858 |
'forcomments' => false, |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1859 |
'walk_dirs' => true, |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1860 |
'endpoints' => true, |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1861 |
); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1862 |
$args = array_intersect_key( $args, $defaults ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1863 |
$args = wp_parse_args( $args, $defaults ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1864 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1865 |
if ( $args['with_front'] ) |
136 | 1866 |
$struct = $this->front . $struct; |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1867 |
else |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1868 |
$struct = $this->root . $struct; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1869 |
$args['struct'] = $struct; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1870 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1871 |
$this->extra_permastructs[ $name ] = $args; |
136 | 1872 |
} |
1873 |
||
1874 |
/** |
|
1875 |
* Remove rewrite rules and then recreate rewrite rules. |
|
1876 |
* |
|
1877 |
* Calls {@link WP_Rewrite::wp_rewrite_rules()} after removing the |
|
1878 |
* 'rewrite_rules' option. If the function named 'save_mod_rewrite_rules' |
|
1879 |
* exists, it will be called. |
|
1880 |
* |
|
1881 |
* @since 2.0.1 |
|
1882 |
* @access public |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1883 |
* @param bool $hard Whether to update .htaccess (hard flush) or just update rewrite_rules option (soft flush). Default is true (hard). |
136 | 1884 |
*/ |
1885 |
function flush_rules($hard = true) { |
|
1886 |
delete_option('rewrite_rules'); |
|
1887 |
$this->wp_rewrite_rules(); |
|
1888 |
if ( $hard && function_exists('save_mod_rewrite_rules') ) |
|
1889 |
save_mod_rewrite_rules(); |
|
1890 |
if ( $hard && function_exists('iis7_save_url_rewrite_rules') ) |
|
1891 |
iis7_save_url_rewrite_rules(); |
|
1892 |
} |
|
1893 |
||
1894 |
/** |
|
1895 |
* Sets up the object's properties. |
|
1896 |
* |
|
1897 |
* The 'use_verbose_page_rules' object property will be set to true if the |
|
1898 |
* permalink structure begins with one of the following: '%postname%', '%category%', |
|
1899 |
* '%tag%', or '%author%'. |
|
1900 |
* |
|
1901 |
* @since 1.5.0 |
|
1902 |
* @access public |
|
1903 |
*/ |
|
1904 |
function init() { |
|
1905 |
$this->extra_rules = $this->non_wp_rules = $this->endpoints = array(); |
|
1906 |
$this->permalink_structure = get_option('permalink_structure'); |
|
1907 |
$this->front = substr($this->permalink_structure, 0, strpos($this->permalink_structure, '%')); |
|
1908 |
$this->root = ''; |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1909 |
if ( $this->using_index_permalinks() ) |
136 | 1910 |
$this->root = $this->index . '/'; |
1911 |
unset($this->author_structure); |
|
1912 |
unset($this->date_structure); |
|
1913 |
unset($this->page_structure); |
|
1914 |
unset($this->search_structure); |
|
1915 |
unset($this->feed_structure); |
|
1916 |
unset($this->comment_feed_structure); |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1917 |
$this->use_trailing_slashes = ( '/' == substr($this->permalink_structure, -1, 1) ); |
136 | 1918 |
|
1919 |
// Enable generic rules for pages if permalink structure doesn't begin with a wildcard. |
|
1920 |
if ( preg_match("/^[^%]*%(?:postname|category|tag|author)%/", $this->permalink_structure) ) |
|
1921 |
$this->use_verbose_page_rules = true; |
|
1922 |
else |
|
1923 |
$this->use_verbose_page_rules = false; |
|
1924 |
} |
|
1925 |
||
1926 |
/** |
|
1927 |
* Set the main permalink structure for the blog. |
|
1928 |
* |
|
1929 |
* Will update the 'permalink_structure' option, if there is a difference |
|
1930 |
* between the current permalink structure and the parameter value. Calls |
|
1931 |
* {@link WP_Rewrite::init()} after the option is updated. |
|
1932 |
* |
|
1933 |
* Fires the 'permalink_structure_changed' action once the init call has |
|
1934 |
* processed passing the old and new values |
|
1935 |
* |
|
1936 |
* @since 1.5.0 |
|
1937 |
* @access public |
|
1938 |
* |
|
1939 |
* @param string $permalink_structure Permalink structure. |
|
1940 |
*/ |
|
1941 |
function set_permalink_structure($permalink_structure) { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1942 |
if ( $permalink_structure != $this->permalink_structure ) { |
204
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
1943 |
$old_permalink_structure = $this->permalink_structure; |
136 | 1944 |
update_option('permalink_structure', $permalink_structure); |
1945 |
$this->init(); |
|
204
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
1946 |
do_action('permalink_structure_changed', $old_permalink_structure, $permalink_structure); |
136 | 1947 |
} |
1948 |
} |
|
1949 |
||
1950 |
/** |
|
1951 |
* Set the category base for the category permalink. |
|
1952 |
* |
|
1953 |
* Will update the 'category_base' option, if there is a difference between |
|
1954 |
* the current category base and the parameter value. Calls |
|
1955 |
* {@link WP_Rewrite::init()} after the option is updated. |
|
1956 |
* |
|
1957 |
* @since 1.5.0 |
|
1958 |
* @access public |
|
1959 |
* |
|
1960 |
* @param string $category_base Category permalink structure base. |
|
1961 |
*/ |
|
1962 |
function set_category_base($category_base) { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1963 |
if ( $category_base != get_option('category_base') ) { |
136 | 1964 |
update_option('category_base', $category_base); |
1965 |
$this->init(); |
|
1966 |
} |
|
1967 |
} |
|
1968 |
||
1969 |
/** |
|
1970 |
* Set the tag base for the tag permalink. |
|
1971 |
* |
|
1972 |
* Will update the 'tag_base' option, if there is a difference between the |
|
1973 |
* current tag base and the parameter value. Calls |
|
1974 |
* {@link WP_Rewrite::init()} after the option is updated. |
|
1975 |
* |
|
1976 |
* @since 2.3.0 |
|
1977 |
* @access public |
|
1978 |
* |
|
1979 |
* @param string $tag_base Tag permalink structure base. |
|
1980 |
*/ |
|
1981 |
function set_tag_base( $tag_base ) { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1982 |
if ( $tag_base != get_option( 'tag_base') ) { |
136 | 1983 |
update_option( 'tag_base', $tag_base ); |
1984 |
$this->init(); |
|
1985 |
} |
|
1986 |
} |
|
1987 |
||
1988 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1989 |
* Constructor - Calls init(), which runs setup. |
136 | 1990 |
* |
1991 |
* @since 1.5.0 |
|
1992 |
* @access public |
|
1993 |
* |
|
1994 |
* @return WP_Rewrite |
|
1995 |
*/ |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1996 |
function __construct() { |
136 | 1997 |
$this->init(); |
1998 |
} |
|
1999 |
} |