- Timestamp:
- 07/15/2026 04:25:35 AM (8 weeks ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-icons-controller.php
r62435 r62748 1 1 <?php 2 2 3 /** 3 4 * REST API: WP_REST_Icons_Controller class … … 10 11 /** 11 12 * Controller which provides a REST endpoint for the editor to read registered 12 * icons. For the time being, only core icons are available, which are defined 13 * in a single manifest file (wp-includes/assets/icon-library-manifest.php). 14 * Icons are comprised of their SVG source, a name and a translatable label. 13 * icons. Icons are grouped into collections (the default one being `core`). 15 14 * 16 15 * @since 7.0.0 … … 34 33 * 35 34 * @since 7.0.0 35 * @since 7.1.0 Added the `/icons/<collection>` collection-scoped route. 36 36 */ 37 37 public function register_routes() { … … 52 52 register_rest_route( 53 53 $this->namespace, 54 '/' . $this->rest_base . '/(?P<name>[a-z][a-z0-9-]*/[a-z][a-z0-9-]*)', 54 '/' . $this->rest_base . '/(?P<collection>[a-z0-9](?:[a-z0-9_-]*[a-z0-9])?)', 55 array( 56 'args' => array( 57 'collection' => array( 58 'description' => __( 'Icon collection slug.' ), 59 'type' => 'string', 60 ), 61 ), 62 array( 63 'methods' => WP_REST_Server::READABLE, 64 'callback' => array( $this, 'get_items' ), 65 'permission_callback' => array( $this, 'get_items_permissions_check' ), 66 'args' => $this->get_collection_params(), 67 ), 68 'schema' => array( $this, 'get_public_item_schema' ), 69 ) 70 ); 71 72 register_rest_route( 73 $this->namespace, 74 '/' . $this->rest_base . '/(?P<name>[a-z0-9](?:[a-z0-9_-]*[a-z0-9])?/[a-z0-9](?:[a-z0-9_-]*[a-z0-9])?)', 55 75 array( 56 76 'args' => array( … … 120 140 121 141 /** 122 * Retrieves all icons. 123 * 124 * @since 7.0.0 142 * Retrieves all icons, optionally scoped to a collection. 143 * 144 * @since 7.0.0 145 * @since 7.1.0 Supports filtering by collection. 125 146 * 126 147 * @param WP_REST_Request $request Full details about the request. … … 128 149 */ 129 150 public function get_items( $request ) { 151 $collection = $request->get_param( 'collection' ); 152 153 if ( null !== $collection && ! WP_Icon_Collections_Registry::get_instance()->is_registered( $collection ) ) { 154 return new WP_Error( 155 'rest_icon_collection_not_found', 156 sprintf( 157 /* translators: %s: Icon collection slug. */ 158 __( 'Icon collection not found: "%s".' ), 159 $collection 160 ), 161 array( 'status' => 404 ) 162 ); 163 } 164 130 165 $response = array(); 131 166 $search = $request->get_param( 'search' ); 132 167 $icons = WP_Icons_Registry::get_instance()->get_registered_icons( $search ); 168 133 169 foreach ( $icons as $icon ) { 170 if ( null !== $collection && ( ! isset( $icon['collection'] ) || $icon['collection'] !== $collection ) ) { 171 continue; 172 } 134 173 $prepared_icon = $this->prepare_item_for_response( $icon, $request ); 135 174 $response[] = $this->prepare_response_for_collection( $prepared_icon ); … … 187 226 * 188 227 * @since 7.0.0 228 * @since 7.1.0 Added the `collection` field. 189 229 * 190 230 * @param array $item Raw icon as registered, before any changes. … … 195 235 $fields = $this->get_fields_for_response( $request ); 196 236 $keys = array( 197 'name' => 'name', 198 'label' => 'label', 199 'content' => 'content', 237 'name' => 'name', 238 'label' => 'label', 239 'content' => 'content', 240 'collection' => 'collection', 200 241 ); 201 242 $data = array(); … … 216 257 * 217 258 * @since 7.0.0 259 * @since 7.1.0 Added the `collection` property. 218 260 * 219 261 * @return array Item schema data. … … 229 271 'type' => 'object', 230 272 'properties' => array( 231 'name' => array(273 'name' => array( 232 274 'description' => __( 'The icon name.' ), 233 275 'type' => 'string', … … 235 277 'context' => array( 'view', 'edit', 'embed' ), 236 278 ), 237 'label' => array(279 'label' => array( 238 280 'description' => __( 'The icon label.' ), 239 281 'type' => 'string', … … 241 283 'context' => array( 'view', 'edit', 'embed' ), 242 284 ), 243 'content' => array(285 'content' => array( 244 286 'description' => __( 'The icon content (SVG markup).' ), 245 287 'type' => 'string', … … 247 289 'context' => array( 'view', 'edit', 'embed' ), 248 290 ), 291 'collection' => array( 292 'description' => __( 'The slug of the collection this icon belongs to.' ), 293 'type' => 'string', 294 'readonly' => true, 295 'context' => array( 'view', 'edit', 'embed' ), 296 ), 249 297 ), 250 298 ); … … 259 307 * 260 308 * @since 7.0.0 309 * @since 7.1.0 Added the `collection` parameter. 261 310 * 262 311 * @return array Collection parameters. … … 265 314 $query_params = parent::get_collection_params(); 266 315 $query_params['context']['default'] = 'view'; 316 $query_params['collection'] = array( 317 'description' => __( 'Limit results to icons belonging to the given collection slug.' ), 318 'type' => 'string', 319 'pattern' => '^[a-z0-9]([a-z0-9_-]*[a-z0-9])?$', 320 ); 267 321 return $query_params; 268 322 }
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)