Taxonomy: add a new 'terms_pre_query' filter to short circuit WP_Term_Query 'get_terms' queries.

Add a new `terms_pre_query` filter which returns null by default. Return a non-null value to bypass WordPress's default `get_terms` queries.

Props jarocks, boonebgorges, spacedmonkey.
Fixes #41246.



git-svn-id: https://develop.svn.wordpress.org/trunk@45584 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Adam Silverstein
2019-07-01 20:47:21 +00:00
parent 9a1549767e
commit b97899a07f
2 changed files with 50 additions and 0 deletions

View File

@@ -737,4 +737,34 @@ class Tests_Term_Query extends WP_UnitTestCase {
return $term;
}
/**
* @ticket 41246
*/
public function test_terms_pre_query_filter_should_bypass_database_query() {
global $wpdb;
add_filter( 'terms_pre_query', array( __CLASS__, 'filter_terms_pre_query' ), 10, 2 );
$num_queries = $wpdb->num_queries;
$q = new WP_Term_Query();
$results = $q->query(
array(
'fields' => 'ids',
)
);
remove_filter( 'terms_pre_query', array( __CLASS__, 'filter_terms_pre_query' ), 10, 2 );
// Make sure no queries were executed.
$this->assertSame( $num_queries, $wpdb->num_queries );
// We manually inserted a non-existing term and overrode the results with it.
$this->assertSame( array( 555 ), $q->terms );
}
public static function filter_terms_pre_query( $terms, $query ) {
return array( 555 );
}
}