[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/rest-api/endpoints/ -> class-wp-rest-block-pattern-categories-controller.php (source)

   1  <?php
   2  /**
   3   * REST API: WP_REST_Block_Pattern_Catergories_Controller class
   4   *
   5   * @package    WordPress
   6   * @subpackage REST_API
   7   * @since      6.0.0
   8   */
   9  
  10  /**
  11   * Core class used to access block pattern categories via the REST API.
  12   *
  13   * @since 6.0.0
  14   *
  15   * @see WP_REST_Controller
  16   */
  17  class WP_REST_Block_Pattern_Categories_Controller extends WP_REST_Controller {
  18  
  19      /**
  20       * Constructs the controller.
  21       *
  22       * @since 6.0.0
  23       */
  24  	public function __construct() {
  25          $this->namespace = 'wp/v2';
  26          $this->rest_base = 'block-patterns/categories';
  27      }
  28  
  29      /**
  30       * Registers the routes for the objects of the controller.
  31       *
  32       * @since 6.0.0
  33       */
  34  	public function register_routes() {
  35          register_rest_route(
  36              $this->namespace,
  37              '/' . $this->rest_base,
  38              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                  ),
  44                  'schema' => array( $this, 'get_public_item_schema' ),
  45              )
  46          );
  47      }
  48  
  49      /**
  50       * Checks whether a given request has permission to read block patterns.
  51       *
  52       * @since 6.0.0
  53       *
  54       * @param WP_REST_Request $request Full details about the request.
  55       * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
  56       */
  57  	public function get_items_permissions_check( $request ) {
  58          if ( current_user_can( 'edit_posts' ) ) {
  59              return true;
  60          }
  61  
  62          foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) {
  63              if ( current_user_can( $post_type->cap->edit_posts ) ) {
  64                  return true;
  65              }
  66          }
  67  
  68          return new WP_Error(
  69              'rest_cannot_view',
  70              __( 'Sorry, you are not allowed to view the registered block pattern categories.' ),
  71              array( 'status' => rest_authorization_required_code() )
  72          );
  73      }
  74  
  75      /**
  76       * Retrieves all block pattern categories.
  77       *
  78       * @since 6.0.0
  79       *
  80       * @param WP_REST_Request $request Full details about the request.
  81       * @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure.
  82       */
  83  	public function get_items( $request ) {
  84          $response   = array();
  85          $categories = WP_Block_Pattern_Categories_Registry::get_instance()->get_all_registered();
  86          foreach ( $categories as $category ) {
  87              $prepared_category = $this->prepare_item_for_response( $category, $request );
  88              $response[]        = $this->prepare_response_for_collection( $prepared_category );
  89          }
  90  
  91          return rest_ensure_response( $response );
  92      }
  93  
  94      /**
  95       * Prepare a raw block pattern category before it gets output in a REST API response.
  96       *
  97       * @since 6.0.0
  98       *
  99       * @param array           $item    Raw category as registered, before any changes.
 100       * @param WP_REST_Request $request Request object.
 101       * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
 102       */
 103  	public function prepare_item_for_response( $item, $request ) {
 104          $fields = $this->get_fields_for_response( $request );
 105          $keys   = array( 'name', 'label' );
 106          $data   = array();
 107          foreach ( $keys as $key ) {
 108              if ( rest_is_field_included( $key, $fields ) ) {
 109                  $data[ $key ] = $item[ $key ];
 110              }
 111          }
 112  
 113          $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
 114          $data    = $this->add_additional_fields_to_object( $data, $request );
 115          $data    = $this->filter_response_by_context( $data, $context );
 116  
 117          return rest_ensure_response( $data );
 118      }
 119  
 120      /**
 121       * Retrieves the block pattern category schema, conforming to JSON Schema.
 122       *
 123       * @since 6.0.0
 124       *
 125       * @return array Item schema data.
 126       */
 127  	public function get_item_schema() {
 128          $schema = array(
 129              '$schema'    => 'http://json-schema.org/draft-04/schema#',
 130              'title'      => 'block-pattern-category',
 131              'type'       => 'object',
 132              'properties' => array(
 133                  'name'  => array(
 134                      'description' => __( 'The category name.' ),
 135                      'type'        => 'string',
 136                      'readonly'    => true,
 137                      'context'     => array( 'view', 'edit', 'embed' ),
 138                  ),
 139                  'label' => array(
 140                      'description' => __( 'The category label, in human readable format.' ),
 141                      'type'        => 'string',
 142                      'readonly'    => true,
 143                      'context'     => array( 'view', 'edit', 'embed' ),
 144                  ),
 145              ),
 146          );
 147  
 148          return $this->add_additional_fields_schema( $schema );
 149      }
 150  }


Generated: Fri Dec 6 01:00:02 2024 Cross-referenced by PHPXref 0.7.1