| 1 |
<?php |
| 2 |
/** |
| 3 |
* REST API: Bundle WP_Icons_Registry_Gutenberg class instead of inheriting per WordPress version class |
| 4 |
* |
| 5 |
* Changes to this class should be synced to the corresponding class |
| 6 |
* in WordPress core: src/wp-includes/rest-api/endpoints/class-wp-rest-icons-controller.php. |
| 7 |
* |
| 8 |
* @package gutenberg |
| 9 |
* @subpackage REST_API |
| 10 |
*/ |
| 11 |
|
| 12 |
/** |
| 13 |
* Gutenberg Icons REST API Controller. |
| 14 |
* |
| 15 |
* @since 7.1.0 |
| 16 |
*/ |
| 17 |
class WP_REST_Icons_Controller_Gutenberg extends WP_REST_Icons_Controller { |
| 18 |
|
| 19 |
/** |
| 20 |
* Registers the routes for icons. |
| 21 |
*/ |
| 22 |
public function register_routes() { |
| 23 |
register_rest_route( |
| 24 |
$this->namespace, |
| 25 |
'/' . $this->rest_base, |
| 26 |
array( |
| 27 |
array( |
| 28 |
'methods' => WP_REST_Server::READABLE, |
| 29 |
'callback' => array( $this, 'get_items' ), |
| 30 |
'permission_callback' => array( $this, 'get_items_permissions_check' ), |
| 31 |
'args' => $this->get_collection_params(), |
| 32 |
), |
| 33 |
'schema' => array( $this, 'get_public_item_schema' ), |
| 34 |
) |
| 35 |
); |
| 36 |
|
| 37 |
register_rest_route( |
| 38 |
$this->namespace, |
| 39 |
'/' . $this->rest_base . '/(?P<name>[a-z0-9](?:[a-z0-9_-]*[a-z0-9])?/[a-z0-9](?:[a-z0-9_-]*[a-z0-9])?)', |
| 40 |
array( |
| 41 |
'args' => array( |
| 42 |
'name' => array( |
| 43 |
'description' => __( 'Icon name.', 'gutenberg' ), |
| 44 |
'type' => 'string', |
| 45 |
), |
| 46 |
), |
| 47 |
array( |
| 48 |
'methods' => WP_REST_Server::READABLE, |
| 49 |
'callback' => array( $this, 'get_item' ), |
| 50 |
'permission_callback' => array( $this, 'get_item_permissions_check' ), |
| 51 |
'args' => array( |
| 52 |
'context' => $this->get_context_param( array( 'default' => 'view' ) ), |
| 53 |
), |
| 54 |
), |
| 55 |
'schema' => array( $this, 'get_public_item_schema' ), |
| 56 |
) |
| 57 |
); |
| 58 |
|
| 59 |
register_rest_route( |
| 60 |
$this->namespace, |
| 61 |
'/' . $this->rest_base . '/(?P<collection>[a-z0-9](?:[a-z0-9_-]*[a-z0-9])?)', |
| 62 |
array( |
| 63 |
array( |
| 64 |
'methods' => WP_REST_Server::READABLE, |
| 65 |
'callback' => array( $this, 'get_items' ), |
| 66 |
'permission_callback' => array( $this, 'get_items_permissions_check' ), |
| 67 |
'args' => $this->get_collection_params(), |
| 68 |
), |
| 69 |
'schema' => array( $this, 'get_public_item_schema' ), |
| 70 |
) |
| 71 |
); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Retrieves the query params for the icons collection. |
| 76 |
* |
| 77 |
* Extends the base params with a `collection` parameter that corresponds |
| 78 |
* to an icon collection slug. The same parameter is also captured as a |
| 79 |
* URL segment by the collection-scoped route. |
| 80 |
* |
| 81 |
* @return array Collection parameters. |
| 82 |
*/ |
| 83 |
public function get_collection_params() { |
| 84 |
$query_params = parent::get_collection_params(); |
| 85 |
$query_params['collection'] = array( |
| 86 |
'description' => __( 'Limit results to icons belonging to the given collection slug.', 'gutenberg' ), |
| 87 |
'type' => 'string', |
| 88 |
'pattern' => '^[a-z0-9]([a-z0-9_-]*[a-z0-9])?$', |
| 89 |
); |
| 90 |
return $query_params; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Retrieves all icons, optionally scoped to a collection via URL segment. |
| 95 |
* |
| 96 |
* @param WP_REST_Request $request Full details about the request. |
| 97 |
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
| 98 |
*/ |
| 99 |
public function get_items( $request ) { |
| 100 |
$collection = $request->get_param( 'collection' ); |
| 101 |
|
| 102 |
if ( null !== $collection && ! WP_Icon_Collections_Registry::get_instance()->is_registered( $collection ) ) { |
| 103 |
return new WP_Error( |
| 104 |
'rest_icon_collection_not_found', |
| 105 |
sprintf( |
| 106 |
/* translators: %s: Icon collection slug. */ |
| 107 |
__( 'Icon collection not found: "%s".', 'gutenberg' ), |
| 108 |
$collection |
| 109 |
), |
| 110 |
array( 'status' => 404 ) |
| 111 |
); |
| 112 |
} |
| 113 |
|
| 114 |
$response = array(); |
| 115 |
$search = $request->get_param( 'search' ); |
| 116 |
$icons = WP_Icons_Registry::get_instance()->get_registered_icons( $search ); |
| 117 |
|
| 118 |
foreach ( $icons as $icon ) { |
| 119 |
if ( null !== $collection && ( ! isset( $icon['collection'] ) || $icon['collection'] !== $collection ) ) { |
| 120 |
continue; |
| 121 |
} |
| 122 |
$prepared_icon = $this->prepare_item_for_response( $icon, $request ); |
| 123 |
$response[] = $this->prepare_response_for_collection( $prepared_icon ); |
| 124 |
} |
| 125 |
|
| 126 |
return rest_ensure_response( $response ); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Prepare a raw icon before it gets output in a REST API response. |
| 131 |
* |
| 132 |
* Adds a `collection` field to the base response while keeping the |
| 133 |
* namespaced icon name (e.g. `core/arrow-left`) as the `name` field. |
| 134 |
* |
| 135 |
* @param array $item Raw icon as registered. |
| 136 |
* @param WP_REST_Request $request Request object. |
| 137 |
* @return WP_REST_Response Response object. |
| 138 |
*/ |
| 139 |
public function prepare_item_for_response( $item, $request ) { |
| 140 |
$response = parent::prepare_item_for_response( $item, $request ); |
| 141 |
$fields = $this->get_fields_for_response( $request ); |
| 142 |
|
| 143 |
if ( rest_is_field_included( 'collection', $fields ) && isset( $item['collection'] ) ) { |
| 144 |
$data = $response->get_data(); |
| 145 |
$data['collection'] = $item['collection']; |
| 146 |
$response->set_data( $data ); |
| 147 |
} |
| 148 |
|
| 149 |
return $response; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Retrieves the icon schema, conforming to JSON Schema. |
| 154 |
* |
| 155 |
* @return array Item schema data. |
| 156 |
*/ |
| 157 |
public function get_item_schema() { |
| 158 |
if ( $this->schema ) { |
| 159 |
return $this->add_additional_fields_schema( $this->schema ); |
| 160 |
} |
| 161 |
|
| 162 |
$schema = parent::get_item_schema(); |
| 163 |
|
| 164 |
$schema['properties']['collection'] = array( |
| 165 |
'description' => __( 'The slug of the collection this icon belongs to.', 'gutenberg' ), |
| 166 |
'type' => 'string', |
| 167 |
'readonly' => true, |
| 168 |
'context' => array( 'view', 'edit', 'embed' ), |
| 169 |
); |
| 170 |
|
| 171 |
$this->schema = $schema; |
| 172 |
|
| 173 |
return $this->add_additional_fields_schema( $this->schema ); |
| 174 |
} |
| 175 |
} |
| 176 |
|