wp/wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
child 22 8c2e4d02f4ef
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
   207 	 * @return WP_REST_Response Response object.
   207 	 * @return WP_REST_Response Response object.
   208 	 */
   208 	 */
   209 	public function prepare_item_for_response( $item, $request ) {
   209 	public function prepare_item_for_response( $item, $request ) {
   210 		// Restores the more descriptive, specific name for use within this method.
   210 		// Restores the more descriptive, specific name for use within this method.
   211 		$taxonomy = $item;
   211 		$taxonomy = $item;
   212 		$base     = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
   212 
       
   213 		$base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
   213 
   214 
   214 		$fields = $this->get_fields_for_response( $request );
   215 		$fields = $this->get_fields_for_response( $request );
   215 		$data   = array();
   216 		$data   = array();
   216 
   217 
   217 		if ( in_array( 'name', $fields, true ) ) {
   218 		if ( in_array( 'name', $fields, true ) ) {
   270 		$data    = $this->filter_response_by_context( $data, $context );
   271 		$data    = $this->filter_response_by_context( $data, $context );
   271 
   272 
   272 		// Wrap the data in a response object.
   273 		// Wrap the data in a response object.
   273 		$response = rest_ensure_response( $data );
   274 		$response = rest_ensure_response( $data );
   274 
   275 
   275 		$response->add_links(
   276 		if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
   276 			array(
   277 			$response->add_links( $this->prepare_links( $taxonomy ) );
   277 				'collection'              => array(
   278 		}
   278 					'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
       
   279 				),
       
   280 				'https://api.w.org/items' => array(
       
   281 					'href' => rest_url( rest_get_route_for_taxonomy_items( $taxonomy->name ) ),
       
   282 				),
       
   283 			)
       
   284 		);
       
   285 
   279 
   286 		/**
   280 		/**
   287 		 * Filters a taxonomy returned from the REST API.
   281 		 * Filters a taxonomy returned from the REST API.
   288 		 *
   282 		 *
   289 		 * Allows modification of the taxonomy data right before it is returned.
   283 		 * Allows modification of the taxonomy data right before it is returned.
   296 		 */
   290 		 */
   297 		return apply_filters( 'rest_prepare_taxonomy', $response, $taxonomy, $request );
   291 		return apply_filters( 'rest_prepare_taxonomy', $response, $taxonomy, $request );
   298 	}
   292 	}
   299 
   293 
   300 	/**
   294 	/**
       
   295 	 * Prepares links for the request.
       
   296 	 *
       
   297 	 * @since 6.1.0
       
   298 	 *
       
   299 	 * @param WP_Taxonomy $taxonomy The taxonomy.
       
   300 	 * @return array Links for the given taxonomy.
       
   301 	 */
       
   302 	protected function prepare_links( $taxonomy ) {
       
   303 		return array(
       
   304 			'collection'              => array(
       
   305 				'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
       
   306 			),
       
   307 			'https://api.w.org/items' => array(
       
   308 				'href' => rest_url( rest_get_route_for_taxonomy_items( $taxonomy->name ) ),
       
   309 			),
       
   310 		);
       
   311 	}
       
   312 
       
   313 	/**
   301 	 * Retrieves the taxonomy's schema, conforming to JSON Schema.
   314 	 * Retrieves the taxonomy's schema, conforming to JSON Schema.
   302 	 *
   315 	 *
   303 	 * @since 4.7.0
   316 	 * @since 4.7.0
   304 	 * @since 5.0.0 The `visibility` property was added.
   317 	 * @since 5.0.0 The `visibility` property was added.
   305 	 * @since 5.9.0 The `rest_namespace` property was added.
   318 	 * @since 5.9.0 The `rest_namespace` property was added.
   434 			'description' => __( 'Limit results to taxonomies associated with a specific post type.' ),
   447 			'description' => __( 'Limit results to taxonomies associated with a specific post type.' ),
   435 			'type'        => 'string',
   448 			'type'        => 'string',
   436 		);
   449 		);
   437 		return $new_params;
   450 		return $new_params;
   438 	}
   451 	}
   439 
       
   440 }
   452 }