wp/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php
changeset 18 be944660c56a
parent 16 a86126ab1dd4
child 19 3d72ae0968f4
equal deleted inserted replaced
17:34716fd837a4 18:be944660c56a
    63 
    63 
    64 		$this->meta = new WP_REST_Term_Meta_Fields( $taxonomy );
    64 		$this->meta = new WP_REST_Term_Meta_Fields( $taxonomy );
    65 	}
    65 	}
    66 
    66 
    67 	/**
    67 	/**
    68 	 * Registers the routes for the objects of the controller.
    68 	 * Registers the routes for terms.
    69 	 *
    69 	 *
    70 	 * @since 4.7.0
    70 	 * @since 4.7.0
    71 	 *
    71 	 *
    72 	 * @see register_rest_route()
    72 	 * @see register_rest_route()
    73 	 */
    73 	 */
   138 	 * Checks if a request has access to read terms in the specified taxonomy.
   138 	 * Checks if a request has access to read terms in the specified taxonomy.
   139 	 *
   139 	 *
   140 	 * @since 4.7.0
   140 	 * @since 4.7.0
   141 	 *
   141 	 *
   142 	 * @param WP_REST_Request $request Full details about the request.
   142 	 * @param WP_REST_Request $request Full details about the request.
   143 	 * @return bool|WP_Error True if the request has read access, otherwise false or WP_Error object.
   143 	 * @return true|WP_Error True if the request has read access, otherwise false or WP_Error object.
   144 	 */
   144 	 */
   145 	public function get_items_permissions_check( $request ) {
   145 	public function get_items_permissions_check( $request ) {
   146 		$tax_obj = get_taxonomy( $this->taxonomy );
   146 		$tax_obj = get_taxonomy( $this->taxonomy );
   147 
   147 
   148 		if ( ! $tax_obj || ! $this->check_is_taxonomy_allowed( $this->taxonomy ) ) {
   148 		if ( ! $tax_obj || ! $this->check_is_taxonomy_allowed( $this->taxonomy ) ) {
   231 				}
   231 				}
   232 			}
   232 			}
   233 		}
   233 		}
   234 
   234 
   235 		/**
   235 		/**
   236 		 * Filters the query arguments before passing them to get_terms().
   236 		 * Filters get_terms() arguments when querying terms via the REST API.
   237 		 *
   237 		 *
   238 		 * The dynamic portion of the hook name, `$this->taxonomy`, refers to the taxonomy slug.
   238 		 * The dynamic portion of the hook name, `$this->taxonomy`, refers to the taxonomy slug.
       
   239 		 *
       
   240 		 * Possible hook names include:
       
   241 		 *
       
   242 		 *  - `rest_category_query`
       
   243 		 *  - `rest_post_tag_query`
   239 		 *
   244 		 *
   240 		 * Enables adding extra arguments or setting defaults for a terms
   245 		 * Enables adding extra arguments or setting defaults for a terms
   241 		 * collection request.
   246 		 * collection request.
   242 		 *
   247 		 *
   243 		 * @since 4.7.0
   248 		 * @since 4.7.0
   244 		 *
   249 		 *
   245 		 * @link https://developer.wordpress.org/reference/functions/get_terms/
   250 		 * @link https://developer.wordpress.org/reference/functions/get_terms/
   246 		 *
   251 		 *
   247 		 * @param array           $prepared_args Array of arguments to be
   252 		 * @param array           $prepared_args Array of arguments for get_terms().
   248 		 *                                       passed to get_terms().
   253 		 * @param WP_REST_Request $request       The REST API request.
   249 		 * @param WP_REST_Request $request       The current request.
       
   250 		 */
   254 		 */
   251 		$prepared_args = apply_filters( "rest_{$this->taxonomy}_query", $prepared_args, $request );
   255 		$prepared_args = apply_filters( "rest_{$this->taxonomy}_query", $prepared_args, $request );
   252 
   256 
   253 		if ( ! empty( $prepared_args['post'] ) ) {
   257 		if ( ! empty( $prepared_args['post'] ) ) {
   254 			$query_result = wp_get_object_terms( $prepared_args['post'], $this->taxonomy, $prepared_args );
   258 			$query_result = wp_get_object_terms( $prepared_args['post'], $this->taxonomy, $prepared_args );
   261 
   265 
   262 		$count_args = $prepared_args;
   266 		$count_args = $prepared_args;
   263 
   267 
   264 		unset( $count_args['number'], $count_args['offset'] );
   268 		unset( $count_args['number'], $count_args['offset'] );
   265 
   269 
   266 		$total_terms = wp_count_terms( $this->taxonomy, $count_args );
   270 		$total_terms = wp_count_terms( $count_args );
   267 
   271 
   268 		// wp_count_terms() can return a falsey value when the term has no children.
   272 		// wp_count_terms() can return a falsey value when the term has no children.
   269 		if ( ! $total_terms ) {
   273 		if ( ! $total_terms ) {
   270 			$total_terms = 0;
   274 			$total_terms = 0;
   271 		}
   275 		}
   345 	 * Checks if a request has access to read or edit the specified term.
   349 	 * Checks if a request has access to read or edit the specified term.
   346 	 *
   350 	 *
   347 	 * @since 4.7.0
   351 	 * @since 4.7.0
   348 	 *
   352 	 *
   349 	 * @param WP_REST_Request $request Full details about the request.
   353 	 * @param WP_REST_Request $request Full details about the request.
   350 	 * @return bool|WP_Error True if the request has read access for the item, otherwise false or WP_Error object.
   354 	 * @return true|WP_Error True if the request has read access for the item, otherwise false or WP_Error object.
   351 	 */
   355 	 */
   352 	public function get_item_permissions_check( $request ) {
   356 	public function get_item_permissions_check( $request ) {
   353 		$term = $this->get_term( $request['id'] );
   357 		$term = $this->get_term( $request['id'] );
   354 
   358 
   355 		if ( is_wp_error( $term ) ) {
   359 		if ( is_wp_error( $term ) ) {
   390 	 * Checks if a request has access to create a term.
   394 	 * Checks if a request has access to create a term.
   391 	 *
   395 	 *
   392 	 * @since 4.7.0
   396 	 * @since 4.7.0
   393 	 *
   397 	 *
   394 	 * @param WP_REST_Request $request Full details about the request.
   398 	 * @param WP_REST_Request $request Full details about the request.
   395 	 * @return bool|WP_Error True if the request has access to create items, false or WP_Error object otherwise.
   399 	 * @return true|WP_Error True if the request has access to create items, false or WP_Error object otherwise.
   396 	 */
   400 	 */
   397 	public function create_item_permissions_check( $request ) {
   401 	public function create_item_permissions_check( $request ) {
   398 
   402 
   399 		if ( ! $this->check_is_taxonomy_allowed( $this->taxonomy ) ) {
   403 		if ( ! $this->check_is_taxonomy_allowed( $this->taxonomy ) ) {
   400 			return false;
   404 			return false;
   473 		/**
   477 		/**
   474 		 * Fires after a single term is created or updated via the REST API.
   478 		 * Fires after a single term is created or updated via the REST API.
   475 		 *
   479 		 *
   476 		 * The dynamic portion of the hook name, `$this->taxonomy`, refers to the taxonomy slug.
   480 		 * The dynamic portion of the hook name, `$this->taxonomy`, refers to the taxonomy slug.
   477 		 *
   481 		 *
       
   482 		 * Possible hook names include:
       
   483 		 *
       
   484 		 *  - `rest_insert_category`
       
   485 		 *  - `rest_insert_post_tag`
       
   486 		 *
   478 		 * @since 4.7.0
   487 		 * @since 4.7.0
   479 		 *
   488 		 *
   480 		 * @param WP_Term         $term     Inserted or updated term object.
   489 		 * @param WP_Term         $term     Inserted or updated term object.
   481 		 * @param WP_REST_Request $request  Request object.
   490 		 * @param WP_REST_Request $request  Request object.
   482 		 * @param bool            $creating True when creating a term, false when updating.
   491 		 * @param bool            $creating True when creating a term, false when updating.
   503 		/**
   512 		/**
   504 		 * Fires after a single term is completely created or updated via the REST API.
   513 		 * Fires after a single term is completely created or updated via the REST API.
   505 		 *
   514 		 *
   506 		 * The dynamic portion of the hook name, `$this->taxonomy`, refers to the taxonomy slug.
   515 		 * The dynamic portion of the hook name, `$this->taxonomy`, refers to the taxonomy slug.
   507 		 *
   516 		 *
       
   517 		 * Possible hook names include:
       
   518 		 *
       
   519 		 *  - `rest_after_insert_category`
       
   520 		 *  - `rest_after_insert_post_tag`
       
   521 		 *
   508 		 * @since 5.0.0
   522 		 * @since 5.0.0
   509 		 *
   523 		 *
   510 		 * @param WP_Term         $term     Inserted or updated term object.
   524 		 * @param WP_Term         $term     Inserted or updated term object.
   511 		 * @param WP_REST_Request $request  Request object.
   525 		 * @param WP_REST_Request $request  Request object.
   512 		 * @param bool            $creating True when creating a term, false when updating.
   526 		 * @param bool            $creating True when creating a term, false when updating.
   526 	 * Checks if a request has access to update the specified term.
   540 	 * Checks if a request has access to update the specified term.
   527 	 *
   541 	 *
   528 	 * @since 4.7.0
   542 	 * @since 4.7.0
   529 	 *
   543 	 *
   530 	 * @param WP_REST_Request $request Full details about the request.
   544 	 * @param WP_REST_Request $request Full details about the request.
   531 	 * @return bool|WP_Error True if the request has access to update the item, false or WP_Error object otherwise.
   545 	 * @return true|WP_Error True if the request has access to update the item, false or WP_Error object otherwise.
   532 	 */
   546 	 */
   533 	public function update_item_permissions_check( $request ) {
   547 	public function update_item_permissions_check( $request ) {
   534 		$term = $this->get_term( $request['id'] );
   548 		$term = $this->get_term( $request['id'] );
   535 
   549 
   536 		if ( is_wp_error( $term ) ) {
   550 		if ( is_wp_error( $term ) ) {
   627 	 * Checks if a request has access to delete the specified term.
   641 	 * Checks if a request has access to delete the specified term.
   628 	 *
   642 	 *
   629 	 * @since 4.7.0
   643 	 * @since 4.7.0
   630 	 *
   644 	 *
   631 	 * @param WP_REST_Request $request Full details about the request.
   645 	 * @param WP_REST_Request $request Full details about the request.
   632 	 * @return bool|WP_Error True if the request has access to delete the item, otherwise false or WP_Error object.
   646 	 * @return true|WP_Error True if the request has access to delete the item, otherwise false or WP_Error object.
   633 	 */
   647 	 */
   634 	public function delete_item_permissions_check( $request ) {
   648 	public function delete_item_permissions_check( $request ) {
   635 		$term = $this->get_term( $request['id'] );
   649 		$term = $this->get_term( $request['id'] );
   636 
   650 
   637 		if ( is_wp_error( $term ) ) {
   651 		if ( is_wp_error( $term ) ) {
   700 		/**
   714 		/**
   701 		 * Fires after a single term is deleted via the REST API.
   715 		 * Fires after a single term is deleted via the REST API.
   702 		 *
   716 		 *
   703 		 * The dynamic portion of the hook name, `$this->taxonomy`, refers to the taxonomy slug.
   717 		 * The dynamic portion of the hook name, `$this->taxonomy`, refers to the taxonomy slug.
   704 		 *
   718 		 *
       
   719 		 * Possible hook names include:
       
   720 		 *
       
   721 		 *  - `rest_delete_category`
       
   722 		 *  - `rest_delete_post_tag`
       
   723 		 *
   705 		 * @since 4.7.0
   724 		 * @since 4.7.0
   706 		 *
   725 		 *
   707 		 * @param WP_Term          $term     The deleted term.
   726 		 * @param WP_Term          $term     The deleted term.
   708 		 * @param WP_REST_Response $response The response data.
   727 		 * @param WP_REST_Response $response The response data.
   709 		 * @param WP_REST_Request  $request  The request sent to the API.
   728 		 * @param WP_REST_Request  $request  The request sent to the API.
   759 		/**
   778 		/**
   760 		 * Filters term data before inserting term via the REST API.
   779 		 * Filters term data before inserting term via the REST API.
   761 		 *
   780 		 *
   762 		 * The dynamic portion of the hook name, `$this->taxonomy`, refers to the taxonomy slug.
   781 		 * The dynamic portion of the hook name, `$this->taxonomy`, refers to the taxonomy slug.
   763 		 *
   782 		 *
       
   783 		 * Possible hook names include:
       
   784 		 *
       
   785 		 *  - `rest_pre_insert_category`
       
   786 		 *  - `rest_pre_insert_post_tag`
       
   787 		 *
   764 		 * @since 4.7.0
   788 		 * @since 4.7.0
   765 		 *
   789 		 *
   766 		 * @param object          $prepared_term Term object.
   790 		 * @param object          $prepared_term Term object.
   767 		 * @param WP_REST_Request $request       Request object.
   791 		 * @param WP_REST_Request $request       Request object.
   768 		 */
   792 		 */
   826 		$response = rest_ensure_response( $data );
   850 		$response = rest_ensure_response( $data );
   827 
   851 
   828 		$response->add_links( $this->prepare_links( $item ) );
   852 		$response->add_links( $this->prepare_links( $item ) );
   829 
   853 
   830 		/**
   854 		/**
   831 		 * Filters a term item returned from the API.
   855 		 * Filters the term data for a REST API response.
   832 		 *
   856 		 *
   833 		 * The dynamic portion of the hook name, `$this->taxonomy`, refers to the taxonomy slug.
   857 		 * The dynamic portion of the hook name, `$this->taxonomy`, refers to the taxonomy slug.
       
   858 		 *
       
   859 		 * Possible hook names include:
       
   860 		 *
       
   861 		 *  - `rest_prepare_category`
       
   862 		 *  - `rest_prepare_post_tag`
   834 		 *
   863 		 *
   835 		 * Allows modification of the term data right before it is returned.
   864 		 * Allows modification of the term data right before it is returned.
   836 		 *
   865 		 *
   837 		 * @since 4.7.0
   866 		 * @since 4.7.0
   838 		 *
   867 		 *
   963 					),
   992 					),
   964 				),
   993 				),
   965 				'taxonomy'    => array(
   994 				'taxonomy'    => array(
   966 					'description' => __( 'Type attribution for the term.' ),
   995 					'description' => __( 'Type attribution for the term.' ),
   967 					'type'        => 'string',
   996 					'type'        => 'string',
   968 					'enum'        => array_keys( get_taxonomies() ),
   997 					'enum'        => array( $this->taxonomy ),
   969 					'context'     => array( 'view', 'embed', 'edit' ),
   998 					'context'     => array( 'view', 'embed', 'edit' ),
   970 					'readonly'    => true,
   999 					'readonly'    => true,
   971 				),
  1000 				),
   972 			),
  1001 			),
   973 		);
  1002 		);
  1079 				'type' => 'string',
  1108 				'type' => 'string',
  1080 			),
  1109 			),
  1081 		);
  1110 		);
  1082 
  1111 
  1083 		/**
  1112 		/**
  1084 		 * Filter collection parameters for the terms controller.
  1113 		 * Filters collection parameters for the terms controller.
  1085 		 *
  1114 		 *
  1086 		 * The dynamic part of the filter `$this->taxonomy` refers to the taxonomy
  1115 		 * The dynamic part of the filter `$this->taxonomy` refers to the taxonomy
  1087 		 * slug for the controller.
  1116 		 * slug for the controller.
  1088 		 *
  1117 		 *
  1089 		 * This filter registers the collection parameter, but does not map the
  1118 		 * This filter registers the collection parameter, but does not map the