wp/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php
changeset 22 8c2e4d02f4ef
parent 21 48c4eec2b7e6
equal deleted inserted replaced
21:48c4eec2b7e6 22:8c2e4d02f4ef
   162 		// Grant access if the post is publicly viewable.
   162 		// Grant access if the post is publicly viewable.
   163 		if ( is_post_publicly_viewable( $post ) ) {
   163 		if ( is_post_publicly_viewable( $post ) ) {
   164 			return true;
   164 			return true;
   165 		}
   165 		}
   166 
   166 
   167 		// Otherwise grant access if the post is readable by the logged in user.
   167 		// Otherwise grant access if the post is readable by the logged-in user.
   168 		if ( current_user_can( 'read_post', $post->ID ) ) {
   168 		if ( current_user_can( 'read_post', $post->ID ) ) {
   169 			return true;
   169 			return true;
   170 		}
   170 		}
   171 
   171 
   172 		// Otherwise, deny access.
   172 		// Otherwise, deny access.
   225 
   225 
   226 	/**
   226 	/**
   227 	 * Retrieves terms associated with a taxonomy.
   227 	 * Retrieves terms associated with a taxonomy.
   228 	 *
   228 	 *
   229 	 * @since 4.7.0
   229 	 * @since 4.7.0
       
   230 	 * @since 6.8.0 Respect default query arguments set for the taxonomy upon registration.
   230 	 *
   231 	 *
   231 	 * @param WP_REST_Request $request Full details about the request.
   232 	 * @param WP_REST_Request $request Full details about the request.
   232 	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
   233 	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
   233 	 */
   234 	 */
   234 	public function get_items( $request ) {
   235 	public function get_items( $request ) {
   293 					$prepared_args['parent'] = $request['parent'];
   294 					$prepared_args['parent'] = $request['parent'];
   294 				}
   295 				}
   295 			}
   296 			}
   296 		}
   297 		}
   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 
   298 		/**
   323 		/**
   299 		 * Filters get_terms() arguments when querying terms via the REST API.
   324 		 * Filters get_terms() arguments when querying terms via the REST API.
   300 		 *
   325 		 *
   301 		 * The dynamic portion of the hook name, `$this->taxonomy`, refers to the taxonomy slug.
   326 		 * The dynamic portion of the hook name, `$this->taxonomy`, refers to the taxonomy slug.
   302 		 *
   327 		 *
   335 		// wp_count_terms() can return a falsey value when the term has no children.
   360 		// wp_count_terms() can return a falsey value when the term has no children.
   336 		if ( ! $total_terms ) {
   361 		if ( ! $total_terms ) {
   337 			$total_terms = 0;
   362 			$total_terms = 0;
   338 		}
   363 		}
   339 
   364 
   340 		$response = array();
   365 		if ( ! $is_head_request ) {
   341 
   366 			$response = array();
   342 		foreach ( $query_result as $term ) {
   367 			foreach ( $query_result as $term ) {
   343 			$data       = $this->prepare_item_for_response( $term, $request );
   368 				$data       = $this->prepare_item_for_response( $term, $request );
   344 			$response[] = $this->prepare_response_for_collection( $data );
   369 				$response[] = $this->prepare_response_for_collection( $data );
   345 		}
   370 			}
   346 
   371 		}
   347 		$response = rest_ensure_response( $response );
   372 
       
   373 		$response = $is_head_request ? new WP_REST_Response( array() ) : rest_ensure_response( $response );
   348 
   374 
   349 		// Store pagination values for headers.
   375 		// Store pagination values for headers.
   350 		$per_page = (int) $prepared_args['number'];
   376 		$per_page = (int) $prepared_args['number'];
   351 		$page     = (int) ceil( ( ( (int) $prepared_args['offset'] ) / $per_page ) + 1 );
   377 		$page     = (int) ceil( ( ( (int) $prepared_args['offset'] ) / $per_page ) + 1 );
   352 
   378 
   460 	 * Checks if a request has access to create a term.
   486 	 * Checks if a request has access to create a term.
   461 	 *
   487 	 *
   462 	 * @since 4.7.0
   488 	 * @since 4.7.0
   463 	 *
   489 	 *
   464 	 * @param WP_REST_Request $request Full details about the request.
   490 	 * @param WP_REST_Request $request Full details about the request.
   465 	 * @return true|WP_Error True if the request has access to create items, false or WP_Error object otherwise.
   491 	 * @return bool|WP_Error True if the request has access to create items, otherwise false or WP_Error object.
   466 	 */
   492 	 */
   467 	public function create_item_permissions_check( $request ) {
   493 	public function create_item_permissions_check( $request ) {
   468 
   494 
   469 		if ( ! $this->check_is_taxonomy_allowed( $this->taxonomy ) ) {
   495 		if ( ! $this->check_is_taxonomy_allowed( $this->taxonomy ) ) {
   470 			return false;
   496 			return false;
   867 	 * @param WP_Term         $item    Term object.
   893 	 * @param WP_Term         $item    Term object.
   868 	 * @param WP_REST_Request $request Request object.
   894 	 * @param WP_REST_Request $request Request object.
   869 	 * @return WP_REST_Response Response object.
   895 	 * @return WP_REST_Response Response object.
   870 	 */
   896 	 */
   871 	public function prepare_item_for_response( $item, $request ) {
   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 		}
   872 
   904 
   873 		$fields = $this->get_fields_for_response( $request );
   905 		$fields = $this->get_fields_for_response( $request );
   874 		$data   = array();
   906 		$data   = array();
   875 
   907 
   876 		if ( in_array( 'id', $fields, true ) ) {
   908 		if ( in_array( 'id', $fields, true ) ) {