Ticket #36292: rewrites.diff
| File rewrites.diff, 10.8 KB (added by , 10 years ago) |
|---|
-
src/wp-includes/class-wp-rewrite-rule.php
1 <?php 2 3 /** 4 * Rewrite rule handler. 5 */ 6 class WP_Rewrite_Rule implements WP_Rewrite_RuleInterface { 7 /** 8 * Traditional query string/array. 9 * 10 * @var string|array `index.php`-prefixed query string, or query argument map. 11 */ 12 public $query = null; 13 14 /** 15 * Should this rule skip the main query? 16 * 17 * @var bool 18 */ 19 public $skip_main_query = false; 20 21 /** 22 * Constructor. 23 * 24 * @param string|array $query Query string/argument map. 25 */ 26 public function __construct( $query = null ) { 27 if ( $query ) { 28 $this->query = $query; 29 } 30 } 31 32 /** 33 * Get string representation of the rule. 34 * 35 * Primarily for backwards compatibility. 36 * 37 * @return string String representation of the rewrite rule. 38 */ 39 public function __toString() { 40 return is_array( $this->query ) ? 'index.php?' . build_query( $this->query ) : $this->query; 41 } 42 43 /** 44 * Should this rule have query vars parsed? 45 * 46 * @return bool 47 */ 48 public function should_parse_query() { 49 return (bool) $this->query; 50 } 51 52 /** 53 * Should this rule skip the main query? 54 * 55 * @return bool 56 */ 57 public function should_skip_main_query() { 58 return $this->skip_main_query; 59 } 60 61 /** 62 * Parse pattern matches into query string. 63 * 64 * @param array $matches Regex matched groups from `$this->pattern` 65 * @return string|array Query vars. 66 */ 67 public function get_query_vars( $matches ) { 68 if ( ! $this->query ) { 69 return ''; 70 } 71 72 // Trim the query of everything up to the '?'. 73 $query = preg_replace( '!^.+\?!', '', $this->query ); 74 75 // Substitute the substring matches into the query. 76 $query = addslashes( WP_MatchesMapRegex::apply( $query, $matches ) ); 77 78 return $query; 79 } 80 81 /** 82 * Get verbose page match parameter. 83 * 84 * @return int|bool Match offset number, or false if non-verbose page pattern. 85 */ 86 public function get_verbose_page_match() { 87 if ( ! $this->query ) { 88 return false; 89 } 90 91 $matches = preg_match( '/pagename=\$matches\[([0-9]+)\]/', $this->query, $varmatch ); 92 if ( ! $matches ) { 93 return false; 94 } 95 96 return $varmatch; 97 } 98 } -
src/wp-includes/class-wp-rewrite-ruleinterface.php
1 <?php 2 3 interface WP_Rewrite_RuleInterface { 4 /** 5 * Should this rule have query vars parsed? 6 * 7 * @return bool 8 */ 9 public function should_parse_query(); 10 11 /** 12 * Parse pattern matches into query string. 13 * 14 * @param array $matches Regex matched groups from `$this->pattern` 15 * @return string|array Query vars. 16 */ 17 public function get_query_vars( $matches ); 18 19 /** 20 * Get verbose page match parameter. 21 * 22 * @return int|bool Match offset number, or false if non-verbose page pattern. 23 */ 24 public function get_verbose_page_match(); 25 } -
src/wp-includes/class-wp-rewrite.php
1454 1454 */ 1455 1455 $this->rules = apply_filters( 'rewrite_rules_array', $this->rules ); 1456 1456 1457 // Upgrade registered rules to Rule objects 1458 $this->rules = $this->upgrade_rules_to_objects( $this->rules ); 1459 1457 1460 return $this->rules; 1458 1461 } 1459 1462 1460 1463 /** 1464 * Convert string-based query-var rules to objects. 1465 * 1466 * @param array $rules Map of pattern => string rule or Rule object. 1467 * @return array Map of pattern => Rule object. 1468 */ 1469 protected static function upgrade_rules_to_objects( $rules ) { 1470 foreach ( $rules as $pattern => &$rule ) { 1471 if ( ! is_string( $rule ) && ! is_array( $rule ) ) { 1472 continue; 1473 } 1474 1475 $rule = new WP_Rewrite_Rule( $rule ); 1476 } 1477 1478 return $rules; 1479 } 1480 1481 /** 1461 1482 * Retrieves the rewrite rules. 1462 1483 * 1463 1484 * The difference between this method and WP_Rewrite::rewrite_rules() is that … … 1637 1658 * or 'bottom'. Default 'bottom'. 1638 1659 */ 1639 1660 public function add_rule( $regex, $query, $after = 'bottom' ) { 1640 if ( is_array( $query ) ) { 1661 if ( $query instanceof WP_Rewrite_RuleInterface ) { 1662 // Rule objects are for internal rewrites only. 1641 1663 $external = false; 1664 } elseif ( is_array( $query ) ) { 1665 $external = false; 1642 1666 $query = add_query_arg( $query, 'index.php' ); 1643 1667 } else { 1644 1668 $index = false === strpos( $query, '?' ) ? strlen( $query ) : strpos( $query, '?' ); … … 1651 1675 if ( $external ) { 1652 1676 $this->add_external_rule( $regex, $query ); 1653 1677 } else { 1678 // Upgrade to rule object 1679 $rule = $query; 1680 if ( ! ( $rule instanceof WP_Rewrite_RuleInterface ) ) { 1681 $rule = new WP_Rewrite_Rule( $query ); 1682 } 1683 1654 1684 if ( 'bottom' == $after ) { 1655 $this->extra_rules = array_merge( $this->extra_rules, array( $regex => $ query) );1685 $this->extra_rules = array_merge( $this->extra_rules, array( $regex => $rule ) ); 1656 1686 } else { 1657 $this->extra_rules_top = array_merge( $this->extra_rules_top, array( $regex => $ query) );1687 $this->extra_rules_top = array_merge( $this->extra_rules_top, array( $regex => $rule ) ); 1658 1688 } 1659 1689 } 1660 1690 } -
src/wp-includes/class-wp.php
65 65 public $request; 66 66 67 67 /** 68 * Rewrite rule the request matched.68 * Rewrite rule pattern the request matched. 69 69 * 70 70 * @since 2.0.0 71 71 * @access public … … 74 74 public $matched_rule; 75 75 76 76 /** 77 * Rewrite rule object the request matched. 78 * 79 * @access public 80 * @var WP_Rewrite_RuleInterface 81 */ 82 public $matched_rule_object; 83 84 /** 77 85 * Rewrite query the request matched. 78 86 * 79 87 * @since 2.0.0 … … 218 226 // An empty request could only match against ^$ regex 219 227 if ( isset( $rewrite['$'] ) ) { 220 228 $this->matched_rule = '$'; 221 $ query= $rewrite['$'];229 $this->matched_rule_object = $rule = $rewrite['$']; 222 230 $matches = array(''); 223 231 } 224 232 } else { 225 foreach ( (array) $rewrite as $match => $ query) {233 foreach ( (array) $rewrite as $match => $rule ) { 226 234 // If the requesting file is the anchor of the match, prepend it to the path info. 227 235 if ( ! empty($req_uri) && strpos($match, $req_uri) === 0 && $req_uri != $request ) 228 236 $request_match = $req_uri . '/' . $request; … … 230 238 if ( preg_match("#^$match#", $request_match, $matches) || 231 239 preg_match("#^$match#", urldecode($request_match), $matches) ) { 232 240 233 if ( $wp_rewrite->use_verbose_page_rules && preg_match( '/pagename=\$matches\[([0-9]+)\]/', $query, $varmatch ) ) { 241 $varmatch = $rule->get_verbose_page_match(); 242 if ( $wp_rewrite->use_verbose_page_rules && $varmatch ) { 234 243 // This is a verbose page match, let's check to be sure about it. 235 244 $page = get_page_by_path( $matches[ $varmatch[1] ] ); 236 245 if ( ! $page ) { … … 246 255 247 256 // Got a match. 248 257 $this->matched_rule = $match; 258 $this->matched_rule_object = $rule; 249 259 break; 250 260 } 251 261 } 252 262 } 253 263 254 if ( isset( $this->matched_rule ) ) { 255 // Trim the query of everything up to the '?'. 256 $query = preg_replace("!^.+\?!", '', $query); 257 264 if ( isset( $this->matched_rule ) && $rule->should_parse_query() ) { 258 265 // Substitute the substring matches into the query. 259 $ query = addslashes(WP_MatchesMapRegex::apply($query, $matches));266 $this->matched_query = $query = $rule->get_query_vars( $matches ); 260 267 261 $this->matched_query = $query;262 263 268 // Parse the query. 264 269 parse_str($query, $perma_query_vars); 265 270 … … 279 284 } 280 285 } 281 286 287 if ( ! $rule || $rule->should_parse_query() ) { 288 $this->generate_query_vars( $perma_query_vars ); 289 } 290 282 291 /** 292 * Filter the array of parsed query variables. 293 * 294 * @since 2.1.0 295 * 296 * @param array $query_vars The array of requested query variables. 297 */ 298 $this->query_vars = apply_filters( 'request', $this->query_vars ); 299 300 /** 301 * Fires once all query variables for the current request have been parsed. 302 * 303 * @since 2.1.0 304 * 305 * @param WP &$this Current WordPress environment instance (passed by reference). 306 */ 307 do_action_ref_array( 'parse_request', array( &$this ) ); 308 } 309 310 protected function generate_query_vars( $perma_query_vars ) { 311 /** 283 312 * Filter the query variables whitelist before processing. 284 313 * 285 314 * Allows (publicly allowed) query vars to be added, removed, or changed prior … … 365 394 366 395 if ( isset($error) ) 367 396 $this->query_vars['error'] = $error; 368 369 /**370 * Filter the array of parsed query variables.371 *372 * @since 2.1.0373 *374 * @param array $query_vars The array of requested query variables.375 */376 $this->query_vars = apply_filters( 'request', $this->query_vars );377 378 /**379 * Fires once all query variables for the current request have been parsed.380 *381 * @since 2.1.0382 *383 * @param WP &$this Current WordPress environment instance (passed by reference).384 */385 do_action_ref_array( 'parse_request', array( &$this ) );386 397 } 387 398 388 399 /** … … 605 616 public function query_posts() { 606 617 global $wp_the_query; 607 618 $this->build_query_string(); 608 $wp_the_query->query( $this->query_vars);619 $wp_the_query->query( $this->query_vars ); 609 620 } 610 621 611 622 /** … … 723 734 $this->init(); 724 735 $this->parse_request($query_args); 725 736 $this->send_headers(); 726 $this->query_posts();727 $this->handle_404();728 $this->register_globals();729 737 738 $skip = $this->matched_rule_object ? $this->matched_rule_object->should_skip_main_query() : false; 739 730 740 /** 741 * Filter whether the main query should be skipped. 742 * 743 * @param bool 744 */ 745 $skip = apply_filters( 'skip_main_query', $skip, $this->matched_rule ); 746 if ( ! $skip ) { 747 $this->query_posts(); 748 $this->handle_404(); 749 $this->register_globals(); 750 } 751 752 /** 731 753 * Fires once the WordPress environment has been set up. 732 754 * 733 755 * @since 2.1.0 -
src/wp-settings.php
158 158 require( ABSPATH . WPINC . '/comment-template.php' ); 159 159 require( ABSPATH . WPINC . '/rewrite.php' ); 160 160 require( ABSPATH . WPINC . '/class-wp-rewrite.php' ); 161 require( ABSPATH . WPINC . '/class-wp-rewrite-ruleinterface.php' ); 162 require( ABSPATH . WPINC . '/class-wp-rewrite-rule.php' ); 161 163 require( ABSPATH . WPINC . '/feed.php' ); 162 164 require( ABSPATH . WPINC . '/bookmark.php' ); 163 165 require( ABSPATH . WPINC . '/bookmark-template.php' );