Skip to content

Commit

Permalink
REST API: Pass correct ID to meta->update_value to permit setting t…
Browse files Browse the repository at this point in the history
…erm meta during term creation.

Props joehoyle.
Merges [43636] to the 4.9 branch.
Fixes #44834.

git-svn-id: https://develop.svn.wordpress.org/branches/4.9@43637 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
SergeyBiryukov authored and nylen committed Dec 16, 2018
1 parent 40e6a29 commit 54462de
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
Expand Up @@ -441,7 +441,7 @@ public function create_item( $request ) {

$schema = $this->get_item_schema();
if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
$meta_update = $this->meta->update_value( $request['meta'], (int) $request['id'] );
$meta_update = $this->meta->update_value( $request['meta'], $term->term_id );

if ( is_wp_error( $meta_update ) ) {
return $meta_update;
Expand Down
31 changes: 31 additions & 0 deletions tests/phpunit/tests/rest-api/rest-tags-controller.php
Expand Up @@ -616,6 +616,37 @@ public function test_create_item_parent_non_hierarchical_taxonomy() {
$this->assertErrorResponse( 'rest_taxonomy_not_hierarchical', $response, 400 );
}

public function test_create_item_with_meta() {
wp_set_current_user( self::$administrator );
$request = new WP_REST_Request( 'POST', '/wp/v2/tags' );
$request->set_param( 'name', 'My Awesome Term' );
$request->set_param( 'meta', array( 'test_tag_single' => 'hello' ) );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 201, $response->get_status() );
$headers = $response->get_headers();
$data = $response->get_data();
$this->assertContains( '/wp/v2/tags/' . $data['id'], $headers['Location'] );
$this->assertEquals( 'My Awesome Term', $data['name'] );
$this->assertEquals( 'hello', get_term_meta( $data['id'], 'test_tag_single', true ) );
}

public function test_create_item_with_meta_wrong_id() {
wp_set_current_user( self::$administrator );
$existing_tag_id = $this->factory->tag->create( array( 'name' => 'My Not So Awesome Term' ) );
$request = new WP_REST_Request( 'POST', '/wp/v2/tags' );
$request->set_param( 'name', 'My Awesome Term' );
$request->set_param( 'meta', array( 'test_tag_single' => 'hello' ) );
$request->set_param( 'id', $existing_tag_id );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 201, $response->get_status() );
$headers = $response->get_headers();
$data = $response->get_data();
$this->assertContains( '/wp/v2/tags/' . $data['id'], $headers['Location'] );
$this->assertEquals( 'My Awesome Term', $data['name'] );
$this->assertEquals( '', get_term_meta( $existing_tag_id, 'test_tag_single', true ) );
$this->assertEquals( 'hello', get_term_meta( $data['id'], 'test_tag_single', true ) );
}

public function test_update_item() {
wp_set_current_user( self::$administrator );
$orig_args = array(
Expand Down

0 comments on commit 54462de

Please sign in to comment.