Multisite: improve sites_pre_query and networks_pre_query filters, avoiding db queries.

Improve the `pre_query` filters in multisite classes introduced in r44983. Return (non null) values immediately,
avoiding the database queries entirely, similar to other `pre_query` filters.

Props spacedmonkey, SergeyBiryukov, felipeelia.
Fixes #47599.



git-svn-id: https://develop.svn.wordpress.org/trunk@46100 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Adam Silverstein
2019-09-12 22:16:08 +00:00
parent b979a99355
commit 2b4cecf316
4 changed files with 79 additions and 71 deletions
@@ -525,6 +525,7 @@ if ( is_multisite() ) :
/**
* @ticket 45749
* @ticket 47599
*/
public function test_networks_pre_query_filter_should_bypass_database_query() {
global $wpdb;
@@ -534,11 +535,7 @@ if ( is_multisite() ) :
$num_queries = $wpdb->num_queries;
$q = new WP_Network_Query();
$results = $q->query(
array(
'fields' => 'ids',
)
);
$results = $q->query( array() );
remove_filter( 'networks_pre_query', array( __CLASS__, 'filter_networks_pre_query' ), 10, 2 );
@@ -546,7 +543,7 @@ if ( is_multisite() ) :
$this->assertSame( $num_queries, $wpdb->num_queries );
// We manually inserted a non-existing site and overrode the results with it.
$this->assertSame( array( 555 ), $q->networks );
$this->assertSame( array( 555 ), $results );
// Make sure manually setting total_users doesn't get overwritten.
$this->assertEquals( 1, $q->found_networks );
+3 -6
View File
@@ -914,6 +914,7 @@ if ( is_multisite() ) :
/**
* @ticket 45749
* @ticket 47599
*/
public function test_sites_pre_query_filter_should_bypass_database_query() {
global $wpdb;
@@ -923,11 +924,7 @@ if ( is_multisite() ) :
$num_queries = $wpdb->num_queries;
$q = new WP_Site_Query();
$results = $q->query(
array(
'fields' => 'ids',
)
);
$results = $q->query( array() );
remove_filter( 'sites_pre_query', array( __CLASS__, 'filter_sites_pre_query' ), 10, 2 );
@@ -935,7 +932,7 @@ if ( is_multisite() ) :
$this->assertSame( $num_queries, $wpdb->num_queries );
// We manually inserted a non-existing site and overrode the results with it.
$this->assertSame( array( 555 ), $q->sites );
$this->assertSame( array( 555 ), $results );
// Make sure manually setting total_users doesn't get overwritten.
$this->assertEquals( 1, $q->found_sites );