From 484e3eebee24d2beeeb0f4641aa35bdd639fdc97 Mon Sep 17 00:00:00 2001 From: Tugdual de Kerviler Date: Thu, 10 Sep 2020 20:37:59 +0200 Subject: [PATCH 1/4] 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 2/4] 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 3/4] 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 4/4] 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 );