[ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * REST API: WP_REST_Block_Pattern_Categories_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_REST_Response|WP_Error 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', 'description' ); 106 $data = array(); 107 foreach ( $keys as $key ) { 108 if ( isset( $item[ $key ] ) && 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 if ( $this->schema ) { 129 return $this->add_additional_fields_schema( $this->schema ); 130 } 131 132 $schema = array( 133 '$schema' => 'http://json-schema.org/draft-04/schema#', 134 'title' => 'block-pattern-category', 135 'type' => 'object', 136 'properties' => array( 137 'name' => array( 138 'description' => __( 'The category name.' ), 139 'type' => 'string', 140 'readonly' => true, 141 'context' => array( 'view', 'edit', 'embed' ), 142 ), 143 'label' => array( 144 'description' => __( 'The category label, in human readable format.' ), 145 'type' => 'string', 146 'readonly' => true, 147 'context' => array( 'view', 'edit', 'embed' ), 148 ), 149 'description' => array( 150 'description' => __( 'The category description, in human readable format.' ), 151 'type' => 'string', 152 'readonly' => true, 153 'context' => array( 'view', 'edit', 'embed' ), 154 ), 155 ), 156 ); 157 158 $this->schema = $schema; 159 160 return $this->add_additional_fields_schema( $this->schema ); 161 } 162 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Thu Nov 21 08:20:01 2024 | Cross-referenced by PHPXref |