PluginProbe
Gutenberg / 9.7.3
Gutenberg v9.7.3
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 12.6.0 7.4.0 All 402 releases
gutenberg / lib / class-wp-rest-widgets-controller.php

class-wp-rest-widgets-controller.php in Gutenberg 9.7.3, at lib/class-wp-rest-widgets-controller.php

784 lines 23.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * REST API: WP_REST_Widgets_Controller class
4 *
5 * @package Gutenberg
6 */
7
8 /**
9 * Core class representing a controller for widgets.
10 */
11 class WP_REST_Widgets_Controller extends WP_REST_Controller {
12
13 /**
14 * Widgets controller constructor.
15 */
16 public function __construct() {
17 $this->namespace = 'wp/v2';
18 $this->rest_base = 'widgets';
19 }
20
21 /**
22 * Registers the widget routes for the controller.
23 */
24 public function register_routes() {
25 register_rest_route(
26 $this->namespace,
27 $this->rest_base,
28 array(
29 array(
30 'methods' => WP_REST_Server::READABLE,
31 'callback' => array( $this, 'get_items' ),
32 'permission_callback' => array( $this, 'get_items_permissions_check' ),
33 'args' => $this->get_collection_params(),
34 ),
35 array(
36 'methods' => WP_REST_Server::CREATABLE,
37 'callback' => array( $this, 'create_item' ),
38 'permission_callback' => array( $this, 'create_item_permissions_check' ),
39 'args' => $this->get_endpoint_args_for_item_schema(),
40 ),
41 'allow_batch' => array( 'v1' => true ),
42 'schema' => array( $this, 'get_public_item_schema' ),
43 )
44 );
45
46 register_rest_route(
47 $this->namespace,
48 $this->rest_base . '/(?P<id>[\w\-]+)',
49 array(
50 array(
51 'methods' => WP_REST_Server::READABLE,
52 'callback' => array( $this, 'get_item' ),
53 'permission_callback' => array( $this, 'get_item_permissions_check' ),
54 'args' => array(
55 'context' => $this->get_context_param( array( 'default' => 'view' ) ),
56 ),
57 ),
58 array(
59 'methods' => WP_REST_Server::EDITABLE,
60 'callback' => array( $this, 'update_item' ),
61 'permission_callback' => array( $this, 'update_item_permissions_check' ),
62 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
63 ),
64 array(
65 'methods' => WP_REST_Server::DELETABLE,
66 'callback' => array( $this, 'delete_item' ),
67 'permission_callback' => array( $this, 'delete_item_permissions_check' ),
68 'args' => array(
69 'force' => array(
70 'description' => __( 'Whether to force removal of the widget, or move it to the inactive sidebar.', 'gutenberg' ),
71 'type' => 'boolean',
72 ),
73 ),
74 ),
75 'allow_batch' => array( 'v1' => true ),
76 'schema' => array( $this, 'get_public_item_schema' ),
77 )
78 );
79 }
80
81 /**
82 * Checks if a given request has access to get widgets.
83 *
84 * @since 5.6.0
85 *
86 * @param WP_REST_Request $request Full details about the request.
87 * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
88 */
89 public function get_items_permissions_check( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
90 return $this->permissions_check();
91 }
92
93 /**
94 * Retrieves a collection of widgets.
95 *
96 * @since 5.6.0
97 *
98 * @param WP_REST_Request $request Full details about the request.
99 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
100 */
101 public function get_items( $request ) {
102 $prepared = array();
103
104 foreach ( wp_get_sidebars_widgets() as $sidebar_id => $widget_ids ) {
105 if ( isset( $request['sidebar'] ) && $sidebar_id !== $request['sidebar'] ) {
106 continue;
107 }
108
109 foreach ( $widget_ids as $widget_id ) {
110 $response = $this->prepare_item_for_response( compact( 'sidebar_id', 'widget_id' ), $request );
111
112 if ( ! is_wp_error( $response ) ) {
113 $prepared[] = $this->prepare_response_for_collection( $response );
114 }
115 }
116 }
117
118 return new WP_REST_Response( $prepared );
119 }
120
121 /**
122 * Checks if a given request has access to get a widget.
123 *
124 * @since 5.6.0
125 *
126 * @param WP_REST_Request $request Full details about the request.
127 * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
128 */
129 public function get_item_permissions_check( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
130 return $this->permissions_check();
131 }
132
133 /**
134 * Gets an individual widget.
135 *
136 * @since 5.6.0
137 *
138 * @param WP_REST_Request $request Full details about the request.
139 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
140 */
141 public function get_item( $request ) {
142 $widget_id = $request['id'];
143 $sidebar_id = $this->find_widgets_sidebar( $widget_id );
144
145 if ( is_wp_error( $sidebar_id ) ) {
146 return $sidebar_id;
147 }
148
149 return $this->prepare_item_for_response( compact( 'sidebar_id', 'widget_id' ), $request );
150 }
151
152 /**
153 * Checks if a given request has access to create widgets.
154 *
155 * @since 5.6.0
156 *
157 * @param WP_REST_Request $request Full details about the request.
158 * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
159 */
160 public function create_item_permissions_check( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
161 return $this->permissions_check();
162 }
163
164 /**
165 * Creates a widget.
166 *
167 * @since 5.6.0
168 *
169 * @param WP_REST_Request $request Full details about the request.
170 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
171 */
172 public function create_item( $request ) {
173 $sidebar_id = $request['sidebar'];
174
175 $backup_post = $_POST;
176 $widget_id = $this->save_widget( $request );
177 $_POST = $backup_post;
178
179 $this->assign_to_sidebar( $widget_id, $sidebar_id );
180
181 $request['context'] = 'edit';
182
183 $response = $this->prepare_item_for_response( compact( 'sidebar_id', 'widget_id' ), $request );
184
185 if ( is_wp_error( $response ) ) {
186 return $response;
187 }
188
189 $response->set_status( 201 );
190
191 return $response;
192 }
193
194 /**
195 * Checks if a given request has access to update widgets.
196 *
197 * @since 5.6.0
198 *
199 * @param WP_REST_Request $request Full details about the request.
200 * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
201 */
202 public function update_item_permissions_check( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
203 return $this->permissions_check();
204 }
205
206 /**
207 * Updates an existing widget.
208 *
209 * @since 5.6.0
210 *
211 * @param WP_REST_Request $request Full details about the request.
212 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
213 */
214 public function update_item( $request ) {
215 $widget_id = $request['id'];
216 $sidebar_id = $this->find_widgets_sidebar( $widget_id );
217
218 if ( is_wp_error( $sidebar_id ) ) {
219 // Allow for an update request to a reference widget if the widget hasn't been assigned to a sidebar yet.
220 if ( $request['sidebar'] && $this->is_reference_widget( $widget_id ) ) {
221 $sidebar_id = $request['sidebar'];
222 $this->assign_to_sidebar( $widget_id, $sidebar_id );
223 } else {
224 return $sidebar_id;
225 }
226 }
227
228 $backup_post = $_POST;
229
230 if ( isset( $request['settings'] ) ) {
231 $this->save_widget( $request );
232 }
233
234 $_POST = $backup_post;
235
236 if ( isset( $request['sidebar'] ) && $request['sidebar'] !== $sidebar_id ) {
237 $sidebar_id = $request['sidebar'];
238 $this->assign_to_sidebar( $widget_id, $sidebar_id );
239 }
240
241 $request['context'] = 'edit';
242
243 return $this->prepare_item_for_response( compact( 'sidebar_id', 'widget_id' ), $request );
244 }
245
246 /**
247 * Checks if a given request has access to delete widgets.
248 *
249 * @since 5.6.0
250 *
251 * @param WP_REST_Request $request Full details about the request.
252 * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
253 */
254 public function delete_item_permissions_check( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
255 return $this->permissions_check();
256 }
257
258 /**
259 * Deletes a widget.
260 *
261 * @since 5.6.0
262 *
263 * @param WP_REST_Request $request Full details about the request.
264 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
265 */
266 public function delete_item( $request ) {
267 $widget_id = $request['id'];
268 $sidebar_id = $this->find_widgets_sidebar( $widget_id );
269
270 if ( is_wp_error( $sidebar_id ) ) {
271 return $sidebar_id;
272 }
273
274 $request['context'] = 'edit';
275
276 if ( $request['force'] ) {
277 $prepared = $this->prepare_item_for_response( compact( 'sidebar_id', 'widget_id' ), $request );
278 $this->assign_to_sidebar( $widget_id, null );
279 $prepared->set_data(
280 array(
281 'deleted' => true,
282 'previous' => $prepared->get_data(),
283 )
284 );
285 } else {
286 $this->assign_to_sidebar( $widget_id, 'wp_inactive_widgets' );
287 $prepared = $this->prepare_item_for_response(
288 array(
289 'sidebar_id' => 'wp_inactive_widgets',
290 'widget_id' => $widget_id,
291 ),
292 $request
293 );
294 }
295
296 return $prepared;
297 }
298
299 /**
300 * Assigns a widget to the given sidebar.
301 *
302 * @param string $widget_id The widget id to assign.
303 * @param string $sidebar_id The sidebar id to assign to.
304 */
305 protected function assign_to_sidebar( $widget_id, $sidebar_id ) {
306 $sidebars = wp_get_sidebars_widgets();
307
308 foreach ( $sidebars as $maybe_sidebar_id => $widgets ) {
309 foreach ( $widgets as $i => $maybe_widget_id ) {
310 if ( $widget_id === $maybe_widget_id && $sidebar_id !== $maybe_sidebar_id ) {
311 unset( $sidebars[ $maybe_sidebar_id ][ $i ] );
312 // We could technically break 2 here, but continue looping in case the id is duplicated.
313 continue 2;
314 }
315 }
316 }
317
318 if ( $sidebar_id ) {
319 $sidebars[ $sidebar_id ][] = $widget_id;
320 }
321
322 wp_set_sidebars_widgets( $sidebars );
323 }
324
325 /**
326 * Performs a permissions check for managing widgets.
327 *
328 * @return true|WP_Error
329 */
330 protected function permissions_check() {
331 if ( ! current_user_can( 'edit_theme_options' ) ) {
332 return new WP_Error(
333 'rest_cannot_manage_widgets',
334 __( 'Sorry, you are not allowed to manage widgets on this site.', 'gutenberg' ),
335 array(
336 'status' => rest_authorization_required_code(),
337 )
338 );
339 }
340
341 return true;
342 }
343
344 /**
345 * Finds the sidebar a widget belongs to.
346 *
347 * @param string $widget_id The widget id to search for.
348 * @return string|WP_Error The found sidebar id, or a WP_Error instance if it does not exist.
349 */
350 protected function find_widgets_sidebar( $widget_id ) {
351 foreach ( wp_get_sidebars_widgets() as $sidebar_id => $widget_ids ) {
352 foreach ( $widget_ids as $maybe_widget_id ) {
353 if ( $maybe_widget_id === $widget_id ) {
354 return (string) $sidebar_id;
355 }
356 }
357 }
358
359 return new WP_Error( 'rest_widget_not_found', __( 'No widget was found with that id.', 'gutenberg' ), array( 'status' => 404 ) );
360 }
361
362 /**
363 * Saves the widget in the request object.
364 *
365 * @param WP_REST_Request $request Full details about the request.
366 *
367 * @return string
368 */
369 protected function save_widget( $request ) {
370 global $wp_registered_widget_updates, $wp_registered_widgets;
371
372 $input_widget = $request->get_params();
373
374 if ( isset( $input_widget['id'] ) && ! $this->is_reference_widget( $input_widget['id'] ) ) {
375 $widget = $wp_registered_widgets[ $input_widget['id'] ];
376
377 $input_widget['number'] = (int) $widget['params'][0]['number'];
378 $input_widget['id_base'] = _get_widget_id_base( $input_widget['id'] );
379 }
380
381 ob_start();
382 if ( isset( $input_widget['id_base'] ) && isset( $wp_registered_widget_updates[ $input_widget['id_base'] ] ) ) {
383 // Class-based widget.
384 $update_control = $wp_registered_widget_updates[ $input_widget['id_base'] ];
385 if ( ! isset( $input_widget['id'] ) ) {
386 $number = $this->get_last_number_for_widget( $input_widget['id_base'] ) + 1;
387 $id = $input_widget['id_base'] . '-' . $number;
388
389 $input_widget['id'] = $id;
390 $input_widget['number'] = $number;
391 }
392 $field = 'widget-' . $input_widget['id_base'];
393 $number = $input_widget['number'];
394 $_POST = $input_widget;
395 $_POST[ $field ][ $number ] = wp_slash( $input_widget['settings'] );
396 call_user_func( $update_control['callback'] );
397 $update_control['callback'][0]->updated = false;
398
399 // Just because we saved new widget doesn't mean it was added to $wp_registered_widgets.
400 // Let's make sure it's there so that it's included in the response.
401 if ( ! isset( $wp_registered_widgets[ $input_widget['id'] ] ) || 1 === $number ) {
402 $widget_class = get_class( $update_control['callback'][0] );
403 $new_object = new $widget_class(
404 $input_widget['id_base'],
405 $input_widget['name'],
406 $input_widget['settings']
407 );
408 $new_object->_set( $number );
409 $new_object->_register();
410 }
411 } else {
412 $registered_widget_id = null;
413 if ( isset( $wp_registered_widget_updates[ $input_widget['id'] ] ) ) {
414 $registered_widget_id = $input_widget['id'];
415 } else {
416 $numberless_id = substr( $input_widget['id'], 0, strrpos( $input_widget['id'], '-' ) );
417 if ( isset( $wp_registered_widget_updates[ $numberless_id ] ) ) {
418 $registered_widget_id = $numberless_id;
419 }
420 }
421
422 if ( $registered_widget_id ) {
423 // Old-style widget.
424 $update_control = $wp_registered_widget_updates[ $registered_widget_id ];
425 $_POST = wp_slash( $input_widget['settings'] );
426 call_user_func( $update_control['callback'] );
427 }
428 }
429 ob_end_clean();
430
431 return $input_widget['id'];
432 }
433
434 /**
435 * Gets the last number used by the given widget.
436 *
437 * @param string $id_base The widget id base.
438 * @return int The last number, or zero if the widget has not been used.
439 */
440 protected function get_last_number_for_widget( $id_base ) {
441 global $wp_registered_widget_updates;
442
443 if ( ! is_array( $wp_registered_widget_updates[ $id_base ]['callback'] ) ) {
444 return 0;
445 }
446
447 if ( ! $wp_registered_widget_updates[ $id_base ]['callback'][0] instanceof WP_Widget ) {
448 return 0;
449 }
450
451 $widget = $wp_registered_widget_updates[ $id_base ]['callback'][0];
452 $instances = array_filter( $widget->get_settings(), 'is_numeric', ARRAY_FILTER_USE_KEY );
453
454 if ( ! $instances ) {
455 return 0;
456 }
457
458 return $widget->number;
459 }
460
461 /**
462 * Prepares the widget for the REST response.
463 *
464 * @since 5.6.0
465 *
466 * @param array $item An array containing a widget_id and sidebar_id.
467 * @param WP_REST_Request $request Request object.
468 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
469 */
470 public function prepare_item_for_response( $item, $request ) {
471 global $wp_registered_widgets, $wp_registered_sidebars, $wp_registered_widget_controls;
472
473 $widget_id = $item['widget_id'];
474 $sidebar_id = $item['sidebar_id'];
475
476 if ( ! isset( $wp_registered_widgets[ $widget_id ] ) ) {
477 return new WP_Error( 'rest_invalid_widget', __( 'The requested widget is invalid.', 'gutenberg' ), array( 'status' => 500 ) );
478 }
479
480 $fields = $this->get_fields_for_response( $request );
481
482 if ( isset( $wp_registered_sidebars[ $sidebar_id ] ) ) {
483 $registered_sidebar = $wp_registered_sidebars[ $sidebar_id ];
484 } elseif ( 'wp_inactive_widgets' === $sidebar_id ) {
485 $registered_sidebar = array();
486 } else {
487 $registered_sidebar = null;
488 }
489
490 $widget = $wp_registered_widgets[ $widget_id ];
491 $prepared = array(
492 'id' => $widget_id,
493 'id_base' => '',
494 'sidebar' => $sidebar_id,
495 'widget_class' => '',
496 'name' => $widget['name'],
497 'description' => ! empty( $widget['description'] ) ? $widget['description'] : '',
498 'number' => 0,
499 'rendered' => '',
500 'rendered_form' => '',
501 'settings' => array(),
502 );
503
504 // Get the widget output.
505 if ( is_callable( $widget['callback'] ) && rest_is_field_included( 'rendered', $fields ) && 'wp_inactive_widgets' !== $sidebar_id ) {
506 // @note: everything up to ob_start is taken from the dynamic_sidebar function.
507 $widget_parameters = array_merge(
508 array(
509 array_merge(
510 (array) $registered_sidebar,
511 array(
512 'widget_id' => $widget_id,
513 'widget_name' => $widget['name'],
514 )
515 ),
516 ),
517 (array) $widget['params']
518 );
519
520 $classname = '';
521 foreach ( (array) $widget['classname'] as $cn ) {
522 if ( is_string( $cn ) ) {
523 $classname .= '_' . $cn;
524 } elseif ( is_object( $cn ) ) {
525 $classname .= '_' . get_class( $cn );
526 }
527 }
528 $classname = ltrim( $classname, '_' );
529 if ( isset( $widget_parameters[0]['before_widget'] ) ) {
530 $widget_parameters[0]['before_widget'] = sprintf(
531 $widget_parameters[0]['before_widget'],
532 $widget_id,
533 $classname
534 );
535 }
536
537 ob_start();
538 call_user_func_array( $widget['callback'], $widget_parameters );
539 $prepared['rendered'] = trim( ob_get_clean() );
540 }
541
542 if ( is_array( $widget['callback'] ) && isset( $widget['callback'][0] ) ) {
543 $instance = $widget['callback'][0];
544 $prepared['widget_class'] = get_class( $instance );
545 $prepared['settings'] = $this->get_sidebar_widget_instance(
546 $registered_sidebar,
547 $widget_id
548 );
549 $prepared['number'] = (int) $widget['params'][0]['number'];
550 $prepared['id_base'] = $instance->id_base;
551 }
552
553 if (
554 rest_is_field_included( 'rendered_form', $fields ) &&
555 isset( $wp_registered_widget_controls[ $widget_id ]['callback'] )
556 ) {
557 $control = $wp_registered_widget_controls[ $widget_id ];
558 $arguments = array();
559 if ( ! empty( $prepared['number'] ) ) {
560 $arguments[0] = array( 'number' => $prepared['number'] );
561 }
562 ob_start();
563 call_user_func_array( $control['callback'], $arguments );
564 $prepared['rendered_form'] = trim( ob_get_clean() );
565 }
566
567 $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
568 $prepared = $this->add_additional_fields_to_object( $prepared, $request );
569 $prepared = $this->filter_response_by_context( $prepared, $context );
570
571 $response = rest_ensure_response( $prepared );
572
573 $response->add_links( $this->prepare_links( $prepared ) );
574
575 /**
576 * Filter widget REST API response.
577 *
578 * @param WP_REST_Response $response The response object.
579 * @param array $prepared Widget data.
580 * @param WP_REST_Request $request Request used to generate the response.
581 */
582 return apply_filters( 'rest_prepare_widget', $response, $prepared, $request );
583 }
584
585 /**
586 * Prepares links for the request.
587 *
588 * @param array $prepared Widget.
589 * @return array Links for the given widget.
590 */
591 protected function prepare_links( $prepared ) {
592 $id_base = ( ! empty( $prepared['id_base'] ) ) ? $prepared['id_base'] : $prepared['id'];
593
594 return array(
595 'collection' => array(
596 'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
597 ),
598 'self' => array(
599 'href' => rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, $prepared['id'] ) ),
600 ),
601 'about' => array(
602 'href' => rest_url( sprintf( 'wp/v2/widget-types/%s', $id_base ) ),
603 'embeddable' => true,
604 ),
605 'https://api.w.org/sidebar' => array(
606 'href' => rest_url( sprintf( 'wp/v2/sidebars/%s/', $prepared['sidebar'] ) ),
607 ),
608 );
609 }
610
611 /**
612 * Retrieves a widget instance.
613 *
614 * @param array $sidebar sidebar data available at $wp_registered_sidebars.
615 * @param string $id Identifier of the widget instance.
616 *
617 * @return array Array containing the widget instance.
618 */
619 protected function get_sidebar_widget_instance( $sidebar, $id ) {
620 list( $object, $number, $name ) = $this->get_widget_info( $id );
621 if ( ! $object ) {
622 return array();
623 }
624
625 $object->_set( $number );
626
627 $instances = $object->get_settings();
628 $instance = $instances[ $number ];
629
630 $args = array_merge(
631 is_array( $sidebar ) ? $sidebar : array(),
632 array(
633 'widget_id' => $id,
634 'widget_name' => $name,
635 )
636 );
637
638 /** This filter is documented in wp-includes/class-wp-widget.php */
639 $instance = apply_filters( 'widget_display_callback', $instance, $object, $args );
640
641 if ( false === $instance ) {
642 return array();
643 }
644
645 return $instance;
646 }
647
648 /**
649 * Given a widget id returns an array containing information about the widget.
650 *
651 * @param string $widget_id Identifier of the widget.
652 *
653 * @return array Array containing the the widget object, the number, and the name.
654 */
655 protected function get_widget_info( $widget_id ) {
656 global $wp_registered_widgets;
657
658 if (
659 ! is_array( $wp_registered_widgets[ $widget_id ]['callback'] ) ||
660 ! isset( $wp_registered_widgets[ $widget_id ]['callback'][0] ) ||
661 ! isset( $wp_registered_widgets[ $widget_id ]['params'][0]['number'] ) ||
662 ! isset( $wp_registered_widgets[ $widget_id ]['name'] ) ||
663 ! ( $wp_registered_widgets[ $widget_id ]['callback'][0] instanceof WP_Widget )
664 ) {
665 return array( null, null, null );
666 }
667
668 $object = $wp_registered_widgets[ $widget_id ]['callback'][0];
669 $number = $wp_registered_widgets[ $widget_id ]['params'][0]['number'];
670 $name = $wp_registered_widgets[ $widget_id ]['name'];
671
672 return array( $object, $number, $name );
673 }
674
675 /**
676 * Checks if the given widget id is a reference widget, ie one that does not use WP_Widget.
677 *
678 * @since 5.6.0
679 *
680 * @param string $widget_id The widget id to check.
681 * @return bool Whether this is a reference widget or not.
682 */
683 protected function is_reference_widget( $widget_id ) {
684 list ( $object ) = $this->get_widget_info( $widget_id );
685
686 return ! $object instanceof WP_Widget;
687 }
688
689 /**
690 * Gets the list of collection params.
691 *
692 * @since 5.6.0
693 *
694 * @return array[]
695 */
696 public function get_collection_params() {
697 return array(
698 'context' => $this->get_context_param( array( 'default' => 'view' ) ),
699 'sidebar' => array(
700 'description' => __( 'The sidebar to return widgets for.', 'gutenberg' ),
701 'type' => 'string',
702 ),
703 );
704 }
705
706 /**
707 * Retrieves the widget's schema, conforming to JSON Schema.
708 *
709 * @since 5.6.0
710 *
711 * @return array Item schema data.
712 */
713 public function get_item_schema() {
714 if ( $this->schema ) {
715 return $this->add_additional_fields_schema( $this->schema );
716 }
717
718 $this->schema = array(
719 '$schema' => 'http://json-schema.org/draft-04/schema#',
720 'title' => 'widget',
721 'type' => 'object',
722 'properties' => array(
723 'id' => array(
724 'description' => __( 'Unique identifier for the widget.', 'gutenberg' ),
725 'type' => 'string',
726 'context' => array( 'view', 'edit', 'embed' ),
727 ),
728 'id_base' => array(
729 'description' => __( 'Type of widget for the object.', 'gutenberg' ),
730 'type' => 'string',
731 'context' => array( 'view', 'edit', 'embed' ),
732 ),
733 'sidebar' => array(
734 'description' => __( 'The sidebar the widget belongs to.', 'gutenberg' ),
735 'type' => 'string',
736 'default' => 'wp_inactive_widgets',
737 'required' => true,
738 'context' => array( 'view', 'edit', 'embed' ),
739 ),
740 'widget_class' => array(
741 'description' => __( 'Class name of the widget implementation.', 'gutenberg' ),
742 'type' => 'string',
743 'context' => array( 'view', 'edit', 'embed' ),
744 ),
745 'name' => array(
746 'description' => __( 'Name of the widget.', 'gutenberg' ),
747 'type' => 'string',
748 'context' => array( 'view', 'edit', 'embed' ),
749 ),
750 'description' => array(
751 'description' => __( 'Description of the widget.', 'gutenberg' ),
752 'type' => 'string',
753 'context' => array( 'view', 'edit', 'embed' ),
754 ),
755 'number' => array(
756 'description' => __( 'Number of the widget.', 'gutenberg' ),
757 'type' => 'integer',
758 'context' => array( 'view', 'edit', 'embed' ),
759 ),
760 'rendered' => array(
761 'description' => __( 'HTML representation of the widget.', 'gutenberg' ),
762 'type' => 'string',
763 'context' => array( 'view', 'edit', 'embed' ),
764 'readonly' => true,
765 ),
766 'rendered_form' => array(
767 'description' => __( 'HTML representation of the widget admin form.', 'gutenberg' ),
768 'type' => 'string',
769 'context' => array( 'edit' ),
770 'readonly' => true,
771 ),
772 'settings' => array(
773 'description' => __( 'Settings of the widget.', 'gutenberg' ),
774 'type' => 'object',
775 'context' => array( 'view', 'edit', 'embed' ),
776 'default' => array(),
777 ),
778 ),
779 );
780
781 return $this->add_additional_fields_schema( $this->schema );
782 }
783 }
784