PluginProbe
Gutenberg / 7.4.0
Gutenberg v7.4.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 7.4.0 All 402 releases
gutenberg / lib / class-wp-rest-widget-areas-controller.php

class-wp-rest-widget-areas-controller.php in Gutenberg 7.4.0, at lib/class-wp-rest-widget-areas-controller.php

276 lines 8.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 Areas REST API: WP_REST_Widget_Areas_Controller class
5 *
6 * @package gutenberg
7 * @since 5.7.0
8 */
9
10 /**
11 * Controller which provides REST endpoint for the widget areas.
12 *
13 * @since 5.2.0
14 *
15 * @see WP_REST_Controller
16 */
17 class WP_REST_Widget_Areas_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-areas';
27 }
28
29 /**
30 * Registers the necessary REST API routes.
31 *
32 * @access public
33 */
34 public function register_routes() {
35 register_rest_route(
36 $this->namespace,
37 '/' . $this->rest_base,
38 array(
39 array(
40 'methods' => WP_REST_Server::READABLE,
41 'callback' => array( $this, 'get_items' ),
42 'permission_callback' => array( $this, 'get_items_permissions_check' ),
43 ),
44 'schema' => array( $this, 'get_public_item_schema' ),
45 )
46 );
47
48 $id_argument = array(
49 'description' => __( 'The sidebar’s ID.', 'gutenberg' ),
50 'type' => 'string',
51 'required' => true,
52 'validate_callback' => 'Experimental_WP_Widget_Blocks_Manager::is_valid_sidebar_id',
53 );
54
55 register_rest_route(
56 $this->namespace,
57 '/' . $this->rest_base . '/(?P<id>.+)',
58 array(
59 'args' => array(
60 'id' => $id_argument,
61 ),
62 array(
63 'methods' => WP_REST_Server::READABLE,
64 'callback' => array( $this, 'get_item' ),
65 'permission_callback' => array( $this, 'get_items_permissions_check' ),
66 ),
67 array(
68 'methods' => WP_REST_Server::EDITABLE,
69 'callback' => array( $this, 'update_item' ),
70 'permission_callback' => array( $this, 'update_item_permissions_check' ),
71 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
72 ),
73 'schema' => array( $this, 'get_public_item_schema' ),
74 )
75 );
76 }
77
78 /**
79 * Retrieves the comment's schema, conforming to JSON Schema.
80 *
81 * @since 6.1.0
82 *
83 * @return array
84 */
85 public function get_item_schema() {
86 $schema = array(
87 '$schema' => 'http://json-schema.org/draft-04/schema#',
88 'title' => 'widget-area',
89 'type' => 'object',
90 'properties' => array(
91 'id' => array(
92 'description' => __( 'Unique identifier for the object.', 'gutenberg' ),
93 'type' => 'string',
94 'context' => array( 'view', 'edit', 'embed' ),
95 'readonly' => true,
96 ),
97 'content' => array(
98 'description' => __( 'The content for the object.', 'gutenberg' ),
99 'type' => 'object',
100 'context' => array( 'view', 'edit', 'embed' ),
101 'arg_options' => array(
102 'sanitize_callback' => null,
103 'validate_callback' => null,
104 ),
105 'properties' => array(
106 'raw' => array(
107 'description' => __( 'Content for the object, as it exists in the database.', 'gutenberg' ),
108 'type' => 'string',
109 'context' => array( 'view', 'edit', 'embed' ),
110 ),
111 'rendered' => array(
112 'description' => __( 'HTML content for the object, transformed for display.', 'gutenberg' ),
113 'type' => 'string',
114 'context' => array( 'view', 'edit', 'embed' ),
115 'readonly' => true,
116 ),
117 'block_version' => array(
118 'description' => __( 'Version of the content block format used by the object.', 'gutenberg' ),
119 'type' => 'integer',
120 'context' => array( 'view', 'edit', 'embed' ),
121 'readonly' => true,
122 ),
123 ),
124 ),
125 ),
126 );
127
128 return $schema;
129 }
130
131 /**
132 * Checks whether a given request has permission to read widget areas.
133 *
134 * @since 5.7.0
135 *
136 * @param WP_REST_Request $request Full details about the request.
137 * @return WP_Error|bool True if the request has read access, WP_Error object otherwise.
138 *
139 * This function is overloading a function defined in WP_REST_Controller so it should have the same parameters.
140 * phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
141 */
142 public function get_items_permissions_check( $request ) {
143 if ( ! current_user_can( 'edit_theme_options' ) ) {
144 return new WP_Error(
145 'rest_user_cannot_view',
146 __( 'Sorry, you are not allowed to read sidebars.', 'gutenberg' )
147 );
148 }
149
150 return true;
151 }
152 /* phpcs:enable */
153
154 /**
155 * Retrieves all widget areas.
156 *
157 * @since 5.7.0
158 *
159 * @param WP_REST_Request $request Full details about the request.
160 * @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure.
161 */
162 public function get_items( $request ) {
163 global $wp_registered_sidebars;
164
165 $data = array();
166
167 foreach ( array_keys( $wp_registered_sidebars ) as $sidebar_id ) {
168 $data[ $sidebar_id ] = $this->get_sidebar_data( $sidebar_id );
169 }
170
171 return rest_ensure_response( $data );
172 }
173
174 /**
175 * Retrieves a specific widget area.
176 *
177 * @since 5.7.0
178 *
179 * @param WP_REST_Request $request Full details about the request.
180 * @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure.
181 */
182 public function get_item( $request ) {
183 return rest_ensure_response( $this->get_sidebar_data( $request['id'] ) );
184 }
185
186 /**
187 * Checks if a given REST request has access to update a widget area.
188 *
189 * @since 5.7.0
190 *
191 * @param WP_REST_Request $request Full details about the request.
192 * @return WP_Error|bool True if the request has access to update the item, error object otherwise.
193 *
194 * This function is overloading a function defined in WP_REST_Controller so it should have the same parameters.
195 * phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
196 */
197 public function update_item_permissions_check( $request ) {
198 if ( ! current_user_can( 'edit_theme_options' ) ) {
199 return new WP_Error(
200 'rest_user_cannot_edit',
201 __( 'Sorry, you are not allowed to edit sidebars.', 'gutenberg' )
202 );
203 }
204
205 return true;
206 }
207 /* phpcs:enable */
208
209 /**
210 * Updates a single widget area.
211 *
212 * @since 5.7.0
213 *
214 * @param WP_REST_Request $request Full details about the request.
215 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
216 */
217 public function update_item( $request ) {
218 $sidebar_id = $request->get_param( 'id' );
219 $sidebar_content = $request->get_param( 'content' );
220
221 if ( ! is_string( $sidebar_content ) && isset( $sidebar_content['raw'] ) ) {
222 $sidebar_content = $sidebar_content['raw'];
223 }
224
225 $id_referenced_in_sidebar = Experimental_WP_Widget_Blocks_Manager::get_post_id_referenced_in_sidebar( $sidebar_id );
226
227 $post_id = wp_insert_post(
228 array(
229 'ID' => $id_referenced_in_sidebar,
230 'post_content' => $sidebar_content,
231 'post_type' => 'wp_area',
232 )
233 );
234
235 if ( 0 === $id_referenced_in_sidebar ) {
236 Experimental_WP_Widget_Blocks_Manager::reference_post_id_in_sidebar( $sidebar_id, $post_id );
237 }
238
239 return rest_ensure_response( $this->get_sidebar_data( $request['id'] ) );
240 }
241
242 /**
243 * Returns the sidebar data together with a content array containing the blocks present in the sidebar.
244 * The bocks may be legacy widget blocks representing the widgets currently present in the sidebar, or the content of a wp_area post that the sidebar references.
245 *
246 * @since 5.7.0
247 *
248 * @param string $sidebar_id Identifier of the sidebar.
249 * @return object Sidebar data with a content array.
250 */
251 protected function get_sidebar_data( $sidebar_id ) {
252 $content_string = '';
253
254 $post_id_referenced_in_sidebar = Experimental_WP_Widget_Blocks_Manager::get_post_id_referenced_in_sidebar( $sidebar_id );
255
256 if ( 0 !== $post_id_referenced_in_sidebar ) {
257 $post = get_post( $post_id_referenced_in_sidebar );
258 $content_string = $post->post_content;
259 } else {
260 $blocks = Experimental_WP_Widget_Blocks_Manager::get_sidebar_as_blocks( $sidebar_id );
261 $content_string = Experimental_WP_Widget_Blocks_Manager::serialize_blocks( $blocks );
262 }
263
264 return array_merge(
265 Experimental_WP_Widget_Blocks_Manager::get_wp_registered_sidebars_sidebar( $sidebar_id ),
266 array(
267 'content' => array(
268 'raw' => $content_string,
269 'rendered' => apply_filters( 'the_content', $content_string ),
270 'block_version' => block_version( $content_string ),
271 ),
272 )
273 );
274 }
275 }
276