Make WordPress Core

Changeset 34212


Ignore:
Timestamp:
09/15/2015 04:34:14 PM (11 years ago)
Author:
wonderboymusic
Message:

WP_Comment_Query: add comment__in to allowed values for $orderby.

Adds unit tests.

Fixes #33883.

Location:
trunk
Files:
2 edited

Legend:

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

    r34205 r34212  
    9595         * @since 4.2.0
    9696         * @since 4.4.0 `$parent__in` and `$parent__not_in` were added.
     97         * @since 4.4.0 Order by `comment__in` was added.
    9798         * @access public
    9899         *
    … …  
    131132         *                                             'comment_date_gmt', 'comment_ID', 'comment_karma',
    132133         *                                             'comment_parent', 'comment_post_ID', 'comment_type', 'user_id',
    133          *                                             'meta_value', 'meta_value_num', the value of $meta_key, and the
     134         *                                             'comment__in', 'meta_value', 'meta_value_num', the value of $meta_key, and the
    134135         *                                             array keys of `$meta_query`. Also accepts false, an empty array,
    135136         *                                             or 'none' to disable `ORDER BY` clause.
    … …  
    391392                                }
    392393
    393                                 if ( ! $found_orderby_comment_ID && 'comment_ID' === $_orderby ) {
     394                                if ( ! $found_orderby_comment_ID && in_array( $_orderby, array( 'comment_ID', 'comment__in' ) ) ) {
    394395                                        $found_orderby_comment_ID = true;
    395396                                }
    … …  
    398399
    399400                                if ( ! $parsed ) {
     401                                        continue;
     402                                }
     403
     404                                if ( 'comment__in' === $_orderby ) {
     405                                        $orderby_array[] = $parsed;
    400406                                        continue;
    401407                                }
    … …  
    773779                } elseif ( $orderby == 'meta_value_num' ) {
    774780                        $parsed = "$wpdb->commentmeta.meta_value+0";
     781                } elseif ( $orderby == 'comment__in' ) {
     782                        $comment__in = implode( ',', array_map( 'absint', $this->query_vars['comment__in'] ) );
     783                        $parsed = "FIELD( {$wpdb->comments}.comment_ID, $comment__in )";
    775784                } elseif ( in_array( $orderby, $allowed_keys ) ) {
    776785
  • trunk/tests/phpunit/tests/comment/query.php

    r34205 r34212  
    18381838                $this->assertEqualSets( array( $c1, $c2 ), $ids->comments );
    18391839        }
     1840
     1841        /**
     1842         * @ticket 33883
     1843         */
     1844        public function test_orderby_comment__in() {
     1845                $this->factory->comment->create( array(
     1846                        'comment_post_ID' => $this->post_id,
     1847                        'comment_approved' => '1'
     1848                ) );
     1849
     1850                $c2 = $this->factory->comment->create( array(
     1851                        'comment_post_ID' => $this->post_id,
     1852                        'comment_approved' => '1'
     1853                ) );
     1854                $c3 = $this->factory->comment->create( array(
     1855                        'comment_post_ID' => $this->post_id,
     1856                        'comment_approved' => '1'
     1857                ) );
     1858
     1859                $this->factory->comment->create( array(
     1860                        'comment_post_ID' => $this->post_id,
     1861                        'comment_approved' => '1'
     1862                ) );
     1863
     1864
     1865                $ids = new WP_Comment_Query( array(
     1866                        'fields' => 'ids',
     1867                        'comment__in' => array( $c2, $c3 ),
     1868                        'orderby' => 'comment__in'
     1869                ) );
     1870
     1871                $this->assertEquals( array( $c2, $c3 ), $ids->comments );
     1872
     1873        }
    18401874}
Note: See TracChangeset for help on using the changeset viewer.