home *** CD-ROM | disk | FTP | other *** search
/ HTML Examples / WP.iso / wordpress / wp-includes / rest-api / endpoints / class-wp-rest-taxonomies-controller.php < prev    next >
Encoding:
PHP Script  |  2017-10-04  |  9.7 KB  |  315 lines

  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( $this->namespace, '/' . $this->rest_base, array(
  39.             array(
  40.                 'methods'         => WP_REST_Server::READABLE,
  41.                 'callback'        => array( $this, 'get_items' ),
  42.                 'permission_callback' => array( $this, 'get_items_permissions_check' ),
  43.                 'args'            => $this->get_collection_params(),
  44.             ),
  45.             'schema' => array( $this, 'get_public_item_schema' ),
  46.         ) );
  47.  
  48.         register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<taxonomy>[\w-]+)', array(
  49.             'args' => array(
  50.                 'taxonomy' => array(
  51.                     'description'  => __( 'An alphanumeric identifier for the taxonomy.' ),
  52.                     'type'         => 'string',
  53.                 ),
  54.             ),
  55.             array(
  56.                 'methods'         => WP_REST_Server::READABLE,
  57.                 'callback'        => array( $this, 'get_item' ),
  58.                 'permission_callback' => array( $this, 'get_item_permissions_check' ),
  59.                 'args'            => array(
  60.                     'context'     => $this->get_context_param( array( 'default' => 'view' ) ),
  61.                 ),
  62.             ),
  63.             'schema' => array( $this, 'get_public_item_schema' ),
  64.         ) );
  65.     }
  66.  
  67.     /**
  68.      * Checks whether a given request has permission to read taxonomies.
  69.      *
  70.      * @since 4.7.0
  71.      *
  72.      * @param WP_REST_Request $request Full details about the request.
  73.      * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
  74.      */
  75.     public function get_items_permissions_check( $request ) {
  76.         if ( 'edit' === $request['context'] ) {
  77.             if ( ! empty( $request['type'] ) ) {
  78.                 $taxonomies = get_object_taxonomies( $request['type'], 'objects' );
  79.             } else {
  80.                 $taxonomies = get_taxonomies( '', 'objects' );
  81.             }
  82.             foreach ( $taxonomies as $taxonomy ) {
  83.                 if ( ! empty( $taxonomy->show_in_rest ) && current_user_can( $taxonomy->cap->manage_terms ) ) {
  84.                     return true;
  85.                 }
  86.             }
  87.             return new WP_Error( 'rest_cannot_view', __( 'Sorry, you are not allowed to manage terms in this taxonomy.' ), array( 'status' => rest_authorization_required_code() ) );
  88.         }
  89.         return true;
  90.     }
  91.  
  92.     /**
  93.      * Retrieves all public taxonomies.
  94.      *
  95.      * @since 4.7.0
  96.      *
  97.      * @param WP_REST_Request $request Full details about the request.
  98.      * @return WP_REST_Response Response object on success, or WP_Error object on failure.
  99.      */
  100.     public function get_items( $request ) {
  101.  
  102.         // Retrieve the list of registered collection query parameters.
  103.         $registered = $this->get_collection_params();
  104.  
  105.         if ( isset( $registered['type'] ) && ! empty( $request['type'] ) ) {
  106.             $taxonomies = get_object_taxonomies( $request['type'], 'objects' );
  107.         } else {
  108.             $taxonomies = get_taxonomies( '', 'objects' );
  109.         }
  110.         $data = array();
  111.         foreach ( $taxonomies as $tax_type => $value ) {
  112.             if ( empty( $value->show_in_rest ) || ( 'edit' === $request['context'] && ! current_user_can( $value->cap->manage_terms ) ) ) {
  113.                 continue;
  114.             }
  115.             $tax = $this->prepare_item_for_response( $value, $request );
  116.             $tax = $this->prepare_response_for_collection( $tax );
  117.             $data[ $tax_type ] = $tax;
  118.         }
  119.  
  120.         if ( empty( $data ) ) {
  121.             // Response should still be returned as a JSON object when it is empty.
  122.             $data = (object) $data;
  123.         }
  124.  
  125.         return rest_ensure_response( $data );
  126.     }
  127.  
  128.     /**
  129.      * Checks if a given request has access to a taxonomy.
  130.      *
  131.      * @since 4.7.0
  132.      *
  133.      * @param  WP_REST_Request $request Full details about the request.
  134.      * @return true|WP_Error True if the request has read access for the item, otherwise false or WP_Error object.
  135.      */
  136.     public function get_item_permissions_check( $request ) {
  137.  
  138.         $tax_obj = get_taxonomy( $request['taxonomy'] );
  139.  
  140.         if ( $tax_obj ) {
  141.             if ( empty( $tax_obj->show_in_rest ) ) {
  142.                 return false;
  143.             }
  144.             if ( 'edit' === $request['context'] && ! current_user_can( $tax_obj->cap->manage_terms ) ) {
  145.                 return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to manage terms in this taxonomy.' ), array( 'status' => rest_authorization_required_code() ) );
  146.             }
  147.         }
  148.  
  149.         return true;
  150.     }
  151.  
  152.     /**
  153.      * Retrieves a specific taxonomy.
  154.      *
  155.      * @since 4.7.0
  156.      *
  157.      * @param WP_REST_Request $request Full details about the request.
  158.      * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
  159.      */
  160.     public function get_item( $request ) {
  161.         $tax_obj = get_taxonomy( $request['taxonomy'] );
  162.         if ( empty( $tax_obj ) ) {
  163.             return new WP_Error( 'rest_taxonomy_invalid', __( 'Invalid taxonomy.' ), array( 'status' => 404 ) );
  164.         }
  165.         $data = $this->prepare_item_for_response( $tax_obj, $request );
  166.         return rest_ensure_response( $data );
  167.     }
  168.  
  169.     /**
  170.      * Prepares a taxonomy object for serialization.
  171.      *
  172.      * @since 4.7.0
  173.      *
  174.      * @param stdClass        $taxonomy Taxonomy data.
  175.      * @param WP_REST_Request $request  Full details about the request.
  176.      * @return WP_REST_Response Response object.
  177.      */
  178.     public function prepare_item_for_response( $taxonomy, $request ) {
  179.         $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
  180.         $data = array(
  181.             'name'         => $taxonomy->label,
  182.             'slug'         => $taxonomy->name,
  183.             'capabilities' => $taxonomy->cap,
  184.             'description'  => $taxonomy->description,
  185.             'labels'       => $taxonomy->labels,
  186.             'types'        => $taxonomy->object_type,
  187.             'show_cloud'   => $taxonomy->show_tagcloud,
  188.             'hierarchical' => $taxonomy->hierarchical,
  189.             'rest_base'    => $base,
  190.         );
  191.  
  192.         $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
  193.         $data = $this->add_additional_fields_to_object( $data, $request );
  194.         $data = $this->filter_response_by_context( $data, $context );
  195.  
  196.         // Wrap the data in a response object.
  197.         $response = rest_ensure_response( $data );
  198.  
  199.         $response->add_links( array(
  200.             'collection'                => array(
  201.                 'href'                  => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
  202.             ),
  203.             'https://api.w.org/items'   => array(
  204.                 'href'                  => rest_url( sprintf( 'wp/v2/%s', $base ) ),
  205.             ),
  206.         ) );
  207.  
  208.         /**
  209.          * Filters a taxonomy returned from the REST API.
  210.          *
  211.          * Allows modification of the taxonomy data right before it is returned.
  212.          *
  213.          * @since 4.7.0
  214.          *
  215.          * @param WP_REST_Response $response The response object.
  216.          * @param object           $item     The original taxonomy object.
  217.          * @param WP_REST_Request  $request  Request used to generate the response.
  218.          */
  219.         return apply_filters( 'rest_prepare_taxonomy', $response, $taxonomy, $request );
  220.     }
  221.  
  222.     /**
  223.      * Retrieves the taxonomy's schema, conforming to JSON Schema.
  224.      *
  225.      * @since 4.7.0
  226.      *
  227.      * @return array Item schema data.
  228.      */
  229.     public function get_item_schema() {
  230.         $schema = array(
  231.             '$schema'              => 'http://json-schema.org/draft-04/schema#',
  232.             'title'                => 'taxonomy',
  233.             'type'                 => 'object',
  234.             'properties'           => array(
  235.                 'capabilities'     => array(
  236.                     'description'  => __( 'All capabilities used by the taxonomy.' ),
  237.                     'type'         => 'object',
  238.                     'context'      => array( 'edit' ),
  239.                     'readonly'     => true,
  240.                 ),
  241.                 'description'      => array(
  242.                     'description'  => __( 'A human-readable description of the taxonomy.' ),
  243.                     'type'         => 'string',
  244.                     'context'      => array( 'view', 'edit' ),
  245.                     'readonly'     => true,
  246.                 ),
  247.                 'hierarchical'     => array(
  248.                     'description'  => __( 'Whether or not the taxonomy should have children.' ),
  249.                     'type'         => 'boolean',
  250.                     'context'      => array( 'view', 'edit' ),
  251.                     'readonly'     => true,
  252.                 ),
  253.                 'labels'           => array(
  254.                     'description'  => __( 'Human-readable labels for the taxonomy for various contexts.' ),
  255.                     'type'         => 'object',
  256.                     'context'      => array( 'edit' ),
  257.                     'readonly'     => true,
  258.                 ),
  259.                 'name'             => array(
  260.                     'description'  => __( 'The title for the taxonomy.' ),
  261.                     'type'         => 'string',
  262.                     'context'      => array( 'view', 'edit', 'embed' ),
  263.                     'readonly'     => true,
  264.                 ),
  265.                 'slug'             => array(
  266.                     'description'  => __( 'An alphanumeric identifier for the taxonomy.' ),
  267.                     'type'         => 'string',
  268.                     'context'      => array( 'view', 'edit', 'embed' ),
  269.                     'readonly'     => true,
  270.                 ),
  271.                 'show_cloud'       => array(
  272.                     'description'  => __( 'Whether or not the term cloud should be displayed.' ),
  273.                     'type'         => 'boolean',
  274.                     'context'      => array( 'edit' ),
  275.                     'readonly'     => true,
  276.                 ),
  277.                 'types'            => array(
  278.                     'description'  => __( 'Types associated with the taxonomy.' ),
  279.                     'type'         => 'array',
  280.                     'items'        => array(
  281.                         'type' => 'string',
  282.                     ),
  283.                     'context'      => array( 'view', 'edit' ),
  284.                     'readonly'     => true,
  285.                 ),
  286.                 'rest_base'            => array(
  287.                     'description'  => __( 'REST base route for the taxonomy.' ),
  288.                     'type'         => 'string',
  289.                     'context'      => array( 'view', 'edit', 'embed' ),
  290.                     'readonly'     => true,
  291.                 ),
  292.             ),
  293.         );
  294.         return $this->add_additional_fields_schema( $schema );
  295.     }
  296.  
  297.     /**
  298.      * Retrieves the query params for collections.
  299.      *
  300.      * @since 4.7.0
  301.      *
  302.      * @return array Collection parameters.
  303.      */
  304.     public function get_collection_params() {
  305.         $new_params = array();
  306.         $new_params['context'] = $this->get_context_param( array( 'default' => 'view' ) );
  307.         $new_params['type'] = array(
  308.             'description'  => __( 'Limit results to taxonomies associated with a specific post type.' ),
  309.             'type'         => 'string',
  310.         );
  311.         return $new_params;
  312.     }
  313.  
  314. }
  315.