wp/wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
   198 
   198 
   199 	/**
   199 	/**
   200 	 * Prepares a taxonomy object for serialization.
   200 	 * Prepares a taxonomy object for serialization.
   201 	 *
   201 	 *
   202 	 * @since 4.7.0
   202 	 * @since 4.7.0
   203 	 *
   203 	 * @since 5.9.0 Renamed `$taxonomy` to `$item` to match parent class for PHP 8 named parameter support.
   204 	 * @param WP_Taxonomy     $taxonomy Taxonomy data.
   204 	 *
   205 	 * @param WP_REST_Request $request  Full details about the request.
   205 	 * @param WP_Taxonomy     $item    Taxonomy data.
       
   206 	 * @param WP_REST_Request $request Full details about the request.
   206 	 * @return WP_REST_Response Response object.
   207 	 * @return WP_REST_Response Response object.
   207 	 */
   208 	 */
   208 	public function prepare_item_for_response( $taxonomy, $request ) {
   209 	public function prepare_item_for_response( $item, $request ) {
   209 		$base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
   210 		// Restores the more descriptive, specific name for use within this method.
       
   211 		$taxonomy = $item;
       
   212 		$base     = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
   210 
   213 
   211 		$fields = $this->get_fields_for_response( $request );
   214 		$fields = $this->get_fields_for_response( $request );
   212 		$data   = array();
   215 		$data   = array();
   213 
   216 
   214 		if ( in_array( 'name', $fields, true ) ) {
   217 		if ( in_array( 'name', $fields, true ) ) {
   243 			$data['hierarchical'] = $taxonomy->hierarchical;
   246 			$data['hierarchical'] = $taxonomy->hierarchical;
   244 		}
   247 		}
   245 
   248 
   246 		if ( in_array( 'rest_base', $fields, true ) ) {
   249 		if ( in_array( 'rest_base', $fields, true ) ) {
   247 			$data['rest_base'] = $base;
   250 			$data['rest_base'] = $base;
       
   251 		}
       
   252 
       
   253 		if ( in_array( 'rest_namespace', $fields, true ) ) {
       
   254 			$data['rest_namespace'] = $taxonomy->rest_namespace;
   248 		}
   255 		}
   249 
   256 
   250 		if ( in_array( 'visibility', $fields, true ) ) {
   257 		if ( in_array( 'visibility', $fields, true ) ) {
   251 			$data['visibility'] = array(
   258 			$data['visibility'] = array(
   252 				'public'             => (bool) $taxonomy->public,
   259 				'public'             => (bool) $taxonomy->public,
   269 			array(
   276 			array(
   270 				'collection'              => array(
   277 				'collection'              => array(
   271 					'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
   278 					'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
   272 				),
   279 				),
   273 				'https://api.w.org/items' => array(
   280 				'https://api.w.org/items' => array(
   274 					'href' => rest_url( sprintf( 'wp/v2/%s', $base ) ),
   281 					'href' => rest_url( rest_get_route_for_taxonomy_items( $taxonomy->name ) ),
   275 				),
   282 				),
   276 			)
   283 			)
   277 		);
   284 		);
   278 
   285 
   279 		/**
   286 		/**
   292 
   299 
   293 	/**
   300 	/**
   294 	 * Retrieves the taxonomy's schema, conforming to JSON Schema.
   301 	 * Retrieves the taxonomy's schema, conforming to JSON Schema.
   295 	 *
   302 	 *
   296 	 * @since 4.7.0
   303 	 * @since 4.7.0
       
   304 	 * @since 5.0.0 The `visibility` property was added.
       
   305 	 * @since 5.9.0 The `rest_namespace` property was added.
   297 	 *
   306 	 *
   298 	 * @return array Item schema data.
   307 	 * @return array Item schema data.
   299 	 */
   308 	 */
   300 	public function get_item_schema() {
   309 	public function get_item_schema() {
   301 		if ( $this->schema ) {
   310 		if ( $this->schema ) {
   305 		$schema = array(
   314 		$schema = array(
   306 			'$schema'    => 'http://json-schema.org/draft-04/schema#',
   315 			'$schema'    => 'http://json-schema.org/draft-04/schema#',
   307 			'title'      => 'taxonomy',
   316 			'title'      => 'taxonomy',
   308 			'type'       => 'object',
   317 			'type'       => 'object',
   309 			'properties' => array(
   318 			'properties' => array(
   310 				'capabilities' => array(
   319 				'capabilities'   => array(
   311 					'description' => __( 'All capabilities used by the taxonomy.' ),
   320 					'description' => __( 'All capabilities used by the taxonomy.' ),
   312 					'type'        => 'object',
   321 					'type'        => 'object',
   313 					'context'     => array( 'edit' ),
   322 					'context'     => array( 'edit' ),
   314 					'readonly'    => true,
   323 					'readonly'    => true,
   315 				),
   324 				),
   316 				'description'  => array(
   325 				'description'    => array(
   317 					'description' => __( 'A human-readable description of the taxonomy.' ),
   326 					'description' => __( 'A human-readable description of the taxonomy.' ),
   318 					'type'        => 'string',
   327 					'type'        => 'string',
   319 					'context'     => array( 'view', 'edit' ),
   328 					'context'     => array( 'view', 'edit' ),
   320 					'readonly'    => true,
   329 					'readonly'    => true,
   321 				),
   330 				),
   322 				'hierarchical' => array(
   331 				'hierarchical'   => array(
   323 					'description' => __( 'Whether or not the taxonomy should have children.' ),
   332 					'description' => __( 'Whether or not the taxonomy should have children.' ),
   324 					'type'        => 'boolean',
   333 					'type'        => 'boolean',
   325 					'context'     => array( 'view', 'edit' ),
   334 					'context'     => array( 'view', 'edit' ),
   326 					'readonly'    => true,
   335 					'readonly'    => true,
   327 				),
   336 				),
   328 				'labels'       => array(
   337 				'labels'         => array(
   329 					'description' => __( 'Human-readable labels for the taxonomy for various contexts.' ),
   338 					'description' => __( 'Human-readable labels for the taxonomy for various contexts.' ),
   330 					'type'        => 'object',
   339 					'type'        => 'object',
   331 					'context'     => array( 'edit' ),
   340 					'context'     => array( 'edit' ),
   332 					'readonly'    => true,
   341 					'readonly'    => true,
   333 				),
   342 				),
   334 				'name'         => array(
   343 				'name'           => array(
   335 					'description' => __( 'The title for the taxonomy.' ),
   344 					'description' => __( 'The title for the taxonomy.' ),
   336 					'type'        => 'string',
   345 					'type'        => 'string',
   337 					'context'     => array( 'view', 'edit', 'embed' ),
   346 					'context'     => array( 'view', 'edit', 'embed' ),
   338 					'readonly'    => true,
   347 					'readonly'    => true,
   339 				),
   348 				),
   340 				'slug'         => array(
   349 				'slug'           => array(
   341 					'description' => __( 'An alphanumeric identifier for the taxonomy.' ),
   350 					'description' => __( 'An alphanumeric identifier for the taxonomy.' ),
   342 					'type'        => 'string',
   351 					'type'        => 'string',
   343 					'context'     => array( 'view', 'edit', 'embed' ),
   352 					'context'     => array( 'view', 'edit', 'embed' ),
   344 					'readonly'    => true,
   353 					'readonly'    => true,
   345 				),
   354 				),
   346 				'show_cloud'   => array(
   355 				'show_cloud'     => array(
   347 					'description' => __( 'Whether or not the term cloud should be displayed.' ),
   356 					'description' => __( 'Whether or not the term cloud should be displayed.' ),
   348 					'type'        => 'boolean',
   357 					'type'        => 'boolean',
   349 					'context'     => array( 'edit' ),
   358 					'context'     => array( 'edit' ),
   350 					'readonly'    => true,
   359 					'readonly'    => true,
   351 				),
   360 				),
   352 				'types'        => array(
   361 				'types'          => array(
   353 					'description' => __( 'Types associated with the taxonomy.' ),
   362 					'description' => __( 'Types associated with the taxonomy.' ),
   354 					'type'        => 'array',
   363 					'type'        => 'array',
   355 					'items'       => array(
   364 					'items'       => array(
   356 						'type' => 'string',
   365 						'type' => 'string',
   357 					),
   366 					),
   358 					'context'     => array( 'view', 'edit' ),
   367 					'context'     => array( 'view', 'edit' ),
   359 					'readonly'    => true,
   368 					'readonly'    => true,
   360 				),
   369 				),
   361 				'rest_base'    => array(
   370 				'rest_base'      => array(
   362 					'description' => __( 'REST base route for the taxonomy.' ),
   371 					'description' => __( 'REST base route for the taxonomy.' ),
   363 					'type'        => 'string',
   372 					'type'        => 'string',
   364 					'context'     => array( 'view', 'edit', 'embed' ),
   373 					'context'     => array( 'view', 'edit', 'embed' ),
   365 					'readonly'    => true,
   374 					'readonly'    => true,
   366 				),
   375 				),
   367 				'visibility'   => array(
   376 				'rest_namespace' => array(
       
   377 					'description' => __( 'REST namespace route for the taxonomy.' ),
       
   378 					'type'        => 'string',
       
   379 					'context'     => array( 'view', 'edit', 'embed' ),
       
   380 					'readonly'    => true,
       
   381 				),
       
   382 				'visibility'     => array(
   368 					'description' => __( 'The visibility settings for the taxonomy.' ),
   383 					'description' => __( 'The visibility settings for the taxonomy.' ),
   369 					'type'        => 'object',
   384 					'type'        => 'object',
   370 					'context'     => array( 'edit' ),
   385 					'context'     => array( 'edit' ),
   371 					'readonly'    => true,
   386 					'readonly'    => true,
   372 					'properties'  => array(
   387 					'properties'  => array(