[ 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'] = $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          $schema = array(
 280              '$schema'    => 'http://json-schema.org/draft-04/schema#',
 281              'title'      => 'taxonomy',
 282              'type'       => 'object',
 283              'properties' => array(
 284                  'capabilities' => array(
 285                      'description' => __( 'All capabilities used by the taxonomy.' ),
 286                      'type'        => 'object',
 287                      'context'     => array( 'edit' ),
 288                      'readonly'    => true,
 289                  ),
 290                  'description'  => array(
 291                      'description' => __( 'A human-readable description of the taxonomy.' ),
 292                      'type'        => 'string',
 293                      'context'     => array( 'view', 'edit' ),
 294                      'readonly'    => true,
 295                  ),
 296                  'hierarchical' => array(
 297                      'description' => __( 'Whether or not the taxonomy should have children.' ),
 298                      'type'        => 'boolean',
 299                      'context'     => array( 'view', 'edit' ),
 300                      'readonly'    => true,
 301                  ),
 302                  'labels'       => array(
 303                      'description' => __( 'Human-readable labels for the taxonomy for various contexts.' ),
 304                      'type'        => 'object',
 305                      'context'     => array( 'edit' ),
 306                      'readonly'    => true,
 307                  ),
 308                  'name'         => array(
 309                      'description' => __( 'The title for the taxonomy.' ),
 310                      'type'        => 'string',
 311                      'context'     => array( 'view', 'edit', 'embed' ),
 312                      'readonly'    => true,
 313                  ),
 314                  'slug'         => array(
 315                      'description' => __( 'An alphanumeric identifier for the taxonomy.' ),
 316                      'type'        => 'string',
 317                      'context'     => array( 'view', 'edit', 'embed' ),
 318                      'readonly'    => true,
 319                  ),
 320                  'show_cloud'   => array(
 321                      'description' => __( 'Whether or not the term cloud should be displayed.' ),
 322                      'type'        => 'boolean',
 323                      'context'     => array( 'edit' ),
 324                      'readonly'    => true,
 325                  ),
 326                  'types'        => array(
 327                      'description' => __( 'Types associated with the taxonomy.' ),
 328                      'type'        => 'array',
 329                      'items'       => array(
 330                          'type' => 'string',
 331                      ),
 332                      'context'     => array( 'view', 'edit' ),
 333                      'readonly'    => true,
 334                  ),
 335                  'rest_base'    => array(
 336                      'description' => __( 'REST base route for the taxonomy.' ),
 337                      'type'        => 'string',
 338                      'context'     => array( 'view', 'edit', 'embed' ),
 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                      ),
 373                  ),
 374              ),
 375          );
 376          return $this->add_additional_fields_schema( $schema );
 377      }
 378  
 379      /**
 380       * Retrieves the query params for collections.
 381       *
 382       * @since 4.7.0
 383       *
 384       * @return array Collection parameters.
 385       */
 386  	public function get_collection_params() {
 387          $new_params            = array();
 388          $new_params['context'] = $this->get_context_param( array( 'default' => 'view' ) );
 389          $new_params['type']    = array(
 390              'description' => __( 'Limit results to taxonomies associated with a specific post type.' ),
 391              'type'        => 'string',
 392          );
 393          return $new_params;
 394      }
 395  
 396  }


Generated: Sat Jul 20 01:00:03 2019 Cross-referenced by PHPXref 0.7.1