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 10 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
125 changes: 125 additions & 0 deletions lib/class-wp-rest-block-editor-global-styles-settings-controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php
/**
* REST API: WP_REST_Block_Editor_Global_Styles_Settings_Controller class
*
* @package WordPress
* @subpackage REST_API
*/

/**
* Core class used to retrieve the block editor global styles settings via the REST API.
*
* @see WP_REST_Controller
*/
class WP_REST_Block_Editor_Global_Styles_Settings_Controller extends WP_REST_Controller {
/**
* Constructs the controller.
*/
public function __construct() {
$this->namespace = 'wp/v2';
$this->rest_base = 'block-editor-global-styles-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' ) ) {
$error = __( 'Sorry, you are not allowed to read the block editor settings.', 'gutenberg' );
return new WP_Error( 'rest_cannot_read_settings', $error, array( 'status' => rest_authorization_required_code() ) );
}

return true;
}

/**
* Returns the block editor's global styles 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.UnusedVariable
$settings = apply_filters( 'block_editor_global_styles_rest_settings', array() );

return rest_ensure_response( $settings );
}

/**
* Retrieves the block editor's global styles 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(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'block-editor-global-styles-settings-item',
'type' => 'object',
'properties' => array(
'colors' => array(
'description' => __( 'Active theme colors.', 'gutenberg' ),
'type' => 'array',
'context' => array( 'view' ),
),

'gradients' => array(
'description' => __( 'Active theme gradients.', 'gutenberg' ),
'type' => 'array',
'context' => array( 'view' ),
),

'globalStyles' => array(
'description' => __( 'Theme support for global styles.', 'gutenberg' ),
'type' => 'boolean',
'context' => array( 'view' ),
),

'globalStylesUserEntityId' => array(
'description' => __( 'User entity id for Global styles settings.', 'gutenberg' ),
'type' => 'integer',
'context' => array( 'view' ),
),

'globalStylesBaseStyles' => array(
'description' => __( 'Global styles settings.', 'gutenberg' ),
'type' => 'object',
'context' => array( 'view' ),
),
),
);

return $this->add_additional_fields_schema( $this->schema );
}
}
35 changes: 35 additions & 0 deletions lib/global-styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,40 @@ function_exists( 'gutenberg_is_edit_site_page' ) &&
return $settings;
}

/**
* Adds the necessary data of Global Styles for the REST API.
*
* @param array $settings Existing block editor settings.
* @return array New block editor settings
*/
function gutenberg_global_styles_rest_settings( $settings ) {
$color_palette = current( (array) get_theme_support( 'editor-color-palette' ) );
if ( false !== $color_palette ) {
$settings['colors'] = $color_palette;
}

$gradient_presets = current( (array) get_theme_support( 'editor-gradient-presets' ) );
if ( false !== $gradient_presets ) {
$settings['gradients'] = $gradient_presets;
}

$settings['globalStyles'] = WP_Theme_JSON_Resolver::theme_has_support();

if (
WP_Theme_JSON_Resolver::theme_has_support() &&
gutenberg_is_fse_theme()
) {
$theme_support_data = gutenberg_experimental_global_styles_get_theme_support_settings( $settings );
$user_cpt_id = WP_Theme_JSON_Resolver::get_user_custom_post_type_id();
$base_styles = WP_Theme_JSON_Resolver::get_merged_data( $theme_support_data, 'theme' )->get_raw_data();

$settings['globalStylesUserEntityId'] = $user_cpt_id;
$settings['globalStylesBaseStyles'] = $base_styles;
}

return $settings;
}

/**
* Register CPT to store/access user data.
*
Expand All @@ -265,6 +299,7 @@ function gutenberg_experimental_global_styles_register_user_cpt() {

add_action( 'init', 'gutenberg_experimental_global_styles_register_user_cpt' );
add_filter( 'block_editor_settings', 'gutenberg_experimental_global_styles_settings', PHP_INT_MAX );
add_filter( 'block_editor_global_styles_rest_settings', 'gutenberg_global_styles_rest_settings', PHP_INT_MAX );
add_action( 'wp_enqueue_scripts', 'gutenberg_experimental_global_styles_enqueue_assets' );


Expand Down
3 changes: 3 additions & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,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_Global_Styles_Settings_Controller' ) ) {
require dirname( __FILE__ ) . '/class-wp-rest-block-editor-global-styles-settings-controller.php';
geriux marked this conversation as resolved.
Show resolved Hide resolved
}
/**
* 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 types REST API routes.
*/
function gutenberg_register_block_editor_global_styles_settings() {
$editor_settings = new WP_REST_Block_Editor_Global_Styles_Settings_Controller();
$editor_settings->register_routes();
}
add_action( 'rest_api_init', 'gutenberg_register_block_editor_global_styles_settings' );

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