Make WordPress Core

Changeset 39055


Ignore:
Timestamp:
10/31/2016 11:05:37 AM (10 years ago)
Author:
pento
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    r39049 r39055  
    11971197                        }
    11981198
    1199                         $terms  = array_map( 'absint', $request[ $base ] );
    1200                         $result = wp_set_object_terms( $post_id, $terms, $taxonomy->name );
     1199                        $result = wp_set_object_terms( $post_id, $request[ $base ], $taxonomy->name );
    12011200
    12021201                        if ( is_wp_error( $result ) ) {
    … …  
    19661965                                        'type'    => 'integer',
    19671966                                ),
     1967                                'arg_options' => array(
     1968                                        'sanitize_callback' => 'wp_parse_id_list',
     1969                                ),
    19681970                                'context'     => array( 'view', 'edit' ),
    19691971                        );
    … …  
    19711973                                'description' => sprintf( __( 'The terms in the %s taxonomy that should not be assigned to the object.' ), $taxonomy->name ),
    19721974                                'type'        => 'array',
     1975                                'items'       => array(
     1976                                        'type'    => 'integer',
     1977                                ),
     1978                                'arg_options' => array(
     1979                                        'sanitize_callback' => 'wp_parse_id_list',
     1980                                ),
    19731981                                'context'     => array( 'view', 'edit' ),
    19741982                        );
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r39047 r39055  
    200200                $this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) );
    201201                $this->assertTrue( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) );
     202
    202203                $request->set_param( 'exclude', array( $id2 ) );
     204                $response = $this->server->dispatch( $request );
     205                $data = $response->get_data();
     206                $this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) );
     207                $this->assertFalse( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) );
     208
     209                $request->set_param( 'exclude', "$id2" );
    203210                $response = $this->server->dispatch( $request );
    204211                $data = $response->get_data();
    … …  
    13031310                $data = $response->get_data();
    13041311                $this->assertEquals( array( $category['term_id'] ), $data['categories'] );
     1312        }
     1313
     1314        public function test_create_post_with_categories_as_csv() {
     1315                wp_set_current_user( self::$editor_id );
     1316                $category = wp_insert_term( 'Chicken', 'category' );
     1317                $category2 = wp_insert_term( 'Ribs', 'category' );
     1318                $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
     1319                $params = $this->set_post_data( array(
     1320                        'categories' => $category['term_id'] . ',' . $category2['term_id'],
     1321                ) );
     1322                $request->set_body_params( $params );
     1323                $response = $this->server->dispatch( $request );
     1324
     1325                $data = $response->get_data();
     1326                $this->assertEquals( array( $category['term_id'], $category2['term_id'] ), $data['categories'] );
    13051327        }
    13061328
Note: See TracChangeset for help on using the changeset viewer.