[ 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 the objects of the controller.
  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              foreach ( $taxonomies as $taxonomy ) {
  91                  if ( ! empty( $taxonomy->show_in_rest ) && current_user_can( $taxonomy->cap->assign_terms ) ) {
  92                      return true;
  93                  }
  94              }
  95              return new WP_Error( 'rest_cannot_view', __( 'Sorry, you are not allowed to manage terms in this taxonomy.' ), array( 'status' => rest_authorization_required_code() ) );
  96          }
  97          return true;
  98      }
  99  
 100      /**
 101       * Retrieves all public taxonomies.
 102       *
 103       * @since 4.7.0
 104       *
 105       * @param WP_REST_Request $request Full details about the request.
 106       * @return WP_REST_Response Response object on success, or WP_Error object on failure.
 107       */
 108  	public function get_items( $request ) {
 109  
 110          // Retrieve the list of registered collection query parameters.
 111          $registered = $this->get_collection_params();
 112  
 113          if ( isset( $registered['type'] ) && ! empty( $request['type'] ) ) {
 114              $taxonomies = get_object_taxonomies( $request['type'], 'objects' );
 115          } else {
 116              $taxonomies = get_taxonomies( '', 'objects' );
 117          }
 118          $data = array();
 119          foreach ( $taxonomies as $tax_type => $value ) {
 120              if ( empty( $value->show_in_rest ) || ( 'edit' === $request['context'] && ! current_user_can( $value->cap->assign_terms ) ) ) {
 121                  continue;
 122              }
 123              $tax               = $this->prepare_item_for_response( $value, $request );
 124              $tax               = $this->prepare_response_for_collection( $tax );
 125              $data[ $tax_type ] = $tax;
 126          }
 127  
 128          if ( empty( $data ) ) {
 129              // Response should still be returned as a JSON object when it is empty.
 130              $data = (object) $data;
 131          }
 132  
 133          return rest_ensure_response( $data );
 134      }
 135  
 136      /**
 137       * Checks if a given request has access to a taxonomy.
 138       *
 139       * @since 4.7.0
 140       *
 141       * @param WP_REST_Request $request Full details about the request.
 142       * @return true|WP_Error True if the request has read access for the item, otherwise false or WP_Error object.
 143       */
 144  	public function get_item_permissions_check( $request ) {
 145  
 146          $tax_obj = get_taxonomy( $request['taxonomy'] );
 147  
 148          if ( $tax_obj ) {
 149              if ( empty( $tax_obj->show_in_rest ) ) {
 150                  return false;
 151              }
 152              if ( 'edit' === $request['context'] && ! current_user_can( $tax_obj->cap->assign_terms ) ) {
 153                  return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to manage terms in this taxonomy.' ), array( 'status' => rest_authorization_required_code() ) );
 154              }
 155          }
 156  
 157          return true;
 158      }
 159  
 160      /**
 161       * Retrieves a specific taxonomy.
 162       *
 163       * @since 4.7.0
 164       *
 165       * @param WP_REST_Request $request Full details about the request.
 166       * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
 167       */
 168  	public function get_item( $request ) {
 169          $tax_obj = get_taxonomy( $request['taxonomy'] );
 170          if ( empty( $tax_obj ) ) {
 171              return new WP_Error( 'rest_taxonomy_invalid', __( 'Invalid taxonomy.' ), array( 'status' => 404 ) );
 172          }
 173          $data = $this->prepare_item_for_response( $tax_obj, $request );
 174          return rest_ensure_response( $data );
 175      }
 176  
 177      /**
 178       * Prepares a taxonomy object for serialization.
 179       *
 180       * @since 4.7.0
 181       *
 182       * @param stdClass        $taxonomy Taxonomy data.
 183       * @param WP_REST_Request $request  Full details about the request.
 184       * @return WP_REST_Response Response object.
 185       */
 186  	public function prepare_item_for_response( $taxonomy, $request ) {
 187          $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
 188  
 189          $fields = $this->get_fields_for_response( $request );
 190          $data   = array();
 191  
 192          if ( in_array( 'name', $fields, true ) ) {
 193              $data['name'] = $taxonomy->label;
 194          }
 195  
 196          if ( in_array( 'slug', $fields, true ) ) {
 197              $data['slug'] = $taxonomy->name;
 198          }
 199  
 200          if ( in_array( 'capabilities', $fields, true ) ) {
 201              $data['capabilities'] = $taxonomy->cap;
 202          }
 203  
 204          if ( in_array( 'description', $fields, true ) ) {
 205              $data['description'] = $taxonomy->description;
 206          }
 207  
 208          if ( in_array( 'labels', $fields, true ) ) {
 209              $data['labels'] = $taxonomy->labels;
 210          }
 211  
 212          if ( in_array( 'types', $fields, true ) ) {
 213              $data['types'] = array_values( $taxonomy->object_type );
 214          }
 215  
 216          if ( in_array( 'show_cloud', $fields, true ) ) {
 217              $data['show_cloud'] = $taxonomy->show_tagcloud;
 218          }
 219  
 220          if ( in_array( 'hierarchical', $fields, true ) ) {
 221              $data['hierarchical'] = $taxonomy->hierarchical;
 222          }
 223  
 224          if ( in_array( 'rest_base', $fields, true ) ) {
 225              $data['rest_base'] = $base;
 226          }
 227  
 228          if ( in_array( 'visibility', $fields, true ) ) {
 229              $data['visibility'] = array(
 230                  'public'             => (bool) $taxonomy->public,
 231                  'publicly_queryable' => (bool) $taxonomy->publicly_queryable,
 232                  'show_admin_column'  => (bool) $taxonomy->show_admin_column,
 233                  'show_in_nav_menus'  => (bool) $taxonomy->show_in_nav_menus,
 234                  'show_in_quick_edit' => (bool) $taxonomy->show_in_quick_edit,
 235                  'show_ui'            => (bool) $taxonomy->show_ui,
 236              );
 237          }
 238  
 239          $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
 240          $data    = $this->add_additional_fields_to_object( $data, $request );
 241          $data    = $this->filter_response_by_context( $data, $context );
 242  
 243          // Wrap the data in a response object.
 244          $response = rest_ensure_response( $data );
 245  
 246          $response->add_links(
 247              array(
 248                  'collection'              => array(
 249                      'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
 250                  ),
 251                  'https://api.w.org/items' => array(
 252                      'href' => rest_url( sprintf( 'wp/v2/%s', $base ) ),
 253                  ),
 254              )
 255          );
 256  
 257          /**
 258           * Filters a taxonomy returned from the REST API.
 259           *
 260           * Allows modification of the taxonomy data right before it is returned.
 261           *
 262           * @since 4.7.0
 263           *
 264           * @param WP_REST_Response $response The response object.
 265           * @param object           $item     The original taxonomy object.
 266           * @param WP_REST_Request  $request  Request used to generate the response.
 267           */
 268          return apply_filters( 'rest_prepare_taxonomy', $response, $taxonomy, $request );
 269      }
 270  
 271      /**
 272       * Retrieves the taxonomy's schema, conforming to JSON Schema.
 273       *
 274       * @since 4.7.0
 275       *
 276       * @return array Item schema data.
 277       */
 278  	public function get_item_schema() {
 279          if ( $this->schema ) {
 280              return $this->add_additional_fields_schema( $this->schema );
 281          }
 282  
 283          $schema = array(
 284              '$schema'    => 'http://json-schema.org/draft-04/schema#',
 285              'title'      => 'taxonomy',
 286              'type'       => 'object',
 287              'properties' => array(
 288                  'capabilities' => array(
 289                      'description' => __( 'All capabilities used by the taxonomy.' ),
 290                      'type'        => 'object',
 291                      'context'     => array( 'edit' ),
 292                      'readonly'    => true,
 293                  ),
 294                  'description'  => array(
 295                      'description' => __( 'A human-readable description of the taxonomy.' ),
 296                      'type'        => 'string',
 297                      'context'     => array( 'view', 'edit' ),
 298                      'readonly'    => true,
 299                  ),
 300                  'hierarchical' => array(
 301                      'description' => __( 'Whether or not the taxonomy should have children.' ),
 302                      'type'        => 'boolean',
 303                      'context'     => array( 'view', 'edit' ),
 304                      'readonly'    => true,
 305                  ),
 306                  'labels'       => array(
 307                      'description' => __( 'Human-readable labels for the taxonomy for various contexts.' ),
 308                      'type'        => 'object',
 309                      'context'     => array( 'edit' ),
 310                      'readonly'    => true,
 311                  ),
 312                  'name'         => array(
 313                      'description' => __( 'The title for the taxonomy.' ),
 314                      'type'        => 'string',
 315                      'context'     => array( 'view', 'edit', 'embed' ),
 316                      'readonly'    => true,
 317                  ),
 318                  'slug'         => array(
 319                      'description' => __( 'An alphanumeric identifier for the taxonomy.' ),
 320                      'type'        => 'string',
 321                      'context'     => array( 'view', 'edit', 'embed' ),
 322                      'readonly'    => true,
 323                  ),
 324                  'show_cloud'   => array(
 325                      'description' => __( 'Whether or not the term cloud should be displayed.' ),
 326                      'type'        => 'boolean',
 327                      'context'     => array( 'edit' ),
 328                      'readonly'    => true,
 329                  ),
 330                  'types'        => array(
 331                      'description' => __( 'Types associated with the taxonomy.' ),
 332                      'type'        => 'array',
 333                      'items'       => array(
 334                          'type' => 'string',
 335                      ),
 336                      'context'     => array( 'view', 'edit' ),
 337                      'readonly'    => true,
 338                  ),
 339                  'rest_base'    => array(
 340                      'description' => __( 'REST base route for the taxonomy.' ),
 341                      'type'        => 'string',
 342                      'context'     => array( 'view', 'edit', 'embed' ),
 343                      'readonly'    => true,
 344                  ),
 345                  'visibility'   => array(
 346                      'description' => __( 'The visibility settings for the taxonomy.' ),
 347                      'type'        => 'object',
 348                      'context'     => array( 'edit' ),
 349                      'readonly'    => true,
 350                      'properties'  => array(
 351                          'public'             => array(
 352                              'description' => __( 'Whether a taxonomy is intended for use publicly either via the admin interface or by front-end users.' ),
 353                              'type'        => 'boolean',
 354                          ),
 355                          'publicly_queryable' => array(
 356                              'description' => __( 'Whether the taxonomy is publicly queryable.' ),
 357                              'type'        => 'boolean',
 358                          ),
 359                          'show_ui'            => array(
 360                              'description' => __( 'Whether to generate a default UI for managing this taxonomy.' ),
 361                              'type'        => 'boolean',
 362                          ),
 363                          'show_admin_column'  => array(
 364                              'description' => __( 'Whether to allow automatic creation of taxonomy columns on associated post-types table.' ),
 365                              'type'        => 'boolean',
 366                          ),
 367                          'show_in_nav_menus'  => array(
 368                              'description' => __( 'Whether to make the taxonomy available for selection in navigation menus.' ),
 369                              'type'        => 'boolean',
 370                          ),
 371                          'show_in_quick_edit' => array(
 372                              'description' => __( 'Whether to show the taxonomy in the quick/bulk edit panel.' ),
 373                              'type'        => 'boolean',
 374                          ),
 375  
 376                      ),
 377                  ),
 378              ),
 379          );
 380  
 381          $this->schema = $schema;
 382          return $this->add_additional_fields_schema( $this->schema );
 383      }
 384  
 385      /**
 386       * Retrieves the query params for collections.
 387       *
 388       * @since 4.7.0
 389       *
 390       * @return array Collection parameters.
 391       */
 392  	public function get_collection_params() {
 393          $new_params            = array();
 394          $new_params['context'] = $this->get_context_param( array( 'default' => 'view' ) );
 395          $new_params['type']    = array(
 396              'description' => __( 'Limit results to taxonomies associated with a specific post type.' ),
 397              'type'        => 'string',
 398          );
 399          return $new_params;
 400      }
 401  
 402  }


Generated: Sat Sep 21 01:00:03 2019 Cross-referenced by PHPXref 0.7.1