WordPress.org

Make WordPress Core

Changeset 30096


Ignore:
Timestamp:
10/29/2014 09:49:08 PM (5 years ago)
Author:
boonebgorges
Message:

Better flexibility for 'type' in WP_Comment_Query.

  • Add support for an array of values in 'type'.
  • Introduce type__in parameter. This duplicates 'type' but is added for better consistency with other query classes.
  • Introduce type__not_in.

Among other things, these changes will make it easier for plugin authors to
manage the appearance of custom comment types in various WP interfaces.

Props dancameron, mordauk.
See #12668.

Location:
trunk
Files:
2 edited

Legend:

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

    r30093 r30096  
    305305            'status' => 'all',
    306306            'type' => '',
     307            'type__in' => '',
     308            'type__not_in' => '',
    307309            'user_id' => '',
    308310            'search' => '',
     
    521523        }
    522524
    523         if ( 'comment' == $this->query_vars['type'] ) {
    524             $where[] = "comment_type = ''";
    525         } elseif( 'pings' == $this->query_vars['type'] ) {
    526             $where[] = 'comment_type IN ("pingback", "trackback")';
    527         } elseif ( ! empty( $this->query_vars['type'] ) ) {
    528             $where[] = $wpdb->prepare( 'comment_type = %s', $this->query_vars['type'] );
     525        // Filtering by comment_type: 'type', 'type__in', 'type__not_in'.
     526        $raw_types = array(
     527            'IN' => array_merge( (array) $this->query_vars['type'], (array) $this->query_vars['type__in'] ),
     528            'NOT IN' => (array) $this->query_vars['type__not_in'],
     529        );
     530
     531        $comment_types = array();
     532        foreach ( $raw_types as $operator => $_raw_types ) {
     533            $_raw_types = array_unique( $_raw_types );
     534
     535            foreach ( $_raw_types as $type ) {
     536                switch ( $type ) {
     537                    // An empty translates to 'all', for backward compatibility
     538                    case '':
     539                    case 'all' :
     540                        break;
     541
     542                    case 'comment':
     543                    case 'comments':
     544                        $comment_types[ $operator ][] = "''";
     545                        break;
     546
     547                    case 'pings':
     548                        $comment_types[ $operator ][] = "'pingback'";
     549                        $comment_types[ $operator ][] = "'trackback'";
     550                        break;
     551
     552                    default:
     553                        $comment_types[ $operator ][] = $wpdb->prepare( '%s', $type );
     554                        break;
     555                }
     556            }
     557
     558            if ( ! empty( $comment_types[ $operator ] ) ) {
     559                $types_sql = implode( ', ', $comment_types[ $operator ] );
     560                $where[] = "comment_type $operator ($types_sql)";
     561            }
    529562        }
    530563
  • trunk/tests/phpunit/tests/comment/query.php

    r30093 r30096  
    1414
    1515        $this->post_id = $this->factory->post->create();
     16    }
     17
     18    public function test_query() {
     19        $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
     20        $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
     21        $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
     22        $c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ) );
     23        $c5 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'luigi' ) );
     24
     25        $q = new WP_Comment_Query();
     26        $found = $q->query( array(
     27            'fields' => 'ids',
     28        ) );
     29
     30        $this->assertEquals( array( $c1, $c2, $c3, $c4, $c5 ), $found );
     31    }
     32
     33    /**
     34     * @ticket 12668
     35     */
     36    public function test_query_type_empty_string() {
     37        $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
     38        $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
     39        $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
     40        $c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ) );
     41        $c5 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'luigi' ) );
     42
     43        $q = new WP_Comment_Query();
     44        $found = $q->query( array(
     45            'type' => '',
     46            'fields' => 'ids',
     47        ) );
     48
     49        $this->assertEquals( array( $c1, $c2, $c3, $c4, $c5 ), $found );
     50    }
     51
     52    /**
     53     * @ticket 12668
     54     */
     55    public function test_query_type_comment() {
     56        $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
     57        $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
     58        $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
     59        $c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ) );
     60        $c5 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'luigi' ) );
     61
     62        $q = new WP_Comment_Query();
     63        $found = $q->query( array(
     64            'type' => 'comment',
     65            'fields' => 'ids',
     66        ) );
     67
     68        $this->assertEquals( array( $c1 ), $found );
     69    }
     70
     71    public function test_query_type_pingback() {
     72        $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
     73        $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
     74        $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
     75        $c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
     76
     77        $q = new WP_Comment_Query();
     78        $found = $q->query( array(
     79            'type' => 'pingback',
     80            'fields' => 'ids',
     81        ) );
     82
     83        $this->assertEquals( array( $c2, $c3 ), $found );
     84
     85    }
     86
     87    public function test_query_type_trackback() {
     88        $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
     89        $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
     90        $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
     91        $c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
     92
     93        $q = new WP_Comment_Query();
     94        $found = $q->query( array(
     95            'type' => 'trackback',
     96            'fields' => 'ids',
     97        ) );
     98
     99        $this->assertEquals( array( $c2, $c3 ), $found );
     100
     101    }
     102
     103    /**
     104     * 'pings' is an alias for 'trackback' + 'pingback'.
     105     */
     106    public function test_query_type_pings() {
     107        $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
     108        $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
     109        $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
     110        $c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ) );
     111        $c5 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'luigi' ) );
     112
     113        $q = new WP_Comment_Query();
     114        $found = $q->query( array(
     115            'type' => 'pings',
     116            'fields' => 'ids',
     117        ) );
     118
     119        $this->assertEquals( array( $c2, $c3 ), $found );
     120    }
     121
     122    /**
     123     * Comments and custom
     124     * @ticket 12668
     125     */
     126    public function test_type_array_comments_and_custom() {
     127        $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
     128        $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
     129        $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
     130        $c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ) );
     131        $c5 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'luigi' ) );
     132        $c6 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ) );
     133
     134        $q = new WP_Comment_Query();
     135        $found = $q->query( array(
     136            'type' => array( 'comments', 'mario' ),
     137            'fields' => 'ids',
     138        ) );
     139
     140        $this->assertEquals( array( $c1, $c4, $c6 ), $found );
     141    }
     142
     143    /**
     144     * @ticket 12668
     145     */
     146    public function test_type_not__in_array_custom() {
     147        $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
     148        $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
     149        $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
     150        $c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ) );
     151        $c5 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'luigi' ) );
     152        $c6 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ) );
     153
     154        $q = new WP_Comment_Query();
     155        $found = $q->query( array(
     156            'type__not_in' => array( 'luigi' ),
     157            'fields' => 'ids',
     158        ) );
     159
     160        $this->assertEquals( array( $c1, $c2, $c3, $c4, $c6 ), $found );
     161    }
     162
     163    /**
     164     * @ticket 12668
     165     */
     166    public function test_type__in_array_and_not_type_array_custom() {
     167        $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
     168        $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
     169        $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
     170        $c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ) );
     171        $c5 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'luigi' ) );
     172        $c6 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ) );
     173
     174        $q = new WP_Comment_Query();
     175        $found = $q->query( array(
     176            'type__in' => array( 'comments' ),
     177            'type__not_in' => array( 'luigi' ),
     178            'fields' => 'ids',
     179        ) );
     180
     181        $this->assertEquals( array( $c1 ), $found );
     182    }
     183
     184    /**
     185     * @ticket 12668
     186     */
     187    public function test_type_array_and_type__not_in_array_custom() {
     188        $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
     189        $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
     190        $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
     191        $c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ) );
     192        $c5 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'luigi' ) );
     193        $c6 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ) );
     194
     195        $q = new WP_Comment_Query();
     196        $found = $q->query( array(
     197            'type' => array( 'pings' ),
     198            'type__not_in' => array( 'mario' ),
     199            'fields' => 'ids',
     200        ) );
     201
     202        $this->assertEquals( array( $c2, $c3 ), $found );
     203    }
     204
     205    /**
     206     * @ticket 12668
     207     */
     208    public function test_type__not_in_custom() {
     209        $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
     210        $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
     211        $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
     212        $c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ) );
     213        $c5 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'luigi' ) );
     214        $c6 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ) );
     215
     216        $q = new WP_Comment_Query();
     217        $found = $q->query( array(
     218            'type__not_in' => 'luigi',
     219            'fields' => 'ids',
     220        ) );
     221
     222        $this->assertEquals( array( $c1, $c2, $c3, $c4, $c6 ), $found );
     223    }
     224
     225    /**
     226     * @ticket 12668
     227     */
     228    public function test_type_array_comments_and_pings() {
     229        $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
     230        $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
     231        $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
     232        $c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ) );
     233        $c5 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'luigi' ) );
     234
     235        $q = new WP_Comment_Query();
     236        $found = $q->query( array(
     237            'type' => array( 'comments', 'pings' ),
     238            'fields' => 'ids',
     239        ) );
     240
     241        $this->assertEquals( array( $c1, $c2, $c3 ), $found );
     242    }
     243
     244    /**
     245     * @ticket 12668
     246     */
     247    public function test_type_array_comment_pings() {
     248        $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
     249        $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
     250        $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
     251
     252        $q = new WP_Comment_Query();
     253        $found = $q->query( array(
     254            'type' => array( 'comment', 'pings' ),
     255            'fields' => 'ids',
     256        ) );
     257
     258        $this->assertEquals( array( $c1, $c2, $c3 ), $found );
     259    }
     260
     261    /**
     262     * @ticket 12668
     263     */
     264    public function test_type_array_pingback() {
     265        $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
     266        $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
     267        $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
     268
     269        $q = new WP_Comment_Query();
     270        $found = $q->query( array(
     271            'type' => array( 'pingback' ),
     272            'fields' => 'ids',
     273        ) );
     274
     275        $this->assertEquals( array( $c2 ), $found );
     276    }
     277
     278    /**
     279     * @ticket 12668
     280     */
     281    public function test_type_array_custom_pingpack() {
     282        $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
     283        $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
     284        $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
     285
     286        $q = new WP_Comment_Query();
     287        $found = $q->query( array(
     288            'type' => array( 'peach', 'pingback' ),
     289            'fields' => 'ids',
     290        ) );
     291
     292        $this->assertEquals( array( $c2 ), $found );
     293    }
     294
     295    /**
     296     * @ticket 12668
     297     */
     298    public function test_type_array_pings() {
     299        $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
     300        $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
     301        $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
     302
     303        $q = new WP_Comment_Query();
     304        $found = $q->query( array(
     305            'type' => array( 'pings' ),
     306            'fields' => 'ids',
     307        ) );
     308
     309        $this->assertEquals( array( $c2, $c3 ), $found );
     310    }
     311
     312    /**
     313     * @ticket 12668
     314     */
     315    public function test_type_status_approved_array_comment_pings() {
     316        $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
     317        $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
     318        $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
     319        $c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '0', 'comment_type' => 'pingback' ) );
     320
     321        $q = new WP_Comment_Query();
     322        $found = $q->query( array(
     323            'status' => 'approve',
     324            'type' => array( 'pings' ),
     325            'fields' => 'ids',
     326        ) );
     327
     328        $this->assertEquals( array( $c3, $c2 ), $found );
     329    }
     330
     331    /**
     332     * @ticket 12668
     333     */
     334    public function test_type_array_trackback() {
     335        $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
     336        $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
     337        $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
     338
     339        $q = new WP_Comment_Query();
     340        $found = $q->query( array(
     341            'type' => array( 'trackback' ),
     342            'fields' => 'ids',
     343        ) );
     344
     345        $this->assertEquals( array( $c2 ), $found );
     346    }
     347
     348    /**
     349     * @ticket 12668
     350     */
     351    public function test_type_array_custom_trackback() {
     352        $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
     353        $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
     354        $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
     355
     356        $q = new WP_Comment_Query();
     357        $found = $q->query( array(
     358            'type' => array( 'toad', 'trackback' ),
     359            'fields' => 'ids',
     360        ) );
     361
     362        $this->assertEquals( array( $c2 ), $found );
     363    }
     364
     365    /**
     366     * @ticket 12668
     367     */
     368    public function test_type_array_pings_approved() {
     369        $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
     370        $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
     371        $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
     372        $c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '0', 'comment_type' => 'trackback' ) );
     373
     374        $q = new WP_Comment_Query();
     375        $found = $q->query( array(
     376            'status' => 'approve',
     377            'type' => array( 'pings' ),
     378            'fields' => 'ids',
     379        ) );
     380
     381        $this->assertEquals( array( $c3, $c2 ), $found );
    16382    }
    17383
Note: See TracChangeset for help on using the changeset viewer.