PluginProbe
Gutenberg / 8.9.3
Gutenberg v8.9.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-widget-utils-controller.php

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

160 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 */
92 private function is_valid_widget( $widget_class ) {
93 $widget_class = urldecode( $widget_class );
94 global $wp_widget_factory;
95 if ( ! $widget_class ) {
96 return false;
97 }
98
99 return isset( $wp_widget_factory->widgets[ $widget_class ] ) &&
100 ( $wp_widget_factory->widgets[ $widget_class ] instanceof WP_Widget );
101 }
102
103 /**
104 * Returns the new widget instance and the form that represents it.
105 *
106 * @param WP_REST_Request $request Full details about the request.
107 *
108 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
109 * @since 5.7.0
110 * @access public
111 */
112 public function compute_widget_form( $request ) {
113 $widget_class = urldecode( $request->get_param( 'widget_class' ) );
114 $instance = $request->get_param( 'instance' );
115
116 global $wp_widget_factory;
117 $widget_obj = $wp_widget_factory->widgets[ $widget_class ];
118
119 $widget_obj->_set( -1 );
120 ob_start();
121
122 $instance = apply_filters( 'widget_form_callback', $instance, $widget_obj );
123
124 $return = null;
125 if ( false !== $instance ) {
126 $return = $widget_obj->form( $instance );
127
128 /**
129 * Fires at the end of the widget control form.
130 *
131 * Use this hook to add extra fields to the widget form. The hook
132 * is only fired if the value passed to the 'widget_form_callback'
133 * hook is not false.
134 *
135 * Note: If the widget has no form, the text echoed from the default
136 * form method can be hidden using CSS.
137 *
138 * @param WP_Widget $widget_obj The widget instance (passed by reference).
139 * @param null $return Return null if new fields are added.
140 * @param array $instance An array of the widget's settings.
141 *
142 * @since 5.2.0
143 */
144 do_action_ref_array( 'in_widget_form', array( &$widget_obj, &$return, $instance ) );
145 }
146 $form = ob_get_clean();
147
148 return rest_ensure_response(
149 array(
150 'instance' => $instance,
151 'form' => $form,
152 )
153 );
154 }
155
156 }
157 /**
158 * End: Include for phase 2
159 */
160