-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathclass-wp-rest-block-editor-settings-controller.php
325 lines (281 loc) · 11.8 KB
/
class-wp-rest-block-editor-settings-controller.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php
/**
* REST API: WP_REST_Block_Editor_Settings_Controller class
*
* @package WordPress
* @subpackage REST_API
*/
if ( ! class_exists( 'WP_REST_Block_Editor_Settings_Controller' ) ) {
/**
* Core class used to retrieve the block editor settings via the REST API.
*
* @see WP_REST_Controller
*/
class WP_REST_Block_Editor_Settings_Controller extends WP_REST_Controller {
/**
* Constructs the controller.
*/
public function __construct() {
$this->namespace = 'wp-block-editor/v1';
$this->rest_base = 'settings';
}
/**
* Registers the necessary REST API routes.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}
/**
* Checks whether a given request has permission to read block editor settings
*
* @since 5.8.0
*
* @param WP_REST_Request $request Full details about the request.
*
* @return WP_Error|bool True if the request has permission, WP_Error object otherwise.
*/
public function get_items_permissions_check( $request ) {// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
if ( current_user_can( 'edit_posts' ) ) {
return true;
}
foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) {
if ( current_user_can( $post_type->cap->edit_posts ) ) {
return true;
}
}
return new WP_Error(
'rest_cannot_read_block_editor_settings',
__( 'Sorry, you are not allowed to read the block editor settings.', 'gutenberg' ),
array( 'status' => rest_authorization_required_code() )
);
}
/**
* Returns the block editor's settings
*
* @since 5.8.0
*
* @param WP_REST_Request $request Full details about the request.
*
* @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure.
*/
public function get_items( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis
switch ( $request['context'] ) {
case 'post-editor':
default:
$editor_context_name = 'core/edit-post';
break;
case 'widgets-editor':
$editor_context_name = 'core/edit-widgets';
break;
case 'site-editor':
$editor_context_name = 'core/edit-site';
break;
case 'mobile':
$editor_context_name = 'core/mobile';
break;
}
add_filter( 'block_editor_settings_all', 'gutenberg_get_block_editor_settings_mobile', PHP_INT_MAX );
$editor_context = new WP_Block_Editor_Context( array( 'name' => $editor_context_name ) );
$settings = get_block_editor_settings( array(), $editor_context );
remove_filter( 'block_editor_settings_all', 'gutenberg_get_block_editor_settings_mobile', PHP_INT_MAX );
return rest_ensure_response( $settings );
}
/**
* Retrieves the block editor's settings schema, conforming to JSON Schema.
*
* @since 5.8.0
*
* @return array Item schema data.
*/
public function get_item_schema() {
if ( $this->schema ) {
return $this->add_additional_fields_schema( $this->schema );
}
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'block-editor-settings-item',
'type' => 'object',
'properties' => array(
'__unstableEnableFullSiteEditingBlocks' => array(
'description' => __( 'Enables experimental Site Editor blocks', 'gutenberg' ),
'type' => 'boolean',
'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ),
),
'styles' => array(
'description' => __( 'Editor styles', 'gutenberg' ),
'type' => 'array',
'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ),
),
'supportsTemplateMode' => array(
'description' => __( 'Indicates whether the current theme supports block-based templates.', 'gutenberg' ),
'type' => 'boolean',
'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ),
),
'supportsLayout' => array(
'description' => __( 'Enable/disable layouts support in container blocks.', 'gutenberg' ),
'type' => 'boolean',
'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ),
),
'widgetTypesToHideFromLegacyWidgetBlock' => array(
'description' => __( 'Widget types to hide from Legacy Widget block.', 'gutenberg' ),
'type' => 'array',
'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ),
),
'__experimentalFeatures' => array(
'description' => __( 'Settings consolidated from core, theme, and user origins.', 'gutenberg' ),
'type' => 'object',
'context' => array( 'post-editor', 'site-editor', 'widgets-editor', 'mobile' ),
),
'__experimentalStyles' => array(
'description' => __( 'Styles consolidated from core, theme, and user origins.', 'gutenberg' ),
'type' => 'object',
'context' => array( 'mobile' ),
),
'__experimentalEnableQuoteBlockV2' => array(
'description' => __( 'Whether the V2 of the quote block that uses inner blocks should be enabled.', 'gutenberg' ),
'type' => 'boolean',
'context' => array( 'mobile' ),
),
'__experimentalEnableListBlockV2' => array(
'description' => __( 'Whether the V2 of the list block that uses inner blocks should be enabled.', 'gutenberg' ),
'type' => 'boolean',
'context' => array( 'mobile' ),
),
'alignWide' => array(
'description' => __( 'Enable/Disable Wide/Full Alignments.', 'gutenberg' ),
'type' => 'boolean',
'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ),
),
'allowedBlockTypes' => array(
'description' => __( 'List of allowed block types.', 'gutenberg' ),
'type' => 'boolean',
'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ),
),
'allowedMimeTypes' => array(
'description' => __( 'List of allowed mime types and file extensions.', 'gutenberg' ),
'type' => 'object',
'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ),
),
'blockCategories' => array(
'description' => __( 'Returns all the categories for block types that will be shown in the block editor.', 'gutenberg' ),
'type' => 'array',
'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ),
),
'blockInspectorTabs' => array(
'description' => __( 'Block inspector tab display overrides.', 'gutenberg' ),
'type' => 'object',
'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ),
),
'disableCustomColors' => array(
'description' => __( 'Disables custom colors.', 'gutenberg' ),
'type' => 'boolean',
'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ),
),
'disableCustomFontSizes' => array(
'description' => __( 'Disables custom font size.', 'gutenberg' ),
'type' => 'boolean',
'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ),
),
'disableCustomGradients' => array(
'description' => __( 'Disables custom font size.', 'gutenberg' ),
'type' => 'boolean',
'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ),
),
'disableLayoutStyles' => array(
'description' => __( 'Disables output of layout styles.', 'gutenberg' ),
'type' => 'boolean',
'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ),
),
'enableCustomLineHeight' => array(
'description' => __( 'Enables custom line height.', 'gutenberg' ),
'type' => 'boolean',
'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ),
),
'enableCustomSpacing' => array(
'description' => __( 'Enables custom spacing.', 'gutenberg' ),
'type' => 'boolean',
'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ),
),
'enableCustomUnits' => array(
'description' => __( 'Enables custom units.', 'gutenberg' ),
'type' => 'boolean',
'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ),
),
'isRTL' => array(
'description' => __( 'Determines whether the current locale is right-to-left (RTL).', 'gutenberg' ),
'type' => 'boolean',
'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ),
),
'imageDefaultSize' => array(
'description' => __( 'Default size for images.', 'gutenberg' ),
'type' => 'string',
'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ),
),
'imageDimensions' => array(
'description' => __( 'Available image dimensions.', 'gutenberg' ),
'type' => 'object',
'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ),
),
'imageEditing' => array(
'description' => __( 'Determines whether the image editing feature is enabled.', 'gutenberg' ),
'type' => 'boolean',
'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ),
),
'imageSizes' => array(
'description' => __( 'Available image sizes.', 'gutenberg' ),
'type' => 'array',
'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ),
),
'maxUploadFileSize' => array(
'description' => __( 'Maximum upload size in bytes allowed for the site.', 'gutenberg' ),
'type' => 'number',
'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ),
),
'colors' => array(
'description' => __( 'Active theme color palette.', 'gutenberg' ),
'type' => 'array',
'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ),
),
'fontSizes' => array(
'description' => __( 'Active theme font sizes.', 'gutenberg' ),
'type' => 'array',
'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ),
),
'gradients' => array(
'description' => __( 'Active theme gradients.', 'gutenberg' ),
'type' => 'array',
'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ),
),
'spacingSizes' => array(
'description' => __( 'Active theme spacing sizes.', 'gutenberg' ),
'type' => 'array',
'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ),
),
'spacingScale' => array(
'description' => __( 'Active theme spacing scale.', 'gutenberg' ),
'type' => 'array',
'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ),
),
'disableCustomSpacingSizes' => array(
'description' => __( 'Disables custom spacing sizes.', 'gutenberg' ),
'type' => 'boolean',
'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ),
),
),
);
$this->schema = $schema;
return $this->add_additional_fields_schema( $this->schema );
}
}
}