Make WordPress Core

Ticket #24826: 24826.1.diff

File 24826.1.diff, 4.2 KB (added by morganestes, 10 years ago)

Refreshed patch

  • src/wp-includes/comment.php

    diff --git src/wp-includes/comment.php src/wp-includes/comment.php
    index ada21ec..1f8b352 100644
    class WP_Comment_Query { 
    265265        public $date_query = false;
    266266
    267267        /**
     268         * Query vars set by the user
     269         *
     270         * @since  3.1.0
     271         * @access public
    268272         * @var array
    269273         */
    270274        public $query_vars;
    271275
    272276        /**
     277         * List of comments.
     278         *
     279         * @since  4.0.0
     280         * @access public
    273281         * @var array
    274282         */
    275283        public $comments;
    class WP_Comment_Query { 
    365373         * }
    366374         * @return int|array Array of comments or number of found comments if `$count` is set to true.
    367375         */
    368         public function query( $query_vars ) {
    369                 global $wpdb;
    370376
     377        /**
     378         * Constructor.
     379         *
     380         * Sets up the comment query, if parameter is not empty.
     381         *
     382         * @since  4.0.0
     383         * @access public
     384         *
     385         * @param string $query URL query string.
     386         * @return WP_Comment_Query
     387         */
     388        function __construct( $query = '' ) {
     389                if ( ! empty( $query ) ) {
     390                        $this->query( $query );
     391                }
     392        }
     393
     394        /**
     395         * Parse a query string and set query type booleans.
     396         *
     397         * @since  1.5.0
     398         * @access public
     399         *
     400         * @param string|array $query Optional query.
     401         */
     402        function parse_query( $query = '' ) {
     403                if ( empty( $query ) ) {
     404                        $query = $this->query_vars;
     405                }
    371406                $defaults = array(
    372407                        'author_email' => '',
    373408                        'author__in' => '',
    class WP_Comment_Query { 
    407442                        'date_query' => null, // See WP_Date_Query
    408443                );
    409444
     445                $this->query_vars = wp_parse_args( $query, $defaults );
     446                do_action_ref_array('parse_comment_query', array(&$this));
     447        }
     448
     449        /**
     450         * Sets up the WordPress query by parsing query string.
     451         *
     452         * @since 1.5.0
     453         * @access public
     454         *
     455         * @param string $query_vars URL query string.
     456         * @return array List of posts.
     457         */
     458        function query( $query_vars ) {
     459                $this->query_vars = wp_parse_args( $query_vars );
     460                return $this->get_comments();
     461        }
     462
     463        /**
     464         * Execute the query
     465         *
     466         * @since 4.0.0
     467         *
     468         * @return int|array
     469         */
     470        function get_comments() {
     471                global $wpdb;
     472
    410473                $groupby = '';
    411474
    412                 $this->query_vars = wp_parse_args( $query_vars, $defaults );
     475                $this->parse_query();
    413476
    414477                // Parse meta query
    415478                $this->meta_query = new WP_Meta_Query();
    class WP_Comment_Query { 
    428491                 */
    429492                do_action_ref_array( 'pre_get_comments', array( &$this ) );
    430493
    431                 // $args can be whatever, only use the args defined in defaults to compute the key
    432                 $key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $defaults ) ) )  );
     494                // $args can be whatever, only use the args defined in the query_vars to compute the key
     495                $key = md5( serialize( $this->query_vars)  );
    433496                $last_changed = wp_cache_get( 'last_changed', 'comment' );
    434497                if ( ! $last_changed ) {
    435498                        $last_changed = microtime();
    class WP_Comment_Query { 
    438501                $cache_key = "get_comments:$key:$last_changed";
    439502
    440503                if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) {
     504                        $this->comments = $cache;
    441505                        return $cache;
    442506                }
    443507
    class WP_Comment_Query { 
    825889                $comments = apply_filters_ref_array( 'the_comments', array( $results, &$this ) );
    826890
    827891                wp_cache_add( $cache_key, $comments, 'comment' );
     892                $this->comments = $comments;
    828893
    829894                return $comments;
    830895        }
  • tests/phpunit/tests/comment/query.php

    diff --git tests/phpunit/tests/comment/query.php tests/phpunit/tests/comment/query.php
    index 55e7d73..4f16a1d 100644
    class Tests_Comment_Query extends WP_UnitTestCase { 
    15871587
    15881588                $this->assertEqualSets( array_merge( $c1, $c3 ), $found );
    15891589        }
     1590
     1591        /**
     1592         * Ticket 24826
     1593         */
     1594        function test_comment_query_object() {
     1595                $comment_id = $this->factory->comment->create();
     1596
     1597                $query1 = new WP_Comment_Query();
     1598                $this->assertNull( $query1->query_vars );
     1599                $this->assertEmpty( $query1->comments );
     1600                $comments = $query1->query( array( 'status' => 'all' ) );
     1601                $this->assertInternalType( 'array', $query1->query_vars );
     1602                $this->assertNotEmpty( $query1->comments );
     1603                $this->assertInternalType( 'array', $query1->comments );
     1604
     1605                $query2 = new WP_Comment_Query( array( 'status' => 'all' ) );
     1606                $this->assertNotEmpty( $query2->query_vars );
     1607                $this->assertNotEmpty( $query2->comments );
     1608                $this->assertEquals( $query2->comments, $query1->get_comments() );
     1609        }
    15901610}