From 0153b0bb9b8ca39f55276cd3d59a1781d71d9c43 Mon Sep 17 00:00:00 2001 From: Gary Pendergast Date: Mon, 31 Oct 2016 11:05:37 +0000 Subject: [PATCH] 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 --- .../class-wp-rest-posts-controller.php | 12 ++++++++-- .../tests/rest-api/rest-posts-controller.php | 22 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php index ad55ac748b..e5d623705b 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php @@ -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' ), ); } diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php index 1a8545b522..73864e9f57 100644 --- a/tests/phpunit/tests/rest-api/rest-posts-controller.php +++ b/tests/phpunit/tests/rest-api/rest-posts-controller.php @@ -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' );