Docs: Add much more complete and syntactically correct documentation throughout the WP_REST_Terms_Controller class.

Props Soean, mrahmadawais, flixos90.
See #38398.

Built from https://develop.svn.wordpress.org/trunk@39033


git-svn-id: http://core.svn.wordpress.org/trunk@38975 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Drew Jaynes 2016-10-30 18:21:30 +00:00
parent b71e62b9eb
commit 90c17ccbf1
2 changed files with 192 additions and 43 deletions

View File

@ -1,13 +1,25 @@
<?php <?php
/**
* REST API: WP_REST_Terms_Controller class
*
* @package WordPress
* @subpackage REST_API
* @since 4.7.0
*/
/** /**
* Access terms associated with a taxonomy. * Core class used to managed terms associated with a taxonomy via the REST API.
*
* @since 4.7.0
*
* @see WP_REST_Controller
*/ */
class WP_REST_Terms_Controller extends WP_REST_Controller { class WP_REST_Terms_Controller extends WP_REST_Controller {
/** /**
* Taxonomy key. * Taxonomy key.
* *
* @since 4.7.0
* @access protected * @access protected
* @var string * @var string
*/ */
@ -16,6 +28,7 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
/** /**
* Instance of a term meta fields object. * Instance of a term meta fields object.
* *
* @since 4.7.0
* @access protected * @access protected
* @var WP_REST_Term_Meta_Fields * @var WP_REST_Term_Meta_Fields
*/ */
@ -24,6 +37,7 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
/** /**
* Column to have the terms be sorted by. * Column to have the terms be sorted by.
* *
* @since 4.7.0
* @access protected * @access protected
* @var string * @var string
*/ */
@ -32,6 +46,7 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
/** /**
* Number of terms that were found. * Number of terms that were found.
* *
* @since 4.7.0
* @access protected * @access protected
* @var int * @var int
*/ */
@ -40,6 +55,9 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
/** /**
* Constructor. * Constructor.
* *
* @since 4.7.0
* @access public
*
* @param string $taxonomy Taxonomy key. * @param string $taxonomy Taxonomy key.
*/ */
public function __construct( $taxonomy ) { public function __construct( $taxonomy ) {
@ -53,6 +71,11 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
/** /**
* Registers the routes for the objects of the controller. * Registers the routes for the objects of the controller.
*
* @since 4.7.0
* @access public
*
* @see register_rest_route()
*/ */
public function register_routes() { public function register_routes() {
@ -71,6 +94,7 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
), ),
'schema' => array( $this, 'get_public_item_schema' ), 'schema' => array( $this, 'get_public_item_schema' ),
) ); ) );
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array( register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
array( array(
'methods' => WP_REST_Server::READABLE, 'methods' => WP_REST_Server::READABLE,
@ -104,8 +128,11 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
/** /**
* Checks if a request has access to read terms in the specified taxonomy. * Checks if a request has access to read terms in the specified taxonomy.
* *
* @since 4.7.0
* @access public
*
* @param WP_REST_Request $request Full details about the request. * @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean * @return bool|WP_Error True if the request has read access, otherwise false or WP_Error object.
*/ */
public function get_items_permissions_check( $request ) { public function get_items_permissions_check( $request ) {
$tax_obj = get_taxonomy( $this->taxonomy ); $tax_obj = get_taxonomy( $this->taxonomy );
@ -119,20 +146,25 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
} }
/** /**
* Gets terms associated with a taxonomy. * Retrieves terms associated with a taxonomy.
*
* @since 4.7.0
* @access public
* *
* @param WP_REST_Request $request Full details about the request. * @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response|WP_Error * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/ */
public function get_items( $request ) { public function get_items( $request ) {
// Retrieve the list of registered collection query parameters. // Retrieve the list of registered collection query parameters.
$registered = $this->get_collection_params(); $registered = $this->get_collection_params();
// This array defines mappings between public API query parameters whose /*
// values are accepted as-passed, and their internal WP_Query parameter * This array defines mappings between public API query parameters whose
// name equivalents (some are the same). Only values which are also * values are accepted as-passed, and their internal WP_Query parameter
// present in $registered will be set. * name equivalents (some are the same). Only values which are also
* present in $registered will be set.
*/
$parameter_mappings = array( $parameter_mappings = array(
'exclude' => 'exclude', 'exclude' => 'exclude',
'include' => 'include', 'include' => 'include',
@ -147,8 +179,10 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
$prepared_args = array(); $prepared_args = array();
// For each known parameter which is both registered and present in the request, /*
// set the parameter's value on the query $prepared_args. * For each known parameter which is both registered and present in the request,
* set the parameter's value on the query $prepared_args.
*/
foreach ( $parameter_mappings as $api_param => $wp_param ) { foreach ( $parameter_mappings as $api_param => $wp_param ) {
if ( isset( $registered[ $api_param ], $request[ $api_param ] ) ) { if ( isset( $registered[ $api_param ], $request[ $api_param ] ) ) {
$prepared_args[ $wp_param ] = $request[ $api_param ]; $prepared_args[ $wp_param ] = $request[ $api_param ];
@ -177,10 +211,14 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
/** /**
* Filters the query arguments before passing them to get_terms(). * Filters the query arguments before passing them to get_terms().
* *
* The dynamic portion of the hook name, `$this->taxonomy`, refers to the taxonomy slug.
*
* Enables adding extra arguments or setting defaults for a terms * Enables adding extra arguments or setting defaults for a terms
* collection request. * collection request.
* *
* @see https://developer.wordpress.org/reference/functions/get_terms/ * @since 4.7.0
*
* @link https://developer.wordpress.org/reference/functions/get_terms/
* *
* @param array $prepared_args Array of arguments to be * @param array $prepared_args Array of arguments to be
* passed to get_terms(). * passed to get_terms().
@ -198,15 +236,18 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
} }
$count_args = $prepared_args; $count_args = $prepared_args;
unset( $count_args['number'], $count_args['offset'] ); unset( $count_args['number'], $count_args['offset'] );
$total_terms = wp_count_terms( $this->taxonomy, $count_args ); $total_terms = wp_count_terms( $this->taxonomy, $count_args );
// wp_count_terms can return a falsy value when the term has no children // wp_count_terms can return a falsy value when the term has no children.
if ( ! $total_terms ) { if ( ! $total_terms ) {
$total_terms = 0; $total_terms = 0;
} }
$response = array(); $response = array();
foreach ( $query_result as $term ) { foreach ( $query_result as $term ) {
$data = $this->prepare_item_for_response( $term, $request ); $data = $this->prepare_item_for_response( $term, $request );
$response[] = $this->prepare_response_for_collection( $data ); $response[] = $this->prepare_response_for_collection( $data );
@ -219,21 +260,26 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
$page = ceil( ( ( (int) $prepared_args['offset'] ) / $per_page ) + 1 ); $page = ceil( ( ( (int) $prepared_args['offset'] ) / $per_page ) + 1 );
$response->header( 'X-WP-Total', (int) $total_terms ); $response->header( 'X-WP-Total', (int) $total_terms );
$max_pages = ceil( $total_terms / $per_page ); $max_pages = ceil( $total_terms / $per_page );
$response->header( 'X-WP-TotalPages', (int) $max_pages ); $response->header( 'X-WP-TotalPages', (int) $max_pages );
$base = add_query_arg( $request->get_query_params(), rest_url( $this->namespace . '/' . $this->rest_base ) ); $base = add_query_arg( $request->get_query_params(), rest_url( $this->namespace . '/' . $this->rest_base ) );
if ( $page > 1 ) { if ( $page > 1 ) {
$prev_page = $page - 1; $prev_page = $page - 1;
if ( $prev_page > $max_pages ) { if ( $prev_page > $max_pages ) {
$prev_page = $max_pages; $prev_page = $max_pages;
} }
$prev_link = add_query_arg( 'page', $prev_page, $base ); $prev_link = add_query_arg( 'page', $prev_page, $base );
$response->link_header( 'prev', $prev_link ); $response->link_header( 'prev', $prev_link );
} }
if ( $max_pages > $page ) { if ( $max_pages > $page ) {
$next_page = $page + 1; $next_page = $page + 1;
$next_link = add_query_arg( 'page', $next_page, $base ); $next_link = add_query_arg( 'page', $next_page, $base );
$response->link_header( 'next', $next_link ); $response->link_header( 'next', $next_link );
} }
@ -243,8 +289,11 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
/** /**
* Checks if a request has access to read the specified term. * Checks if a request has access to read the specified term.
* *
* @since 4.7.0
* @access public
*
* @param WP_REST_Request $request Full details about the request. * @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean * @return bool|WP_Error True if the request has read access for the item, otherwise false or WP_Error object.
*/ */
public function get_item_permissions_check( $request ) { public function get_item_permissions_check( $request ) {
$tax_obj = get_taxonomy( $this->taxonomy ); $tax_obj = get_taxonomy( $this->taxonomy );
@ -260,15 +309,20 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
/** /**
* Gets a single term from a taxonomy. * Gets a single term from a taxonomy.
* *
* @param WP_REST_Request $request Full details about the request * @since 4.7.0
* @return WP_REST_Request|WP_Error * @access public
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/ */
public function get_item( $request ) { public function get_item( $request ) {
$term = get_term( (int) $request['id'], $this->taxonomy ); $term = get_term( (int) $request['id'], $this->taxonomy );
if ( ! $term || $term->taxonomy !== $this->taxonomy ) { if ( ! $term || $term->taxonomy !== $this->taxonomy ) {
return new WP_Error( 'rest_term_invalid', __( "Resource doesn't exist." ), array( 'status' => 404 ) ); return new WP_Error( 'rest_term_invalid', __( "Resource doesn't exist." ), array( 'status' => 404 ) );
} }
if ( is_wp_error( $term ) ) { if ( is_wp_error( $term ) ) {
return $term; return $term;
} }
@ -281,8 +335,11 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
/** /**
* Checks if a request has access to create a term. * Checks if a request has access to create a term.
* *
* @since 4.7.0
* @access public
*
* @param WP_REST_Request $request Full details about the request. * @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean * @return bool|WP_Error True if the request has access to create items, false or WP_Error object otherwise.
*/ */
public function create_item_permissions_check( $request ) { public function create_item_permissions_check( $request ) {
@ -301,8 +358,11 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
/** /**
* Creates a single term in a taxonomy. * Creates a single term in a taxonomy.
* *
* @param WP_REST_Request $request Full details about the request * @since 4.7.0
* @return WP_REST_Request|WP_Error * @access public
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/ */
public function create_item( $request ) { public function create_item( $request ) {
if ( isset( $request['parent'] ) ) { if ( isset( $request['parent'] ) ) {
@ -321,7 +381,6 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
$term = wp_insert_term( $prepared_term->name, $this->taxonomy, $prepared_term ); $term = wp_insert_term( $prepared_term->name, $this->taxonomy, $prepared_term );
if ( is_wp_error( $term ) ) { if ( is_wp_error( $term ) ) {
/* /*
* If we're going to inform the client that the term already exists, * If we're going to inform the client that the term already exists,
* give them the identifier for future use. * give them the identifier for future use.
@ -339,38 +398,50 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
/** /**
* Fires after a single term is created or updated via the REST API. * Fires after a single term is created or updated via the REST API.
* *
* The dynamic portion of the hook name, `$this->taxonomy`, refers to the taxonomy slug.
*
* @since 4.7.0
*
* @param WP_Term $term Inserted Term object. * @param WP_Term $term Inserted Term object.
* @param WP_REST_Request $request Request object. * @param WP_REST_Request $request Request object.
* @param boolean $creating True when creating term, false when updating. * @param bool $creating True when creating term, false when updating.
*/ */
do_action( "rest_insert_{$this->taxonomy}", $term, $request, true ); do_action( "rest_insert_{$this->taxonomy}", $term, $request, true );
$schema = $this->get_item_schema(); $schema = $this->get_item_schema();
if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) { if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
$meta_update = $this->meta->update_value( $request['meta'], (int) $request['id'] ); $meta_update = $this->meta->update_value( $request['meta'], (int) $request['id'] );
if ( is_wp_error( $meta_update ) ) { if ( is_wp_error( $meta_update ) ) {
return $meta_update; return $meta_update;
} }
} }
$fields_update = $this->update_additional_fields_for_object( $term, $request ); $fields_update = $this->update_additional_fields_for_object( $term, $request );
if ( is_wp_error( $fields_update ) ) { if ( is_wp_error( $fields_update ) ) {
return $fields_update; return $fields_update;
} }
$request->set_param( 'context', 'view' ); $request->set_param( 'context', 'view' );
$response = $this->prepare_item_for_response( $term, $request ); $response = $this->prepare_item_for_response( $term, $request );
$response = rest_ensure_response( $response ); $response = rest_ensure_response( $response );
$response->set_status( 201 ); $response->set_status( 201 );
$response->header( 'Location', rest_url( $this->namespace . '/' . $this->rest_base . '/' . $term->term_id ) ); $response->header( 'Location', rest_url( $this->namespace . '/' . $this->rest_base . '/' . $term->term_id ) );
return $response; return $response;
} }
/** /**
* Checks if a request has access to update the specified term. * Checks if a request has access to update the specified term.
* *
* @since 4.7.0
* @access public
*
* @param WP_REST_Request $request Full details about the request. * @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean * @return bool|WP_Error True if the request has access to update the item, false or WP_Error object otherwise.
*/ */
public function update_item_permissions_check( $request ) { public function update_item_permissions_check( $request ) {
@ -379,6 +450,7 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
} }
$term = get_term( (int) $request['id'], $this->taxonomy ); $term = get_term( (int) $request['id'], $this->taxonomy );
if ( ! $term ) { if ( ! $term ) {
return new WP_Error( 'rest_term_invalid', __( "Resource doesn't exist." ), array( 'status' => 404 ) ); return new WP_Error( 'rest_term_invalid', __( "Resource doesn't exist." ), array( 'status' => 404 ) );
} }
@ -393,8 +465,11 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
/** /**
* Updates a single term from a taxonomy. * Updates a single term from a taxonomy.
* *
* @param WP_REST_Request $request Full details about the request * @since 4.7.0
* @return WP_REST_Request|WP_Error * @access public
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/ */
public function update_item( $request ) { public function update_item( $request ) {
if ( isset( $request['parent'] ) ) { if ( isset( $request['parent'] ) ) {
@ -416,6 +491,7 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
// Only update the term if we haz something to update. // Only update the term if we haz something to update.
if ( ! empty( $prepared_term ) ) { if ( ! empty( $prepared_term ) ) {
$update = wp_update_term( $term->term_id, $term->taxonomy, (array) $prepared_term ); $update = wp_update_term( $term->term_id, $term->taxonomy, (array) $prepared_term );
if ( is_wp_error( $update ) ) { if ( is_wp_error( $update ) ) {
return $update; return $update;
} }
@ -429,32 +505,41 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
$schema = $this->get_item_schema(); $schema = $this->get_item_schema();
if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) { if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
$meta_update = $this->meta->update_value( $request['meta'], (int) $request['id'] ); $meta_update = $this->meta->update_value( $request['meta'], (int) $request['id'] );
if ( is_wp_error( $meta_update ) ) { if ( is_wp_error( $meta_update ) ) {
return $meta_update; return $meta_update;
} }
} }
$fields_update = $this->update_additional_fields_for_object( $term, $request ); $fields_update = $this->update_additional_fields_for_object( $term, $request );
if ( is_wp_error( $fields_update ) ) { if ( is_wp_error( $fields_update ) ) {
return $fields_update; return $fields_update;
} }
$request->set_param( 'context', 'view' ); $request->set_param( 'context', 'view' );
$response = $this->prepare_item_for_response( $term, $request ); $response = $this->prepare_item_for_response( $term, $request );
return rest_ensure_response( $response ); return rest_ensure_response( $response );
} }
/** /**
* Checks if a request has access to delete the specified term. * Checks if a request has access to delete the specified term.
* *
* @since 4.7.0
* @access public
*
* @param WP_REST_Request $request Full details about the request. * @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean * @return bool|WP_Error True if the request has access to delete the item, otherwise false or WP_Error object.
*/ */
public function delete_item_permissions_check( $request ) { public function delete_item_permissions_check( $request ) {
if ( ! $this->check_is_taxonomy_allowed( $this->taxonomy ) ) { if ( ! $this->check_is_taxonomy_allowed( $this->taxonomy ) ) {
return false; return false;
} }
$term = get_term( (int) $request['id'], $this->taxonomy ); $term = get_term( (int) $request['id'], $this->taxonomy );
if ( ! $term ) { if ( ! $term ) {
return new WP_Error( 'rest_term_invalid', __( "Resource doesn't exist." ), array( 'status' => 404 ) ); return new WP_Error( 'rest_term_invalid', __( "Resource doesn't exist." ), array( 'status' => 404 ) );
} }
@ -462,14 +547,18 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
if ( ! current_user_can( 'delete_term', $term->term_id ) ) { if ( ! current_user_can( 'delete_term', $term->term_id ) ) {
return new WP_Error( 'rest_cannot_delete', __( 'Sorry, you cannot delete resource.' ), array( 'status' => rest_authorization_required_code() ) ); return new WP_Error( 'rest_cannot_delete', __( 'Sorry, you cannot delete resource.' ), array( 'status' => rest_authorization_required_code() ) );
} }
return true; return true;
} }
/** /**
* Deletes a single term from a taxonomy. * Deletes a single term from a taxonomy.
* *
* @param WP_REST_Request $request Full details about the request * @since 4.7.0
* @return WP_REST_Response|WP_Error * @access public
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/ */
public function delete_item( $request ) { public function delete_item( $request ) {
@ -481,10 +570,13 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
} }
$term = get_term( (int) $request['id'], $this->taxonomy ); $term = get_term( (int) $request['id'], $this->taxonomy );
$request->set_param( 'context', 'view' ); $request->set_param( 'context', 'view' );
$response = $this->prepare_item_for_response( $term, $request ); $response = $this->prepare_item_for_response( $term, $request );
$retval = wp_delete_term( $term->term_id, $term->taxonomy ); $retval = wp_delete_term( $term->term_id, $term->taxonomy );
if ( ! $retval ) { if ( ! $retval ) {
return new WP_Error( 'rest_cannot_delete', __( 'The resource cannot be deleted.' ), array( 'status' => 500 ) ); return new WP_Error( 'rest_cannot_delete', __( 'The resource cannot be deleted.' ), array( 'status' => 500 ) );
} }
@ -492,6 +584,10 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
/** /**
* Fires after a single term is deleted via the REST API. * Fires after a single term is deleted via the REST API.
* *
* The dynamic portion of the hook name, `$this->taxonomy`, refers to the taxonomy slug.
*
* @since 4.7.0
*
* @param WP_Term $term The deleted term. * @param WP_Term $term The deleted term.
* @param WP_REST_Response $response The response data. * @param WP_REST_Response $response The response data.
* @param WP_REST_Request $request The request sent to the API. * @param WP_REST_Request $request The request sent to the API.
@ -504,6 +600,9 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
/** /**
* Prepares a single term for create or update. * Prepares a single term for create or update.
* *
* @since 4.7.0
* @access public
*
* @param WP_REST_Request $request Request object. * @param WP_REST_Request $request Request object.
* @return object $prepared_term Term object. * @return object $prepared_term Term object.
*/ */
@ -541,6 +640,10 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
/** /**
* Filters term data before inserting term via the REST API. * Filters term data before inserting term via the REST API.
* *
* The dynamic portion of the hook name, `$this->taxonomy`, refers to the taxonomy slug.
*
* @since 4.7.0
*
* @param object $prepared_term Term object. * @param object $prepared_term Term object.
* @param WP_REST_Request $request Request object. * @param WP_REST_Request $request Request object.
*/ */
@ -550,38 +653,50 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
/** /**
* Prepares a single term output for response. * Prepares a single term output for response.
* *
* @since 4.7.0
* @access public
*
* @param obj $item Term object. * @param obj $item Term object.
* @param WP_REST_Request $request Request object. * @param WP_REST_Request $request Request object.
* @return WP_REST_Response $response * @return WP_REST_Response $response Response object.
*/ */
public function prepare_item_for_response( $item, $request ) { public function prepare_item_for_response( $item, $request ) {
$schema = $this->get_item_schema(); $schema = $this->get_item_schema();
$data = array(); $data = array();
if ( ! empty( $schema['properties']['id'] ) ) { if ( ! empty( $schema['properties']['id'] ) ) {
$data['id'] = (int) $item->term_id; $data['id'] = (int) $item->term_id;
} }
if ( ! empty( $schema['properties']['count'] ) ) { if ( ! empty( $schema['properties']['count'] ) ) {
$data['count'] = (int) $item->count; $data['count'] = (int) $item->count;
} }
if ( ! empty( $schema['properties']['description'] ) ) { if ( ! empty( $schema['properties']['description'] ) ) {
$data['description'] = $item->description; $data['description'] = $item->description;
} }
if ( ! empty( $schema['properties']['link'] ) ) { if ( ! empty( $schema['properties']['link'] ) ) {
$data['link'] = get_term_link( $item ); $data['link'] = get_term_link( $item );
} }
if ( ! empty( $schema['properties']['name'] ) ) { if ( ! empty( $schema['properties']['name'] ) ) {
$data['name'] = $item->name; $data['name'] = $item->name;
} }
if ( ! empty( $schema['properties']['slug'] ) ) { if ( ! empty( $schema['properties']['slug'] ) ) {
$data['slug'] = $item->slug; $data['slug'] = $item->slug;
} }
if ( ! empty( $schema['properties']['taxonomy'] ) ) { if ( ! empty( $schema['properties']['taxonomy'] ) ) {
$data['taxonomy'] = $item->taxonomy; $data['taxonomy'] = $item->taxonomy;
} }
if ( ! empty( $schema['properties']['parent'] ) ) { if ( ! empty( $schema['properties']['parent'] ) ) {
$data['parent'] = (int) $item->parent; $data['parent'] = (int) $item->parent;
} }
if ( ! empty( $schema['properties']['meta'] ) ) { if ( ! empty( $schema['properties']['meta'] ) ) {
$data['meta'] = $this->meta->get_value( $item->term_id, $request ); $data['meta'] = $this->meta->get_value( $item->term_id, $request );
} }
@ -597,8 +712,12 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
/** /**
* Filters a term item returned from the API. * Filters a term item returned from the API.
* *
* The dynamic portion of the hook name, `$this->taxonomy`, refers to the taxonomy slug.
*
* Allows modification of the term data right before it is returned. * Allows modification of the term data right before it is returned.
* *
* @since 4.7.0
*
* @param WP_REST_Response $response The response object. * @param WP_REST_Response $response The response object.
* @param object $item The original term object. * @param object $item The original term object.
* @param WP_REST_Request $request Request used to generate the response. * @param WP_REST_Request $request Request used to generate the response.
@ -609,6 +728,9 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
/** /**
* Prepares links for the request. * Prepares links for the request.
* *
* @since 4.7.0
* @access protected
*
* @param object $term Term object. * @param object $term Term object.
* @return array Links for the given term. * @return array Links for the given term.
*/ */
@ -628,6 +750,7 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
if ( $term->parent ) { if ( $term->parent ) {
$parent_term = get_term( (int) $term->parent, $term->taxonomy ); $parent_term = get_term( (int) $term->parent, $term->taxonomy );
if ( $parent_term ) { if ( $parent_term ) {
$links['up'] = array( $links['up'] = array(
'href' => rest_url( trailingslashit( $base ) . $parent_term->term_id ), 'href' => rest_url( trailingslashit( $base ) . $parent_term->term_id ),
@ -637,21 +760,26 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
} }
$taxonomy_obj = get_taxonomy( $term->taxonomy ); $taxonomy_obj = get_taxonomy( $term->taxonomy );
if ( empty( $taxonomy_obj->object_type ) ) { if ( empty( $taxonomy_obj->object_type ) ) {
return $links; return $links;
} }
$post_type_links = array(); $post_type_links = array();
foreach ( $taxonomy_obj->object_type as $type ) { foreach ( $taxonomy_obj->object_type as $type ) {
$post_type_object = get_post_type_object( $type ); $post_type_object = get_post_type_object( $type );
if ( empty( $post_type_object->show_in_rest ) ) { if ( empty( $post_type_object->show_in_rest ) ) {
continue; continue;
} }
$rest_base = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name; $rest_base = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name;
$post_type_links[] = array( $post_type_links[] = array(
'href' => add_query_arg( $this->rest_base, $term->term_id, rest_url( sprintf( 'wp/v2/%s', $rest_base ) ) ), 'href' => add_query_arg( $this->rest_base, $term->term_id, rest_url( sprintf( 'wp/v2/%s', $rest_base ) ) ),
); );
} }
if ( ! empty( $post_type_links ) ) { if ( ! empty( $post_type_links ) ) {
$links['https://api.w.org/post_type'] = $post_type_links; $links['https://api.w.org/post_type'] = $post_type_links;
} }
@ -660,9 +788,12 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
} }
/** /**
* Gets the term's schema, conforming to JSON Schema. * Retrieves the term's schema, conforming to JSON Schema.
* *
* @return array * @since 4.7.0
* @access public
*
* @return array Item schema data.
*/ */
public function get_item_schema() { public function get_item_schema() {
$schema = array( $schema = array(
@ -723,7 +854,9 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
), ),
), ),
); );
$taxonomy = get_taxonomy( $this->taxonomy ); $taxonomy = get_taxonomy( $this->taxonomy );
if ( $taxonomy->hierarchical ) { if ( $taxonomy->hierarchical ) {
$schema['properties']['parent'] = array( $schema['properties']['parent'] = array(
'description' => __( 'The id for the parent of the resource.' ), 'description' => __( 'The id for the parent of the resource.' ),
@ -733,13 +866,17 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
} }
$schema['properties']['meta'] = $this->meta->get_field_schema(); $schema['properties']['meta'] = $this->meta->get_field_schema();
return $this->add_additional_fields_schema( $schema ); return $this->add_additional_fields_schema( $schema );
} }
/** /**
* Gets the query params for collections. * Retrieves the query params for collections.
* *
* @return array * @since 4.7.0
* @access public
*
* @return array Collection parameters.
*/ */
public function get_collection_params() { public function get_collection_params() {
$query_params = parent::get_collection_params(); $query_params = parent::get_collection_params();
@ -753,12 +890,14 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
'default' => array(), 'default' => array(),
'sanitize_callback' => 'wp_parse_id_list', 'sanitize_callback' => 'wp_parse_id_list',
); );
$query_params['include'] = array( $query_params['include'] = array(
'description' => __( 'Limit result set to specific ids.' ), 'description' => __( 'Limit result set to specific ids.' ),
'type' => 'array', 'type' => 'array',
'default' => array(), 'default' => array(),
'sanitize_callback' => 'wp_parse_id_list', 'sanitize_callback' => 'wp_parse_id_list',
); );
if ( ! $taxonomy->hierarchical ) { if ( ! $taxonomy->hierarchical ) {
$query_params['offset'] = array( $query_params['offset'] = array(
'description' => __( 'Offset the result set by a specific number of items.' ), 'description' => __( 'Offset the result set by a specific number of items.' ),
@ -767,6 +906,7 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
'validate_callback' => 'rest_validate_request_arg', 'validate_callback' => 'rest_validate_request_arg',
); );
} }
$query_params['order'] = array( $query_params['order'] = array(
'description' => __( 'Order sort attribute ascending or descending.' ), 'description' => __( 'Order sort attribute ascending or descending.' ),
'type' => 'string', 'type' => 'string',
@ -778,6 +918,7 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
), ),
'validate_callback' => 'rest_validate_request_arg', 'validate_callback' => 'rest_validate_request_arg',
); );
$query_params['orderby'] = array( $query_params['orderby'] = array(
'description' => __( 'Sort collection by resource attribute.' ), 'description' => __( 'Sort collection by resource attribute.' ),
'type' => 'string', 'type' => 'string',
@ -794,6 +935,7 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
), ),
'validate_callback' => 'rest_validate_request_arg', 'validate_callback' => 'rest_validate_request_arg',
); );
$query_params['hide_empty'] = array( $query_params['hide_empty'] = array(
'description' => __( 'Whether to hide resources not assigned to any posts.' ), 'description' => __( 'Whether to hide resources not assigned to any posts.' ),
'type' => 'boolean', 'type' => 'boolean',
@ -801,6 +943,7 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
'sanitize_callback' => 'rest_sanitize_request_arg', 'sanitize_callback' => 'rest_sanitize_request_arg',
'validate_callback' => 'rest_validate_request_arg', 'validate_callback' => 'rest_validate_request_arg',
); );
if ( $taxonomy->hierarchical ) { if ( $taxonomy->hierarchical ) {
$query_params['parent'] = array( $query_params['parent'] = array(
'description' => __( 'Limit result set to resources assigned to a specific parent.' ), 'description' => __( 'Limit result set to resources assigned to a specific parent.' ),
@ -809,25 +952,31 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
'validate_callback' => 'rest_validate_request_arg', 'validate_callback' => 'rest_validate_request_arg',
); );
} }
$query_params['post'] = array( $query_params['post'] = array(
'description' => __( 'Limit result set to resources assigned to a specific post.' ), 'description' => __( 'Limit result set to resources assigned to a specific post.' ),
'type' => 'integer', 'type' => 'integer',
'default' => null, 'default' => null,
'validate_callback' => 'rest_validate_request_arg', 'validate_callback' => 'rest_validate_request_arg',
); );
$query_params['slug'] = array( $query_params['slug'] = array(
'description' => __( 'Limit result set to resources with a specific slug.' ), 'description' => __( 'Limit result set to resources with a specific slug.' ),
'type' => 'string', 'type' => 'string',
'validate_callback' => 'rest_validate_request_arg', 'validate_callback' => 'rest_validate_request_arg',
); );
return $query_params; return $query_params;
} }
/** /**
* Checks that the taxonomy is valid. * Checks that the taxonomy is valid.
* *
* @since 4.7.0
* @access protected
*
* @param string $taxonomy Taxonomy to check. * @param string $taxonomy Taxonomy to check.
* @return WP_Error|boolean * @return bool Whether the taxonomy is allowed for REST management.
*/ */
protected function check_is_taxonomy_allowed( $taxonomy ) { protected function check_is_taxonomy_allowed( $taxonomy ) {
$taxonomy_obj = get_taxonomy( $taxonomy ); $taxonomy_obj = get_taxonomy( $taxonomy );

View File

@ -4,7 +4,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '4.7-beta1-39032'; $wp_version = '4.7-beta1-39033';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.