mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-06-28 22:30:04 +00:00
In WP_Comment_Query, parse meta_query vars after the pre_get_comments hook.
[31467] included a change that involved generating meta_query SQL before the `pre_get_comments` hook, with the result that `pre_get_comments` callbacks were no longer able to modify comment meta queries. We fix the problem by moving the SQL generation to after the hook. This changeset also includes a second call to `meta_query->parse_query_vars()`, to ensure that modifications to metadata-related query vars (such as `meta_key` and `meta_value`) performed in `pre_get_comments` callbacks have the expected effect on the comment query. Fixes #32762. git-svn-id: https://develop.svn.wordpress.org/trunk@32911 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -1679,4 +1679,62 @@ class Tests_Comment_Query extends WP_UnitTestCase {
|
||||
|
||||
$this->assertSame( $num_queries, $wpdb->num_queries );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 32762
|
||||
*/
|
||||
public function test_it_should_be_possible_to_modify_meta_query_using_pre_get_comments_action() {
|
||||
$comments = $this->factory->comment->create_many( 2, array(
|
||||
'comment_post_ID' => $this->post_id,
|
||||
) );
|
||||
|
||||
add_comment_meta( $comments[1], 'foo', 'bar' );
|
||||
|
||||
add_action( 'pre_get_comments', array( $this, 'modify_meta_query' ) );
|
||||
|
||||
$q = new WP_Comment_Query( array(
|
||||
'comment_post_ID' => $this->post_id,
|
||||
'fields' => 'ids',
|
||||
) );
|
||||
|
||||
remove_action( 'pre_get_comments', array( $this, 'modify_meta_query' ) );
|
||||
|
||||
$this->assertEqualSets( array( $comments[1] ), $q->comments );
|
||||
}
|
||||
|
||||
public function modify_meta_query( $q ) {
|
||||
$q->meta_query = new WP_Meta_Query( array(
|
||||
array(
|
||||
'key' => 'foo',
|
||||
'value' => 'bar',
|
||||
),
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 32762
|
||||
*/
|
||||
public function test_it_should_be_possible_to_modify_meta_params_using_pre_get_comments_action() {
|
||||
$comments = $this->factory->comment->create_many( 2, array(
|
||||
'comment_post_ID' => $this->post_id,
|
||||
) );
|
||||
|
||||
add_comment_meta( $comments[1], 'foo', 'bar' );
|
||||
|
||||
add_action( 'pre_get_comments', array( $this, 'modify_meta_params' ) );
|
||||
|
||||
$q = new WP_Comment_Query( array(
|
||||
'comment_post_ID' => $this->post_id,
|
||||
'fields' => 'ids',
|
||||
) );
|
||||
|
||||
remove_action( 'pre_get_comments', array( $this, 'modify_meta_params' ) );
|
||||
|
||||
$this->assertEqualSets( array( $comments[1] ), $q->comments );
|
||||
}
|
||||
|
||||
public function modify_meta_params( $q ) {
|
||||
$q->query_vars['meta_key'] = 'foo';
|
||||
$q->query_vars['meta_value'] = 'bar';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user