REST API: Pass correct ID to `meta->update_value` to permit setting term meta during term creation.

Props joehoyle.
Fixes #44834.



git-svn-id: https://develop.svn.wordpress.org/trunk@43636 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
K. Adam White 2018-09-12 23:55:47 +00:00
parent 82bf9baa32
commit e29d895ffd
2 changed files with 32 additions and 1 deletions

View File

@ -454,7 +454,7 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
$schema = $this->get_item_schema(); $schema = $this->get_item_schema();
if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) { 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 ) ) { if ( is_wp_error( $meta_update ) ) {
return $meta_update; return $meta_update;

View File

@ -757,6 +757,37 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase {
$this->assertErrorResponse( 'rest_taxonomy_not_hierarchical', $response, 400 ); $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() { public function test_update_item() {
wp_set_current_user( self::$administrator ); wp_set_current_user( self::$administrator );
$orig_args = array( $orig_args = array(