Make WordPress Core

Ticket #24826: 24826.diff

File 24826.diff, 4.1 KB (added by westonruter, 11 years ago)

Refreshed patch. PR: https://github.com/x-team/wordpress-develop/pull/24

  • src/wp-includes/comment.php

    diff --git src/wp-includes/comment.php src/wp-includes/comment.php
    index dd45821..4d56fd9 100644
    class WP_Comment_Query { 
    233233        }
    234234
    235235        /**
    236          * Execute the query
     236         * Query vars set by the user
    237237         *
    238238         * @since 3.1.0
     239         * @access public
     240         * @var array
     241         */
     242        public $query_vars;
     243
     244        /**
     245         * List of comments.
    239246         *
    240          * @param string|array $query_vars
    241          * @return int|array
     247         * @since 4.0.0
     248         * @access public
     249         * @var array
    242250         */
    243         public function query( $query_vars ) {
    244                 global $wpdb;
     251        public $comments;
    245252
     253        /**
     254         * Constructor.
     255         *
     256         * Sets up the comment query, if parameter is not empty.
     257         *
     258         * @since 4.0.0
     259         * @access public
     260         *
     261         * @param string $query URL query string.
     262         * @return WP_Comment_Query
     263         */
     264        function __construct($query = '') {
     265                if ( ! empty($query) ) {
     266                        $this->query($query);
     267                }
     268        }
     269
     270        /**
     271         * Parse a query string and set query type booleans.
     272         *
     273         * @since 1.5.0
     274         * @access public
     275         *
     276         * @param string|array $query Optional query.
     277         */
     278        function parse_query( $query =  '' ) {
     279                if ( empty( $query ) ) {
     280                        $query = $this->query_vars;
     281                }
    246282                $defaults = array(
    247283                        'author_email' => '',
    248284                        'ID' => '',
    class WP_Comment_Query { 
    270306                        'date_query' => null, // See WP_Date_Query
    271307                );
    272308
     309                $this->query_vars = wp_parse_args( $query, $defaults );
     310                do_action_ref_array('parse_comment_query', array(&$this));
     311        }
     312
     313        /**
     314         * Sets up the WordPress query by parsing query string.
     315         *
     316         * @since 1.5.0
     317         * @access public
     318         *
     319         * @param string $query_vars URL query string.
     320         * @return array List of posts.
     321         */
     322        function query( $query_vars ) {
     323                $this->query_vars = wp_parse_args( $query_vars );
     324                return $this->get_comments();
     325        }
     326
     327        /**
     328         * Execute the query
     329         *
     330         * @since 4.0.0
     331         *
     332         * @return int|array
     333         */
     334        function get_comments() {
     335                global $wpdb;
     336
    273337                $groupby = '';
    274338
    275                 $this->query_vars = wp_parse_args( $query_vars, $defaults );
     339                $this->parse_query();
    276340
    277341                // Parse meta query
    278342                $this->meta_query = new WP_Meta_Query();
    class WP_Comment_Query { 
    287351                 */
    288352                do_action_ref_array( 'pre_get_comments', array( &$this ) );
    289353
    290                 // $args can be whatever, only use the args defined in defaults to compute the key
    291                 $key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $defaults ) ) )  );
     354                // $args can be whatever, only use the args defined in the query_vars to compute the key
     355                $key = md5( serialize( $this->query_vars)  );
    292356                $last_changed = wp_cache_get( 'last_changed', 'comment' );
    293357                if ( ! $last_changed ) {
    294358                        $last_changed = microtime();
    class WP_Comment_Query { 
    297361                $cache_key = "get_comments:$key:$last_changed";
    298362
    299363                if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) {
     364                        $this->comments = $cache;
    300365                        return $cache;
    301366                }
    302367
    class WP_Comment_Query { 
    472537                $comments = apply_filters_ref_array( 'the_comments', array( $results, &$this ) );
    473538
    474539                wp_cache_add( $cache_key, $comments, 'comment' );
     540                $this->comments = $comments;
    475541
    476542                return $comments;
    477543        }
  • tests/phpunit/tests/comment/query.php

    diff --git tests/phpunit/tests/comment/query.php tests/phpunit/tests/comment/query.php
    index 2dc3623..0c1084d 100644
    class Tests_Comment_Query extends WP_UnitTestCase { 
    180180                $this->assertEquals( $users[1], $comments[2]->user_id );
    181181
    182182        }
     183
     184        /**
     185         * Ticket 24826
     186         */
     187        function test_comment_query_object() {
     188                $comment_id = $this->factory->comment->create();
     189
     190                $query1 = new WP_Comment_Query();
     191                $this->assertNull( $query1->query_vars );
     192                $this->assertEmpty( $query1->comments );
     193                $comments = $query1->query( array( 'status' => 'all' ) );
     194                $this->assertInternalType( 'array', $query1->query_vars );
     195                $this->assertNotEmpty( $query1->comments );
     196                $this->assertInternalType( 'array', $query1->comments );
     197
     198                $query2 = new WP_Comment_Query( array( 'status' => 'all' ) );
     199                $this->assertNotEmpty( $query2->query_vars );
     200                $this->assertNotEmpty( $query2->comments );
     201                $this->assertEquals( $query2->comments, $query1->get_comments() );
     202        }
    183203}