Changeset 39957 for branches/4.7/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php
- Timestamp:
- 01/26/2017 01:46:54 PM (8 years ago)
- Location:
- branches/4.7
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/4.7
-
branches/4.7/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php
r39631 r39957 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 doesn't exist." ), array( 'status' => 404 ) ); 325 } 354 $term = $this->get_term( $request['id'] ); 326 355 327 356 if ( is_wp_error( $term ) ) { … … 446 475 */ 447 476 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 doesn't exist." ), array( 'status' => 404 ) ); 477 $term = $this->get_term( $request['id'] ); 478 if ( is_wp_error( $term ) ) { 479 return $term; 457 480 } 458 481 … … 474 497 */ 475 498 public function update_item( $request ) { 499 $term = $this->get_term( $request['id'] ); 500 if ( is_wp_error( $term ) ) { 501 return $term; 502 } 503 476 504 if ( isset( $request['parent'] ) ) { 477 505 if ( ! is_taxonomy_hierarchical( $this->taxonomy ) ) { … … 488 516 $prepared_term = $this->prepare_item_for_database( $request ); 489 517 490 $term = get_term( (int) $request['id'], $this->taxonomy );491 492 518 // Only update the term if we haz something to update. 493 519 if ( ! empty( $prepared_term ) ) { … … 499 525 } 500 526 501 $term = get_term( (int) $request['id'], $this->taxonomy );527 $term = get_term( $term->term_id, $this->taxonomy ); 502 528 503 529 /* This action is documented in lib/endpoints/class-wp-rest-terms-controller.php */ … … 506 532 $schema = $this->get_item_schema(); 507 533 if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) { 508 $meta_update = $this->meta->update_value( $request['meta'], (int) $request['id']);534 $meta_update = $this->meta->update_value( $request['meta'], $term->term_id ); 509 535 510 536 if ( is_wp_error( $meta_update ) ) { … … 536 562 */ 537 563 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 doesn't exist." ), array( 'status' => 404 ) ); 564 $term = $this->get_term( $request['id'] ); 565 if ( is_wp_error( $term ) ) { 566 return $term; 546 567 } 547 568 … … 563 584 */ 564 585 public function delete_item( $request ) { 586 $term = $this->get_term( $request['id'] ); 587 if ( is_wp_error( $term ) ) { 588 return $term; 589 } 565 590 566 591 $force = isset( $request['force'] ) ? (bool) $request['force'] : false; … … 570 595 return new WP_Error( 'rest_trash_not_supported', __( 'Terms do not support trashing. Set force=true to delete.' ), array( 'status' => 501 ) ); 571 596 } 572 573 $term = get_term( (int) $request['id'], $this->taxonomy );574 597 575 598 $request->set_param( 'context', 'view' );
Note: See TracChangeset
for help on using the changeset viewer.