Make WordPress Core

Ticket #48223: 48223.diff

File 48223.diff, 3.1 KB (added by apedog, 5 years ago)
  • wp-includes/class-wp.php

    diff --git a/wp-includes/class-wp.php b/wp-includes/class-wp.php
    index 508fae2f03..e2ee96404c 100644
    a b class WP { 
    6666         */
    6767        public $matched_rule;
    6868
     69        /**
     70         * Array of matched rules to loop over.
     71         *
     72         * @since x.x.x
     73         * @var string[]
     74         */
     75        public $matched_rules;
     76
     77        /**
     78         * Array of all potential matched rules found.
     79         *
     80         * @since x.x.x
     81         * @var string[]
     82         */
     83        public $all_matched_rules;
     84
    6985        /**
    7086         * Rewrite query the request matched.
    7187         *
    class WP { 
    205221
    206222                        // Look for matches.
    207223                        $request_match = $requested_path;
     224
     225                        if ( empty($this->all_matched_rules) ){
     226                                $this->all_matched_rules = array();
     227                                $this->matched_rules = $this->all_matched_rules;
     228                        }
     229
    208230                        if ( empty( $request_match ) ) {
    209231                                // An empty request could only match against ^$ regex
    210232                                if ( isset( $rewrite['$'] ) ) {
    211                                         $this->matched_rule = '$';
     233                                        $this->all_matched_rules[] = '$';
    212234                                        $query              = $rewrite['$'];
    213235                                        $matches            = array( '' );
    214236                                }
    215                         } else {
     237                        } else if ( empty( $this->all_matched_rules ) ){
    216238                                foreach ( (array) $rewrite as $match => $query ) {
    217239                                        // If the requested file is the anchor of the match, prepend it to the path info.
    218240                                        if ( ! empty( $requested_file ) && strpos( $match, $requested_file ) === 0 && $requested_file != $requested_path ) {
    class WP { 
    236258                                                        }
    237259                                                }
    238260
    239                                                 // Got a match.
    240                                                 $this->matched_rule = $match;
    241                                                 break;
     261                                                // Got a match - add to matched_rules array.
     262                                                $this->all_matched_rules[] = array(
     263                                                        'matches' => $matches,
     264                                                        'match' => $match,
     265                                                        'query' => $query,
     266                                                );
    242267                                        }
     268                                        $this->matched_rules = $this->all_matched_rules;
    243269                                }
    244270                        }
    245271
     272                        // we set $this->matched_rule here
     273                        if (! empty($this->matched_rules)){
     274                                $pop = reset($this->matched_rules);
     275                                array_shift($this->matched_rules);
     276       
     277                                $this->matched_rule = $pop['match'];
     278                                $query = $pop['query'];
     279                                $matches = $pop['matches'];
     280                        }
     281
    246282                        if ( isset( $this->matched_rule ) ) {
    247283                                // Trim the query of everything up to the '?'.
    248284                                $query = preg_replace( '!^.+\?!', '', $query );
    class WP { 
    718754                nocache_headers();
    719755        }
    720756
     757        /**
     758         * Try to parse another rule/query that matches our request.
     759         *
     760         * @since x.x.x
     761         */
     762        function maybe_try_another_matched_rule($preemt, $wp_query){
     763               
     764                if ( empty($wp_query->posts) && !empty($this->matched_rules) ){
     765                       
     766                        // there is some redundancy here - no need to re-parse $_SERVER etc.
     767                        // but we do need to try parsing another matched rule and query
     768                        $this->parse_request();
     769                        $this->query_posts();
     770                        $this->handle_404();
     771                }
     772
     773                return false;
     774        }
     775
    721776        /**
    722777         * Sets up all of the variables required by the WordPress environment.
    723778         *
    class WP { 
    730785         * @param string|array $query_args Passed to parse_request().
    731786         */
    732787        public function main( $query_args = '' ) {
     788
     789                add_action( 'pre_handle_404', array($this, 'maybe_try_another_matched_rule'), 10, 2 );
     790
    733791                $this->init();
    734792                $this->parse_request( $query_args );
    735793                $this->send_headers();