diff --git src/wp-includes/class-wp-comment-query.php src/wp-includes/class-wp-comment-query.php
index eba9ddf54e..28ac6082e3 100644
|
|
|
class WP_Comment_Query { |
| 379 | 379 | $this->meta_query_clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this ); |
| 380 | 380 | } |
| 381 | 381 | |
| | 382 | $comment_data = null; |
| | 383 | |
| | 384 | /** |
| | 385 | * Filter the comments data before the query takes place. |
| | 386 | * |
| | 387 | * Return a non-null value to bypass WordPress's default comment queries. |
| | 388 | * |
| | 389 | * The expected return type from this filter depends on the value passed in the request query_vars: |
| | 390 | * When $this->query_vars['count'] is set, the filter should return the comment count as an int. |
| | 391 | * When `'ids' == $this->query_vars['fields']`, the filter should return an array of comment ids. |
| | 392 | * Otherwise the filter should return an array of WP_Comment objects. |
| | 393 | * |
| | 394 | * @since 5.3.0 |
| | 395 | * |
| | 396 | * @param array|int|null $comment_data Return an array of comment data to short-circuit WP's comment query, |
| | 397 | * or null to allow WP to run its normal queries. |
| | 398 | * @param WP_Comment_Query $this The WP_Comment_Query instance, passed by reference. |
| | 399 | */ |
| | 400 | $comment_data = apply_filters_ref_array( 'comments_pre_query', array( $comment_data, &$this ) ); |
| | 401 | |
| | 402 | if ( null !== $comment_data ) { |
| | 403 | return $comment_data; |
| | 404 | } |
| | 405 | |
| 382 | 406 | /* |
| 383 | 407 | * Only use the args defined in the query_var_defaults to compute the key, |
| 384 | 408 | * but ignore 'fields', which does not affect query results. |
diff --git tests/phpunit/tests/comment/query.php tests/phpunit/tests/comment/query.php
index 9966f2d1bd..372fa475c4 100644
|
|
|
class Tests_Comment_Query extends WP_UnitTestCase { |
| 4882 | 4882 | |
| 4883 | 4883 | $this->assertEqualSets( $c1, $found ); |
| 4884 | 4884 | } |
| | 4885 | |
| | 4886 | /** |
| | 4887 | * @ticket 45800 |
| | 4888 | */ |
| | 4889 | public function test_comments_pre_query_filter_should_bypass_database_query() { |
| | 4890 | global $wpdb; |
| | 4891 | |
| | 4892 | add_filter( 'comments_pre_query', array( __CLASS__, 'filter_comments_pre_query' ), 10, 2 ); |
| | 4893 | |
| | 4894 | $num_queries = $wpdb->num_queries; |
| | 4895 | |
| | 4896 | $q = new WP_Comment_Query(); |
| | 4897 | $results = $q->query( array() ); |
| | 4898 | |
| | 4899 | remove_filter( 'comments_pre_query', array( __CLASS__, 'filter_comments_pre_query' ), 10, 2 ); |
| | 4900 | |
| | 4901 | // Make sure no queries were executed. |
| | 4902 | $this->assertSame( $num_queries, $wpdb->num_queries ); |
| | 4903 | |
| | 4904 | // We manually inserted a non-existing site and overrode the results with it. |
| | 4905 | $this->assertSame( array( 555 ), $results ); |
| | 4906 | |
| | 4907 | // Make sure manually setting total_users doesn't get overwritten. |
| | 4908 | $this->assertEquals( 1, $q->found_comments ); |
| | 4909 | } |
| | 4910 | |
| | 4911 | public static function filter_comments_pre_query( $comments, $query ) { |
| | 4912 | $query->found_comments = 1; |
| | 4913 | |
| | 4914 | return array( 555 ); |
| | 4915 | } |
| 4885 | 4916 | } |