Skip to content
Permalink
Browse files

REST API: Pass correct ID to `meta->update_value` to permit setting t…

…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 committed Sep 13, 2018
1 parent bf71d67 commit e44849ece7695fae4f3d47d3ffd3b9de9e736935
@@ -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;
@@ -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(

0 comments on commit e44849e

Please sign in to comment.
You can’t perform that action at this time.