- Timestamp:
- 01/26/2017 01:38:27 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php
r39671 r39954 97 97 98 98 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array( 99 'args' => array( 100 'id' => array( 101 'description' => __( 'Unique identifier for the term.' ), 102 'type' => 'integer', 103 ), 104 ), 99 105 array( 100 106 'methods' => WP_REST_Server::READABLE, … … 109 115 'callback' => array( $this, 'update_item' ), 110 116 'permission_callback' => array( $this, 'update_item_permissions_check' ), 111 'args' 117 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), 112 118 ), 113 119 array( … … 289 295 290 296 /** 297 * Get the term, if the ID is valid. 298 * 299 * @since 4.7.2 300 * 301 * @param int $id Supplied ID. 302 * @return WP_Term|WP_Error Term object if ID is valid, WP_Error otherwise. 303 */ 304 protected function get_term( $id ) { 305 $error = new WP_Error( 'rest_term_invalid', __( 'Term does not exist.' ), array( 'status' => 404 ) ); 306 307 if ( ! $this->check_is_taxonomy_allowed( $this->taxonomy ) ) { 308 return $error; 309 } 310 311 if ( (int) $id <= 0 ) { 312 return $error; 313 } 314 315 $term = get_term( (int) $id, $this->taxonomy ); 316 if ( empty( $term ) || $term->taxonomy !== $this->taxonomy ) { 317 return $error; 318 } 319 320 return $term; 321 } 322 323 /** 291 324 * Checks if a request has access to read or edit the specified term. 292 325 * … … 298 331 */ 299 332 public function get_item_permissions_check( $request ) { 300 $tax_obj = get_taxonomy( $this->taxonomy ); 301 if ( ! $tax_obj || ! $this->check_is_taxonomy_allowed( $this->taxonomy ) ) { 302 return false; 303 } 304 if ( 'edit' === $request['context'] && ! current_user_can( 'edit_term', (int) $request['id'] ) ) { 333 $term = $this->get_term( $request['id'] ); 334 if ( is_wp_error( $term ) ) { 335 return $term; 336 } 337 338 if ( 'edit' === $request['context'] && ! current_user_can( 'edit_term', $term->term_id ) ) { 305 339 return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to edit this term.' ), array( 'status' => rest_authorization_required_code() ) ); 306 340 } … … 318 352 */ 319 353 public function get_item( $request ) { 320 321 $term = get_term( (int) $request['id'], $this->taxonomy ); 322 323 if ( ! $term || $term->taxonomy !== $this->taxonomy ) { 324 return new WP_Error( 'rest_term_invalid', __( 'Term does not exist.' ), array( 'status' => 404 ) ); 325 } 326 354 $term = $this->get_term( $request['id'] ); 327 355 if ( is_wp_error( $term ) ) { 328 356 return $term; … … 446 474 */ 447 475 public function update_item_permissions_check( $request ) { 448 449 if ( ! $this->check_is_taxonomy_allowed( $this->taxonomy ) ) { 450 return false; 451 } 452 453 $term = get_term( (int) $request['id'], $this->taxonomy ); 454 455 if ( ! $term ) { 456 return new WP_Error( 'rest_term_invalid', __( 'Term does not exist.' ), array( 'status' => 404 ) ); 476 $term = $this->get_term( $request['id'] ); 477 if ( is_wp_error( $term ) ) { 478 return $term; 457 479 } 458 480 … … 474 496 */ 475 497 public function update_item( $request ) { 498 $term = $this->get_term( $request['id'] ); 499 if ( is_wp_error( $term ) ) { 500 return $term; 501 } 502 476 503 if ( isset( $request['parent'] ) ) { 477 504 if ( ! is_taxonomy_hierarchical( $this->taxonomy ) ) { … … 488 515 $prepared_term = $this->prepare_item_for_database( $request ); 489 516 490 $term = get_term( (int) $request['id'], $this->taxonomy );491 492 517 // Only update the term if we haz something to update. 493 518 if ( ! empty( $prepared_term ) ) { … … 499 524 } 500 525 501 $term = get_term( (int) $request['id'], $this->taxonomy );526 $term = get_term( $term->term_id, $this->taxonomy ); 502 527 503 528 /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php */ … … 506 531 $schema = $this->get_item_schema(); 507 532 if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) { 508 $meta_update = $this->meta->update_value( $request['meta'], (int) $request['id']);533 $meta_update = $this->meta->update_value( $request['meta'], $term->term_id ); 509 534 510 535 if ( is_wp_error( $meta_update ) ) { … … 536 561 */ 537 562 public function delete_item_permissions_check( $request ) { 538 if ( ! $this->check_is_taxonomy_allowed( $this->taxonomy ) ) { 539 return false; 540 } 541 542 $term = get_term( (int) $request['id'], $this->taxonomy ); 543 544 if ( ! $term ) { 545 return new WP_Error( 'rest_term_invalid', __( 'Term does not exist.' ), array( 'status' => 404 ) ); 563 $term = $this->get_term( $request['id'] ); 564 if ( is_wp_error( $term ) ) { 565 return $term; 546 566 } 547 567 … … 563 583 */ 564 584 public function delete_item( $request ) { 585 $term = $this->get_term( $request['id'] ); 586 if ( is_wp_error( $term ) ) { 587 return $term; 588 } 565 589 566 590 $force = isset( $request['force'] ) ? (bool) $request['force'] : false; … … 570 594 return new WP_Error( 'rest_trash_not_supported', __( 'Terms do not support trashing. Set force=true to delete.' ), array( 'status' => 501 ) ); 571 595 } 572 573 $term = get_term( (int) $request['id'], $this->taxonomy );574 596 575 597 $request->set_param( 'context', 'view' );
Note: See TracChangeset
for help on using the changeset viewer.