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.


Built from https://develop.svn.wordpress.org/trunk@52814


git-svn-id: http://core.svn.wordpress.org/trunk@52403 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
spacedmonkey 2022-03-02 15:12:02 +00:00
parent f891f6f0c6
commit 01040717aa
2 changed files with 11 additions and 7 deletions

View File

@ -129,6 +129,7 @@ class WP {
* @global WP_Rewrite $wp_rewrite WordPress rewrite component. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
* *
* @param array|string $extra_query_vars Set the extra query variables. * @param array|string $extra_query_vars Set the extra query variables.
* @return bool Whether the request was parsed.
*/ */
public function parse_request( $extra_query_vars = '' ) { public function parse_request( $extra_query_vars = '' ) {
global $wp_rewrite; global $wp_rewrite;
@ -143,7 +144,7 @@ class WP {
* @param array|string $extra_query_vars Extra passed query variables. * @param array|string $extra_query_vars Extra passed query variables.
*/ */
if ( ! apply_filters( 'do_parse_request', true, $this, $extra_query_vars ) ) { if ( ! apply_filters( 'do_parse_request', true, $this, $extra_query_vars ) ) {
return; return false;
} }
$this->query_vars = array(); $this->query_vars = array();
@ -394,6 +395,8 @@ class WP {
* @param WP $wp Current WordPress environment instance (passed by reference). * @param WP $wp Current WordPress environment instance (passed by reference).
*/ */
do_action_ref_array( 'parse_request', array( &$this ) ); do_action_ref_array( 'parse_request', array( &$this ) );
return true;
} }
/** /**
@ -755,12 +758,13 @@ class WP {
*/ */
public function main( $query_args = '' ) { public function main( $query_args = '' ) {
$this->init(); $this->init();
$this->parse_request( $query_args ); $parsed = $this->parse_request( $query_args );
$this->send_headers(); $this->send_headers();
if ( $parsed ) {
$this->query_posts(); $this->query_posts();
$this->handle_404(); $this->handle_404();
$this->register_globals(); $this->register_globals();
}
/** /**
* Fires once the WordPress environment has been set up. * Fires once the WordPress environment has been set up.
* *

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.0-alpha-52813'; $wp_version = '6.0-alpha-52814';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.