@@ -44,7 +44,10 @@ public function register_routes() {
44
44
'methods ' => WP_REST_Server::READABLE ,
45
45
'callback ' => array ( $ this , 'get_items ' ),
46
46
'permission_callback ' => array ( $ this , 'get_items_permissions_check ' ),
47
+ 'args ' => $ this ->get_collection_params (),
48
+
47
49
),
50
+ 'schema ' => array ( $ this , 'get_public_item_schema ' ),
48
51
)
49
52
);
50
53
@@ -56,7 +59,11 @@ public function register_routes() {
56
59
'methods ' => WP_REST_Server::READABLE ,
57
60
'callback ' => array ( $ this , 'get_item ' ),
58
61
'permission_callback ' => array ( $ this , 'get_items_permissions_check ' ),
62
+ 'args ' => array (
63
+ 'context ' => $ this ->get_context_param ( array ( 'default ' => 'view ' ) ),
64
+ ),
59
65
),
66
+ 'schema ' => array ( $ this , 'get_public_item_schema ' ),
60
67
)
61
68
);
62
69
}
@@ -68,13 +75,61 @@ public function register_routes() {
68
75
*
69
76
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
70
77
*/
71
- public function get_items ( $ request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
72
- $ collections = array ();
73
- foreach ( WP_Font_Library::get_font_collections () as $ collection ) {
74
- $ collections [] = $ collection ->get_config ();
78
+ public function get_items ( $ request ) {
79
+ $ collections_all = WP_Font_Library::get_font_collections ();
80
+
81
+ $ page = $ request ['page ' ];
82
+ $ per_page = $ request ['per_page ' ];
83
+ $ total_items = count ( $ collections_all );
84
+ $ max_pages = ceil ( $ total_items / $ per_page );
85
+
86
+ if ( $ page > $ max_pages && $ total_items > 0 ) {
87
+ return new WP_Error (
88
+ 'rest_post_invalid_page_number ' ,
89
+ __ ( 'The page number requested is larger than the number of pages available. ' , 'default ' ),
90
+ array ( 'status ' => 400 )
91
+ );
92
+ }
93
+
94
+ $ collections_page = array_slice ( $ collections_all , ( $ page - 1 ) * $ per_page , $ per_page );
95
+
96
+ $ items = array ();
97
+ foreach ( $ collections_page as $ collection ) {
98
+ $ item = $ this ->prepare_item_for_response ( $ collection , $ request );
99
+ if ( is_wp_error ( $ item ) ) {
100
+ return $ item ;
101
+ }
102
+ $ item = $ this ->prepare_response_for_collection ( $ item );
103
+ $ items [] = $ item ;
104
+ }
105
+
106
+ $ response = rest_ensure_response ( $ items );
107
+
108
+ $ response ->header ( 'X-WP-Total ' , (int ) $ total_items );
109
+ $ response ->header ( 'X-WP-TotalPages ' , (int ) $ max_pages );
110
+
111
+ $ request_params = $ request ->get_query_params ();
112
+ $ collection_url = rest_url ( $ this ->namespace . '/ ' . $ this ->rest_base );
113
+ $ base = add_query_arg ( urlencode_deep ( $ request_params ), $ collection_url );
114
+
115
+ if ( $ page > 1 ) {
116
+ $ prev_page = $ page - 1 ;
117
+
118
+ if ( $ prev_page > $ max_pages ) {
119
+ $ prev_page = $ max_pages ;
120
+ }
121
+
122
+ $ prev_link = add_query_arg ( 'page ' , $ prev_page , $ base );
123
+ $ response ->link_header ( 'prev ' , $ prev_link );
124
+ }
125
+ if ( $ max_pages > $ page ) {
126
+ $ next_page = $ page + 1 ;
127
+ $ next_link = add_query_arg ( 'page ' , $ next_page , $ base );
128
+
129
+ $ response ->link_header ( 'next ' , $ next_link );
75
130
}
76
131
77
- return rest_ensure_response ( $ collections , 200 ) ;
132
+ return $ response ;
78
133
}
79
134
80
135
/**
@@ -95,17 +150,170 @@ public function get_item( $request ) {
95
150
return $ collection ;
96
151
}
97
152
98
- $ config = $ collection ->get_config ();
99
- $ contents = $ collection ->get_content ();
153
+ $ item = $ this ->prepare_item_for_response ( $ collection , $ request );
100
154
101
- // If there was an error getting the collection data, return the error.
102
- if ( is_wp_error ( $ contents ) ) {
103
- $ contents ->add_data ( array ( 'status ' => 500 ) );
104
- return $ contents ;
155
+ if ( is_wp_error ( $ item ) ) {
156
+ return $ item ;
105
157
}
106
158
107
- $ collection_data = array_merge ( $ config , $ contents );
108
- return rest_ensure_response ( $ collection_data );
159
+ return $ item ;
160
+ }
161
+
162
+ /*
163
+ * Prepare a single collection output for response.
164
+ *
165
+ * @since 6.5.0
166
+ *
167
+ * @param WP_Font_Collection $collection Collection object.
168
+ * @param WP_REST_Request $request Request object.
169
+ * @return array|WP_Error
170
+ */
171
+ public function prepare_item_for_response ( $ collection , $ request ) {
172
+ $ fields = $ this ->get_fields_for_response ( $ request );
173
+ $ item = array ();
174
+
175
+ $ config_fields = array ( 'slug ' , 'name ' , 'description ' );
176
+ foreach ( $ config_fields as $ field ) {
177
+ if ( in_array ( $ field , $ fields , true ) ) {
178
+ $ item [ $ field ] = $ collection ->$ field ;
179
+ }
180
+ }
181
+
182
+ $ data_fields = array ( 'font_families ' , 'categories ' );
183
+ if ( in_array ( 'font_families ' , $ fields , true ) || in_array ( 'categories ' , $ fields , true ) ) {
184
+ $ content = $ collection ->get_content ();
185
+
186
+ // If there was an error getting the collection data, return the error.
187
+ if ( is_wp_error ( $ content ) ) {
188
+ $ content ->add_data ( array ( 'status ' => 500 ) );
189
+ return $ content ;
190
+ }
191
+
192
+ foreach ( $ data_fields as $ field ) {
193
+ if ( in_array ( $ field , $ fields , true ) ) {
194
+ $ item [ $ field ] = $ content [ $ field ];
195
+ }
196
+ }
197
+ }
198
+
199
+ $ response = rest_ensure_response ( $ item );
200
+
201
+ if ( rest_is_field_included ( '_links ' , $ fields ) ) {
202
+ $ links = $ this ->prepare_links ( $ collection );
203
+ $ response ->add_links ( $ links );
204
+ }
205
+
206
+ $ context = ! empty ( $ request ['context ' ] ) ? $ request ['context ' ] : 'view ' ;
207
+ $ response ->data = $ this ->add_additional_fields_to_object ( $ response ->data , $ request );
208
+ $ response ->data = $ this ->filter_response_by_context ( $ response ->data , $ context );
209
+
210
+ /**
211
+ * Filters a font collection returned from the REST API.
212
+ *
213
+ * Allows modification of the font collection right before it is returned.
214
+ *
215
+ * @since 6.5.0
216
+ *
217
+ * @param WP_REST_Response $response The response object.
218
+ * @param WP_Font_Collection $collection The Font Collection object.
219
+ * @param WP_REST_Request $request Request used to generate the response.
220
+ */
221
+ return apply_filters ( 'rest_prepare_font_collection ' , $ response , $ collection , $ request );
222
+ }
223
+
224
+ /**
225
+ * Retrieves the font collection's schema, conforming to JSON Schema.
226
+ *
227
+ * @since 6.5.0
228
+ *
229
+ * @return array Item schema data.
230
+ */
231
+ public function get_item_schema () {
232
+ if ( $ this ->schema ) {
233
+ return $ this ->add_additional_fields_schema ( $ this ->schema );
234
+ }
235
+
236
+ $ schema = array (
237
+ '$schema ' => 'http://json-schema.org/draft-04/schema# ' ,
238
+ 'title ' => 'font-collection ' ,
239
+ 'type ' => 'object ' ,
240
+ 'properties ' => array (
241
+ 'slug ' => array (
242
+ 'description ' => __ ( 'Unique identifier for the font collection. ' , 'gutenberg ' ),
243
+ 'type ' => 'string ' ,
244
+ 'context ' => array ( 'view ' , 'edit ' , 'embed ' ),
245
+ 'readonly ' => true ,
246
+ ),
247
+ 'name ' => array (
248
+ 'description ' => __ ( 'The name for the font collection. ' , 'gutenberg ' ),
249
+ 'type ' => 'string ' ,
250
+ 'context ' => array ( 'view ' , 'edit ' , 'embed ' ),
251
+ ),
252
+ 'description ' => array (
253
+ 'description ' => __ ( 'The description for the font collection. ' , 'gutenberg ' ),
254
+ 'type ' => 'string ' ,
255
+ 'context ' => array ( 'view ' , 'edit ' , 'embed ' ),
256
+ ),
257
+ 'font_families ' => array (
258
+ 'description ' => __ ( 'The font families for the font collection. ' , 'gutenberg ' ),
259
+ 'type ' => 'array ' ,
260
+ 'context ' => array ( 'view ' , 'edit ' , 'embed ' ),
261
+ ),
262
+ 'categories ' => array (
263
+ 'description ' => __ ( 'The categories for the font collection. ' , 'gutenberg ' ),
264
+ 'type ' => 'array ' ,
265
+ 'context ' => array ( 'view ' , 'edit ' , 'embed ' ),
266
+ ),
267
+ ),
268
+ );
269
+
270
+ $ this ->schema = $ schema ;
271
+
272
+ return $ this ->add_additional_fields_schema ( $ this ->schema );
273
+ }
274
+
275
+ /**
276
+ * Prepares links for the request.
277
+ *
278
+ * @since 6.5.0
279
+ *
280
+ * @param WP_Font_Collection $collection Font collection data
281
+ * @return array Links for the given font collection.
282
+ */
283
+ protected function prepare_links ( $ collection ) {
284
+ $ links = array (
285
+ 'self ' => array (
286
+ 'href ' => rest_url ( sprintf ( '%s/%s/%s ' , $ this ->namespace , $ this ->rest_base , $ collection ->slug ) ),
287
+ ),
288
+ 'collection ' => array (
289
+ 'href ' => rest_url ( sprintf ( '%s/%s ' , $ this ->namespace , $ this ->rest_base ) ),
290
+ ),
291
+ );
292
+ return $ links ;
293
+ }
294
+
295
+ /**
296
+ * Retrieves the search params for the font collections.
297
+ *
298
+ * @since 6.5.0
299
+ *
300
+ * @return array Collection parameters.
301
+ */
302
+ public function get_collection_params () {
303
+ $ query_params = parent ::get_collection_params ();
304
+
305
+ $ query_params ['context ' ] = $ this ->get_context_param ( array ( 'default ' => 'view ' ) );
306
+
307
+ unset( $ query_params ['search ' ] );
308
+
309
+ /**
310
+ * Filters REST API collection parameters for the font collections controller.
311
+ *
312
+ * @since 6.5.0
313
+ *
314
+ * @param array $query_params JSON Schema-formatted collection parameters.
315
+ */
316
+ return apply_filters ( 'rest_font_collections_collection_params ' , $ query_params );
109
317
}
110
318
111
319
/**
0 commit comments