Comments: Assign the array of comment data returned from the comments_pre_query
filter to the comments
property of the current WP_Comment_Query
instance.
This avoids the performance overhead of calling `WP_Comment_Query::get_comments()` twice: first when creating the object instance, then to retrieve the filtered results. This also makes the filter a bit more consistent with other similar filters, e.g. `posts_pre_query`, `terms_pre_query`, or `users_pre_query`. Follow-up to [46086]. Props dinhtungdu, imath, spacedmonkey, adamsilverstein, SergeyBiryukov. Fixes #50521. git-svn-id: https://develop.svn.wordpress.org/trunk@48990 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
7da52b8eae
commit
cb38bc973e
@ -394,7 +394,17 @@ class WP_Comment_Query {
|
||||
* an array of comment IDs.
|
||||
* - Otherwise the filter should return an array of WP_Comment objects.
|
||||
*
|
||||
* Note that if the filter returns an array of comment data, it will be assigned
|
||||
* to the `comments` property of the current WP_Comment_Query instance.
|
||||
*
|
||||
* Filtering functions that require pagination information are encouraged to set
|
||||
* the `found_comments` and `max_num_pages` properties of the WP_Comment_Query object,
|
||||
* passed to the filter by reference. If WP_Comment_Query does not perform a database
|
||||
* query, it will not have enough information to generate these values itself.
|
||||
*
|
||||
* @since 5.3.0
|
||||
* @since 5.6.0 The returned array of comment data is assigned to the `comments` property
|
||||
* of the current WP_Comment_Query instance.
|
||||
*
|
||||
* @param array|int|null $comment_data Return an array of comment data to short-circuit WP's comment query,
|
||||
* the comment count as an integer if `$this->query_vars['count']` is set,
|
||||
@ -404,6 +414,10 @@ class WP_Comment_Query {
|
||||
$comment_data = apply_filters_ref_array( 'comments_pre_query', array( $comment_data, &$this ) );
|
||||
|
||||
if ( null !== $comment_data ) {
|
||||
if ( is_array( $comment_data ) && ! $this->query_vars['count'] ) {
|
||||
$this->comments = $comment_data;
|
||||
}
|
||||
|
||||
return $comment_data;
|
||||
}
|
||||
|
||||
|
@ -4916,4 +4916,33 @@ class Tests_Comment_Query extends WP_UnitTestCase {
|
||||
|
||||
return array( 555 );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 50521
|
||||
*/
|
||||
public function test_comments_pre_query_filter_should_set_comments_property() {
|
||||
add_filter( 'comments_pre_query', array( __CLASS__, 'filter_comments_pre_query_and_set_comments' ), 10, 2 );
|
||||
|
||||
$q = new WP_Comment_Query();
|
||||
$results = $q->query( array() );
|
||||
|
||||
remove_filter( 'comments_pre_query', array( __CLASS__, 'filter_comments_pre_query_and_set_comments' ), 10, 2 );
|
||||
|
||||
// Make sure the comments property is the same as the results.
|
||||
$this->assertSame( $results, $q->comments );
|
||||
|
||||
// Make sure the comment type is `foobar`.
|
||||
$this->assertSame( 'foobar', $q->comments[0]->comment_type );
|
||||
}
|
||||
|
||||
public static function filter_comments_pre_query_and_set_comments( $comments, $query ) {
|
||||
$c = self::factory()->comment->create(
|
||||
array(
|
||||
'comment_type' => 'foobar',
|
||||
'comment_approved' => '1',
|
||||
)
|
||||
);
|
||||
|
||||
return array( get_comment( $c ) );
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user