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