PluginProbe
Gutenberg / 9.1.1
Gutenberg v9.1.1
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 12.6.0 All 403 releases
gutenberg / lib / class-wp-rest-widget-utils-controller.php

class-wp-rest-widget-utils-controller.php in Gutenberg 9.1.1, at lib/class-wp-rest-widget-utils-controller.php

161 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Start: Include for phase 2
4 * Widget Updater REST API: WP_REST_Widget_Forms class
5 *
6 * @package gutenberg
7 * @since 5.2.0
8 */
9
10 /**
11 * Controller which provides REST endpoint for updating a widget.
12 *
13 * @since 5.2.0
14 *
15 * @see WP_REST_Controller
16 */
17 class WP_REST_Widget_Utils_Controller extends WP_REST_Controller {
18
19 /**
20 * Constructs the controller.
21 *
22 * @access public
23 */
24 public function __construct() {
25 $this->namespace = '__experimental';
26 $this->rest_base = 'widget-utils';
27 }
28
29 /**
30 * Registers the necessary REST API route.
31 *
32 * @access public
33 */
34 public function register_routes() {
35 register_rest_route(
36 $this->namespace,
37 '/' . $this->rest_base . '/form/(?P<widget_class>[^/]*)/',
38 array(
39 'args' => array(
40 'widget_class' => array(
41 'description' => __( 'Class name of the widget.', 'gutenberg' ),
42 'type' => 'string',
43 'required' => true,
44 'validate_callback' => array( $this, 'is_valid_widget' ),
45 ),
46 'instance' => array(
47 'description' => __( 'Current widget instance', 'gutenberg' ),
48 'type' => 'object',
49 'default' => array(),
50 ),
51 ),
52 array(
53 'methods' => WP_REST_Server::EDITABLE,
54 'permission_callback' => array( $this, 'permissions_check' ),
55 'callback' => array( $this, 'compute_widget_form' ),
56 ),
57 )
58 );
59 }
60
61 /**
62 * Checks if the user has permissions to make the request.
63 *
64 * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
65 * @since 5.2.0
66 * @access public
67 */
68 public function permissions_check() {
69 // Verify if the current user has edit_theme_options capability.
70 // This capability is required to access the widgets screen.
71 if ( ! current_user_can( 'edit_theme_options' ) ) {
72 return new WP_Error(
73 'widgets_cannot_access',
74 __( 'Sorry, you are not allowed to access widgets on this site.', 'gutenberg' ),
75 array(
76 'status' => rest_authorization_required_code(),
77 )
78 );
79 }
80
81 return true;
82 }
83
84 /**
85 * Checks if the widget being referenced is valid.
86 *
87 * @param string $widget_class Name of the class the widget references.
88 *
89 * @return boolean| True if the widget being referenced exists and false otherwise.
90 * @since 5.2.0
91 * @access public
92 */
93 public function is_valid_widget( $widget_class ) {
94 $widget_class = urldecode( $widget_class );
95 global $wp_widget_factory;
96 if ( ! $widget_class ) {
97 return false;
98 }
99
100 return isset( $wp_widget_factory->widgets[ $widget_class ] ) &&
101 ( $wp_widget_factory->widgets[ $widget_class ] instanceof WP_Widget );
102 }
103
104 /**
105 * Returns the new widget instance and the form that represents it.
106 *
107 * @param WP_REST_Request $request Full details about the request.
108 *
109 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
110 * @since 5.7.0
111 * @access public
112 */
113 public function compute_widget_form( $request ) {
114 $widget_class = urldecode( $request->get_param( 'widget_class' ) );
115 $instance = $request->get_param( 'instance' );
116
117 global $wp_widget_factory;
118 $widget_obj = $wp_widget_factory->widgets[ $widget_class ];
119
120 $widget_obj->_set( -1 );
121 ob_start();
122
123 $instance = apply_filters( 'widget_form_callback', $instance, $widget_obj );
124
125 $return = null;
126 if ( false !== $instance ) {
127 $return = $widget_obj->form( $instance );
128
129 /**
130 * Fires at the end of the widget control form.
131 *
132 * Use this hook to add extra fields to the widget form. The hook
133 * is only fired if the value passed to the 'widget_form_callback'
134 * hook is not false.
135 *
136 * Note: If the widget has no form, the text echoed from the default
137 * form method can be hidden using CSS.
138 *
139 * @param WP_Widget $widget_obj The widget instance (passed by reference).
140 * @param null $return Return null if new fields are added.
141 * @param array $instance An array of the widget's settings.
142 *
143 * @since 5.2.0
144 */
145 do_action_ref_array( 'in_widget_form', array( &$widget_obj, &$return, $instance ) );
146 }
147 $form = ob_get_clean();
148
149 return rest_ensure_response(
150 array(
151 'instance' => $instance,
152 'form' => $form,
153 )
154 );
155 }
156
157 }
158 /**
159 * End: Include for phase 2
160 */
161