WordPress.org

Make WordPress Core

Changeset 43637


Ignore:
Timestamp:
09/13/2018 09:52:47 AM (15 months ago)
Author:
SergeyBiryukov
Message:

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.

Location:
branches/4.9
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/4.9

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

    r43445 r43637  
    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 ) ) {
  • branches/4.9/tests/phpunit/tests/rest-api/rest-tags-controller.php

    r43445 r43637  
    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 );
Note: See TracChangeset for help on using the changeset viewer.