PluginProbe
Gutenberg / 24.1.0
Gutenberg v24.1.0
24.1.0 24.0.0 23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 All 404 releases
← All changes | lib/class-wp-rest-icons-controller-gutenberg.php +194 -0 23.5.3 → 24.1.0 View file →
@@ -14,5 +14,199 @@
14 14 *
15 15 * @since 7.1.0
16 16 */
17 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 + $collections_registry = WP_Icon_Collections_Registry::get_instance();
102 + $registered_collection = null !== $collection ? $collections_registry->get_registered( $collection ) : null;
103 +
104 + if ( null !== $collection && ( null === $registered_collection || ! $registered_collection['public'] ) ) {
105 + return new WP_Error(
106 + 'rest_icon_collection_not_found',
107 + sprintf(
108 + /* translators: %s: Icon collection slug. */
109 + __( 'Icon collection not found: "%s".', 'gutenberg' ),
110 + $collection
111 + ),
112 + array( 'status' => 404 )
113 + );
114 + }
115 +
116 + $response = array();
117 + $search = $request->get_param( 'search' );
118 + $icons = WP_Icons_Registry::get_instance()->get_registered_icons( $search );
119 +
120 + foreach ( $icons as $icon ) {
121 + if ( null !== $collection && ( ! isset( $icon['collection'] ) || $icon['collection'] !== $collection ) ) {
122 + continue;
123 + }
124 + $icon_collection = $collections_registry->get_registered( $icon['collection'] );
125 + if ( null === $icon_collection || ! $icon_collection['public'] ) {
126 + continue;
127 + }
128 + $prepared_icon = $this->prepare_item_for_response( $icon, $request );
129 + $response[] = $this->prepare_response_for_collection( $prepared_icon );
130 + }
131 +
132 + return rest_ensure_response( $response );
133 + }
134 +
135 + /**
136 + * Retrieves a specific icon from the registry.
137 + *
138 + * Icons belonging to non-public collections are reported as not found.
139 + *
140 + * @param string $name Icon name.
141 + * @return array|WP_Error Icon data on success, or WP_Error object on failure.
142 + */
143 + public function get_icon( $name ) {
144 + $icon = parent::get_icon( $name );
145 +
146 + if ( is_wp_error( $icon ) ) {
147 + return $icon;
148 + }
149 +
150 + $collection = WP_Icon_Collections_Registry::get_instance()->get_registered( $icon['collection'] );
151 + if ( null === $collection || ! $collection['public'] ) {
152 + return new WP_Error(
153 + 'rest_icon_not_found',
154 + sprintf(
155 + // translators: %s is the name of any user-provided name
156 + __( 'Icon not found: "%s".', 'gutenberg' ),
157 + $name
158 + ),
159 + array( 'status' => 404 )
160 + );
161 + }
162 +
163 + return $icon;
164 + }
165 +
166 + /**
167 + * Prepare a raw icon before it gets output in a REST API response.
168 + *
169 + * Adds a `collection` field to the base response while keeping the
170 + * namespaced icon name (e.g. `core/arrow-left`) as the `name` field.
171 + *
172 + * @param array $item Raw icon as registered.
173 + * @param WP_REST_Request $request Request object.
174 + * @return WP_REST_Response Response object.
175 + */
176 + public function prepare_item_for_response( $item, $request ) {
177 + $response = parent::prepare_item_for_response( $item, $request );
178 + $fields = $this->get_fields_for_response( $request );
179 +
180 + if ( rest_is_field_included( 'collection', $fields ) && isset( $item['collection'] ) ) {
181 + $data = $response->get_data();
182 + $data['collection'] = $item['collection'];
183 + $response->set_data( $data );
184 + }
185 +
186 + return $response;
187 + }
188 +
189 + /**
190 + * Retrieves the icon schema, conforming to JSON Schema.
191 + *
192 + * @return array Item schema data.
193 + */
194 + public function get_item_schema() {
195 + if ( $this->schema ) {
196 + return $this->add_additional_fields_schema( $this->schema );
197 + }
198 +
199 + $schema = parent::get_item_schema();
200 +
201 + $schema['properties']['collection'] = array(
202 + 'description' => __( 'The slug of the collection this icon belongs to.', 'gutenberg' ),
203 + 'type' => 'string',
204 + 'readonly' => true,
205 + 'context' => array( 'view', 'edit', 'embed' ),
206 + );
207 +
208 + $this->schema = $schema;
209 +
210 + return $this->add_additional_fields_schema( $this->schema );
211 + }
18 212 }