From 484e3eebee24d2beeeb0f4641aa35bdd639fdc97 Mon Sep 17 00:00:00 2001 From: Tugdual de Kerviler Date: Thu, 10 Sep 2020 20:37:59 +0200 Subject: [PATCH 01/19] Add a new endpoint that exposes block editor settings through the REST API --- ...-rest-block-editor-settings-controller.php | 72 +++++++++++++++++++ lib/global-styles.php | 8 +-- lib/load.php | 3 + lib/rest-api.php | 9 +++ 4 files changed, 85 insertions(+), 7 deletions(-) create mode 100644 lib/class-wp-rest-block-editor-settings-controller.php diff --git a/lib/class-wp-rest-block-editor-settings-controller.php b/lib/class-wp-rest-block-editor-settings-controller.php new file mode 100644 index 00000000000000..b10d8a4ff9dc38 --- /dev/null +++ b/lib/class-wp-rest-block-editor-settings-controller.php @@ -0,0 +1,72 @@ +namespace = 'wp/v2';//'__experimental'; + $this->rest_base = 'block-editor-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_settings' ), + 'permission_callback' => array( $this, 'get_settings_permissions_check' ) + ) + ) + ); + } + + /** + * Checks whether a given request has permission to read block editor settings + * + * @since 5.5.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_settings_permissions_check( $request ) { + if ( ! current_user_can( 'edit_post', $request['id'] ) ) { + $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; + } + + /** + * Return all block editor settings + * + * @since 5.5.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_settings( $request ) { + $settings = apply_filters( 'block_editor_settings', [] ); + + return rest_ensure_response( $settings ); + } +} diff --git a/lib/global-styles.php b/lib/global-styles.php index 147eeeeb218fd8..eb27fa2f5800e9 100644 --- a/lib/global-styles.php +++ b/lib/global-styles.php @@ -685,13 +685,7 @@ function gutenberg_experimental_global_styles_settings( $settings ) { // The client needs some information to be able to access/update the user styles. // We only do this if the theme has support for theme.json, though, // as an indicator that the theme will know how to combine this with its stylesheet. - $screen = get_current_screen(); - if ( - ! empty( $screen ) && - function_exists( 'gutenberg_is_edit_site_page' ) && - gutenberg_is_edit_site_page( $screen->id ) && - gutenberg_experimental_global_styles_has_theme_json_support() - ) { + if ( gutenberg_experimental_global_styles_has_theme_json_support() ) { $settings['__experimentalGlobalStylesUserEntityId'] = gutenberg_experimental_global_styles_get_user_cpt_id(); $settings['__experimentalGlobalStylesContexts'] = gutenberg_experimental_global_styles_get_block_data(); $settings['__experimentalGlobalStylesBaseStyles'] = gutenberg_experimental_global_styles_merge_trees( diff --git a/lib/load.php b/lib/load.php index 540539f56ba59a..eb476f170b5d87 100644 --- a/lib/load.php +++ b/lib/load.php @@ -38,6 +38,9 @@ function gutenberg_is_experiment_enabled( $name ) { if ( ! class_exists( 'WP_REST_Block_Directory_Controller' ) ) { require dirname( __FILE__ ) . '/class-wp-rest-block-directory-controller.php'; } + if ( ! class_exists( 'WP_REST_Block_Editor_Settings_Controller' ) ) { + require dirname( __FILE__ ) . '/class-wp-rest-block-editor-settings-controller.php'; + } if ( ! class_exists( 'WP_REST_Block_Types_Controller' ) ) { require dirname( __FILE__ ) . '/class-wp-rest-block-types-controller.php'; } diff --git a/lib/rest-api.php b/lib/rest-api.php index e10f52805bcb43..2067214fa1ada4 100644 --- a/lib/rest-api.php +++ b/lib/rest-api.php @@ -152,6 +152,15 @@ function gutenberg_register_rest_block_directory() { } add_filter( 'rest_api_init', 'gutenberg_register_rest_block_directory' ); +/** + * Registers the Block types REST API routes. + */ +function gutenberg_register_rest_block_editor_settings() { + $editor_settings = new WP_REST_Block_Editor_Settings_Controller(); + $editor_settings->register_routes(); +} +add_action( 'rest_api_init', 'gutenberg_register_rest_block_editor_settings' ); + /** * Registers the Block types REST API routes. */ From 0b90f30948c3c9c7e20dd3ed9b0221378da56ca7 Mon Sep 17 00:00:00 2001 From: Gerardo Pacheco Date: Thu, 25 Feb 2021 17:03:08 +0100 Subject: [PATCH 02/19] Update permission check --- lib/class-wp-rest-block-editor-settings-controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/class-wp-rest-block-editor-settings-controller.php b/lib/class-wp-rest-block-editor-settings-controller.php index 7f9c7bf8349305..857a6e3f16daa5 100644 --- a/lib/class-wp-rest-block-editor-settings-controller.php +++ b/lib/class-wp-rest-block-editor-settings-controller.php @@ -47,7 +47,7 @@ public function register_routes() { * @return WP_Error|bool True if the request has permission, WP_Error object otherwise. */ public function get_items_permissions_check( $request ) { - if ( ! current_user_can( 'edit_post', $request['id'] ) ) { + 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() ) ); } From ba80bccfdd199d13078bce50fa6972195a68832b Mon Sep 17 00:00:00 2001 From: Gerardo Pacheco Date: Mon, 1 Mar 2021 16:25:32 +0100 Subject: [PATCH 03/19] Global styles - Add check for mobile --- ...s-wp-rest-block-editor-settings-controller.php | 2 +- lib/global-styles.php | 15 +++++++-------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/lib/class-wp-rest-block-editor-settings-controller.php b/lib/class-wp-rest-block-editor-settings-controller.php index 857a6e3f16daa5..9764f02968e909 100644 --- a/lib/class-wp-rest-block-editor-settings-controller.php +++ b/lib/class-wp-rest-block-editor-settings-controller.php @@ -16,7 +16,7 @@ class WP_REST_Block_Editor_Settings_Controller extends WP_REST_Controller { * Constructs the controller. */ public function __construct() { - $this->namespace = 'wp/v2';//'__experimental'; + $this->namespace = 'wp/v2'; $this->rest_base = 'block-editor-settings'; } diff --git a/lib/global-styles.php b/lib/global-styles.php index 2ed63bf074a587..102f4c7a002889 100644 --- a/lib/global-styles.php +++ b/lib/global-styles.php @@ -209,6 +209,8 @@ function gutenberg_experimental_global_styles_settings( $settings ) { $origin = 'user'; } $tree = WP_Theme_JSON_Resolver::get_merged_data( $theme_support_data, $origin ); + // Check for mobile editor. + $is_mobile = isset( $_REQUEST['is_mobile'] ) && 'true' === $_REQUEST['is_mobile']; // STEP 1: ADD FEATURES // @@ -223,15 +225,12 @@ function gutenberg_experimental_global_styles_settings( $settings ) { // In the site editor, the user can change styles, so the client // needs the ability to create them. Hence, we pass it some data // for this: base styles (core+theme) and the ID of the user CPT. - // $screen = get_current_screen(); - // if ( - // ! empty( $screen ) && - // function_exists( 'gutenberg_is_edit_site_page' ) && - // gutenberg_is_edit_site_page( $screen->id ) && - // WP_Theme_JSON_Resolver::theme_has_support() && - // gutenberg_is_fse_theme() - // ) { + $screen = ! $is_mobile && get_current_screen(); if ( + ( ( ! empty( $screen ) && + function_exists( 'gutenberg_is_edit_site_page' ) && + gutenberg_is_edit_site_page( $screen->id ) || + $is_mobile ) ) && WP_Theme_JSON_Resolver::theme_has_support() && gutenberg_is_fse_theme() ) { From d1cc0f5e445fecf93fa9339e6bdcc06dcf6cb510 Mon Sep 17 00:00:00 2001 From: Gerardo Pacheco Date: Mon, 1 Mar 2021 16:28:57 +0100 Subject: [PATCH 04/19] Fix lint issues --- ...-wp-rest-block-editor-settings-controller.php | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/lib/class-wp-rest-block-editor-settings-controller.php b/lib/class-wp-rest-block-editor-settings-controller.php index 9764f02968e909..c46869e2000fbf 100644 --- a/lib/class-wp-rest-block-editor-settings-controller.php +++ b/lib/class-wp-rest-block-editor-settings-controller.php @@ -31,8 +31,8 @@ public function register_routes() { array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_items' ), - 'permission_callback' => array( $this, 'get_items_permissions_check' ) - ) + 'permission_callback' => array( $this, 'get_items_permissions_check' ), + ), ) ); } @@ -40,13 +40,9 @@ public function register_routes() { /** * Checks whether a given request has permission to read block editor settings * - * @since 5.5.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 ) { + public function get_items_permissions_check() { 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() ) ); @@ -58,13 +54,9 @@ public function get_items_permissions_check( $request ) { /** * Return all block editor settings * - * @since 5.5.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 ) { + public function get_items() { $settings = apply_filters( 'block_editor_settings', array() ); return rest_ensure_response( $settings ); From 052d24157ad4e21394d9573bc94c86fd6d1e921f Mon Sep 17 00:00:00 2001 From: Gerardo Pacheco Date: Tue, 16 Mar 2021 16:12:50 +0100 Subject: [PATCH 05/19] Block editor mobile settings endpoint --- ...ock-editor-mobile-settings-controller.php} | 20 ++++++--- lib/global-styles.php | 43 ++++++++++++++++--- lib/load.php | 4 +- lib/rest-api.php | 6 +-- 4 files changed, 56 insertions(+), 17 deletions(-) rename lib/{class-wp-rest-block-editor-settings-controller.php => class-wp-rest-block-editor-mobile-settings-controller.php} (65%) diff --git a/lib/class-wp-rest-block-editor-settings-controller.php b/lib/class-wp-rest-block-editor-mobile-settings-controller.php similarity index 65% rename from lib/class-wp-rest-block-editor-settings-controller.php rename to lib/class-wp-rest-block-editor-mobile-settings-controller.php index c46869e2000fbf..ee5b9cc6113094 100644 --- a/lib/class-wp-rest-block-editor-settings-controller.php +++ b/lib/class-wp-rest-block-editor-mobile-settings-controller.php @@ -1,6 +1,6 @@ namespace = 'wp/v2'; - $this->rest_base = 'block-editor-settings'; + $this->rest_base = 'block-editor-mobile-settings'; } /** @@ -40,9 +40,13 @@ public function register_routes() { /** * Checks whether a given request has permission to read block editor settings * + * @since 5.6.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() { + 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() ) ); @@ -54,10 +58,14 @@ public function get_items_permissions_check() { /** * Return all block editor settings * + * @since 5.6.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() { - $settings = apply_filters( 'block_editor_settings', array() ); + public function get_items( $request ) {// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable + $settings = apply_filters( 'block_editor_settings_mobile', array() ); return rest_ensure_response( $settings ); } diff --git a/lib/global-styles.php b/lib/global-styles.php index 231d1a5e1aae56..b471138765f494 100644 --- a/lib/global-styles.php +++ b/lib/global-styles.php @@ -204,8 +204,6 @@ function gutenberg_experimental_global_styles_settings( $settings ) { $origin = 'user'; } $tree = WP_Theme_JSON_Resolver::get_merged_data( $theme_support_data, $origin ); - // Check for mobile editor. - $is_mobile = isset( $_REQUEST['is_mobile'] ) && 'true' === $_REQUEST['is_mobile']; // STEP 1: ADD FEATURES // @@ -220,12 +218,11 @@ function gutenberg_experimental_global_styles_settings( $settings ) { // In the site editor, the user can change styles, so the client // needs the ability to create them. Hence, we pass it some data // for this: base styles (core+theme) and the ID of the user CPT. - $screen = ! $is_mobile && get_current_screen(); + $screen = get_current_screen(); if ( - ( ( ! empty( $screen ) && + ! empty( $screen ) && function_exists( 'gutenberg_is_edit_site_page' ) && - gutenberg_is_edit_site_page( $screen->id ) || - $is_mobile ) ) && + gutenberg_is_edit_site_page( $screen->id ) && WP_Theme_JSON_Resolver::theme_has_support() && gutenberg_is_fse_theme() ) { @@ -253,6 +250,39 @@ function_exists( 'gutenberg_is_edit_site_page' ) && return $settings; } +/** + * Adds the necessary data for the Global Styles mobile client UI to the block settings. + * + * @param array $settings Existing block editor settings. + * @return array New block editor settings + */ +function gutenberg_global_styles_settings_mobile( $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; + } + + $theme_support_data = gutenberg_experimental_global_styles_get_theme_support_settings( $settings ); + if ( + WP_Theme_JSON_Resolver::theme_has_support() && + gutenberg_is_fse_theme() + ) { + $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['globalStyles'] = WP_Theme_JSON_Resolver::theme_has_support(); + $settings['globalStylesUserEntityId'] = $user_cpt_id; + $settings['globalStylesBaseStyles'] = $base_styles; + } + + return $settings; +} + /** * Register CPT to store/access user data. * @@ -268,6 +298,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_settings_mobile', 'gutenberg_global_styles_settings_mobile', PHP_INT_MAX ); add_action( 'wp_enqueue_scripts', 'gutenberg_experimental_global_styles_enqueue_assets' ); diff --git a/lib/load.php b/lib/load.php index 263d68502a5e8b..0024b984cafd80 100644 --- a/lib/load.php +++ b/lib/load.php @@ -61,8 +61,8 @@ 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 dirname( __FILE__ ) . '/class-wp-rest-block-editor-settings-controller.php'; + if ( ! class_exists( 'WP_REST_Block_Editor_Mobile_Settings_Controller' ) ) { + require dirname( __FILE__ ) . '/class-wp-rest-block-editor-mobile-settings-controller.php'; } /** * End: Include for phase 2 diff --git a/lib/rest-api.php b/lib/rest-api.php index be0095081ba1db..dbf5ff43ee2e36 100644 --- a/lib/rest-api.php +++ b/lib/rest-api.php @@ -76,11 +76,11 @@ function gutenberg_register_batch_endpoint() { /** * Registers the Block types REST API routes. */ -function gutenberg_register_rest_block_editor_settings() { - $editor_settings = new WP_REST_Block_Editor_Settings_Controller(); +function gutenberg_register_rest_block_editor_mobile_settings() { + $editor_settings = new WP_REST_Block_Editor_Mobile_Settings_Controller(); $editor_settings->register_routes(); } -add_action( 'rest_api_init', 'gutenberg_register_rest_block_editor_settings' ); +add_action( 'rest_api_init', 'gutenberg_register_rest_block_editor_mobile_settings' ); /** * Hook in to the nav menu item post type and enable a post type rest endpoint. From b4205d528a266fc91de68dd29421a86651797f43 Mon Sep 17 00:00:00 2001 From: Gerardo Pacheco Date: Wed, 17 Mar 2021 13:49:07 +0100 Subject: [PATCH 06/19] Move theme support data within the if condition --- lib/global-styles.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/global-styles.php b/lib/global-styles.php index b471138765f494..31eb7ac906bc8c 100644 --- a/lib/global-styles.php +++ b/lib/global-styles.php @@ -267,11 +267,11 @@ function gutenberg_global_styles_settings_mobile( $settings ) { $settings['gradients'] = $gradient_presets; } - $theme_support_data = gutenberg_experimental_global_styles_get_theme_support_settings( $settings ); 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(); From d332e6e12d15af5b777a3d073b7d245d4c09ecde Mon Sep 17 00:00:00 2001 From: Gerardo Pacheco Date: Thu, 18 Mar 2021 10:53:27 +0100 Subject: [PATCH 07/19] Add schema --- ...lock-editor-mobile-settings-controller.php | 59 ++++++++++++++++++- lib/global-styles.php | 7 ++- 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/lib/class-wp-rest-block-editor-mobile-settings-controller.php b/lib/class-wp-rest-block-editor-mobile-settings-controller.php index ee5b9cc6113094..963442c8e62117 100644 --- a/lib/class-wp-rest-block-editor-mobile-settings-controller.php +++ b/lib/class-wp-rest-block-editor-mobile-settings-controller.php @@ -7,7 +7,7 @@ */ /** - * Core class used to retrieve block editor settings via the REST API. + * Core class used to retrieve mobile block editor settings via the REST API. * * @see WP_REST_Controller */ @@ -33,6 +33,7 @@ public function register_routes() { 'callback' => array( $this, 'get_items' ), 'permission_callback' => array( $this, 'get_items_permissions_check' ), ), + 'schema' => array( $this, 'get_public_item_schema' ), ) ); } @@ -40,7 +41,7 @@ public function register_routes() { /** * Checks whether a given request has permission to read block editor settings * - * @since 5.6.0 + * @since 5.8.0 * * @param WP_REST_Request $request Full details about the request. * @@ -58,7 +59,7 @@ public function get_items_permissions_check( $request ) {// phpcs:ignore Variabl /** * Return all block editor settings * - * @since 5.6.0 + * @since 5.8.0 * * @param WP_REST_Request $request Full details about the request. * @@ -69,4 +70,56 @@ public function get_items( $request ) {// phpcs:ignore VariableAnalysis.CodeAnal return rest_ensure_response( $settings ); } + + /** + * Retrieves the pattern's 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_settings_mobile-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 ); + } } diff --git a/lib/global-styles.php b/lib/global-styles.php index 31eb7ac906bc8c..1604d1bcbb6381 100644 --- a/lib/global-styles.php +++ b/lib/global-styles.php @@ -267,15 +267,16 @@ function gutenberg_global_styles_settings_mobile( $settings ) { $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(); + $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['globalStyles'] = WP_Theme_JSON_Resolver::theme_has_support(); $settings['globalStylesUserEntityId'] = $user_cpt_id; $settings['globalStylesBaseStyles'] = $base_styles; } From e3e1c7784ace14a9c94a4adae2b88ca431690284 Mon Sep 17 00:00:00 2001 From: Gerardo Pacheco Date: Thu, 18 Mar 2021 14:36:04 +0100 Subject: [PATCH 08/19] Update naming --- ...editor-global-styles-settings-controller.php} | 16 ++++++++-------- lib/global-styles.php | 6 +++--- lib/load.php | 4 ++-- lib/rest-api.php | 6 +++--- 4 files changed, 16 insertions(+), 16 deletions(-) rename lib/{class-wp-rest-block-editor-mobile-settings-controller.php => class-wp-rest-block-editor-global-styles-settings-controller.php} (83%) diff --git a/lib/class-wp-rest-block-editor-mobile-settings-controller.php b/lib/class-wp-rest-block-editor-global-styles-settings-controller.php similarity index 83% rename from lib/class-wp-rest-block-editor-mobile-settings-controller.php rename to lib/class-wp-rest-block-editor-global-styles-settings-controller.php index 963442c8e62117..a948b8a79b4918 100644 --- a/lib/class-wp-rest-block-editor-mobile-settings-controller.php +++ b/lib/class-wp-rest-block-editor-global-styles-settings-controller.php @@ -1,23 +1,23 @@ namespace = 'wp/v2'; - $this->rest_base = 'block-editor-mobile-settings'; + $this->rest_base = 'block-editor-global-styles-settings'; } /** @@ -57,7 +57,7 @@ public function get_items_permissions_check( $request ) {// phpcs:ignore Variabl } /** - * Return all block editor settings + * Returns the block editor's global styles settings * * @since 5.8.0 * @@ -66,13 +66,13 @@ public function get_items_permissions_check( $request ) {// phpcs:ignore Variabl * @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_settings_mobile', array() ); + $settings = apply_filters( 'block_editor_global_styles_rest_settings', array() ); return rest_ensure_response( $settings ); } /** - * Retrieves the pattern's schema, conforming to JSON Schema. + * Retrieves the block editor's global styles schema, conforming to JSON Schema. * * @since 5.8.0 * @@ -85,7 +85,7 @@ public function get_item_schema() { $this->schema = array( '$schema' => 'http://json-schema.org/draft-04/schema#', - 'title' => 'block_editor_settings_mobile-item', + 'title' => 'block-editor-global-styles-settings-item', 'type' => 'object', 'properties' => array( 'colors' => array( diff --git a/lib/global-styles.php b/lib/global-styles.php index 1604d1bcbb6381..3c12daf0c072ca 100644 --- a/lib/global-styles.php +++ b/lib/global-styles.php @@ -251,12 +251,12 @@ function_exists( 'gutenberg_is_edit_site_page' ) && } /** - * Adds the necessary data for the Global Styles mobile client UI to the block 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_settings_mobile( $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; @@ -299,7 +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_settings_mobile', 'gutenberg_global_styles_settings_mobile', 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' ); diff --git a/lib/load.php b/lib/load.php index 0024b984cafd80..9bdc23097a5528 100644 --- a/lib/load.php +++ b/lib/load.php @@ -61,8 +61,8 @@ 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_Mobile_Settings_Controller' ) ) { - require dirname( __FILE__ ) . '/class-wp-rest-block-editor-mobile-settings-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'; } /** * End: Include for phase 2 diff --git a/lib/rest-api.php b/lib/rest-api.php index dbf5ff43ee2e36..52bc2761d7a230 100644 --- a/lib/rest-api.php +++ b/lib/rest-api.php @@ -76,11 +76,11 @@ function gutenberg_register_batch_endpoint() { /** * Registers the Block types REST API routes. */ -function gutenberg_register_rest_block_editor_mobile_settings() { - $editor_settings = new WP_REST_Block_Editor_Mobile_Settings_Controller(); +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_rest_block_editor_mobile_settings' ); +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. From 2c7abac6df3cdee52777e4ea8110d87d2bdd8af8 Mon Sep 17 00:00:00 2001 From: Gerardo Pacheco Date: Mon, 22 Mar 2021 10:46:30 +0100 Subject: [PATCH 09/19] Update endpoint name --- ...wp-rest-block-editor-global-styles-settings-controller.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/class-wp-rest-block-editor-global-styles-settings-controller.php b/lib/class-wp-rest-block-editor-global-styles-settings-controller.php index a948b8a79b4918..bcdd8fdbdf58d8 100644 --- a/lib/class-wp-rest-block-editor-global-styles-settings-controller.php +++ b/lib/class-wp-rest-block-editor-global-styles-settings-controller.php @@ -16,8 +16,8 @@ class WP_REST_Block_Editor_Global_Styles_Settings_Controller extends WP_REST_Con * Constructs the controller. */ public function __construct() { - $this->namespace = 'wp/v2'; - $this->rest_base = 'block-editor-global-styles-settings'; + $this->namespace = 'wp-block-editor/v1'; + $this->rest_base = 'global-styles'; } /** From ffe9ac1c03faee6f0621e607b5a83598781ed533 Mon Sep 17 00:00:00 2001 From: Gerardo Pacheco Date: Mon, 22 Mar 2021 10:46:38 +0100 Subject: [PATCH 10/19] Use require_once --- lib/load.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/load.php b/lib/load.php index 9bdc23097a5528..f1322d4d296086 100644 --- a/lib/load.php +++ b/lib/load.php @@ -62,7 +62,7 @@ function gutenberg_is_experiment_enabled( $name ) { 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'; + require_once dirname( __FILE__ ) . '/class-wp-rest-block-editor-global-styles-settings-controller.php'; } /** * End: Include for phase 2 From 6837fa773727794fb7fff0d3646875989399274b Mon Sep 17 00:00:00 2001 From: Gerardo Pacheco Date: Thu, 25 Mar 2021 11:43:12 +0100 Subject: [PATCH 11/19] Use block editor settings --- ...rest-block-editor-settings-controller.php} | 45 ++++++++----------- lib/global-styles.php | 45 +++---------------- lib/load.php | 4 +- lib/rest-api.php | 8 ++-- 4 files changed, 31 insertions(+), 71 deletions(-) rename lib/{class-wp-rest-block-editor-global-styles-settings-controller.php => class-wp-rest-block-editor-settings-controller.php} (65%) diff --git a/lib/class-wp-rest-block-editor-global-styles-settings-controller.php b/lib/class-wp-rest-block-editor-settings-controller.php similarity index 65% rename from lib/class-wp-rest-block-editor-global-styles-settings-controller.php rename to lib/class-wp-rest-block-editor-settings-controller.php index bcdd8fdbdf58d8..c2550e54271912 100644 --- a/lib/class-wp-rest-block-editor-global-styles-settings-controller.php +++ b/lib/class-wp-rest-block-editor-settings-controller.php @@ -1,23 +1,23 @@ namespace = 'wp-block-editor/v1'; - $this->rest_base = 'global-styles'; + $this->rest_base = 'settings'; } /** @@ -57,7 +57,7 @@ public function get_items_permissions_check( $request ) {// phpcs:ignore Variabl } /** - * Returns the block editor's global styles settings + * Returns the block editor's settings * * @since 5.8.0 * @@ -66,13 +66,18 @@ public function get_items_permissions_check( $request ) {// phpcs:ignore Variabl * @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() ); + $settings = apply_filters( 'block_editor_settings', array() ); + unset( $settings['styles'] ); + unset( $settings['defaultEditorStyles'] ); + unset( $settings['availableLegacyWidgets'] ); + unset( $settings['__unstableEnableFullSiteEditingBlocks'] ); + unset( $settings['__experimentalGlobalStylesUserEntityId'] ); return rest_ensure_response( $settings ); } /** - * Retrieves the block editor's global styles schema, conforming to JSON Schema. + * Retrieves the block editor's settings schema, conforming to JSON Schema. * * @since 5.8.0 * @@ -85,34 +90,22 @@ public function get_item_schema() { $this->schema = array( '$schema' => 'http://json-schema.org/draft-04/schema#', - 'title' => 'block-editor-global-styles-settings-item', + 'title' => 'block-editor-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', + '__experimentalFeatures' => array( + 'description' => __( 'Active theme settings and default values.', 'gutenberg' ), + 'type' => 'object', 'context' => array( 'view' ), ), - 'globalStyles' => array( - 'description' => __( 'Theme support for global styles.', 'gutenberg' ), + 'isFSETheme' => array( + 'description' => __( 'Theme support for full site editing.', 'gutenberg' ), 'type' => 'boolean', 'context' => array( 'view' ), ), - 'globalStylesUserEntityId' => array( - 'description' => __( 'User entity id for Global styles settings.', 'gutenberg' ), - 'type' => 'integer', - 'context' => array( 'view' ), - ), - - 'globalStylesBaseStyles' => array( + '__experimentalGlobalStylesBaseStyles' => array( 'description' => __( 'Global styles settings.', 'gutenberg' ), 'type' => 'object', 'context' => array( 'view' ), diff --git a/lib/global-styles.php b/lib/global-styles.php index 3c12daf0c072ca..a54047d94cfd2a 100644 --- a/lib/global-styles.php +++ b/lib/global-styles.php @@ -195,7 +195,8 @@ function gutenberg_experimental_global_styles_settings( $settings ) { unset( $settings['fontSizes'] ); unset( $settings['gradients'] ); - $origin = 'theme'; + $is_rest_request = defined( 'REST_REQUEST' ) && REST_REQUEST; + $origin = 'theme'; if ( WP_Theme_JSON_Resolver::theme_has_support() && gutenberg_is_fse_theme() @@ -218,11 +219,12 @@ function gutenberg_experimental_global_styles_settings( $settings ) { // In the site editor, the user can change styles, so the client // needs the ability to create them. Hence, we pass it some data // for this: base styles (core+theme) and the ID of the user CPT. - $screen = get_current_screen(); + $screen = ! $is_rest_request ? get_current_screen() : null; if ( - ! empty( $screen ) && + ( ( ! empty( $screen ) && function_exists( 'gutenberg_is_edit_site_page' ) && - gutenberg_is_edit_site_page( $screen->id ) && + gutenberg_is_edit_site_page( $screen->id ) || + $is_rest_request ) ) && WP_Theme_JSON_Resolver::theme_has_support() && gutenberg_is_fse_theme() ) { @@ -250,40 +252,6 @@ 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. * @@ -299,7 +267,6 @@ 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' ); diff --git a/lib/load.php b/lib/load.php index f1322d4d296086..b2711190d33268 100644 --- a/lib/load.php +++ b/lib/load.php @@ -61,8 +61,8 @@ 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_once dirname( __FILE__ ) . '/class-wp-rest-block-editor-global-styles-settings-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 diff --git a/lib/rest-api.php b/lib/rest-api.php index 52bc2761d7a230..19a6baa8c82a07 100644 --- a/lib/rest-api.php +++ b/lib/rest-api.php @@ -74,13 +74,13 @@ function gutenberg_register_batch_endpoint() { add_action( 'rest_api_init', 'gutenberg_register_batch_endpoint' ); /** - * Registers the Block types REST API routes. + * Registers the Block editor settings REST API routes. */ -function gutenberg_register_block_editor_global_styles_settings() { - $editor_settings = new WP_REST_Block_Editor_Global_Styles_Settings_Controller(); +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_global_styles_settings' ); +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. From 2328ff65b24db2523afea0abc107aa8276ad89be Mon Sep 17 00:00:00 2001 From: Gerardo Pacheco Date: Mon, 5 Apr 2021 16:43:51 +0200 Subject: [PATCH 12/19] Usage of get_default_block_editor_settings for the rest api endpoint --- ...-rest-block-editor-settings-controller.php | 160 ++++++++++++++++-- 1 file changed, 148 insertions(+), 12 deletions(-) diff --git a/lib/class-wp-rest-block-editor-settings-controller.php b/lib/class-wp-rest-block-editor-settings-controller.php index c2550e54271912..511d77cb96fb5c 100644 --- a/lib/class-wp-rest-block-editor-settings-controller.php +++ b/lib/class-wp-rest-block-editor-settings-controller.php @@ -16,7 +16,7 @@ class WP_REST_Block_Editor_Settings_Controller extends WP_REST_Controller { * Constructs the controller. */ public function __construct() { - $this->namespace = 'wp-block-editor/v1'; + $this->namespace = '__experimental/wp-block-editor/v1'; $this->rest_base = 'settings'; } @@ -66,12 +66,10 @@ public function get_items_permissions_check( $request ) {// phpcs:ignore Variabl * @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_settings', array() ); - unset( $settings['styles'] ); - unset( $settings['defaultEditorStyles'] ); - unset( $settings['availableLegacyWidgets'] ); - unset( $settings['__unstableEnableFullSiteEditingBlocks'] ); - unset( $settings['__experimentalGlobalStylesUserEntityId'] ); + $settings = array_merge( + apply_filters( 'block_editor_settings', array() ), + get_default_block_editor_settings() + ); return rest_ensure_response( $settings ); } @@ -93,23 +91,161 @@ public function get_item_schema() { 'title' => 'block-editor-settings-item', 'type' => 'object', 'properties' => array( - '__experimentalFeatures' => array( - 'description' => __( 'Active theme settings and default values.', 'gutenberg' ), - 'type' => 'object', + '__unstableEnableFullSiteEditingBlocks' => array( + 'description' => __( 'Enables Full site editing blocks', 'gutenberg' ), + 'type' => 'boolean', + 'context' => array( 'view' ), + ), + + 'styles' => array( + 'description' => __( 'Editor styles', 'gutenberg' ), + 'type' => 'array', + 'context' => array( 'view' ), + ), + + 'supportsLayout' => array( + 'description' => __( 'Enable/disable layouts support in container blocks.', 'gutenberg' ), + 'type' => 'boolean', 'context' => array( 'view' ), ), - 'isFSETheme' => array( + 'isFSETheme' => array( 'description' => __( 'Theme support for full site editing.', 'gutenberg' ), 'type' => 'boolean', 'context' => array( 'view' ), ), - '__experimentalGlobalStylesBaseStyles' => array( + 'widgetTypesToHideFromLegacyWidgetBlock' => array( + 'description' => __( 'Widget types to hide from legacy widget block.', 'gutenberg' ), + 'type' => 'array', + 'context' => array( 'view' ), + ), + + '__experimentalFeatures' => array( + 'description' => __( 'Active theme settings and default values.', 'gutenberg' ), + 'type' => 'object', + 'context' => array( 'view' ), + ), + + '__experimentalGlobalStylesUserEntityId' => array( + 'description' => __( 'Global styles user entity ID.', 'gutenberg' ), + 'type' => 'integer', + 'context' => array( 'view' ), + ), + + '__experimentalGlobalStylesBaseStyles' => array( 'description' => __( 'Global styles settings.', 'gutenberg' ), 'type' => 'object', 'context' => array( 'view' ), ), + + 'alignWide' => array( + 'description' => __( 'Enable/Disable Wide/Full Alignments.', 'gutenberg' ), + 'type' => 'boolean', + 'context' => array( 'view' ), + ), + + 'allowedBlockTypes' => array( + 'description' => __( 'Enable/Disable Wide/Full Alignments.', 'gutenberg' ), + 'type' => 'boolean', + 'context' => array( 'view' ), + ), + + 'allowedMimeTypes' => array( + 'description' => __( 'List of allowed mime types and file extensions.', 'gutenberg' ), + 'type' => 'object', + 'context' => array( 'view' ), + ), + + 'disableCustomColors' => array( + 'description' => __( 'Disables custom colors.', 'gutenberg' ), + 'type' => 'boolean', + 'context' => array( 'view' ), + ), + + 'disableCustomFontSizes' => array( + 'description' => __( 'Disables custom font size.', 'gutenberg' ), + 'type' => 'boolean', + 'context' => array( 'view' ), + ), + + 'disableCustomGradients' => array( + 'description' => __( 'Disables custom font size.', 'gutenberg' ), + 'type' => 'boolean', + 'context' => array( 'view' ), + ), + + 'enableCustomLineHeight' => array( + 'description' => __( 'Enables custom line height.', 'gutenberg' ), + 'type' => 'boolean', + 'context' => array( 'view' ), + ), + + 'enableCustomSpacing' => array( + 'description' => __( 'Enables custom spacing.', 'gutenberg' ), + 'type' => 'boolean', + 'context' => array( 'view' ), + ), + + 'enableCustomUnits' => array( + 'description' => __( 'Enables custom units.', 'gutenberg' ), + 'type' => 'boolean', + 'context' => array( 'view' ), + ), + + 'isRTL' => array( + 'description' => __( 'Determines whether the current locale is right-to-left (RTL).', 'gutenberg' ), + 'type' => 'boolean', + 'context' => array( 'view' ), + ), + + 'imageDefaultSize' => array( + 'description' => __( 'Image default size slug.', 'gutenberg' ), + 'type' => 'string', + 'context' => array( 'view' ), + ), + + 'imageDimensions' => array( + 'description' => __( 'Available image dimensions.', 'gutenberg' ), + 'type' => 'object', + 'context' => array( 'view' ), + ), + + 'imageEditing' => array( + 'description' => __( 'Image Editing settings set to false to disable.', 'gutenberg' ), + 'type' => 'boolean', + 'context' => array( 'view' ), + ), + + 'imageSizes' => array( + 'description' => __( 'Available image sizes.', 'gutenberg' ), + 'type' => 'array', + 'context' => array( 'view' ), + ), + + 'maxUploadFileSize' => array( + 'description' => __( 'Maximum upload size in bytes allowed for the site.', 'gutenberg' ), + 'type' => 'number', + 'context' => array( 'view' ), + ), + + 'colors' => array( + 'description' => __( 'Active theme color palette.', 'gutenberg' ), + 'type' => 'array', + 'context' => array( 'view' ), + ), + + 'fontSizes' => array( + 'description' => __( 'Active theme font sizes.', 'gutenberg' ), + 'type' => 'array', + 'context' => array( 'view' ), + ), + + 'gradients' => array( + 'description' => __( 'Active theme gradients.', 'gutenberg' ), + 'type' => 'array', + 'context' => array( 'view' ), + ), ), ); From ede0fc1bbfd336864143c53e935d8b26e8c72b68 Mon Sep 17 00:00:00 2001 From: Gerardo Pacheco Date: Mon, 19 Apr 2021 11:08:11 +0200 Subject: [PATCH 13/19] Block editor settings update schema --- ...-wp-rest-block-editor-settings-controller.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/class-wp-rest-block-editor-settings-controller.php b/lib/class-wp-rest-block-editor-settings-controller.php index 511d77cb96fb5c..b90aca9c057030 100644 --- a/lib/class-wp-rest-block-editor-settings-controller.php +++ b/lib/class-wp-rest-block-editor-settings-controller.php @@ -68,7 +68,7 @@ public function get_items_permissions_check( $request ) {// phpcs:ignore Variabl public function get_items( $request ) {// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable $settings = array_merge( apply_filters( 'block_editor_settings', array() ), - get_default_block_editor_settings() + gutenberg_get_default_block_editor_settings() ); return rest_ensure_response( $settings ); @@ -103,14 +103,14 @@ public function get_item_schema() { 'context' => array( 'view' ), ), - 'supportsLayout' => array( - 'description' => __( 'Enable/disable layouts support in container blocks.', 'gutenberg' ), + 'supportsTemplateMode' => array( + 'description' => __( 'Returns if the current theme is full site editing-enabled or not.', 'gutenberg' ), 'type' => 'boolean', 'context' => array( 'view' ), ), - 'isFSETheme' => array( - 'description' => __( 'Theme support for full site editing.', 'gutenberg' ), + 'supportsLayout' => array( + 'description' => __( 'Enable/disable layouts support in container blocks.', 'gutenberg' ), 'type' => 'boolean', 'context' => array( 'view' ), ), @@ -157,6 +157,12 @@ public function get_item_schema() { 'context' => array( 'view' ), ), + 'blockCategories' => array( + 'description' => __( 'Returns all the categories for block types that will be shown in the block editor.', 'gutenberg' ), + 'type' => 'array', + 'context' => array( 'view' ), + ), + 'disableCustomColors' => array( 'description' => __( 'Disables custom colors.', 'gutenberg' ), 'type' => 'boolean', From 3a3908f3ae673f4af07a65f491782ae4892d1d65 Mon Sep 17 00:00:00 2001 From: Gerardo Pacheco Date: Tue, 20 Apr 2021 15:51:19 +0200 Subject: [PATCH 14/19] Add context as a param to get the block editor settings, update permissions check --- ...-rest-block-editor-settings-controller.php | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/class-wp-rest-block-editor-settings-controller.php b/lib/class-wp-rest-block-editor-settings-controller.php index b90aca9c057030..3731ab0596c847 100644 --- a/lib/class-wp-rest-block-editor-settings-controller.php +++ b/lib/class-wp-rest-block-editor-settings-controller.php @@ -48,12 +48,21 @@ public function register_routes() { * @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() ) ); + if ( current_user_can( 'edit_posts' ) ) { + return true; } - 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() ) + ); } /** @@ -65,10 +74,11 @@ public function get_items_permissions_check( $request ) {// phpcs:ignore Variabl * * @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 + public function get_items( $request ) { + $context = ! empty( $request['context'] ) ? $request['context'] : 'post-editor'; $settings = array_merge( apply_filters( 'block_editor_settings', array() ), - gutenberg_get_default_block_editor_settings() + gutenberg_get_block_editor_settings( $context ) ); return rest_ensure_response( $settings ); From 108debd50c508327da451246ec1ab71526456004 Mon Sep 17 00:00:00 2001 From: Gerardo Pacheco Date: Tue, 20 Apr 2021 16:20:12 +0200 Subject: [PATCH 15/19] Remove unneeded filter --- lib/class-wp-rest-block-editor-settings-controller.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/class-wp-rest-block-editor-settings-controller.php b/lib/class-wp-rest-block-editor-settings-controller.php index 3731ab0596c847..b5a8696ab885c0 100644 --- a/lib/class-wp-rest-block-editor-settings-controller.php +++ b/lib/class-wp-rest-block-editor-settings-controller.php @@ -76,10 +76,7 @@ public function get_items_permissions_check( $request ) {// phpcs:ignore Variabl */ public function get_items( $request ) { $context = ! empty( $request['context'] ) ? $request['context'] : 'post-editor'; - $settings = array_merge( - apply_filters( 'block_editor_settings', array() ), - gutenberg_get_block_editor_settings( $context ) - ); + $settings = gutenberg_get_block_editor_settings( $context ); return rest_ensure_response( $settings ); } From 2d24002d8e697b9c1c557cede29a037d89a9ea81 Mon Sep 17 00:00:00 2001 From: Gerardo Pacheco Date: Tue, 20 Apr 2021 16:20:26 +0200 Subject: [PATCH 16/19] Remove unneeded changes --- lib/global-styles.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/global-styles.php b/lib/global-styles.php index d9e252068fe3c2..de843af00fee59 100644 --- a/lib/global-styles.php +++ b/lib/global-styles.php @@ -96,8 +96,7 @@ function gutenberg_experimental_global_styles_settings( $settings ) { unset( $settings['fontSizes'] ); unset( $settings['gradients'] ); - $is_rest_request = defined( 'REST_REQUEST' ) && REST_REQUEST; - $origin = 'theme'; + $origin = 'theme'; if ( WP_Theme_JSON_Resolver::theme_has_support() && gutenberg_supports_block_templates() @@ -120,12 +119,11 @@ function gutenberg_experimental_global_styles_settings( $settings ) { // In the site editor, the user can change styles, so the client // needs the ability to create them. Hence, we pass it some data // for this: base styles (core+theme) and the ID of the user CPT. - $screen = ! $is_rest_request ? get_current_screen() : null; + $screen = get_current_screen(); if ( - ( ( ! empty( $screen ) && + ! empty( $screen ) && function_exists( 'gutenberg_is_edit_site_page' ) && - gutenberg_is_edit_site_page( $screen->id ) || - $is_rest_request ) ) && + gutenberg_is_edit_site_page( $screen->id ) && WP_Theme_JSON_Resolver::theme_has_support() && gutenberg_supports_block_templates() ) { From ec6fa2164a6647dee2e799977c87d3c6108aa206 Mon Sep 17 00:00:00 2001 From: Gerardo Pacheco Date: Wed, 21 Apr 2021 15:14:08 +0200 Subject: [PATCH 17/19] Update context in schema --- ...-rest-block-editor-settings-controller.php | 54 +++++++++---------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/lib/class-wp-rest-block-editor-settings-controller.php b/lib/class-wp-rest-block-editor-settings-controller.php index b5a8696ab885c0..86bcf1e82477a1 100644 --- a/lib/class-wp-rest-block-editor-settings-controller.php +++ b/lib/class-wp-rest-block-editor-settings-controller.php @@ -101,163 +101,163 @@ public function get_item_schema() { '__unstableEnableFullSiteEditingBlocks' => array( 'description' => __( 'Enables Full site editing blocks', 'gutenberg' ), 'type' => 'boolean', - 'context' => array( 'view' ), + 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), ), 'styles' => array( 'description' => __( 'Editor styles', 'gutenberg' ), 'type' => 'array', - 'context' => array( 'view' ), + 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), ), 'supportsTemplateMode' => array( 'description' => __( 'Returns if the current theme is full site editing-enabled or not.', 'gutenberg' ), 'type' => 'boolean', - 'context' => array( 'view' ), + 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), ), 'supportsLayout' => array( 'description' => __( 'Enable/disable layouts support in container blocks.', 'gutenberg' ), 'type' => 'boolean', - 'context' => array( 'view' ), + 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), ), 'widgetTypesToHideFromLegacyWidgetBlock' => array( 'description' => __( 'Widget types to hide from legacy widget block.', 'gutenberg' ), 'type' => 'array', - 'context' => array( 'view' ), + 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), ), '__experimentalFeatures' => array( 'description' => __( 'Active theme settings and default values.', 'gutenberg' ), 'type' => 'object', - 'context' => array( 'view' ), + 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), ), '__experimentalGlobalStylesUserEntityId' => array( 'description' => __( 'Global styles user entity ID.', 'gutenberg' ), 'type' => 'integer', - 'context' => array( 'view' ), + 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), ), '__experimentalGlobalStylesBaseStyles' => array( 'description' => __( 'Global styles settings.', 'gutenberg' ), 'type' => 'object', - 'context' => array( 'view' ), + 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), ), 'alignWide' => array( 'description' => __( 'Enable/Disable Wide/Full Alignments.', 'gutenberg' ), 'type' => 'boolean', - 'context' => array( 'view' ), + 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), ), 'allowedBlockTypes' => array( 'description' => __( 'Enable/Disable Wide/Full Alignments.', 'gutenberg' ), 'type' => 'boolean', - 'context' => array( 'view' ), + 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), ), 'allowedMimeTypes' => array( 'description' => __( 'List of allowed mime types and file extensions.', 'gutenberg' ), 'type' => 'object', - 'context' => array( 'view' ), + 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), ), 'blockCategories' => array( 'description' => __( 'Returns all the categories for block types that will be shown in the block editor.', 'gutenberg' ), 'type' => 'array', - 'context' => array( 'view' ), + 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), ), 'disableCustomColors' => array( 'description' => __( 'Disables custom colors.', 'gutenberg' ), 'type' => 'boolean', - 'context' => array( 'view' ), + 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), ), 'disableCustomFontSizes' => array( 'description' => __( 'Disables custom font size.', 'gutenberg' ), 'type' => 'boolean', - 'context' => array( 'view' ), + 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), ), 'disableCustomGradients' => array( 'description' => __( 'Disables custom font size.', 'gutenberg' ), 'type' => 'boolean', - 'context' => array( 'view' ), + 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), ), 'enableCustomLineHeight' => array( 'description' => __( 'Enables custom line height.', 'gutenberg' ), 'type' => 'boolean', - 'context' => array( 'view' ), + 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), ), 'enableCustomSpacing' => array( 'description' => __( 'Enables custom spacing.', 'gutenberg' ), 'type' => 'boolean', - 'context' => array( 'view' ), + 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), ), 'enableCustomUnits' => array( 'description' => __( 'Enables custom units.', 'gutenberg' ), 'type' => 'boolean', - 'context' => array( 'view' ), + 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), ), 'isRTL' => array( 'description' => __( 'Determines whether the current locale is right-to-left (RTL).', 'gutenberg' ), 'type' => 'boolean', - 'context' => array( 'view' ), + 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), ), 'imageDefaultSize' => array( 'description' => __( 'Image default size slug.', 'gutenberg' ), 'type' => 'string', - 'context' => array( 'view' ), + 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), ), 'imageDimensions' => array( 'description' => __( 'Available image dimensions.', 'gutenberg' ), 'type' => 'object', - 'context' => array( 'view' ), + 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), ), 'imageEditing' => array( 'description' => __( 'Image Editing settings set to false to disable.', 'gutenberg' ), 'type' => 'boolean', - 'context' => array( 'view' ), + 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), ), 'imageSizes' => array( 'description' => __( 'Available image sizes.', 'gutenberg' ), 'type' => 'array', - 'context' => array( 'view' ), + 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), ), 'maxUploadFileSize' => array( 'description' => __( 'Maximum upload size in bytes allowed for the site.', 'gutenberg' ), 'type' => 'number', - 'context' => array( 'view' ), + 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), ), 'colors' => array( 'description' => __( 'Active theme color palette.', 'gutenberg' ), 'type' => 'array', - 'context' => array( 'view' ), + 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), ), 'fontSizes' => array( 'description' => __( 'Active theme font sizes.', 'gutenberg' ), 'type' => 'array', - 'context' => array( 'view' ), + 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), ), 'gradients' => array( 'description' => __( 'Active theme gradients.', 'gutenberg' ), 'type' => 'array', - 'context' => array( 'view' ), + 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), ), ), ); From f5db2fc9b24690af743e14ee2b09fea20f42b2ed Mon Sep 17 00:00:00 2001 From: Gerardo Pacheco Date: Mon, 26 Apr 2021 15:28:07 +0200 Subject: [PATCH 18/19] Update context for global styles attributes --- lib/class-wp-rest-block-editor-settings-controller.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/class-wp-rest-block-editor-settings-controller.php b/lib/class-wp-rest-block-editor-settings-controller.php index 86bcf1e82477a1..66ef69043d80f0 100644 --- a/lib/class-wp-rest-block-editor-settings-controller.php +++ b/lib/class-wp-rest-block-editor-settings-controller.php @@ -137,13 +137,13 @@ public function get_item_schema() { '__experimentalGlobalStylesUserEntityId' => array( 'description' => __( 'Global styles user entity ID.', 'gutenberg' ), 'type' => 'integer', - 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), + 'context' => array( 'site-editor' ), ), '__experimentalGlobalStylesBaseStyles' => array( 'description' => __( 'Global styles settings.', 'gutenberg' ), 'type' => 'object', - 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), + 'context' => array( 'site-editor' ), ), 'alignWide' => array( From 3897d19f7e41a81f11b98e2337de81169dab8949 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Zi=C3=B3=C5=82kowski?= Date: Tue, 4 May 2021 07:48:10 +0200 Subject: [PATCH 19/19] Update class-wp-rest-block-editor-settings-controller.php --- ...-rest-block-editor-settings-controller.php | 60 +++++++++---------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/lib/class-wp-rest-block-editor-settings-controller.php b/lib/class-wp-rest-block-editor-settings-controller.php index 66ef69043d80f0..70841e152f79e3 100644 --- a/lib/class-wp-rest-block-editor-settings-controller.php +++ b/lib/class-wp-rest-block-editor-settings-controller.php @@ -99,39 +99,39 @@ public function get_item_schema() { 'type' => 'object', 'properties' => array( '__unstableEnableFullSiteEditingBlocks' => array( - 'description' => __( 'Enables Full site editing blocks', 'gutenberg' ), + 'description' => __( 'Enables experimental Full Site Editing blocks', 'gutenberg' ), 'type' => 'boolean', - 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), + 'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ), ), 'styles' => array( 'description' => __( 'Editor styles', 'gutenberg' ), 'type' => 'array', - 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), + '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', 'widget-editor' ), + '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', 'widget-editor' ), + 'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ), ), 'widgetTypesToHideFromLegacyWidgetBlock' => array( - 'description' => __( 'Widget types to hide from legacy widget block.', 'gutenberg' ), + 'description' => __( 'Widget types to hide from Legacy Widget block.', 'gutenberg' ), 'type' => 'array', - 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), + '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', 'widget-editor' ), + 'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ), ), '__experimentalGlobalStylesUserEntityId' => array( @@ -149,115 +149,115 @@ public function get_item_schema() { 'alignWide' => array( 'description' => __( 'Enable/Disable Wide/Full Alignments.', 'gutenberg' ), 'type' => 'boolean', - 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), + 'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ), ), 'allowedBlockTypes' => array( - 'description' => __( 'Enable/Disable Wide/Full Alignments.', 'gutenberg' ), + 'description' => __( 'List of allowed block types.', 'gutenberg' ), 'type' => 'boolean', - 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), + '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', 'widget-editor' ), + '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', 'widget-editor' ), + 'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ), ), 'disableCustomColors' => array( 'description' => __( 'Disables custom colors.', 'gutenberg' ), 'type' => 'boolean', - 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), + 'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ), ), 'disableCustomFontSizes' => array( 'description' => __( 'Disables custom font size.', 'gutenberg' ), 'type' => 'boolean', - 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), + 'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ), ), 'disableCustomGradients' => array( 'description' => __( 'Disables custom font size.', 'gutenberg' ), 'type' => 'boolean', - 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), + 'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ), ), 'enableCustomLineHeight' => array( 'description' => __( 'Enables custom line height.', 'gutenberg' ), 'type' => 'boolean', - 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), + 'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ), ), 'enableCustomSpacing' => array( 'description' => __( 'Enables custom spacing.', 'gutenberg' ), 'type' => 'boolean', - 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), + 'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ), ), 'enableCustomUnits' => array( 'description' => __( 'Enables custom units.', 'gutenberg' ), 'type' => 'boolean', - 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), + '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', 'widget-editor' ), + 'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ), ), 'imageDefaultSize' => array( - 'description' => __( 'Image default size slug.', 'gutenberg' ), + 'description' => __( 'Default size for images.', 'gutenberg' ), 'type' => 'string', - 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), + 'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ), ), 'imageDimensions' => array( 'description' => __( 'Available image dimensions.', 'gutenberg' ), 'type' => 'object', - 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), + 'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ), ), 'imageEditing' => array( - 'description' => __( 'Image Editing settings set to false to disable.', 'gutenberg' ), + 'description' => __( 'Determines whether the image editing feature is enabled.', 'gutenberg' ), 'type' => 'boolean', - 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), + 'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ), ), 'imageSizes' => array( 'description' => __( 'Available image sizes.', 'gutenberg' ), 'type' => 'array', - 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), + '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', 'widget-editor' ), + 'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ), ), 'colors' => array( 'description' => __( 'Active theme color palette.', 'gutenberg' ), 'type' => 'array', - 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), + 'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ), ), 'fontSizes' => array( 'description' => __( 'Active theme font sizes.', 'gutenberg' ), 'type' => 'array', - 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), + 'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ), ), 'gradients' => array( 'description' => __( 'Active theme gradients.', 'gutenberg' ), 'type' => 'array', - 'context' => array( 'post-editor', 'site-editor', 'widget-editor' ), + 'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ), ), ), );