[ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * REST API: WP_REST_Terms_Controller class 4 * 5 * @package WordPress 6 * @subpackage REST_API 7 * @since 4.7.0 8 */ 9 10 /** 11 * Core class used to managed terms associated with a taxonomy via the REST API. 12 * 13 * @since 4.7.0 14 * 15 * @see WP_REST_Controller 16 */ 17 class WP_REST_Terms_Controller extends WP_REST_Controller { 18 19 /** 20 * Taxonomy key. 21 * 22 * @since 4.7.0 23 * @var string 24 */ 25 protected $taxonomy; 26 27 /** 28 * Instance of a term meta fields object. 29 * 30 * @since 4.7.0 31 * @var WP_REST_Term_Meta_Fields 32 */ 33 protected $meta; 34 35 /** 36 * Column to have the terms be sorted by. 37 * 38 * @since 4.7.0 39 * @var string 40 */ 41 protected $sort_column; 42 43 /** 44 * Number of terms that were found. 45 * 46 * @since 4.7.0 47 * @var int 48 */ 49 protected $total_terms; 50 51 /** 52 * Whether the controller supports batching. 53 * 54 * @since 5.9.0 55 * @var array 56 */ 57 protected $allow_batch = array( 'v1' => true ); 58 59 /** 60 * Constructor. 61 * 62 * @since 4.7.0 63 * 64 * @param string $taxonomy Taxonomy key. 65 */ 66 public function __construct( $taxonomy ) { 67 $this->taxonomy = $taxonomy; 68 $tax_obj = get_taxonomy( $taxonomy ); 69 $this->rest_base = ! empty( $tax_obj->rest_base ) ? $tax_obj->rest_base : $tax_obj->name; 70 $this->namespace = ! empty( $tax_obj->rest_namespace ) ? $tax_obj->rest_namespace : 'wp/v2'; 71 72 $this->meta = new WP_REST_Term_Meta_Fields( $taxonomy ); 73 } 74 75 /** 76 * Registers the routes for terms. 77 * 78 * @since 4.7.0 79 * 80 * @see register_rest_route() 81 */ 82 public function register_routes() { 83 84 register_rest_route( 85 $this->namespace, 86 '/' . $this->rest_base, 87 array( 88 array( 89 'methods' => WP_REST_Server::READABLE, 90 'callback' => array( $this, 'get_items' ), 91 'permission_callback' => array( $this, 'get_items_permissions_check' ), 92 'args' => $this->get_collection_params(), 93 ), 94 array( 95 'methods' => WP_REST_Server::CREATABLE, 96 'callback' => array( $this, 'create_item' ), 97 'permission_callback' => array( $this, 'create_item_permissions_check' ), 98 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), 99 ), 100 'allow_batch' => $this->allow_batch, 101 'schema' => array( $this, 'get_public_item_schema' ), 102 ) 103 ); 104 105 register_rest_route( 106 $this->namespace, 107 '/' . $this->rest_base . '/(?P<id>[\d]+)', 108 array( 109 'args' => array( 110 'id' => array( 111 'description' => __( 'Unique identifier for the term.' ), 112 'type' => 'integer', 113 ), 114 ), 115 array( 116 'methods' => WP_REST_Server::READABLE, 117 'callback' => array( $this, 'get_item' ), 118 'permission_callback' => array( $this, 'get_item_permissions_check' ), 119 'args' => array( 120 'context' => $this->get_context_param( array( 'default' => 'view' ) ), 121 ), 122 ), 123 array( 124 'methods' => WP_REST_Server::EDITABLE, 125 'callback' => array( $this, 'update_item' ), 126 'permission_callback' => array( $this, 'update_item_permissions_check' ), 127 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), 128 ), 129 array( 130 'methods' => WP_REST_Server::DELETABLE, 131 'callback' => array( $this, 'delete_item' ), 132 'permission_callback' => array( $this, 'delete_item_permissions_check' ), 133 'args' => array( 134 'force' => array( 135 'type' => 'boolean', 136 'default' => false, 137 'description' => __( 'Required to be true, as terms do not support trashing.' ), 138 ), 139 ), 140 ), 141 'allow_batch' => $this->allow_batch, 142 'schema' => array( $this, 'get_public_item_schema' ), 143 ) 144 ); 145 } 146 147 /** 148 * Checks if the terms for a post can be read. 149 * 150 * @since 6.0.3 151 * 152 * @param WP_Post $post Post object. 153 * @param WP_REST_Request $request Full details about the request. 154 * @return bool Whether the terms for the post can be read. 155 */ 156 public function check_read_terms_permission_for_post( $post, $request ) { 157 // If the requested post isn't associated with this taxonomy, deny access. 158 if ( ! is_object_in_taxonomy( $post->post_type, $this->taxonomy ) ) { 159 return false; 160 } 161 162 // Grant access if the post is publicly viewable. 163 if ( is_post_publicly_viewable( $post ) ) { 164 return true; 165 } 166 167 // Otherwise grant access if the post is readable by the logged-in user. 168 if ( current_user_can( 'read_post', $post->ID ) ) { 169 return true; 170 } 171 172 // Otherwise, deny access. 173 return false; 174 } 175 176 /** 177 * Checks if a request has access to read terms in the specified taxonomy. 178 * 179 * @since 4.7.0 180 * 181 * @param WP_REST_Request $request Full details about the request. 182 * @return bool|WP_Error True if the request has read access, otherwise false or WP_Error object. 183 */ 184 public function get_items_permissions_check( $request ) { 185 $tax_obj = get_taxonomy( $this->taxonomy ); 186 187 if ( ! $tax_obj || ! $this->check_is_taxonomy_allowed( $this->taxonomy ) ) { 188 return false; 189 } 190 191 if ( 'edit' === $request['context'] && ! current_user_can( $tax_obj->cap->edit_terms ) ) { 192 return new WP_Error( 193 'rest_forbidden_context', 194 __( 'Sorry, you are not allowed to edit terms in this taxonomy.' ), 195 array( 'status' => rest_authorization_required_code() ) 196 ); 197 } 198 199 if ( ! empty( $request['post'] ) ) { 200 $post = get_post( $request['post'] ); 201 202 if ( ! $post ) { 203 return new WP_Error( 204 'rest_post_invalid_id', 205 __( 'Invalid post ID.' ), 206 array( 207 'status' => 400, 208 ) 209 ); 210 } 211 212 if ( ! $this->check_read_terms_permission_for_post( $post, $request ) ) { 213 return new WP_Error( 214 'rest_forbidden_context', 215 __( 'Sorry, you are not allowed to view terms for this post.' ), 216 array( 217 'status' => rest_authorization_required_code(), 218 ) 219 ); 220 } 221 } 222 223 return true; 224 } 225 226 /** 227 * Retrieves terms associated with a taxonomy. 228 * 229 * @since 4.7.0 230 * @since 6.8.0 Respect default query arguments set for the taxonomy upon registration. 231 * 232 * @param WP_REST_Request $request Full details about the request. 233 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. 234 */ 235 public function get_items( $request ) { 236 237 // Retrieve the list of registered collection query parameters. 238 $registered = $this->get_collection_params(); 239 240 /* 241 * This array defines mappings between public API query parameters whose 242 * values are accepted as-passed, and their internal WP_Query parameter 243 * name equivalents (some are the same). Only values which are also 244 * present in $registered will be set. 245 */ 246 $parameter_mappings = array( 247 'exclude' => 'exclude', 248 'include' => 'include', 249 'order' => 'order', 250 'orderby' => 'orderby', 251 'post' => 'post', 252 'hide_empty' => 'hide_empty', 253 'per_page' => 'number', 254 'search' => 'search', 255 'slug' => 'slug', 256 ); 257 258 $prepared_args = array( 'taxonomy' => $this->taxonomy ); 259 260 /* 261 * For each known parameter which is both registered and present in the request, 262 * set the parameter's value on the query $prepared_args. 263 */ 264 foreach ( $parameter_mappings as $api_param => $wp_param ) { 265 if ( isset( $registered[ $api_param ], $request[ $api_param ] ) ) { 266 $prepared_args[ $wp_param ] = $request[ $api_param ]; 267 } 268 } 269 270 if ( isset( $prepared_args['orderby'] ) && isset( $request['orderby'] ) ) { 271 $orderby_mappings = array( 272 'include_slugs' => 'slug__in', 273 ); 274 275 if ( isset( $orderby_mappings[ $request['orderby'] ] ) ) { 276 $prepared_args['orderby'] = $orderby_mappings[ $request['orderby'] ]; 277 } 278 } 279 280 if ( isset( $registered['offset'] ) && ! empty( $request['offset'] ) ) { 281 $prepared_args['offset'] = $request['offset']; 282 } else { 283 $prepared_args['offset'] = ( $request['page'] - 1 ) * $prepared_args['number']; 284 } 285 286 $taxonomy_obj = get_taxonomy( $this->taxonomy ); 287 288 if ( $taxonomy_obj->hierarchical && isset( $registered['parent'], $request['parent'] ) ) { 289 if ( 0 === $request['parent'] ) { 290 // Only query top-level terms. 291 $prepared_args['parent'] = 0; 292 } else { 293 if ( $request['parent'] ) { 294 $prepared_args['parent'] = $request['parent']; 295 } 296 } 297 } 298 299 /* 300 * When a taxonomy is registered with an 'args' array, 301 * those params override the `$args` passed to this function. 302 * 303 * We only need to do this if no `post` argument is provided. 304 * Otherwise, terms will be fetched using `wp_get_object_terms()`, 305 * which respects the default query arguments set for the taxonomy. 306 */ 307 if ( 308 empty( $prepared_args['post'] ) && 309 isset( $taxonomy_obj->args ) && 310 is_array( $taxonomy_obj->args ) 311 ) { 312 $prepared_args = array_merge( $prepared_args, $taxonomy_obj->args ); 313 } 314 315 $is_head_request = $request->is_method( 'HEAD' ); 316 if ( $is_head_request ) { 317 // Force the 'fields' argument. For HEAD requests, only term IDs are required. 318 $prepared_args['fields'] = 'ids'; 319 // Disable priming term meta for HEAD requests to improve performance. 320 $prepared_args['update_term_meta_cache'] = false; 321 } 322 323 /** 324 * Filters get_terms() arguments when querying terms via the REST API. 325 * 326 * The dynamic portion of the hook name, `$this->taxonomy`, refers to the taxonomy slug. 327 * 328 * Possible hook names include: 329 * 330 * - `rest_category_query` 331 * - `rest_post_tag_query` 332 * 333 * Enables adding extra arguments or setting defaults for a terms 334 * collection request. 335 * 336 * @since 4.7.0 337 * 338 * @link https://developer.wordpress.org/reference/functions/get_terms/ 339 * 340 * @param array $prepared_args Array of arguments for get_terms(). 341 * @param WP_REST_Request $request The REST API request. 342 */ 343 $prepared_args = apply_filters( "rest_{$this->taxonomy}_query", $prepared_args, $request ); 344 345 if ( ! empty( $prepared_args['post'] ) ) { 346 $query_result = wp_get_object_terms( $prepared_args['post'], $this->taxonomy, $prepared_args ); 347 348 // Used when calling wp_count_terms() below. 349 $prepared_args['object_ids'] = $prepared_args['post']; 350 } else { 351 $query_result = get_terms( $prepared_args ); 352 } 353 354 $count_args = $prepared_args; 355 356 unset( $count_args['number'], $count_args['offset'] ); 357 358 $total_terms = wp_count_terms( $count_args ); 359 360 // wp_count_terms() can return a falsey value when the term has no children. 361 if ( ! $total_terms ) { 362 $total_terms = 0; 363 } 364 365 if ( ! $is_head_request ) { 366 $response = array(); 367 foreach ( $query_result as $term ) { 368 $data = $this->prepare_item_for_response( $term, $request ); 369 $response[] = $this->prepare_response_for_collection( $data ); 370 } 371 } 372 373 $response = $is_head_request ? new WP_REST_Response( array() ) : rest_ensure_response( $response ); 374 375 // Store pagination values for headers. 376 $per_page = (int) $prepared_args['number']; 377 $page = (int) ceil( ( ( (int) $prepared_args['offset'] ) / $per_page ) + 1 ); 378 379 $response->header( 'X-WP-Total', (int) $total_terms ); 380 381 $max_pages = (int) ceil( $total_terms / $per_page ); 382 383 $response->header( 'X-WP-TotalPages', $max_pages ); 384 385 $request_params = $request->get_query_params(); 386 $collection_url = rest_url( rest_get_route_for_taxonomy_items( $this->taxonomy ) ); 387 $base = add_query_arg( urlencode_deep( $request_params ), $collection_url ); 388 389 if ( $page > 1 ) { 390 $prev_page = $page - 1; 391 392 if ( $prev_page > $max_pages ) { 393 $prev_page = $max_pages; 394 } 395 396 $prev_link = add_query_arg( 'page', $prev_page, $base ); 397 $response->link_header( 'prev', $prev_link ); 398 } 399 if ( $max_pages > $page ) { 400 $next_page = $page + 1; 401 $next_link = add_query_arg( 'page', $next_page, $base ); 402 403 $response->link_header( 'next', $next_link ); 404 } 405 406 return $response; 407 } 408 409 /** 410 * Get the term, if the ID is valid. 411 * 412 * @since 4.7.2 413 * 414 * @param int $id Supplied ID. 415 * @return WP_Term|WP_Error Term object if ID is valid, WP_Error otherwise. 416 */ 417 protected function get_term( $id ) { 418 $error = new WP_Error( 419 'rest_term_invalid', 420 __( 'Term does not exist.' ), 421 array( 'status' => 404 ) 422 ); 423 424 if ( ! $this->check_is_taxonomy_allowed( $this->taxonomy ) ) { 425 return $error; 426 } 427 428 if ( (int) $id <= 0 ) { 429 return $error; 430 } 431 432 $term = get_term( (int) $id, $this->taxonomy ); 433 if ( empty( $term ) || $term->taxonomy !== $this->taxonomy ) { 434 return $error; 435 } 436 437 return $term; 438 } 439 440 /** 441 * Checks if a request has access to read or edit the specified term. 442 * 443 * @since 4.7.0 444 * 445 * @param WP_REST_Request $request Full details about the request. 446 * @return true|WP_Error True if the request has read access for the item, otherwise WP_Error object. 447 */ 448 public function get_item_permissions_check( $request ) { 449 $term = $this->get_term( $request['id'] ); 450 451 if ( is_wp_error( $term ) ) { 452 return $term; 453 } 454 455 if ( 'edit' === $request['context'] && ! current_user_can( 'edit_term', $term->term_id ) ) { 456 return new WP_Error( 457 'rest_forbidden_context', 458 __( 'Sorry, you are not allowed to edit this term.' ), 459 array( 'status' => rest_authorization_required_code() ) 460 ); 461 } 462 463 return true; 464 } 465 466 /** 467 * Gets a single term from a taxonomy. 468 * 469 * @since 4.7.0 470 * 471 * @param WP_REST_Request $request Full details about the request. 472 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. 473 */ 474 public function get_item( $request ) { 475 $term = $this->get_term( $request['id'] ); 476 if ( is_wp_error( $term ) ) { 477 return $term; 478 } 479 480 $response = $this->prepare_item_for_response( $term, $request ); 481 482 return rest_ensure_response( $response ); 483 } 484 485 /** 486 * Checks if a request has access to create a term. 487 * 488 * @since 4.7.0 489 * 490 * @param WP_REST_Request $request Full details about the request. 491 * @return bool|WP_Error True if the request has access to create items, otherwise false or WP_Error object. 492 */ 493 public function create_item_permissions_check( $request ) { 494 495 if ( ! $this->check_is_taxonomy_allowed( $this->taxonomy ) ) { 496 return false; 497 } 498 499 $taxonomy_obj = get_taxonomy( $this->taxonomy ); 500 501 if ( ( is_taxonomy_hierarchical( $this->taxonomy ) 502 && ! current_user_can( $taxonomy_obj->cap->edit_terms ) ) 503 || ( ! is_taxonomy_hierarchical( $this->taxonomy ) 504 && ! current_user_can( $taxonomy_obj->cap->assign_terms ) ) ) { 505 return new WP_Error( 506 'rest_cannot_create', 507 __( 'Sorry, you are not allowed to create terms in this taxonomy.' ), 508 array( 'status' => rest_authorization_required_code() ) 509 ); 510 } 511 512 return true; 513 } 514 515 /** 516 * Creates a single term in a taxonomy. 517 * 518 * @since 4.7.0 519 * 520 * @param WP_REST_Request $request Full details about the request. 521 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. 522 */ 523 public function create_item( $request ) { 524 if ( isset( $request['parent'] ) ) { 525 if ( ! is_taxonomy_hierarchical( $this->taxonomy ) ) { 526 return new WP_Error( 527 'rest_taxonomy_not_hierarchical', 528 __( 'Cannot set parent term, taxonomy is not hierarchical.' ), 529 array( 'status' => 400 ) 530 ); 531 } 532 533 $parent = get_term( (int) $request['parent'], $this->taxonomy ); 534 535 if ( ! $parent ) { 536 return new WP_Error( 537 'rest_term_invalid', 538 __( 'Parent term does not exist.' ), 539 array( 'status' => 400 ) 540 ); 541 } 542 } 543 544 $prepared_term = $this->prepare_item_for_database( $request ); 545 546 $term = wp_insert_term( wp_slash( $prepared_term->name ), $this->taxonomy, wp_slash( (array) $prepared_term ) ); 547 if ( is_wp_error( $term ) ) { 548 /* 549 * If we're going to inform the client that the term already exists, 550 * give them the identifier for future use. 551 */ 552 $term_id = $term->get_error_data( 'term_exists' ); 553 if ( $term_id ) { 554 $existing_term = get_term( $term_id, $this->taxonomy ); 555 $term->add_data( $existing_term->term_id, 'term_exists' ); 556 $term->add_data( 557 array( 558 'status' => 400, 559 'term_id' => $term_id, 560 ) 561 ); 562 } 563 564 return $term; 565 } 566 567 $term = get_term( $term['term_id'], $this->taxonomy ); 568 569 /** 570 * Fires after a single term is created or updated via the REST API. 571 * 572 * The dynamic portion of the hook name, `$this->taxonomy`, refers to the taxonomy slug. 573 * 574 * Possible hook names include: 575 * 576 * - `rest_insert_category` 577 * - `rest_insert_post_tag` 578 * 579 * @since 4.7.0 580 * 581 * @param WP_Term $term Inserted or updated term object. 582 * @param WP_REST_Request $request Request object. 583 * @param bool $creating True when creating a term, false when updating. 584 */ 585 do_action( "rest_insert_{$this->taxonomy}", $term, $request, true ); 586 587 $schema = $this->get_item_schema(); 588 if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) { 589 $meta_update = $this->meta->update_value( $request['meta'], $term->term_id ); 590 591 if ( is_wp_error( $meta_update ) ) { 592 return $meta_update; 593 } 594 } 595 596 $fields_update = $this->update_additional_fields_for_object( $term, $request ); 597 598 if ( is_wp_error( $fields_update ) ) { 599 return $fields_update; 600 } 601 602 $request->set_param( 'context', 'edit' ); 603 604 /** 605 * Fires after a single term is completely created or updated via the REST API. 606 * 607 * The dynamic portion of the hook name, `$this->taxonomy`, refers to the taxonomy slug. 608 * 609 * Possible hook names include: 610 * 611 * - `rest_after_insert_category` 612 * - `rest_after_insert_post_tag` 613 * 614 * @since 5.0.0 615 * 616 * @param WP_Term $term Inserted or updated term object. 617 * @param WP_REST_Request $request Request object. 618 * @param bool $creating True when creating a term, false when updating. 619 */ 620 do_action( "rest_after_insert_{$this->taxonomy}", $term, $request, true ); 621 622 $response = $this->prepare_item_for_response( $term, $request ); 623 $response = rest_ensure_response( $response ); 624 625 $response->set_status( 201 ); 626 $response->header( 'Location', rest_url( $this->namespace . '/' . $this->rest_base . '/' . $term->term_id ) ); 627 628 return $response; 629 } 630 631 /** 632 * Checks if a request has access to update the specified term. 633 * 634 * @since 4.7.0 635 * 636 * @param WP_REST_Request $request Full details about the request. 637 * @return true|WP_Error True if the request has access to update the item, false or WP_Error object otherwise. 638 */ 639 public function update_item_permissions_check( $request ) { 640 $term = $this->get_term( $request['id'] ); 641 642 if ( is_wp_error( $term ) ) { 643 return $term; 644 } 645 646 if ( ! current_user_can( 'edit_term', $term->term_id ) ) { 647 return new WP_Error( 648 'rest_cannot_update', 649 __( 'Sorry, you are not allowed to edit this term.' ), 650 array( 'status' => rest_authorization_required_code() ) 651 ); 652 } 653 654 return true; 655 } 656 657 /** 658 * Updates a single term from a taxonomy. 659 * 660 * @since 4.7.0 661 * 662 * @param WP_REST_Request $request Full details about the request. 663 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. 664 */ 665 public function update_item( $request ) { 666 $term = $this->get_term( $request['id'] ); 667 if ( is_wp_error( $term ) ) { 668 return $term; 669 } 670 671 if ( isset( $request['parent'] ) ) { 672 if ( ! is_taxonomy_hierarchical( $this->taxonomy ) ) { 673 return new WP_Error( 674 'rest_taxonomy_not_hierarchical', 675 __( 'Cannot set parent term, taxonomy is not hierarchical.' ), 676 array( 'status' => 400 ) 677 ); 678 } 679 680 $parent = get_term( (int) $request['parent'], $this->taxonomy ); 681 682 if ( ! $parent ) { 683 return new WP_Error( 684 'rest_term_invalid', 685 __( 'Parent term does not exist.' ), 686 array( 'status' => 400 ) 687 ); 688 } 689 } 690 691 $prepared_term = $this->prepare_item_for_database( $request ); 692 693 // Only update the term if we have something to update. 694 if ( ! empty( $prepared_term ) ) { 695 $update = wp_update_term( $term->term_id, $term->taxonomy, wp_slash( (array) $prepared_term ) ); 696 697 if ( is_wp_error( $update ) ) { 698 return $update; 699 } 700 } 701 702 $term = get_term( $term->term_id, $this->taxonomy ); 703 704 /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php */ 705 do_action( "rest_insert_{$this->taxonomy}", $term, $request, false ); 706 707 $schema = $this->get_item_schema(); 708 if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) { 709 $meta_update = $this->meta->update_value( $request['meta'], $term->term_id ); 710 711 if ( is_wp_error( $meta_update ) ) { 712 return $meta_update; 713 } 714 } 715 716 $fields_update = $this->update_additional_fields_for_object( $term, $request ); 717 718 if ( is_wp_error( $fields_update ) ) { 719 return $fields_update; 720 } 721 722 $request->set_param( 'context', 'edit' ); 723 724 /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php */ 725 do_action( "rest_after_insert_{$this->taxonomy}", $term, $request, false ); 726 727 $response = $this->prepare_item_for_response( $term, $request ); 728 729 return rest_ensure_response( $response ); 730 } 731 732 /** 733 * Checks if a request has access to delete the specified term. 734 * 735 * @since 4.7.0 736 * 737 * @param WP_REST_Request $request Full details about the request. 738 * @return true|WP_Error True if the request has access to delete the item, otherwise false or WP_Error object. 739 */ 740 public function delete_item_permissions_check( $request ) { 741 $term = $this->get_term( $request['id'] ); 742 743 if ( is_wp_error( $term ) ) { 744 return $term; 745 } 746 747 if ( ! current_user_can( 'delete_term', $term->term_id ) ) { 748 return new WP_Error( 749 'rest_cannot_delete', 750 __( 'Sorry, you are not allowed to delete this term.' ), 751 array( 'status' => rest_authorization_required_code() ) 752 ); 753 } 754 755 return true; 756 } 757 758 /** 759 * Deletes a single term from a taxonomy. 760 * 761 * @since 4.7.0 762 * 763 * @param WP_REST_Request $request Full details about the request. 764 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. 765 */ 766 public function delete_item( $request ) { 767 $term = $this->get_term( $request['id'] ); 768 if ( is_wp_error( $term ) ) { 769 return $term; 770 } 771 772 $force = isset( $request['force'] ) ? (bool) $request['force'] : false; 773 774 // We don't support trashing for terms. 775 if ( ! $force ) { 776 return new WP_Error( 777 'rest_trash_not_supported', 778 /* translators: %s: force=true */ 779 sprintf( __( "Terms do not support trashing. Set '%s' to delete." ), 'force=true' ), 780 array( 'status' => 501 ) 781 ); 782 } 783 784 $request->set_param( 'context', 'view' ); 785 786 $previous = $this->prepare_item_for_response( $term, $request ); 787 788 $retval = wp_delete_term( $term->term_id, $term->taxonomy ); 789 790 if ( ! $retval ) { 791 return new WP_Error( 792 'rest_cannot_delete', 793 __( 'The term cannot be deleted.' ), 794 array( 'status' => 500 ) 795 ); 796 } 797 798 $response = new WP_REST_Response(); 799 $response->set_data( 800 array( 801 'deleted' => true, 802 'previous' => $previous->get_data(), 803 ) 804 ); 805 806 /** 807 * Fires after a single term is deleted via the REST API. 808 * 809 * The dynamic portion of the hook name, `$this->taxonomy`, refers to the taxonomy slug. 810 * 811 * Possible hook names include: 812 * 813 * - `rest_delete_category` 814 * - `rest_delete_post_tag` 815 * 816 * @since 4.7.0 817 * 818 * @param WP_Term $term The deleted term. 819 * @param WP_REST_Response $response The response data. 820 * @param WP_REST_Request $request The request sent to the API. 821 */ 822 do_action( "rest_delete_{$this->taxonomy}", $term, $response, $request ); 823 824 return $response; 825 } 826 827 /** 828 * Prepares a single term for create or update. 829 * 830 * @since 4.7.0 831 * 832 * @param WP_REST_Request $request Request object. 833 * @return object Term object. 834 */ 835 public function prepare_item_for_database( $request ) { 836 $prepared_term = new stdClass(); 837 838 $schema = $this->get_item_schema(); 839 if ( isset( $request['name'] ) && ! empty( $schema['properties']['name'] ) ) { 840 $prepared_term->name = $request['name']; 841 } 842 843 if ( isset( $request['slug'] ) && ! empty( $schema['properties']['slug'] ) ) { 844 $prepared_term->slug = $request['slug']; 845 } 846 847 if ( isset( $request['taxonomy'] ) && ! empty( $schema['properties']['taxonomy'] ) ) { 848 $prepared_term->taxonomy = $request['taxonomy']; 849 } 850 851 if ( isset( $request['description'] ) && ! empty( $schema['properties']['description'] ) ) { 852 $prepared_term->description = $request['description']; 853 } 854 855 if ( isset( $request['parent'] ) && ! empty( $schema['properties']['parent'] ) ) { 856 $parent_term_id = 0; 857 $requested_parent = (int) $request['parent']; 858 859 if ( $requested_parent ) { 860 $parent_term = get_term( $requested_parent, $this->taxonomy ); 861 862 if ( $parent_term instanceof WP_Term ) { 863 $parent_term_id = $parent_term->term_id; 864 } 865 } 866 867 $prepared_term->parent = $parent_term_id; 868 } 869 870 /** 871 * Filters term data before inserting term via the REST API. 872 * 873 * The dynamic portion of the hook name, `$this->taxonomy`, refers to the taxonomy slug. 874 * 875 * Possible hook names include: 876 * 877 * - `rest_pre_insert_category` 878 * - `rest_pre_insert_post_tag` 879 * 880 * @since 4.7.0 881 * 882 * @param object $prepared_term Term object. 883 * @param WP_REST_Request $request Request object. 884 */ 885 return apply_filters( "rest_pre_insert_{$this->taxonomy}", $prepared_term, $request ); 886 } 887 888 /** 889 * Prepares a single term output for response. 890 * 891 * @since 4.7.0 892 * 893 * @param WP_Term $item Term object. 894 * @param WP_REST_Request $request Request object. 895 * @return WP_REST_Response Response object. 896 */ 897 public function prepare_item_for_response( $item, $request ) { 898 899 // Don't prepare the response body for HEAD requests. 900 if ( $request->is_method( 'HEAD' ) ) { 901 /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php */ 902 return apply_filters( "rest_prepare_{$this->taxonomy}", new WP_REST_Response( array() ), $item, $request ); 903 } 904 905 $fields = $this->get_fields_for_response( $request ); 906 $data = array(); 907 908 if ( in_array( 'id', $fields, true ) ) { 909 $data['id'] = (int) $item->term_id; 910 } 911 912 if ( in_array( 'count', $fields, true ) ) { 913 $data['count'] = (int) $item->count; 914 } 915 916 if ( in_array( 'description', $fields, true ) ) { 917 $data['description'] = $item->description; 918 } 919 920 if ( in_array( 'link', $fields, true ) ) { 921 $data['link'] = get_term_link( $item ); 922 } 923 924 if ( in_array( 'name', $fields, true ) ) { 925 $data['name'] = $item->name; 926 } 927 928 if ( in_array( 'slug', $fields, true ) ) { 929 $data['slug'] = $item->slug; 930 } 931 932 if ( in_array( 'taxonomy', $fields, true ) ) { 933 $data['taxonomy'] = $item->taxonomy; 934 } 935 936 if ( in_array( 'parent', $fields, true ) ) { 937 $data['parent'] = (int) $item->parent; 938 } 939 940 if ( in_array( 'meta', $fields, true ) ) { 941 $data['meta'] = $this->meta->get_value( $item->term_id, $request ); 942 } 943 944 $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; 945 $data = $this->add_additional_fields_to_object( $data, $request ); 946 $data = $this->filter_response_by_context( $data, $context ); 947 948 $response = rest_ensure_response( $data ); 949 950 if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) { 951 $response->add_links( $this->prepare_links( $item ) ); 952 } 953 954 /** 955 * Filters the term data for a REST API response. 956 * 957 * The dynamic portion of the hook name, `$this->taxonomy`, refers to the taxonomy slug. 958 * 959 * Possible hook names include: 960 * 961 * - `rest_prepare_category` 962 * - `rest_prepare_post_tag` 963 * 964 * Allows modification of the term data right before it is returned. 965 * 966 * @since 4.7.0 967 * 968 * @param WP_REST_Response $response The response object. 969 * @param WP_Term $item The original term object. 970 * @param WP_REST_Request $request Request used to generate the response. 971 */ 972 return apply_filters( "rest_prepare_{$this->taxonomy}", $response, $item, $request ); 973 } 974 975 /** 976 * Prepares links for the request. 977 * 978 * @since 4.7.0 979 * 980 * @param WP_Term $term Term object. 981 * @return array Links for the given term. 982 */ 983 protected function prepare_links( $term ) { 984 $links = array( 985 'self' => array( 986 'href' => rest_url( rest_get_route_for_term( $term ) ), 987 ), 988 'collection' => array( 989 'href' => rest_url( rest_get_route_for_taxonomy_items( $this->taxonomy ) ), 990 ), 991 'about' => array( 992 'href' => rest_url( sprintf( 'wp/v2/taxonomies/%s', $this->taxonomy ) ), 993 ), 994 ); 995 996 if ( $term->parent ) { 997 $parent_term = get_term( (int) $term->parent, $term->taxonomy ); 998 999 if ( $parent_term ) { 1000 $links['up'] = array( 1001 'href' => rest_url( rest_get_route_for_term( $parent_term ) ), 1002 'embeddable' => true, 1003 ); 1004 } 1005 } 1006 1007 $taxonomy_obj = get_taxonomy( $term->taxonomy ); 1008 1009 if ( empty( $taxonomy_obj->object_type ) ) { 1010 return $links; 1011 } 1012 1013 $post_type_links = array(); 1014 1015 foreach ( $taxonomy_obj->object_type as $type ) { 1016 $rest_path = rest_get_route_for_post_type_items( $type ); 1017 1018 if ( empty( $rest_path ) ) { 1019 continue; 1020 } 1021 1022 $post_type_links[] = array( 1023 'href' => add_query_arg( $this->rest_base, $term->term_id, rest_url( $rest_path ) ), 1024 ); 1025 } 1026 1027 if ( ! empty( $post_type_links ) ) { 1028 $links['https://api.w.org/post_type'] = $post_type_links; 1029 } 1030 1031 return $links; 1032 } 1033 1034 /** 1035 * Retrieves the term's schema, conforming to JSON Schema. 1036 * 1037 * @since 4.7.0 1038 * 1039 * @return array Item schema data. 1040 */ 1041 public function get_item_schema() { 1042 if ( $this->schema ) { 1043 return $this->add_additional_fields_schema( $this->schema ); 1044 } 1045 1046 $schema = array( 1047 '$schema' => 'http://json-schema.org/draft-04/schema#', 1048 'title' => 'post_tag' === $this->taxonomy ? 'tag' : $this->taxonomy, 1049 'type' => 'object', 1050 'properties' => array( 1051 'id' => array( 1052 'description' => __( 'Unique identifier for the term.' ), 1053 'type' => 'integer', 1054 'context' => array( 'view', 'embed', 'edit' ), 1055 'readonly' => true, 1056 ), 1057 'count' => array( 1058 'description' => __( 'Number of published posts for the term.' ), 1059 'type' => 'integer', 1060 'context' => array( 'view', 'edit' ), 1061 'readonly' => true, 1062 ), 1063 'description' => array( 1064 'description' => __( 'HTML description of the term.' ), 1065 'type' => 'string', 1066 'context' => array( 'view', 'edit' ), 1067 ), 1068 'link' => array( 1069 'description' => __( 'URL of the term.' ), 1070 'type' => 'string', 1071 'format' => 'uri', 1072 'context' => array( 'view', 'embed', 'edit' ), 1073 'readonly' => true, 1074 ), 1075 'name' => array( 1076 'description' => __( 'HTML title for the term.' ), 1077 'type' => 'string', 1078 'context' => array( 'view', 'embed', 'edit' ), 1079 'arg_options' => array( 1080 'sanitize_callback' => 'sanitize_text_field', 1081 ), 1082 'required' => true, 1083 ), 1084 'slug' => array( 1085 'description' => __( 'An alphanumeric identifier for the term unique to its type.' ), 1086 'type' => 'string', 1087 'context' => array( 'view', 'embed', 'edit' ), 1088 'arg_options' => array( 1089 'sanitize_callback' => array( $this, 'sanitize_slug' ), 1090 ), 1091 ), 1092 'taxonomy' => array( 1093 'description' => __( 'Type attribution for the term.' ), 1094 'type' => 'string', 1095 'enum' => array( $this->taxonomy ), 1096 'context' => array( 'view', 'embed', 'edit' ), 1097 'readonly' => true, 1098 ), 1099 ), 1100 ); 1101 1102 $taxonomy = get_taxonomy( $this->taxonomy ); 1103 1104 if ( $taxonomy->hierarchical ) { 1105 $schema['properties']['parent'] = array( 1106 'description' => __( 'The parent term ID.' ), 1107 'type' => 'integer', 1108 'context' => array( 'view', 'edit' ), 1109 ); 1110 } 1111 1112 $schema['properties']['meta'] = $this->meta->get_field_schema(); 1113 1114 $this->schema = $schema; 1115 1116 return $this->add_additional_fields_schema( $this->schema ); 1117 } 1118 1119 /** 1120 * Retrieves the query params for collections. 1121 * 1122 * @since 4.7.0 1123 * 1124 * @return array Collection parameters. 1125 */ 1126 public function get_collection_params() { 1127 $query_params = parent::get_collection_params(); 1128 $taxonomy = get_taxonomy( $this->taxonomy ); 1129 1130 $query_params['context']['default'] = 'view'; 1131 1132 $query_params['exclude'] = array( 1133 'description' => __( 'Ensure result set excludes specific IDs.' ), 1134 'type' => 'array', 1135 'items' => array( 1136 'type' => 'integer', 1137 ), 1138 'default' => array(), 1139 ); 1140 1141 $query_params['include'] = array( 1142 'description' => __( 'Limit result set to specific IDs.' ), 1143 'type' => 'array', 1144 'items' => array( 1145 'type' => 'integer', 1146 ), 1147 'default' => array(), 1148 ); 1149 1150 if ( ! $taxonomy->hierarchical ) { 1151 $query_params['offset'] = array( 1152 'description' => __( 'Offset the result set by a specific number of items.' ), 1153 'type' => 'integer', 1154 ); 1155 } 1156 1157 $query_params['order'] = array( 1158 'description' => __( 'Order sort attribute ascending or descending.' ), 1159 'type' => 'string', 1160 'default' => 'asc', 1161 'enum' => array( 1162 'asc', 1163 'desc', 1164 ), 1165 ); 1166 1167 $query_params['orderby'] = array( 1168 'description' => __( 'Sort collection by term attribute.' ), 1169 'type' => 'string', 1170 'default' => 'name', 1171 'enum' => array( 1172 'id', 1173 'include', 1174 'name', 1175 'slug', 1176 'include_slugs', 1177 'term_group', 1178 'description', 1179 'count', 1180 ), 1181 ); 1182 1183 $query_params['hide_empty'] = array( 1184 'description' => __( 'Whether to hide terms not assigned to any posts.' ), 1185 'type' => 'boolean', 1186 'default' => false, 1187 ); 1188 1189 if ( $taxonomy->hierarchical ) { 1190 $query_params['parent'] = array( 1191 'description' => __( 'Limit result set to terms assigned to a specific parent.' ), 1192 'type' => 'integer', 1193 ); 1194 } 1195 1196 $query_params['post'] = array( 1197 'description' => __( 'Limit result set to terms assigned to a specific post.' ), 1198 'type' => 'integer', 1199 'default' => null, 1200 ); 1201 1202 $query_params['slug'] = array( 1203 'description' => __( 'Limit result set to terms with one or more specific slugs.' ), 1204 'type' => 'array', 1205 'items' => array( 1206 'type' => 'string', 1207 ), 1208 ); 1209 1210 /** 1211 * Filters collection parameters for the terms controller. 1212 * 1213 * The dynamic part of the filter `$this->taxonomy` refers to the taxonomy 1214 * slug for the controller. 1215 * 1216 * This filter registers the collection parameter, but does not map the 1217 * collection parameter to an internal WP_Term_Query parameter. Use the 1218 * `rest_{$this->taxonomy}_query` filter to set WP_Term_Query parameters. 1219 * 1220 * @since 4.7.0 1221 * 1222 * @param array $query_params JSON Schema-formatted collection parameters. 1223 * @param WP_Taxonomy $taxonomy Taxonomy object. 1224 */ 1225 return apply_filters( "rest_{$this->taxonomy}_collection_params", $query_params, $taxonomy ); 1226 } 1227 1228 /** 1229 * Checks that the taxonomy is valid. 1230 * 1231 * @since 4.7.0 1232 * 1233 * @param string $taxonomy Taxonomy to check. 1234 * @return bool Whether the taxonomy is allowed for REST management. 1235 */ 1236 protected function check_is_taxonomy_allowed( $taxonomy ) { 1237 $taxonomy_obj = get_taxonomy( $taxonomy ); 1238 if ( $taxonomy_obj && ! empty( $taxonomy_obj->show_in_rest ) ) { 1239 return true; 1240 } 1241 return false; 1242 } 1243 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Thu Apr 10 08:20:01 2025 | Cross-referenced by PHPXref |