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

(WIP) Add a new endpoint that exposes block editor settings through the REST API #25226

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
72 changes: 72 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,72 @@
<?php
/**
* REST API: WP_REST_Block_Editor_Settings_Controller class
*
* @package WordPress
* @subpackage REST_API
*/

/**
* Core class used to retrieve 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 = '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 );
}
}
8 changes: 1 addition & 7 deletions lib/global-styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
3 changes: 3 additions & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand Down
9 changes: 9 additions & 0 deletions lib/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down