WordPress.org

Make WordPress Core

Changeset 44965


Ignore:
Timestamp:
03/21/2019 06:58:50 PM (8 months ago)
Author:
kadamwhite
Message:

REST API: Avoid undefined-property notice when setting parent term to 0.

Only try to access term_id once $parent_term is known to be a WP_Term.

Props dlh, earnjam.
Fixes #44983.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php

    r43987 r44965  
    686686
    687687        if ( isset( $request['parent'] ) && ! empty( $schema['properties']['parent'] ) ) {
    688             $parent_term_id = 0;
    689             $parent_term    = get_term( (int) $request['parent'], $this->taxonomy );
    690 
    691             if ( $parent_term ) {
    692                 $parent_term_id = $parent_term->term_id;
     688            $parent_term_id   = 0;
     689            $requested_parent = (int) $request['parent'];
     690
     691            if ( $requested_parent ) {
     692                $parent_term = get_term( $requested_parent, $this->taxonomy );
     693
     694                if ( $parent_term instanceof WP_Term ) {
     695                    $parent_term_id = $parent_term->term_id;
     696                }
    693697            }
    694698
  • trunk/tests/phpunit/tests/rest-api/rest-categories-controller.php

    r44510 r44965  
    860860    }
    861861
     862    public function test_create_item_with_no_parent() {
     863        wp_set_current_user( self::$administrator );
     864        $parent  = 0;
     865        $request = new WP_REST_Request( 'POST', '/wp/v2/categories' );
     866        $request->set_param( 'name', 'My Awesome Term' );
     867        $request->set_param( 'parent', $parent );
     868        $response = rest_get_server()->dispatch( $request );
     869        $this->assertEquals( 201, $response->get_status() );
     870        $data = $response->get_data();
     871        $this->assertEquals( $parent, $data['parent'] );
     872    }
     873
    862874    public function test_update_item() {
    863875        wp_set_current_user( self::$administrator );
     
    928940        $data = $response->get_data();
    929941        $this->assertEquals( $parent->term_id, $data['parent'] );
     942    }
     943
     944    public function test_update_item_remove_parent() {
     945        wp_set_current_user( self::$administrator );
     946
     947        $old_parent_term = get_term_by( 'id', $this->factory->category->create(), 'category' );
     948        $new_parent_id   = 0;
     949
     950        $term = get_term_by(
     951            'id',
     952            $this->factory->category->create(
     953                [
     954                    'parent' => $old_parent_term->term_id,
     955                ]
     956            ),
     957            'category'
     958        );
     959
     960        $this->assertEquals( $old_parent_term->term_id, $term->parent );
     961
     962        $request = new WP_REST_Request( 'POST', '/wp/v2/categories/' . $term->term_id );
     963        $request->set_param( 'parent', $new_parent_id );
     964        $response = rest_get_server()->dispatch( $request );
     965        $this->assertEquals( 200, $response->get_status() );
     966
     967        $data = $response->get_data();
     968        $this->assertEquals( $new_parent_id, $data['parent'] );
    930969    }
    931970
Note: See TracChangeset for help on using the changeset viewer.