Make WordPress Core

Changeset 32911


Ignore:
Timestamp:
06/23/2015 02:35:29 PM (11 years ago)
Author:
boonebgorges
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/comment.php

    r32874 r32911  
    483483                $this->meta_query->parse_query_vars( $this->query_vars );
    484484
    485                 if ( ! empty( $this->meta_query->queries ) ) {
    486                         $meta_query_clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this );
    487                 }
    488 
    489485                /**
    490486                 * Fires before comments are retrieved.
     
    495491                 */
    496492                do_action_ref_array( 'pre_get_comments', array( &$this ) );
     493
     494                // Reparse query vars, in case they were modified in a 'pre_get_comments' callback.
     495                $this->meta_query->parse_query_vars( $this->query_vars );
     496                if ( ! empty( $this->meta_query->queries ) ) {
     497                        $meta_query_clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this );
     498                }
    497499
    498500                // $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
  • trunk/tests/phpunit/tests/comment/query.php

    r32461 r32911  
    16801680                $this->assertSame( $num_queries, $wpdb->num_queries );
    16811681        }
     1682
     1683        /**
     1684         * @ticket 32762
     1685         */
     1686        public function test_it_should_be_possible_to_modify_meta_query_using_pre_get_comments_action() {
     1687                $comments = $this->factory->comment->create_many( 2, array(
     1688                        'comment_post_ID' => $this->post_id,
     1689                ) );
     1690
     1691                add_comment_meta( $comments[1], 'foo', 'bar' );
     1692
     1693                add_action( 'pre_get_comments', array( $this, 'modify_meta_query' ) );
     1694
     1695                $q = new WP_Comment_Query( array(
     1696                        'comment_post_ID' => $this->post_id,
     1697                        'fields' => 'ids',
     1698                ) );
     1699
     1700                remove_action( 'pre_get_comments', array( $this, 'modify_meta_query' ) );
     1701
     1702                $this->assertEqualSets( array( $comments[1] ), $q->comments );
     1703        }
     1704
     1705        public function modify_meta_query( $q ) {
     1706                $q->meta_query = new WP_Meta_Query( array(
     1707                        array(
     1708                                'key' => 'foo',
     1709                                'value' => 'bar',
     1710                        ),
     1711                ) );
     1712        }
     1713
     1714        /**
     1715         * @ticket 32762
     1716         */
     1717        public function test_it_should_be_possible_to_modify_meta_params_using_pre_get_comments_action() {
     1718                $comments = $this->factory->comment->create_many( 2, array(
     1719                        'comment_post_ID' => $this->post_id,
     1720                ) );
     1721
     1722                add_comment_meta( $comments[1], 'foo', 'bar' );
     1723
     1724                add_action( 'pre_get_comments', array( $this, 'modify_meta_params' ) );
     1725
     1726                $q = new WP_Comment_Query( array(
     1727                        'comment_post_ID' => $this->post_id,
     1728                        'fields' => 'ids',
     1729                ) );
     1730
     1731                remove_action( 'pre_get_comments', array( $this, 'modify_meta_params' ) );
     1732
     1733                $this->assertEqualSets( array( $comments[1] ), $q->comments );
     1734        }
     1735
     1736        public function modify_meta_params( $q ) {
     1737                $q->query_vars['meta_key'] = 'foo';
     1738                $q->query_vars['meta_value'] = 'bar';
     1739        }
    16821740}
Note: See TracChangeset for help on using the changeset viewer.