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.


git-svn-id: https://develop.svn.wordpress.org/trunk@50157 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Timothy Jacobs
2021-02-02 19:23:08 +00:00
parent 051c135c6e
commit 032e946633
3 changed files with 571 additions and 97 deletions

View File

@@ -1115,6 +1115,185 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
$this->assertSame( $id1, $data[2]['id'] );
}
/**
* @ticket 39494
*/
public function test_get_items_with_category_including_children() {
$taxonomy = get_taxonomy( 'category' );
$cat1 = static::factory()->term->create( array( 'taxonomy' => $taxonomy->name ) );
$cat2 = static::factory()->term->create(
array(
'taxonomy' => $taxonomy->name,
'parent' => $cat1,
)
);
$post_ids = array(
static::factory()->post->create(
array(
'post_status' => 'publish',
'post_category' => array( $cat1 ),
)
),
static::factory()->post->create(
array(
'post_status' => 'publish',
'post_category' => array( $cat2 ),
)
),
);
$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
$request->set_param(
$taxonomy->rest_base,
array(
'terms' => array( $cat1 ),
'include_children' => true,
)
);
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertEqualSets( $post_ids, array_column( $data, 'id' ) );
}
/**
* @ticket 39494
*/
public function test_get_items_with_category_excluding_children() {
$taxonomy = get_taxonomy( 'category' );
$cat1 = static::factory()->term->create( array( 'taxonomy' => $taxonomy->name ) );
$cat2 = static::factory()->term->create(
array(
'taxonomy' => $taxonomy->name,
'parent' => $cat1,
)
);
$post_ids = array(
static::factory()->post->create(
array(
'post_status' => 'publish',
'post_category' => array( $cat1 ),
)
),
static::factory()->post->create(
array(
'post_status' => 'publish',
'post_category' => array( $cat2 ),
)
),
);
$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
$request->set_param(
$taxonomy->rest_base,
array(
'terms' => array( $cat1 ),
'include_children' => false,
)
);
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertCount( 1, $data );
$this->assertEquals( $post_ids[0], $data[0]['id'] );
}
/**
* @ticket 39494
*/
public function test_get_items_without_category_or_its_children() {
$taxonomy = get_taxonomy( 'category' );
$cat1 = static::factory()->term->create( array( 'taxonomy' => $taxonomy->name ) );
$cat2 = static::factory()->term->create(
array(
'taxonomy' => $taxonomy->name,
'parent' => $cat1,
)
);
$post_ids = array(
static::factory()->post->create(
array(
'post_status' => 'publish',
'post_category' => array( $cat1 ),
)
),
static::factory()->post->create(
array(
'post_status' => 'publish',
'post_category' => array( $cat2 ),
)
),
);
$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
$request->set_param(
$taxonomy->rest_base . '_exclude',
array(
'terms' => array( $cat1 ),
'include_children' => true,
)
);
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertEmpty(
array_intersect(
$post_ids,
array_column( $data, 'id' )
)
);
}
/**
* @ticket 39494
*/
public function test_get_items_without_category_but_allowing_its_children() {
$taxonomy = get_taxonomy( 'category' );
$cat1 = static::factory()->term->create( array( 'taxonomy' => $taxonomy->name ) );
$cat2 = static::factory()->term->create(
array(
'taxonomy' => $taxonomy->name,
'parent' => $cat1,
)
);
$p1 = static::factory()->post->create(
array(
'post_status' => 'publish',
'post_category' => array( $cat1 ),
)
);
$p2 = static::factory()->post->create(
array(
'post_status' => 'publish',
'post_category' => array( $cat2 ),
)
);
$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
$request->set_param(
$taxonomy->rest_base . '_exclude',
array(
'terms' => array( $cat1 ),
'include_children' => false,
)
);
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$found_ids = array_column( $data, 'id' );
$this->assertNotContains( $p1, $found_ids );
$this->assertContains( $p2, $found_ids );
}
/**
* @ticket 44326
*/