REST API: Allow a CSV list of term IDs to be passed to `/posts`.

[39048] added CSV support to array types, this change explicitly parses term lists as IDs, and adds tests.

Props timmydcrawford, pento.
Fixes #38553.



git-svn-id: https://develop.svn.wordpress.org/trunk@39055 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Gary Pendergast 2016-10-31 11:05:37 +00:00
parent ee84a7a31a
commit 0153b0bb9b
2 changed files with 32 additions and 2 deletions

View File

@ -1196,8 +1196,7 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
continue;
}
$terms = array_map( 'absint', $request[ $base ] );
$result = wp_set_object_terms( $post_id, $terms, $taxonomy->name );
$result = wp_set_object_terms( $post_id, $request[ $base ], $taxonomy->name );
if ( is_wp_error( $result ) ) {
return $result;
@ -1965,11 +1964,20 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
'items' => array(
'type' => 'integer',
),
'arg_options' => array(
'sanitize_callback' => 'wp_parse_id_list',
),
'context' => array( 'view', 'edit' ),
);
$schema['properties'][ $base . '_exclude' ] = array(
'description' => sprintf( __( 'The terms in the %s taxonomy that should not be assigned to the object.' ), $taxonomy->name ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'arg_options' => array(
'sanitize_callback' => 'wp_parse_id_list',
),
'context' => array( 'view', 'edit' ),
);
}

View File

@ -199,11 +199,18 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
$data = $response->get_data();
$this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) );
$this->assertTrue( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) );
$request->set_param( 'exclude', array( $id2 ) );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) );
$this->assertFalse( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) );
$request->set_param( 'exclude', "$id2" );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) );
$this->assertFalse( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) );
}
public function test_get_items_search_query() {
@ -1304,6 +1311,21 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
$this->assertEquals( array( $category['term_id'] ), $data['categories'] );
}
public function test_create_post_with_categories_as_csv() {
wp_set_current_user( self::$editor_id );
$category = wp_insert_term( 'Chicken', 'category' );
$category2 = wp_insert_term( 'Ribs', 'category' );
$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
$params = $this->set_post_data( array(
'categories' => $category['term_id'] . ',' . $category2['term_id'],
) );
$request->set_body_params( $params );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( array( $category['term_id'], $category2['term_id'] ), $data['categories'] );
}
public function test_create_post_with_invalid_categories() {
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );