mirror of
git://develop.git.wordpress.org/
synced 2025-03-21 04:20:01 +01:00
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. git-svn-id: https://develop.svn.wordpress.org/trunk@52814 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
99fd93c7df
commit
69b9940da2
@ -129,6 +129,7 @@ class WP {
|
||||
* @global WP_Rewrite $wp_rewrite WordPress rewrite component.
|
||||
*
|
||||
* @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 = '' ) {
|
||||
global $wp_rewrite;
|
||||
@ -143,7 +144,7 @@ class WP {
|
||||
* @param array|string $extra_query_vars Extra passed query variables.
|
||||
*/
|
||||
if ( ! apply_filters( 'do_parse_request', true, $this, $extra_query_vars ) ) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->query_vars = array();
|
||||
@ -394,6 +395,8 @@ class WP {
|
||||
* @param WP $wp Current WordPress environment instance (passed by reference).
|
||||
*/
|
||||
do_action_ref_array( 'parse_request', array( &$this ) );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -755,12 +758,13 @@ class WP {
|
||||
*/
|
||||
public function main( $query_args = '' ) {
|
||||
$this->init();
|
||||
$this->parse_request( $query_args );
|
||||
$parsed = $this->parse_request( $query_args );
|
||||
$this->send_headers();
|
||||
$this->query_posts();
|
||||
$this->handle_404();
|
||||
$this->register_globals();
|
||||
|
||||
if ( $parsed ) {
|
||||
$this->query_posts();
|
||||
$this->handle_404();
|
||||
$this->register_globals();
|
||||
}
|
||||
/**
|
||||
* Fires once the WordPress environment has been set up.
|
||||
*
|
||||
|
@ -39,4 +39,20 @@ class Tests_WP_ParseRequest extends WP_UnitTestCase {
|
||||
$this->wp->parse_request();
|
||||
$this->assertSame( '', $this->wp->request );
|
||||
}
|
||||
/**
|
||||
* Test that the parse_request() returns bool
|
||||
*
|
||||
* @ticket 10886
|
||||
*/
|
||||
public function test_parse_request_returns_bool() {
|
||||
|
||||
// check if parse_request() returns true for default setup.
|
||||
$this->assertTrue( $this->wp->parse_request(), 'returns true' );
|
||||
|
||||
// add filter to shortcut the parse_request function.
|
||||
add_filter( 'do_parse_request', '__return_false' );
|
||||
$this->assertFalse( $this->wp->parse_request(), 'returns false' );
|
||||
remove_filter( 'do_parse_request', '__return_false' );
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user