wp/wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
    33 	 *
    33 	 *
    34 	 * @see register_rest_route()
    34 	 * @see register_rest_route()
    35 	 */
    35 	 */
    36 	public function register_routes() {
    36 	public function register_routes() {
    37 
    37 
    38 		register_rest_route( $this->namespace, '/' . $this->rest_base, array(
    38 		register_rest_route(
       
    39 			$this->namespace,
       
    40 			'/' . $this->rest_base,
    39 			array(
    41 			array(
    40 				'methods'         => WP_REST_Server::READABLE,
    42 				array(
    41 				'callback'        => array( $this, 'get_items' ),
    43 					'methods'             => WP_REST_Server::READABLE,
    42 				'permission_callback' => array( $this, 'get_items_permissions_check' ),
    44 					'callback'            => array( $this, 'get_items' ),
    43 				'args'            => $this->get_collection_params(),
    45 					'permission_callback' => array( $this, 'get_items_permissions_check' ),
    44 			),
    46 					'args'                => $this->get_collection_params(),
    45 			'schema' => array( $this, 'get_public_item_schema' ),
    47 				),
    46 		) );
    48 				'schema' => array( $this, 'get_public_item_schema' ),
    47 
    49 			)
    48 		register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<taxonomy>[\w-]+)', array(
    50 		);
    49 			'args' => array(
    51 
    50 				'taxonomy' => array(
    52 		register_rest_route(
    51 					'description'  => __( 'An alphanumeric identifier for the taxonomy.' ),
    53 			$this->namespace,
    52 					'type'         => 'string',
    54 			'/' . $this->rest_base . '/(?P<taxonomy>[\w-]+)',
    53 				),
       
    54 			),
       
    55 			array(
    55 			array(
    56 				'methods'         => WP_REST_Server::READABLE,
    56 				'args'   => array(
    57 				'callback'        => array( $this, 'get_item' ),
    57 					'taxonomy' => array(
    58 				'permission_callback' => array( $this, 'get_item_permissions_check' ),
    58 						'description' => __( 'An alphanumeric identifier for the taxonomy.' ),
    59 				'args'            => array(
    59 						'type'        => 'string',
    60 					'context'     => $this->get_context_param( array( 'default' => 'view' ) ),
    60 					),
    61 				),
    61 				),
    62 			),
    62 				array(
    63 			'schema' => array( $this, 'get_public_item_schema' ),
    63 					'methods'             => WP_REST_Server::READABLE,
    64 		) );
    64 					'callback'            => array( $this, 'get_item' ),
       
    65 					'permission_callback' => array( $this, 'get_item_permissions_check' ),
       
    66 					'args'                => array(
       
    67 						'context' => $this->get_context_param( array( 'default' => 'view' ) ),
       
    68 					),
       
    69 				),
       
    70 				'schema' => array( $this, 'get_public_item_schema' ),
       
    71 			)
       
    72 		);
    65 	}
    73 	}
    66 
    74 
    67 	/**
    75 	/**
    68 	 * Checks whether a given request has permission to read taxonomies.
    76 	 * Checks whether a given request has permission to read taxonomies.
    69 	 *
    77 	 *
   110 		$data = array();
   118 		$data = array();
   111 		foreach ( $taxonomies as $tax_type => $value ) {
   119 		foreach ( $taxonomies as $tax_type => $value ) {
   112 			if ( empty( $value->show_in_rest ) || ( 'edit' === $request['context'] && ! current_user_can( $value->cap->assign_terms ) ) ) {
   120 			if ( empty( $value->show_in_rest ) || ( 'edit' === $request['context'] && ! current_user_can( $value->cap->assign_terms ) ) ) {
   113 				continue;
   121 				continue;
   114 			}
   122 			}
   115 			$tax = $this->prepare_item_for_response( $value, $request );
   123 			$tax               = $this->prepare_item_for_response( $value, $request );
   116 			$tax = $this->prepare_response_for_collection( $tax );
   124 			$tax               = $this->prepare_response_for_collection( $tax );
   117 			$data[ $tax_type ] = $tax;
   125 			$data[ $tax_type ] = $tax;
   118 		}
   126 		}
   119 
   127 
   120 		if ( empty( $data ) ) {
   128 		if ( empty( $data ) ) {
   121 			// Response should still be returned as a JSON object when it is empty.
   129 			// Response should still be returned as a JSON object when it is empty.
   227 				'show_ui'            => (bool) $taxonomy->show_ui,
   235 				'show_ui'            => (bool) $taxonomy->show_ui,
   228 			);
   236 			);
   229 		}
   237 		}
   230 
   238 
   231 		$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
   239 		$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
   232 		$data = $this->add_additional_fields_to_object( $data, $request );
   240 		$data    = $this->add_additional_fields_to_object( $data, $request );
   233 		$data = $this->filter_response_by_context( $data, $context );
   241 		$data    = $this->filter_response_by_context( $data, $context );
   234 
   242 
   235 		// Wrap the data in a response object.
   243 		// Wrap the data in a response object.
   236 		$response = rest_ensure_response( $data );
   244 		$response = rest_ensure_response( $data );
   237 
   245 
   238 		$response->add_links( array(
   246 		$response->add_links(
   239 			'collection'                => array(
   247 			array(
   240 				'href'                  => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
   248 				'collection'              => array(
   241 			),
   249 					'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
   242 			'https://api.w.org/items'   => array(
   250 				),
   243 				'href'                  => rest_url( sprintf( 'wp/v2/%s', $base ) ),
   251 				'https://api.w.org/items' => array(
   244 			),
   252 					'href' => rest_url( sprintf( 'wp/v2/%s', $base ) ),
   245 		) );
   253 				),
       
   254 			)
       
   255 		);
   246 
   256 
   247 		/**
   257 		/**
   248 		 * Filters a taxonomy returned from the REST API.
   258 		 * Filters a taxonomy returned from the REST API.
   249 		 *
   259 		 *
   250 		 * Allows modification of the taxonomy data right before it is returned.
   260 		 * Allows modification of the taxonomy data right before it is returned.
   265 	 *
   275 	 *
   266 	 * @return array Item schema data.
   276 	 * @return array Item schema data.
   267 	 */
   277 	 */
   268 	public function get_item_schema() {
   278 	public function get_item_schema() {
   269 		$schema = array(
   279 		$schema = array(
   270 			'$schema'              => 'http://json-schema.org/draft-04/schema#',
   280 			'$schema'    => 'http://json-schema.org/draft-04/schema#',
   271 			'title'                => 'taxonomy',
   281 			'title'      => 'taxonomy',
   272 			'type'                 => 'object',
   282 			'type'       => 'object',
   273 			'properties'           => array(
   283 			'properties' => array(
   274 				'capabilities'     => array(
   284 				'capabilities' => array(
   275 					'description'  => __( 'All capabilities used by the taxonomy.' ),
   285 					'description' => __( 'All capabilities used by the taxonomy.' ),
   276 					'type'         => 'object',
   286 					'type'        => 'object',
   277 					'context'      => array( 'edit' ),
   287 					'context'     => array( 'edit' ),
   278 					'readonly'     => true,
   288 					'readonly'    => true,
   279 				),
   289 				),
   280 				'description'      => array(
   290 				'description'  => array(
   281 					'description'  => __( 'A human-readable description of the taxonomy.' ),
   291 					'description' => __( 'A human-readable description of the taxonomy.' ),
   282 					'type'         => 'string',
   292 					'type'        => 'string',
   283 					'context'      => array( 'view', 'edit' ),
   293 					'context'     => array( 'view', 'edit' ),
   284 					'readonly'     => true,
   294 					'readonly'    => true,
   285 				),
   295 				),
   286 				'hierarchical'     => array(
   296 				'hierarchical' => array(
   287 					'description'  => __( 'Whether or not the taxonomy should have children.' ),
   297 					'description' => __( 'Whether or not the taxonomy should have children.' ),
   288 					'type'         => 'boolean',
   298 					'type'        => 'boolean',
   289 					'context'      => array( 'view', 'edit' ),
   299 					'context'     => array( 'view', 'edit' ),
   290 					'readonly'     => true,
   300 					'readonly'    => true,
   291 				),
   301 				),
   292 				'labels'           => array(
   302 				'labels'       => array(
   293 					'description'  => __( 'Human-readable labels for the taxonomy for various contexts.' ),
   303 					'description' => __( 'Human-readable labels for the taxonomy for various contexts.' ),
   294 					'type'         => 'object',
   304 					'type'        => 'object',
   295 					'context'      => array( 'edit' ),
   305 					'context'     => array( 'edit' ),
   296 					'readonly'     => true,
   306 					'readonly'    => true,
   297 				),
   307 				),
   298 				'name'             => array(
   308 				'name'         => array(
   299 					'description'  => __( 'The title for the taxonomy.' ),
   309 					'description' => __( 'The title for the taxonomy.' ),
   300 					'type'         => 'string',
   310 					'type'        => 'string',
   301 					'context'      => array( 'view', 'edit', 'embed' ),
   311 					'context'     => array( 'view', 'edit', 'embed' ),
   302 					'readonly'     => true,
   312 					'readonly'    => true,
   303 				),
   313 				),
   304 				'slug'             => array(
   314 				'slug'         => array(
   305 					'description'  => __( 'An alphanumeric identifier for the taxonomy.' ),
   315 					'description' => __( 'An alphanumeric identifier for the taxonomy.' ),
   306 					'type'         => 'string',
   316 					'type'        => 'string',
   307 					'context'      => array( 'view', 'edit', 'embed' ),
   317 					'context'     => array( 'view', 'edit', 'embed' ),
   308 					'readonly'     => true,
   318 					'readonly'    => true,
   309 				),
   319 				),
   310 				'show_cloud'       => array(
   320 				'show_cloud'   => array(
   311 					'description'  => __( 'Whether or not the term cloud should be displayed.' ),
   321 					'description' => __( 'Whether or not the term cloud should be displayed.' ),
   312 					'type'         => 'boolean',
   322 					'type'        => 'boolean',
   313 					'context'      => array( 'edit' ),
   323 					'context'     => array( 'edit' ),
   314 					'readonly'     => true,
   324 					'readonly'    => true,
   315 				),
   325 				),
   316 				'types'            => array(
   326 				'types'        => array(
   317 					'description'  => __( 'Types associated with the taxonomy.' ),
   327 					'description' => __( 'Types associated with the taxonomy.' ),
   318 					'type'         => 'array',
   328 					'type'        => 'array',
   319 					'items'        => array(
   329 					'items'       => array(
   320 						'type' => 'string',
   330 						'type' => 'string',
   321 					),
   331 					),
   322 					'context'      => array( 'view', 'edit' ),
   332 					'context'     => array( 'view', 'edit' ),
   323 					'readonly'     => true,
   333 					'readonly'    => true,
   324 				),
   334 				),
   325 				'rest_base'            => array(
   335 				'rest_base'    => array(
   326 					'description'  => __( 'REST base route for the taxonomy.' ),
   336 					'description' => __( 'REST base route for the taxonomy.' ),
   327 					'type'         => 'string',
   337 					'type'        => 'string',
   328 					'context'      => array( 'view', 'edit', 'embed' ),
   338 					'context'     => array( 'view', 'edit', 'embed' ),
   329 					'readonly'     => true,
   339 					'readonly'    => true,
       
   340 				),
       
   341 				'visibility'   => array(
       
   342 					'description' => __( 'The visibility settings for the taxonomy.' ),
       
   343 					'type'        => 'object',
       
   344 					'context'     => array( 'edit' ),
       
   345 					'readonly'    => true,
       
   346 					'properties'  => array(
       
   347 						'public'             => array(
       
   348 							'description' => __( 'Whether a taxonomy is intended for use publicly either via the admin interface or by front-end users.' ),
       
   349 							'type'        => 'boolean',
       
   350 						),
       
   351 						'publicly_queryable' => array(
       
   352 							'description' => __( 'Whether the taxonomy is publicly queryable.' ),
       
   353 							'type'        => 'boolean',
       
   354 						),
       
   355 						'show_ui'            => array(
       
   356 							'description' => __( 'Whether to generate a default UI for managing this taxonomy.' ),
       
   357 							'type'        => 'boolean',
       
   358 						),
       
   359 						'show_admin_column'  => array(
       
   360 							'description' => __( 'Whether to allow automatic creation of taxonomy columns on associated post-types table.' ),
       
   361 							'type'        => 'boolean',
       
   362 						),
       
   363 						'show_in_nav_menus'  => array(
       
   364 							'description' => __( 'Whether to make the taxonomy available for selection in navigation menus.' ),
       
   365 							'type'        => 'boolean',
       
   366 						),
       
   367 						'show_in_quick_edit' => array(
       
   368 							'description' => __( 'Whether to show the taxonomy in the quick/bulk edit panel.' ),
       
   369 							'type'        => 'boolean',
       
   370 						),
       
   371 
       
   372 					),
   330 				),
   373 				),
   331 			),
   374 			),
   332 		);
   375 		);
   333 		return $this->add_additional_fields_schema( $schema );
   376 		return $this->add_additional_fields_schema( $schema );
   334 	}
   377 	}
   339 	 * @since 4.7.0
   382 	 * @since 4.7.0
   340 	 *
   383 	 *
   341 	 * @return array Collection parameters.
   384 	 * @return array Collection parameters.
   342 	 */
   385 	 */
   343 	public function get_collection_params() {
   386 	public function get_collection_params() {
   344 		$new_params = array();
   387 		$new_params            = array();
   345 		$new_params['context'] = $this->get_context_param( array( 'default' => 'view' ) );
   388 		$new_params['context'] = $this->get_context_param( array( 'default' => 'view' ) );
   346 		$new_params['type'] = array(
   389 		$new_params['type']    = array(
   347 			'description'  => __( 'Limit results to taxonomies associated with a specific post type.' ),
   390 			'description' => __( 'Limit results to taxonomies associated with a specific post type.' ),
   348 			'type'         => 'string',
   391 			'type'        => 'string',
   349 		);
   392 		);
   350 		return $new_params;
   393 		return $new_params;
   351 	}
   394 	}
   352 
   395 
   353 }
   396 }