Make WordPress Core

Changeset 52814

Timestamp:
03/02/2022 03:10:48 PM (2 years ago)
Author:
spacedmonkey
Message:

Bootstrap/Load: Stop unnecessary queries when using the do_parse_request filter.

Developers of plugins and themes can use the do_parse_request filter to hot-wire requests and hook in early to render custom pages. However, even through these request may not need post queries and 404 lookups to be run, they run anyway. This can results in unnecessary SQL queries running on these requests. By adding a return value to the parse_request method of the WP class, these queries can now be skipped.

Props junsuijin, ryan, westi, sivel, dd32, wonderboymusic, arnee, tyxla, DrewAPicture, lukecavanagh, SergeyBiryukov, davidbaumwald, Spacedmonkey, pbearne.
Fixes #10886.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp.php

    r52805 r52814  
    130130     *
    131131     * @param array|string $extra_query_vars Set the extra query variables.
     132
    132133     */
    133134    public function parse_request( $extra_query_vars = '' ) {
     
    144145         */
    145146        if ( ! apply_filters( 'do_parse_request', true, $this, $extra_query_vars ) ) {
    146             return;
     147            return;
    147148        }
    148149
     
    395396         */
    396397        do_action_ref_array( 'parse_request', array( &$this ) );
     398
     399
    397400    }
    398401
     
    756759    public function main( $query_args = '' ) {
    757760        $this->init();
    758         $this->parse_request( $query_args );
     761        $this->parse_request( $query_args );
    759762        $this->send_headers();
    760         $this->query_posts();
    761         $this->handle_404();
    762         $this->register_globals();
    763 
     763        if ( $parsed ) {
     764            $this->query_posts();
     765            $this->handle_404();
     766            $this->register_globals();
     767        }
    764768        /**
    765769         * Fires once the WordPress environment has been set up.
  • trunk/tests/phpunit/tests/wp/parseRequest.php

    r51622 r52814  
    4040        $this->assertSame( '', $this->wp->request );
    4141    }
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
    4258}
Note: See TracChangeset for help on using the changeset viewer.