WordPress.org

Make WordPress Core

Changeset 31015


Ignore:
Timestamp:
12/31/2014 07:37:09 PM (6 years ago)
Author:
boonebgorges
Message:

Support array values for post-related parameters in WP_Comment_Query.

Props nprasath002, c3mdigital, ianmjones.
Fixes #20006.

Location:
trunk
Files:
2 edited

Legend:

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

    r30681 r31015  
    675675            $join_posts_table = true;
    676676            foreach ( $post_fields as $field_name => $field_value ) {
    677                 $where[] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} = %s", $field_value );
     677                // $field_value may be an array.
     678                $esses = array_fill( 0, count( (array) $field_value ), '%s' );
     679                $where[] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ',', $esses ) . ')', $field_value );
    678680            }
    679681        }
  • trunk/tests/phpunit/tests/comment/query.php

    r30548 r31015  
    11171117        $this->assertEquals( 2, $found );
    11181118    }
     1119
     1120    public function test_post_type_single_value() {
     1121        register_post_type( 'post-type-1' );
     1122        register_post_type( 'post-type-2' );
     1123
     1124        $p1 = $this->factory->post->create( array( 'post_type' => 'post-type-1' ) );
     1125        $p2 = $this->factory->post->create( array( 'post_type' => 'post-type-2' ) );
     1126
     1127        $c1 = $this->factory->comment->create_post_comments( $p1, 1 );
     1128        $c2 = $this->factory->comment->create_post_comments( $p2, 1 );
     1129
     1130        $q = new WP_Comment_Query();
     1131        $found = $q->query( array(
     1132            'fields' => 'ids',
     1133            'post_type' => 'post-type-2',
     1134        ) );
     1135
     1136        $this->assertEqualSets( $c2, $found );
     1137
     1138        _unregister_post_type( 'post-type-1' );
     1139        _unregister_post_type( 'post-type-2' );
     1140    }
     1141
     1142    /**
     1143     * @ticket 20006
     1144     */
     1145    public function test_post_type_singleton_array() {
     1146        register_post_type( 'post-type-1' );
     1147        register_post_type( 'post-type-2' );
     1148
     1149        $p1 = $this->factory->post->create( array( 'post_type' => 'post-type-1' ) );
     1150        $p2 = $this->factory->post->create( array( 'post_type' => 'post-type-2' ) );
     1151
     1152        $c1 = $this->factory->comment->create_post_comments( $p1, 1 );
     1153        $c2 = $this->factory->comment->create_post_comments( $p2, 1 );
     1154
     1155        $q = new WP_Comment_Query();
     1156        $found = $q->query( array(
     1157            'fields' => 'ids',
     1158            'post_type' => array( 'post-type-2' ),
     1159        ) );
     1160
     1161        $this->assertEqualSets( $c2, $found );
     1162
     1163        _unregister_post_type( 'post-type-1' );
     1164        _unregister_post_type( 'post-type-2' );
     1165    }
     1166
     1167    /**
     1168     * @ticket 20006
     1169     */
     1170    public function test_post_type_array() {
     1171        register_post_type( 'post-type-1' );
     1172        register_post_type( 'post-type-2' );
     1173
     1174        $p1 = $this->factory->post->create( array( 'post_type' => 'post-type-1' ) );
     1175        $p2 = $this->factory->post->create( array( 'post_type' => 'post-type-2' ) );
     1176        $p3 = $this->factory->post->create( array( 'post_type' => 'post-type-3' ) );
     1177
     1178        $c1 = $this->factory->comment->create_post_comments( $p1, 1 );
     1179        $c2 = $this->factory->comment->create_post_comments( $p2, 1 );
     1180        $c3 = $this->factory->comment->create_post_comments( $p3, 1 );
     1181
     1182        $q = new WP_Comment_Query();
     1183        $found = $q->query( array(
     1184            'fields' => 'ids',
     1185            'post_type' => array( 'post-type-1', 'post-type-3' ),
     1186        ) );
     1187
     1188        $this->assertEqualSets( array_merge( $c1, $c3 ), $found );
     1189
     1190        _unregister_post_type( 'post-type-1' );
     1191        _unregister_post_type( 'post-type-2' );
     1192    }
     1193
     1194    public function test_post_name_single_value() {
     1195        $p1 = $this->factory->post->create( array( 'post_name' => 'foo' ) );
     1196        $p2 = $this->factory->post->create( array( 'post_name' => 'bar' ) );
     1197
     1198        $c1 = $this->factory->comment->create_post_comments( $p1, 1 );
     1199        $c2 = $this->factory->comment->create_post_comments( $p2, 1 );
     1200
     1201        $q = new WP_Comment_Query();
     1202        $found = $q->query( array(
     1203            'fields' => 'ids',
     1204            'post_name' => 'bar',
     1205        ) );
     1206
     1207        $this->assertEqualSets( $c2, $found );
     1208    }
     1209
     1210    /**
     1211     * @ticket 20006
     1212     */
     1213    public function test_post_name_singleton_array() {
     1214        $p1 = $this->factory->post->create( array( 'post_name' => 'foo' ) );
     1215        $p2 = $this->factory->post->create( array( 'post_name' => 'bar' ) );
     1216
     1217        $c1 = $this->factory->comment->create_post_comments( $p1, 1 );
     1218        $c2 = $this->factory->comment->create_post_comments( $p2, 1 );
     1219
     1220        $q = new WP_Comment_Query();
     1221        $found = $q->query( array(
     1222            'fields' => 'ids',
     1223            'post_name' => array( 'bar' ),
     1224        ) );
     1225
     1226        $this->assertEqualSets( $c2, $found );
     1227    }
     1228
     1229    /**
     1230     * @ticket 20006
     1231     */
     1232    public function test_post_name_array() {
     1233        $p1 = $this->factory->post->create( array( 'post_name' => 'foo' ) );
     1234        $p2 = $this->factory->post->create( array( 'post_name' => 'bar' ) );
     1235        $p3 = $this->factory->post->create( array( 'post_name' => 'baz' ) );
     1236
     1237        $c1 = $this->factory->comment->create_post_comments( $p1, 1 );
     1238        $c2 = $this->factory->comment->create_post_comments( $p2, 1 );
     1239        $c3 = $this->factory->comment->create_post_comments( $p3, 1 );
     1240
     1241        $q = new WP_Comment_Query();
     1242        $found = $q->query( array(
     1243            'fields' => 'ids',
     1244            'post_name' => array( 'foo', 'baz' ),
     1245        ) );
     1246
     1247        $this->assertEqualSets( array_merge( $c1, $c3 ), $found );
     1248    }
     1249
     1250    public function test_post_status_single_value() {
     1251        $p1 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
     1252        $p2 = $this->factory->post->create( array( 'post_status' => 'draft' ) );
     1253
     1254        $c1 = $this->factory->comment->create_post_comments( $p1, 1 );
     1255        $c2 = $this->factory->comment->create_post_comments( $p2, 1 );
     1256
     1257        $q = new WP_Comment_Query();
     1258        $found = $q->query( array(
     1259            'fields' => 'ids',
     1260            'post_status' => 'draft',
     1261        ) );
     1262
     1263        $this->assertEqualSets( $c2, $found );
     1264    }
     1265
     1266    /**
     1267     * @ticket 20006
     1268     */
     1269    public function test_post_status_singleton_array() {
     1270        $p1 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
     1271        $p2 = $this->factory->post->create( array( 'post_status' => 'draft' ) );
     1272
     1273        $c1 = $this->factory->comment->create_post_comments( $p1, 1 );
     1274        $c2 = $this->factory->comment->create_post_comments( $p2, 1 );
     1275
     1276        $q = new WP_Comment_Query();
     1277        $found = $q->query( array(
     1278            'fields' => 'ids',
     1279            'post_status' => array( 'draft' ),
     1280        ) );
     1281
     1282        $this->assertEqualSets( $c2, $found );
     1283    }
     1284
     1285    /**
     1286     * @ticket 20006
     1287     */
     1288    public function test_post_status_array() {
     1289        $p1 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
     1290        $p2 = $this->factory->post->create( array( 'post_status' => 'draft' ) );
     1291        $p3 = $this->factory->post->create( array( 'post_status' => 'future' ) );
     1292
     1293        $c1 = $this->factory->comment->create_post_comments( $p1, 1 );
     1294        $c2 = $this->factory->comment->create_post_comments( $p2, 1 );
     1295        $c3 = $this->factory->comment->create_post_comments( $p3, 1 );
     1296
     1297        $q = new WP_Comment_Query();
     1298        $found = $q->query( array(
     1299            'fields' => 'ids',
     1300            'post_status' => array( 'publish', 'future' ),
     1301        ) );
     1302
     1303        $this->assertEqualSets( array_merge( $c1, $c3 ), $found );
     1304    }
    11191305}
Note: See TracChangeset for help on using the changeset viewer.