Make WordPress Core

Ticket #32762: 32762.diff

File 32762.diff, 3.1 KB (added by boonebgorges, 10 years ago)
  • src/wp-includes/comment.php

    diff --git src/wp-includes/comment.php src/wp-includes/comment.php
    index 80f30e2..ccd8464 100644
    class WP_Comment_Query { 
    482482                $this->meta_query = new WP_Meta_Query();
    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.
    491487                 *
    class WP_Comment_Query { 
    495491                 */
    496492                do_action_ref_array( 'pre_get_comments', array( &$this ) );
    497493
     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                }
     499
    498500                // $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
    499501                $key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ) ) );
    500502                $last_changed = wp_cache_get( 'last_changed', 'comment' );
  • tests/phpunit/tests/comment/query.php

    diff --git tests/phpunit/tests/comment/query.php tests/phpunit/tests/comment/query.php
    index 0d7269c..420d8f2 100644
    class Tests_Comment_Query extends WP_UnitTestCase { 
    16791679
    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}