| [ 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 * 124 * @param WP_REST_Request $request Full details about the request. 125 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. 126 */ 127 public function get_items( $request ) { 128 $response = array(); 129 $collections = WP_Icon_Collections_Registry::get_instance()->get_all_registered(); 130 foreach ( $collections as $collection ) { 131 $prepared_collection = $this->prepare_item_for_response( $collection, $request ); 132 $response[] = $this->prepare_response_for_collection( $prepared_collection ); 133 } 134 return rest_ensure_response( $response ); 135 } 136 137 /** 138 * Retrieves a specific icon collection. 139 * 140 * @since 7.1.0 141 * 142 * @param WP_REST_Request $request Full details about the request. 143 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. 144 */ 145 public function get_item( $request ) { 146 $collection = $this->get_icon_collection( $request['slug'] ); 147 if ( is_wp_error( $collection ) ) { 148 return $collection; 149 } 150 151 $data = $this->prepare_item_for_response( $collection, $request ); 152 return rest_ensure_response( $data ); 153 } 154 155 /** 156 * Retrieves a specific icon collection from the registry. 157 * 158 * @since 7.1.0 159 * 160 * @param string $slug Icon collection slug. 161 * @return array|WP_Error Icon collection data on success, or WP_Error object on failure. 162 */ 163 public function get_icon_collection( $slug ) { 164 $registry = WP_Icon_Collections_Registry::get_instance(); 165 $collection = $registry->get_registered( $slug ); 166 167 if ( null === $collection ) { 168 return new WP_Error( 169 'rest_icon_collection_not_found', 170 sprintf( 171 /* translators: %s: Icon collection slug. */ 172 __( 'Icon collection not found: "%s".' ), 173 $slug 174 ), 175 array( 'status' => 404 ) 176 ); 177 } 178 179 return $collection; 180 } 181 182 /** 183 * Prepares a raw icon collection before it gets output in a REST API response. 184 * 185 * @since 7.1.0 186 * 187 * @param array $item Raw icon collection as registered, before any changes. 188 * @param WP_REST_Request $request Request object. 189 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. 190 */ 191 public function prepare_item_for_response( $item, $request ) { 192 $fields = $this->get_fields_for_response( $request ); 193 $keys = array( 194 'slug' => 'slug', 195 'label' => 'label', 196 'description' => 'description', 197 ); 198 $data = array(); 199 foreach ( $keys as $item_key => $rest_key ) { 200 if ( isset( $item[ $item_key ] ) && rest_is_field_included( $rest_key, $fields ) ) { 201 $data[ $rest_key ] = $item[ $item_key ]; 202 } 203 } 204 205 $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; 206 $data = $this->add_additional_fields_to_object( $data, $request ); 207 $data = $this->filter_response_by_context( $data, $context ); 208 return rest_ensure_response( $data ); 209 } 210 211 /** 212 * Retrieves the icon collection schema, conforming to JSON Schema. 213 * 214 * @since 7.1.0 215 * 216 * @return array Item schema data. 217 */ 218 public function get_item_schema() { 219 if ( $this->schema ) { 220 return $this->add_additional_fields_schema( $this->schema ); 221 } 222 223 $schema = array( 224 '$schema' => 'http://json-schema.org/draft-04/schema#', 225 'title' => 'icon-collection', 226 'type' => 'object', 227 'properties' => array( 228 'slug' => array( 229 'description' => __( 'The icon collection slug.' ), 230 'type' => 'string', 231 'readonly' => true, 232 'context' => array( 'view', 'edit', 'embed' ), 233 ), 234 'label' => array( 235 'description' => __( 'The icon collection label.' ), 236 'type' => 'string', 237 'readonly' => true, 238 'context' => array( 'view', 'edit', 'embed' ), 239 ), 240 'description' => array( 241 'description' => __( 'The icon collection description.' ), 242 'type' => 'string', 243 'readonly' => true, 244 'context' => array( 'view', 'edit', 'embed' ), 245 ), 246 ), 247 ); 248 249 $this->schema = $schema; 250 251 return $this->add_additional_fields_schema( $this->schema ); 252 } 253 254 /** 255 * Retrieves the query params for the icon collections collection. 256 * 257 * @since 7.1.0 258 * 259 * @return array Collection parameters. 260 */ 261 public function get_collection_params() { 262 $query_params = parent::get_collection_params(); 263 $query_params['context']['default'] = 'view'; 264 return $query_params; 265 } 266 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Wed Jul 15 08:20:16 2026 | Cross-referenced by PHPXref |