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