Comments: add a new comments_pre_query filter to short circuit WP_Comment_Query 'get_comments' queries.

Return a non-null value to bypass WordPress's default comment queries.

Props felipeelia, spacedmonkey.
Fixes #45800.



git-svn-id: https://develop.svn.wordpress.org/trunk@46086 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Adam Silverstein
2019-09-10 18:41:03 +00:00
parent 23b6ca3bcb
commit 4ba54c2104
2 changed files with 55 additions and 0 deletions

View File

@@ -4882,4 +4882,35 @@ class Tests_Comment_Query extends WP_UnitTestCase {
$this->assertEqualSets( $c1, $found );
}
/**
* @ticket 45800
*/
public function test_comments_pre_query_filter_should_bypass_database_query() {
global $wpdb;
add_filter( 'comments_pre_query', array( __CLASS__, 'filter_comments_pre_query' ), 10, 2 );
$num_queries = $wpdb->num_queries;
$q = new WP_Comment_Query();
$results = $q->query( array() );
remove_filter( 'comments_pre_query', array( __CLASS__, 'filter_comments_pre_query' ), 10, 2 );
// Make sure no queries were executed.
$this->assertSame( $num_queries, $wpdb->num_queries );
// We manually inserted a non-existing site and overrode the results with it.
$this->assertSame( array( 555 ), $results );
// Make sure manually setting total_users doesn't get overwritten.
$this->assertEquals( 1, $q->found_comments );
}
public static function filter_comments_pre_query( $comments, $query ) {
$query->found_comments = 1;
return array( 555 );
}
}