| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * REST API: WP_REST_Icon_Collections_Controller class 4 * 5 * @package WordPress 6 * @subpackage REST_API 7 * @since 7.1.0 8 */ 9 10 /** 11 * Controller which provides a REST endpoint for the editor to read registered 12 * icon collections. 13 * 14 * @since 7.1.0 15 * 16 * @see WP_REST_Controller 17 */ 18 class WP_REST_Icon_Collections_Controller extends WP_REST_Controller { 19 20 /** 21 * Constructs the controller. 22 * 23 * @since 7.1.0 24 */ 25 public function __construct() { 26 $this->namespace = 'wp/v2'; 27 $this->rest_base = 'icon-collections'; 28 } 29 30 /** 31 * Registers the routes for the objects of the controller. 32 * 33 * @since 7.1.0 34 */ 35 public function register_routes() { 36 register_rest_route( 37 $this->namespace, 38 '/' . $this->rest_base, 39 array( 40 array( 41 'methods' => WP_REST_Server::READABLE, 42 'callback' => array( $this, 'get_items' ), 43 'permission_callback' => array( $this, 'get_items_permissions_check' ), 44 'args' => $this->get_collection_params(), 45 ), 46 'schema' => array( $this, 'get_public_item_schema' ), 47 ) 48 ); 49 50 register_rest_route( 51 $this->namespace, 52 '/' . $this->rest_base . '/(?P<slug>[a-z0-9](?:[a-z0-9_-]*[a-z0-9])?)', 53 array( 54 'args' => array( 55 'slug' => array( 56 'description' => __( 'Icon collection slug.' ), 57 'type' => 'string', 58 ), 59 ), 60 array( 61 'methods' => WP_REST_Server::READABLE, 62 'callback' => array( $this, 'get_item' ), 63 'permission_callback' => array( $this, 'get_item_permissions_check' ), 64 'args' => array( 65 'context' => $this->get_context_param( array( 'default' => 'view' ) ), 66 ), 67 ), 68 'schema' => array( $this, 'get_public_item_schema' ), 69 ) 70 ); 71 } 72 73 /** 74 * Checks whether a given request has permission to read icon collections. 75 * 76 * @since 7.1.0 77 * 78 * @param WP_REST_Request $request Full details about the request. 79 * @return true|WP_Error True if the request has read access, WP_Error object otherwise. 80 */ 81 public function get_items_permissions_check( 82 // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable 83 $request 84 ) { 85 if ( current_user_can( 'edit_posts' ) ) { 86 return true; 87 } 88 89 foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) { 90 if ( current_user_can( $post_type->cap->edit_posts ) ) { 91 return true; 92 } 93 } 94 95 return new WP_Error( 96 'rest_cannot_view', 97 __( 'Sorry, you are not allowed to view the registered icon collections.' ), 98 array( 'status' => rest_authorization_required_code() ) 99 ); 100 } 101 102 /** 103 * Checks if a given request has access to read a specific icon collection. 104 * 105 * @since 7.1.0 106 * 107 * @param WP_REST_Request $request Full details about the request. 108 * @return true|WP_Error True if the request has read access for the item, WP_Error object otherwise. 109 */ 110 public function get_item_permissions_check( $request ) { 111 $check = $this->get_items_permissions_check( $request ); 112 if ( is_wp_error( $check ) ) { 113 return $check; 114 } 115 116 return true; 117 } 118 119 /** 120 * Retrieves all icon collections. 121 * 122 * @since 7.1.0 123 * @since 7.2.0 Non-public collections are omitted. 124 * 125 * @param WP_REST_Request $request Full details about the request. 126 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. 127 */ 128 public function get_items( $request ) { 129 $response = array(); 130 $collections = WP_Icon_Collections_Registry::get_instance()->get_all_registered(); 131 foreach ( $collections as $collection ) { 132 if ( ! $collection['public'] ) { 133 continue; 134 } 135 $prepared_collection = $this->prepare_item_for_response( $collection, $request ); 136 $response[] = $this->prepare_response_for_collection( $prepared_collection ); 137 } 138 return rest_ensure_response( $response ); 139 } 140 141 /** 142 * Retrieves a specific icon collection. 143 * 144 * @since 7.1.0 145 * 146 * @param WP_REST_Request $request Full details about the request. 147 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. 148 */ 149 public function get_item( $request ) { 150 $collection = $this->get_icon_collection( $request['slug'] ); 151 if ( is_wp_error( $collection ) ) { 152 return $collection; 153 } 154 155 $data = $this->prepare_item_for_response( $collection, $request ); 156 return rest_ensure_response( $data ); 157 } 158 159 /** 160 * Retrieves a specific icon collection from the registry. 161 * 162 * @since 7.1.0 163 * @since 7.2.0 Non-public collections are reported as not found. 164 * 165 * @param string $slug Icon collection slug. 166 * @return array|WP_Error Icon collection data on success, or WP_Error object on failure. 167 */ 168 public function get_icon_collection( $slug ) { 169 $registry = WP_Icon_Collections_Registry::get_instance(); 170 $collection = $registry->get_registered( $slug ); 171 172 if ( null === $collection || ! $collection['public'] ) { 173 return new WP_Error( 174 'rest_icon_collection_not_found', 175 sprintf( 176 /* translators: %s: Icon collection slug. */ 177 __( 'Icon collection not found: "%s".' ), 178 $slug 179 ), 180 array( 'status' => 404 ) 181 ); 182 } 183 184 return $collection; 185 } 186 187 /** 188 * Prepares a raw icon collection before it gets output in a REST API response. 189 * 190 * @since 7.1.0 191 * 192 * @param array $item Raw icon collection as registered, before any changes. 193 * @param WP_REST_Request $request Request object. 194 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. 195 */ 196 public function prepare_item_for_response( $item, $request ) { 197 $fields = $this->get_fields_for_response( $request ); 198 $keys = array( 199 'slug' => 'slug', 200 'label' => 'label', 201 'description' => 'description', 202 ); 203 $data = array(); 204 foreach ( $keys as $item_key => $rest_key ) { 205 if ( isset( $item[ $item_key ] ) && rest_is_field_included( $rest_key, $fields ) ) { 206 $data[ $rest_key ] = $item[ $item_key ]; 207 } 208 } 209 210 $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; 211 $data = $this->add_additional_fields_to_object( $data, $request ); 212 $data = $this->filter_response_by_context( $data, $context ); 213 return rest_ensure_response( $data ); 214 } 215 216 /** 217 * Retrieves the icon collection schema, conforming to JSON Schema. 218 * 219 * @since 7.1.0 220 * 221 * @return array Item schema data. 222 */ 223 public function get_item_schema() { 224 if ( $this->schema ) { 225 return $this->add_additional_fields_schema( $this->schema ); 226 } 227 228 $schema = array( 229 '$schema' => 'http://json-schema.org/draft-04/schema#', 230 'title' => 'icon-collection', 231 'type' => 'object', 232 'properties' => array( 233 'slug' => array( 234 'description' => __( 'The icon collection slug.' ), 235 'type' => 'string', 236 'readonly' => true, 237 'context' => array( 'view', 'edit', 'embed' ), 238 ), 239 'label' => array( 240 'description' => __( 'The icon collection label.' ), 241 'type' => 'string', 242 'readonly' => true, 243 'context' => array( 'view', 'edit', 'embed' ), 244 ), 245 'description' => array( 246 'description' => __( 'The icon collection description.' ), 247 'type' => 'string', 248 'readonly' => true, 249 'context' => array( 'view', 'edit', 'embed' ), 250 ), 251 ), 252 ); 253 254 $this->schema = $schema; 255 256 return $this->add_additional_fields_schema( $this->schema ); 257 } 258 259 /** 260 * Retrieves the query params for the icon collections collection. 261 * 262 * @since 7.1.0 263 * 264 * @return array Collection parameters. 265 */ 266 public function get_collection_params() { 267 $query_params = parent::get_collection_params(); 268 $query_params['context']['default'] = 'view'; 269 return $query_params; 270 } 271 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Sat Oct 24 08:20:34 2026 | Cross-referenced by PHPXref |