[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/rest-api/endpoints/ -> class-wp-rest-taxonomies-controller.php (source)

   1  <?php
   2  /**
   3   * REST API: WP_REST_Taxonomies_Controller class
   4   *
   5   * @package WordPress
   6   * @subpackage REST_API
   7   * @since 4.7.0
   8   */
   9  
  10  /**
  11   * Core class used to manage taxonomies via the REST API.
  12   *
  13   * @since 4.7.0
  14   *
  15   * @see WP_REST_Controller
  16   */
  17  class WP_REST_Taxonomies_Controller extends WP_REST_Controller {
  18  
  19      /**
  20       * Constructor.
  21       *
  22       * @since 4.7.0
  23       */
  24  	public function __construct() {
  25          $this->namespace = 'wp/v2';
  26          $this->rest_base = 'taxonomies';
  27      }
  28  
  29      /**
  30       * Registers the routes for taxonomies.
  31       *
  32       * @since 4.7.0
  33       *
  34       * @see register_rest_route()
  35       */
  36  	public function register_routes() {
  37  
  38          register_rest_route(
  39              $this->namespace,
  40              '/' . $this->rest_base,
  41              array(
  42                  array(
  43                      'methods'             => WP_REST_Server::READABLE,
  44                      'callback'            => array( $this, 'get_items' ),
  45                      'permission_callback' => array( $this, 'get_items_permissions_check' ),
  46                      'args'                => $this->get_collection_params(),
  47                  ),
  48                  'schema' => array( $this, 'get_public_item_schema' ),
  49              )
  50          );
  51  
  52          register_rest_route(
  53              $this->namespace,
  54              '/' . $this->rest_base . '/(?P<taxonomy>[\w-]+)',
  55              array(
  56                  'args'   => array(
  57                      'taxonomy' => array(
  58                          'description' => __( 'An alphanumeric identifier for the taxonomy.' ),
  59                          'type'        => 'string',
  60                      ),
  61                  ),
  62                  array(
  63                      'methods'             => WP_REST_Server::READABLE,
  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          );
  73      }
  74  
  75      /**
  76       * Checks whether a given request has permission to read taxonomies.
  77       *
  78       * @since 4.7.0
  79       *
  80       * @param WP_REST_Request $request Full details about the request.
  81       * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
  82       */
  83  	public function get_items_permissions_check( $request ) {
  84          if ( 'edit' === $request['context'] ) {
  85              if ( ! empty( $request['type'] ) ) {
  86                  $taxonomies = get_object_taxonomies( $request['type'], 'objects' );
  87              } else {
  88                  $taxonomies = get_taxonomies( '', 'objects' );
  89              }
  90  
  91              foreach ( $taxonomies as $taxonomy ) {
  92                  if ( ! empty( $taxonomy->show_in_rest ) && current_user_can( $taxonomy->cap->assign_terms ) ) {
  93                      return true;
  94                  }
  95              }
  96  
  97              return new WP_Error(
  98                  'rest_cannot_view',
  99                  __( 'Sorry, you are not allowed to manage terms in this taxonomy.' ),
 100                  array( 'status' => rest_authorization_required_code() )
 101              );
 102          }
 103  
 104          return true;
 105      }
 106  
 107      /**
 108       * Retrieves all public taxonomies.
 109       *
 110       * @since 4.7.0
 111       *
 112       * @param WP_REST_Request $request Full details about the request.
 113       * @return WP_REST_Response Response object on success, or WP_Error object on failure.
 114       */
 115  	public function get_items( $request ) {
 116  
 117          // Retrieve the list of registered collection query parameters.
 118          $registered = $this->get_collection_params();
 119  
 120          if ( isset( $registered['type'] ) && ! empty( $request['type'] ) ) {
 121              $taxonomies = get_object_taxonomies( $request['type'], 'objects' );
 122          } else {
 123              $taxonomies = get_taxonomies( '', 'objects' );
 124          }
 125  
 126          $data = array();
 127  
 128          foreach ( $taxonomies as $tax_type => $value ) {
 129              if ( empty( $value->show_in_rest ) || ( 'edit' === $request['context'] && ! current_user_can( $value->cap->assign_terms ) ) ) {
 130                  continue;
 131              }
 132  
 133              $tax               = $this->prepare_item_for_response( $value, $request );
 134              $tax               = $this->prepare_response_for_collection( $tax );
 135              $data[ $tax_type ] = $tax;
 136          }
 137  
 138          if ( empty( $data ) ) {
 139              // Response should still be returned as a JSON object when it is empty.
 140              $data = (object) $data;
 141          }
 142  
 143          return rest_ensure_response( $data );
 144      }
 145  
 146      /**
 147       * Checks if a given request has access to a taxonomy.
 148       *
 149       * @since 4.7.0
 150       *
 151       * @param WP_REST_Request $request Full details about the request.
 152       * @return true|WP_Error True if the request has read access for the item, otherwise false or WP_Error object.
 153       */
 154  	public function get_item_permissions_check( $request ) {
 155  
 156          $tax_obj = get_taxonomy( $request['taxonomy'] );
 157  
 158          if ( $tax_obj ) {
 159              if ( empty( $tax_obj->show_in_rest ) ) {
 160                  return false;
 161              }
 162  
 163              if ( 'edit' === $request['context'] && ! current_user_can( $tax_obj->cap->assign_terms ) ) {
 164                  return new WP_Error(
 165                      'rest_forbidden_context',
 166                      __( 'Sorry, you are not allowed to manage terms in this taxonomy.' ),
 167                      array( 'status' => rest_authorization_required_code() )
 168                  );
 169              }
 170          }
 171  
 172          return true;
 173      }
 174  
 175      /**
 176       * Retrieves a specific taxonomy.
 177       *
 178       * @since 4.7.0
 179       *
 180       * @param WP_REST_Request $request Full details about the request.
 181       * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
 182       */
 183  	public function get_item( $request ) {
 184          $tax_obj = get_taxonomy( $request['taxonomy'] );
 185  
 186          if ( empty( $tax_obj ) ) {
 187              return new WP_Error(
 188                  'rest_taxonomy_invalid',
 189                  __( 'Invalid taxonomy.' ),
 190                  array( 'status' => 404 )
 191              );
 192          }
 193  
 194          $data = $this->prepare_item_for_response( $tax_obj, $request );
 195  
 196          return rest_ensure_response( $data );
 197      }
 198  
 199      /**
 200       * Prepares a taxonomy object for serialization.
 201       *
 202       * @since 4.7.0
 203       * @since 5.9.0 Renamed `$taxonomy` to `$item` to match parent class for PHP 8 named parameter support.
 204       *
 205       * @param WP_Taxonomy     $item    Taxonomy data.
 206       * @param WP_REST_Request $request Full details about the request.
 207       * @return WP_REST_Response Response object.
 208       */
 209  	public function prepare_item_for_response( $item, $request ) {
 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;
 213  
 214          $fields = $this->get_fields_for_response( $request );
 215          $data   = array();
 216  
 217          if ( in_array( 'name', $fields, true ) ) {
 218              $data['name'] = $taxonomy->label;
 219          }
 220  
 221          if ( in_array( 'slug', $fields, true ) ) {
 222              $data['slug'] = $taxonomy->name;
 223          }
 224  
 225          if ( in_array( 'capabilities', $fields, true ) ) {
 226              $data['capabilities'] = $taxonomy->cap;
 227          }
 228  
 229          if ( in_array( 'description', $fields, true ) ) {
 230              $data['description'] = $taxonomy->description;
 231          }
 232  
 233          if ( in_array( 'labels', $fields, true ) ) {
 234              $data['labels'] = $taxonomy->labels;
 235          }
 236  
 237          if ( in_array( 'types', $fields, true ) ) {
 238              $data['types'] = array_values( $taxonomy->object_type );
 239          }
 240  
 241          if ( in_array( 'show_cloud', $fields, true ) ) {
 242              $data['show_cloud'] = $taxonomy->show_tagcloud;
 243          }
 244  
 245          if ( in_array( 'hierarchical', $fields, true ) ) {
 246              $data['hierarchical'] = $taxonomy->hierarchical;
 247          }
 248  
 249          if ( in_array( 'rest_base', $fields, true ) ) {
 250              $data['rest_base'] = $base;
 251          }
 252  
 253          if ( in_array( 'visibility', $fields, true ) ) {
 254              $data['visibility'] = array(
 255                  'public'             => (bool) $taxonomy->public,
 256                  'publicly_queryable' => (bool) $taxonomy->publicly_queryable,
 257                  'show_admin_column'  => (bool) $taxonomy->show_admin_column,
 258                  'show_in_nav_menus'  => (bool) $taxonomy->show_in_nav_menus,
 259                  'show_in_quick_edit' => (bool) $taxonomy->show_in_quick_edit,
 260                  'show_ui'            => (bool) $taxonomy->show_ui,
 261              );
 262          }
 263  
 264          $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
 265          $data    = $this->add_additional_fields_to_object( $data, $request );
 266          $data    = $this->filter_response_by_context( $data, $context );
 267  
 268          // Wrap the data in a response object.
 269          $response = rest_ensure_response( $data );
 270  
 271          $response->add_links(
 272              array(
 273                  'collection'              => array(
 274                      'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
 275                  ),
 276                  'https://api.w.org/items' => array(
 277                      'href' => rest_url( sprintf( 'wp/v2/%s', $base ) ),
 278                  ),
 279              )
 280          );
 281  
 282          /**
 283           * Filters a taxonomy returned from the REST API.
 284           *
 285           * Allows modification of the taxonomy data right before it is returned.
 286           *
 287           * @since 4.7.0
 288           *
 289           * @param WP_REST_Response $response The response object.
 290           * @param WP_Taxonomy      $item     The original taxonomy object.
 291           * @param WP_REST_Request  $request  Request used to generate the response.
 292           */
 293          return apply_filters( 'rest_prepare_taxonomy', $response, $taxonomy, $request );
 294      }
 295  
 296      /**
 297       * Retrieves the taxonomy's schema, conforming to JSON Schema.
 298       *
 299       * @since 4.7.0
 300       *
 301       * @return array Item schema data.
 302       */
 303  	public function get_item_schema() {
 304          if ( $this->schema ) {
 305              return $this->add_additional_fields_schema( $this->schema );
 306          }
 307  
 308          $schema = array(
 309              '$schema'    => 'http://json-schema.org/draft-04/schema#',
 310              'title'      => 'taxonomy',
 311              'type'       => 'object',
 312              'properties' => array(
 313                  'capabilities' => array(
 314                      'description' => __( 'All capabilities used by the taxonomy.' ),
 315                      'type'        => 'object',
 316                      'context'     => array( 'edit' ),
 317                      'readonly'    => true,
 318                  ),
 319                  'description'  => array(
 320                      'description' => __( 'A human-readable description of the taxonomy.' ),
 321                      'type'        => 'string',
 322                      'context'     => array( 'view', 'edit' ),
 323                      'readonly'    => true,
 324                  ),
 325                  'hierarchical' => array(
 326                      'description' => __( 'Whether or not the taxonomy should have children.' ),
 327                      'type'        => 'boolean',
 328                      'context'     => array( 'view', 'edit' ),
 329                      'readonly'    => true,
 330                  ),
 331                  'labels'       => array(
 332                      'description' => __( 'Human-readable labels for the taxonomy for various contexts.' ),
 333                      'type'        => 'object',
 334                      'context'     => array( 'edit' ),
 335                      'readonly'    => true,
 336                  ),
 337                  'name'         => array(
 338                      'description' => __( 'The title for the taxonomy.' ),
 339                      'type'        => 'string',
 340                      'context'     => array( 'view', 'edit', 'embed' ),
 341                      'readonly'    => true,
 342                  ),
 343                  'slug'         => array(
 344                      'description' => __( 'An alphanumeric identifier for the taxonomy.' ),
 345                      'type'        => 'string',
 346                      'context'     => array( 'view', 'edit', 'embed' ),
 347                      'readonly'    => true,
 348                  ),
 349                  'show_cloud'   => array(
 350                      'description' => __( 'Whether or not the term cloud should be displayed.' ),
 351                      'type'        => 'boolean',
 352                      'context'     => array( 'edit' ),
 353                      'readonly'    => true,
 354                  ),
 355                  'types'        => array(
 356                      'description' => __( 'Types associated with the taxonomy.' ),
 357                      'type'        => 'array',
 358                      'items'       => array(
 359                          'type' => 'string',
 360                      ),
 361                      'context'     => array( 'view', 'edit' ),
 362                      'readonly'    => true,
 363                  ),
 364                  'rest_base'    => array(
 365                      'description' => __( 'REST base route for the taxonomy.' ),
 366                      'type'        => 'string',
 367                      'context'     => array( 'view', 'edit', 'embed' ),
 368                      'readonly'    => true,
 369                  ),
 370                  'visibility'   => array(
 371                      'description' => __( 'The visibility settings for the taxonomy.' ),
 372                      'type'        => 'object',
 373                      'context'     => array( 'edit' ),
 374                      'readonly'    => true,
 375                      'properties'  => array(
 376                          'public'             => array(
 377                              'description' => __( 'Whether a taxonomy is intended for use publicly either via the admin interface or by front-end users.' ),
 378                              'type'        => 'boolean',
 379                          ),
 380                          'publicly_queryable' => array(
 381                              'description' => __( 'Whether the taxonomy is publicly queryable.' ),
 382                              'type'        => 'boolean',
 383                          ),
 384                          'show_ui'            => array(
 385                              'description' => __( 'Whether to generate a default UI for managing this taxonomy.' ),
 386                              'type'        => 'boolean',
 387                          ),
 388                          'show_admin_column'  => array(
 389                              'description' => __( 'Whether to allow automatic creation of taxonomy columns on associated post-types table.' ),
 390                              'type'        => 'boolean',
 391                          ),
 392                          'show_in_nav_menus'  => array(
 393                              'description' => __( 'Whether to make the taxonomy available for selection in navigation menus.' ),
 394                              'type'        => 'boolean',
 395                          ),
 396                          'show_in_quick_edit' => array(
 397                              'description' => __( 'Whether to show the taxonomy in the quick/bulk edit panel.' ),
 398                              'type'        => 'boolean',
 399                          ),
 400  
 401                      ),
 402                  ),
 403              ),
 404          );
 405  
 406          $this->schema = $schema;
 407  
 408          return $this->add_additional_fields_schema( $this->schema );
 409      }
 410  
 411      /**
 412       * Retrieves the query params for collections.
 413       *
 414       * @since 4.7.0
 415       *
 416       * @return array Collection parameters.
 417       */
 418  	public function get_collection_params() {
 419          $new_params            = array();
 420          $new_params['context'] = $this->get_context_param( array( 'default' => 'view' ) );
 421          $new_params['type']    = array(
 422              'description' => __( 'Limit results to taxonomies associated with a specific post type.' ),
 423              'type'        => 'string',
 424          );
 425          return $new_params;
 426      }
 427  
 428  }


Generated: Sun Sep 19 01:00:04 2021 Cross-referenced by PHPXref 0.7.1