Skip to content

Commit e44849e

Browse files
REST API: Pass correct ID to meta->update_value to permit setting term 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
1 parent bf71d67 commit e44849e

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ public function create_item( $request ) {
441441

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

446446
if ( is_wp_error( $meta_update ) ) {
447447
return $meta_update;

tests/phpunit/tests/rest-api/rest-tags-controller.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,37 @@ public function test_create_item_parent_non_hierarchical_taxonomy() {
616616
$this->assertErrorResponse( 'rest_taxonomy_not_hierarchical', $response, 400 );
617617
}
618618

619+
public function test_create_item_with_meta() {
620+
wp_set_current_user( self::$administrator );
621+
$request = new WP_REST_Request( 'POST', '/wp/v2/tags' );
622+
$request->set_param( 'name', 'My Awesome Term' );
623+
$request->set_param( 'meta', array( 'test_tag_single' => 'hello' ) );
624+
$response = rest_get_server()->dispatch( $request );
625+
$this->assertEquals( 201, $response->get_status() );
626+
$headers = $response->get_headers();
627+
$data = $response->get_data();
628+
$this->assertContains( '/wp/v2/tags/' . $data['id'], $headers['Location'] );
629+
$this->assertEquals( 'My Awesome Term', $data['name'] );
630+
$this->assertEquals( 'hello', get_term_meta( $data['id'], 'test_tag_single', true ) );
631+
}
632+
633+
public function test_create_item_with_meta_wrong_id() {
634+
wp_set_current_user( self::$administrator );
635+
$existing_tag_id = $this->factory->tag->create( array( 'name' => 'My Not So Awesome Term' ) );
636+
$request = new WP_REST_Request( 'POST', '/wp/v2/tags' );
637+
$request->set_param( 'name', 'My Awesome Term' );
638+
$request->set_param( 'meta', array( 'test_tag_single' => 'hello' ) );
639+
$request->set_param( 'id', $existing_tag_id );
640+
$response = rest_get_server()->dispatch( $request );
641+
$this->assertEquals( 201, $response->get_status() );
642+
$headers = $response->get_headers();
643+
$data = $response->get_data();
644+
$this->assertContains( '/wp/v2/tags/' . $data['id'], $headers['Location'] );
645+
$this->assertEquals( 'My Awesome Term', $data['name'] );
646+
$this->assertEquals( '', get_term_meta( $existing_tag_id, 'test_tag_single', true ) );
647+
$this->assertEquals( 'hello', get_term_meta( $data['id'], 'test_tag_single', true ) );
648+
}
649+
619650
public function test_update_item() {
620651
wp_set_current_user( self::$administrator );
621652
$orig_args = array(

0 commit comments

Comments
 (0)