Make WordPress Core

Changeset 40376 for trunk


Ignore:
Timestamp:
04/05/2017 08:24:27 PM (10 years ago)
Author:
jnylen0
Message:

REST API: Allow fetching multiple terms at once via the slug parameter.

This matches a similar change previously made for posts (#38579) and an upcoming change for users (#40213).

Props wonderboymusic, MatheusGimenez, curdin.
Fixes #40027.

Location:
trunk
Files:
3 edited

Legend:

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

    r39954 r40376  
    979979                $query_params['slug'] = array(
    980980                        'description'       => __( 'Limit result set to terms with a specific slug.' ),
    981                         'type'              => 'string',
     981                        'type'              => 'array',
     982                        'items'             => array(
     983                                'type'          => 'string'
     984                        ),
    982985                );
    983986
  • trunk/tests/phpunit/tests/rest-api/rest-tags-controller.php

    r39913 r40376  
    377377                $this->assertEquals( 1, count( $data ) );
    378378                $this->assertEquals( 'Apple', $data[0]['name'] );
     379        }
     380
     381        public function test_get_items_slug_array_arg() {
     382                $id1 = $this->factory->tag->create( array( 'name' => 'Taco' ) );
     383                $id2 = $this->factory->tag->create( array( 'name' => 'Enchilada' ) );
     384                $id3 = $this->factory->tag->create( array( 'name' => 'Burrito' ) );
     385                $this->factory->tag->create( array( 'name' => 'Pizza' ) );
     386                $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
     387                $request->set_param( 'slug', array(
     388                        'taco',
     389                        'burrito',
     390                        'enchilada',
     391                ) );
     392                $response = $this->server->dispatch( $request );
     393                $this->assertEquals( 200, $response->get_status() );
     394                $data = $response->get_data();
     395                $names = wp_list_pluck( $data, 'name' );
     396                sort( $names );
     397                $this->assertEquals( array( 'Burrito', 'Enchilada', 'Taco' ), $names );
     398        }
     399
     400        public function test_get_items_slug_csv_arg() {
     401                $id1 = $this->factory->tag->create( array( 'name' => 'Taco' ) );
     402                $id2 = $this->factory->tag->create( array( 'name' => 'Enchilada' ) );
     403                $id3 = $this->factory->tag->create( array( 'name' => 'Burrito' ) );
     404                $this->factory->tag->create( array( 'name' => 'Pizza' ) );
     405                $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
     406                $request->set_param( 'slug', 'taco,burrito, enchilada');
     407                $response = $this->server->dispatch( $request );
     408                $this->assertEquals( 200, $response->get_status() );
     409                $data = $response->get_data();
     410                $names = wp_list_pluck( $data, 'name' );
     411                sort( $names );
     412                $this->assertEquals( array( 'Burrito', 'Enchilada', 'Taco' ), $names );
    379413        }
    380414
  • trunk/tests/qunit/fixtures/wp-api-generated.js

    r40238 r40376  
    19911991                            "required": false,
    19921992                            "description": "Limit result set to terms with a specific slug.",
    1993                             "type": "string"
     1993                            "type": "array",
     1994                            "items": {
     1995                                "type": "string"
     1996                            }
    19941997                        }
    19951998                    }
    … …  
    22262229                            "required": false,
    22272230                            "description": "Limit result set to terms with a specific slug.",
    2228                             "type": "string"
     2231                            "type": "array",
     2232                            "items": {
     2233                                "type": "string"
     2234                            }
    22292235                        }
    22302236                    }
Note: See TracChangeset for help on using the changeset viewer.