Make WordPress Core


Ignore:
Timestamp:
02/02/2021 07:23:08 PM (4 years ago)
Author:
TimothyBlynJacobs
Message:

REST API: Allow for the posts endpoint include/exclude terms query to include_children.

For example the categories or categories_exclude parameters can now optionally accept an object with a terms property that accepts the list of term ids and a new include_children property which controls the Tax Query include_children field.

Props jason_the_adams, jnylen0, birgire, dlh.
Fixes #39494.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r50024 r50157  
    11141114        $this->assertSame( $id2, $data[1]['id'] );
    11151115        $this->assertSame( $id1, $data[2]['id'] );
     1116    }
     1117
     1118    /**
     1119     * @ticket 39494
     1120     */
     1121    public function test_get_items_with_category_including_children() {
     1122        $taxonomy = get_taxonomy( 'category' );
     1123
     1124        $cat1 = static::factory()->term->create( array( 'taxonomy' => $taxonomy->name ) );
     1125        $cat2 = static::factory()->term->create(
     1126            array(
     1127                'taxonomy' => $taxonomy->name,
     1128                'parent'   => $cat1,
     1129            )
     1130        );
     1131
     1132        $post_ids = array(
     1133            static::factory()->post->create(
     1134                array(
     1135                    'post_status'   => 'publish',
     1136                    'post_category' => array( $cat1 ),
     1137                )
     1138            ),
     1139            static::factory()->post->create(
     1140                array(
     1141                    'post_status'   => 'publish',
     1142                    'post_category' => array( $cat2 ),
     1143                )
     1144            ),
     1145        );
     1146
     1147        $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     1148        $request->set_param(
     1149            $taxonomy->rest_base,
     1150            array(
     1151                'terms'            => array( $cat1 ),
     1152                'include_children' => true,
     1153            )
     1154        );
     1155        $response = rest_get_server()->dispatch( $request );
     1156        $data     = $response->get_data();
     1157
     1158        $this->assertEqualSets( $post_ids, array_column( $data, 'id' ) );
     1159    }
     1160
     1161    /**
     1162     * @ticket 39494
     1163     */
     1164    public function test_get_items_with_category_excluding_children() {
     1165        $taxonomy = get_taxonomy( 'category' );
     1166
     1167        $cat1 = static::factory()->term->create( array( 'taxonomy' => $taxonomy->name ) );
     1168        $cat2 = static::factory()->term->create(
     1169            array(
     1170                'taxonomy' => $taxonomy->name,
     1171                'parent'   => $cat1,
     1172            )
     1173        );
     1174
     1175        $post_ids = array(
     1176            static::factory()->post->create(
     1177                array(
     1178                    'post_status'   => 'publish',
     1179                    'post_category' => array( $cat1 ),
     1180                )
     1181            ),
     1182            static::factory()->post->create(
     1183                array(
     1184                    'post_status'   => 'publish',
     1185                    'post_category' => array( $cat2 ),
     1186                )
     1187            ),
     1188        );
     1189
     1190        $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     1191        $request->set_param(
     1192            $taxonomy->rest_base,
     1193            array(
     1194                'terms'            => array( $cat1 ),
     1195                'include_children' => false,
     1196            )
     1197        );
     1198        $response = rest_get_server()->dispatch( $request );
     1199        $data     = $response->get_data();
     1200
     1201        $this->assertCount( 1, $data );
     1202        $this->assertEquals( $post_ids[0], $data[0]['id'] );
     1203    }
     1204
     1205    /**
     1206     * @ticket 39494
     1207     */
     1208    public function test_get_items_without_category_or_its_children() {
     1209        $taxonomy = get_taxonomy( 'category' );
     1210
     1211        $cat1 = static::factory()->term->create( array( 'taxonomy' => $taxonomy->name ) );
     1212        $cat2 = static::factory()->term->create(
     1213            array(
     1214                'taxonomy' => $taxonomy->name,
     1215                'parent'   => $cat1,
     1216            )
     1217        );
     1218
     1219        $post_ids = array(
     1220            static::factory()->post->create(
     1221                array(
     1222                    'post_status'   => 'publish',
     1223                    'post_category' => array( $cat1 ),
     1224                )
     1225            ),
     1226            static::factory()->post->create(
     1227                array(
     1228                    'post_status'   => 'publish',
     1229                    'post_category' => array( $cat2 ),
     1230                )
     1231            ),
     1232        );
     1233
     1234        $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     1235        $request->set_param(
     1236            $taxonomy->rest_base . '_exclude',
     1237            array(
     1238                'terms'            => array( $cat1 ),
     1239                'include_children' => true,
     1240            )
     1241        );
     1242        $response = rest_get_server()->dispatch( $request );
     1243        $data     = $response->get_data();
     1244
     1245        $this->assertEmpty(
     1246            array_intersect(
     1247                $post_ids,
     1248                array_column( $data, 'id' )
     1249            )
     1250        );
     1251    }
     1252
     1253    /**
     1254     * @ticket 39494
     1255     */
     1256    public function test_get_items_without_category_but_allowing_its_children() {
     1257        $taxonomy = get_taxonomy( 'category' );
     1258
     1259        $cat1 = static::factory()->term->create( array( 'taxonomy' => $taxonomy->name ) );
     1260        $cat2 = static::factory()->term->create(
     1261            array(
     1262                'taxonomy' => $taxonomy->name,
     1263                'parent'   => $cat1,
     1264            )
     1265        );
     1266
     1267        $p1 = static::factory()->post->create(
     1268            array(
     1269                'post_status'   => 'publish',
     1270                'post_category' => array( $cat1 ),
     1271            )
     1272        );
     1273        $p2 = static::factory()->post->create(
     1274            array(
     1275                'post_status'   => 'publish',
     1276                'post_category' => array( $cat2 ),
     1277            )
     1278        );
     1279
     1280        $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     1281        $request->set_param(
     1282            $taxonomy->rest_base . '_exclude',
     1283            array(
     1284                'terms'            => array( $cat1 ),
     1285                'include_children' => false,
     1286            )
     1287        );
     1288        $response = rest_get_server()->dispatch( $request );
     1289        $data     = $response->get_data();
     1290
     1291        $found_ids = array_column( $data, 'id' );
     1292
     1293        $this->assertNotContains( $p1, $found_ids );
     1294        $this->assertContains( $p2, $found_ids );
    11161295    }
    11171296
Note: See TracChangeset for help on using the changeset viewer.