Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new endpoint that exposes block editor settings through the REST API #29969

Merged
merged 24 commits into from
May 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
484e3ee
Add a new endpoint that exposes block editor settings through the RES…
Tug Sep 10, 2020
9737fbc
Merge branch 'master' into add/block-editor-settings-rest-endpoint
Feb 25, 2021
0b90f30
Update permission check
Feb 25, 2021
ba80bcc
Global styles - Add check for mobile
Mar 1, 2021
d1cc0f5
Fix lint issues
Mar 1, 2021
1cfd9d1
Merge branch 'trunk' into add/block-editor-settings-rest-endpoint
Mar 15, 2021
052d241
Block editor mobile settings endpoint
Mar 16, 2021
b4205d5
Move theme support data within the if condition
Mar 17, 2021
d332e6e
Add schema
Mar 18, 2021
e3e1c77
Update naming
Mar 18, 2021
2c7abac
Update endpoint name
Mar 22, 2021
ffe9ac1
Use require_once
Mar 22, 2021
6837fa7
Use block editor settings
Mar 25, 2021
d9f3277
Merge branch 'trunk' into add/global-styles-mobile-endpoint
Apr 8, 2021
e18eaa3
Merge branch 'trunk' into add/global-styles-mobile-endpoint
Apr 19, 2021
2328ff6
Usage of get_default_block_editor_settings for the rest api endpoint
Apr 5, 2021
ede0fc1
Block editor settings update schema
Apr 19, 2021
ddb0dcd
Merge branch 'trunk' into add/global-styles-mobile-endpoint
Apr 20, 2021
3a3908f
Add context as a param to get the block editor settings, update permi…
Apr 20, 2021
108debd
Remove unneeded filter
Apr 20, 2021
2d24002
Remove unneeded changes
Apr 20, 2021
ec6fa21
Update context in schema
Apr 21, 2021
f5db2fc
Update context for global styles attributes
Apr 26, 2021
3897d19
Update class-wp-rest-block-editor-settings-controller.php
gziolo May 4, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
267 changes: 267 additions & 0 deletions lib/class-wp-rest-block-editor-settings-controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
<?php
/**
* REST API: WP_REST_Block_Editor_Settings_Controller class
*
* @package WordPress
* @subpackage REST_API
*/

/**
* 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 = '__experimental/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 ) {
$context = ! empty( $request['context'] ) ? $request['context'] : 'post-editor';
$settings = gutenberg_get_block_editor_settings( $context );

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 );
}

$this->schema = array(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sounds great. It looks like there are some items that are off in terms of in which context they're available. Some that I've noticed that should be site-editor only are: __experimentalGlobalStylesUserEntityId, __experimentalGlobalStylesBaseStyles. Other than that, it seems that we can land this PR already? The global styles settings use the old filter block_editor_settings and we should update it but it doesn't block this PR, as far as I'm aware.

Also: any thoughts on best practices to keep this schema up to date as things evolve would be great.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also: any thoughts on best practices to keep this schema up to date as things evolve would be great.

If we would start using the same endpoint for the web then it could help to maintain the schema 😄

In related efforts, I plan we switch to the REST API endpoint for block types on the web rather than expose them with JS function call on the server.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @nosolosw !

Some that I've noticed that should be site-editor only are: __experimentalGlobalStylesUserEntityId, __experimentalGlobalStylesBaseStyles

I updated those to be site-editor only.

The global styles settings use the old filter block_editor_settings and we should update it but it doesn't block this PR, as far as I'm aware.

Awesome 😃! Do you know more or less when that's going to be updated?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just merged #31762 which adds more data for mobile. Upon hitting the button, it just occurred to me that something was off. I think it boils down to the contexts we define in this PR: post-editor, site-editor, widgets-editor. Shouldn't we also have a mobile context?

My understanding is that we use this to filter what kind of data we pass through the request. If the plan is to move the settings retrieval to use the REST request, checks like https://github.com/WordPress/gutenberg/pull/31762/files#diff-0b8e98dead5542e95159bd3c482158111d3f16637e7eef9af13373358fa42dedR91 don't make sense. However, the current contexts are also not enough. I'm thinking we actually need a mobile context as well. @geriux et all, do you have thoughts on this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking we actually need a mobile context as well. @geriux et all, do you have thoughts on this?

I think we might if we want specific data like the consolidated styles we added here since the web editor doesn't look like it'd need that right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, prepared #31819 to add a mobile context.

'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'block-editor-settings-item',
'type' => 'object',
'properties' => array(
'__unstableEnableFullSiteEditingBlocks' => array(
'description' => __( 'Enables experimental Full Site Editing 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' => __( 'Returns if the current theme is full site editing-enabled or not.', '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' => __( 'Active theme settings and default values.', 'gutenberg' ),
'type' => 'object',
'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ),
),

'__experimentalGlobalStylesUserEntityId' => array(
'description' => __( 'Global styles user entity ID.', 'gutenberg' ),
'type' => 'integer',
'context' => array( 'site-editor' ),
),

'__experimentalGlobalStylesBaseStyles' => array(
'description' => __( 'Global styles settings.', 'gutenberg' ),
'type' => 'object',
'context' => array( 'site-editor' ),
),

'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' ),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've noted this in other PRs and on Trac, but the lack of post type context constitutes a large functional break for all of the Gutenberg sites I've worked on, and I expect for many others as well. Being able to restrict certain blocks to certain post types is a crucial feature of many advanced block editor implementations.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We discussed with @chrisvanpatten in WordPress/wordpress-develop#1118 (comment) that it should be considered passing additional params in addition to the context to bring feature parity with how the edit post editor works today. It looks like the possibility to change settings per individual post/page is used by the community. It means that we should explore how params like post_id could be passed and consumed by this endpoint.

),

'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' ),
),

'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' ),
),

'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' ),
),
),
);

return $this->add_additional_fields_schema( $this->schema );
}
}
3 changes: 3 additions & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ function gutenberg_is_experiment_enabled( $name ) {
if ( ! class_exists( 'WP_REST_Templates_Controller' ) ) {
require_once __DIR__ . '/full-site-editing/class-wp-rest-templates-controller.php';
}
if ( ! class_exists( 'WP_REST_Block_Editor_Settings_Controller' ) ) {
require_once dirname( __FILE__ ) . '/class-wp-rest-block-editor-settings-controller.php';
}
/**
* End: Include for phase 2
*/
Expand Down
9 changes: 9 additions & 0 deletions lib/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ function gutenberg_register_batch_endpoint() {
}
add_action( 'rest_api_init', 'gutenberg_register_batch_endpoint' );

/**
* Registers the Block editor settings REST API routes.
*/
function gutenberg_register_block_editor_settings() {
$editor_settings = new WP_REST_Block_Editor_Settings_Controller();
$editor_settings->register_routes();
}
add_action( 'rest_api_init', 'gutenberg_register_block_editor_settings' );

/**
* Hook in to the nav menu item post type and enable a post type rest endpoint.
*
Expand Down