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 all commits
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
64 changes: 64 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,64 @@
<?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';
$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_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
),
)
);
}

/**
* Checks whether a given request has permission to read block editor settings
*
* @return WP_Error|bool True if the request has permission, WP_Error object otherwise.
*/
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() ) );
}

return true;
}

/**
* Return all block editor settings
*
* @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() );

return rest_ensure_response( $settings );
}
}
9 changes: 6 additions & 3 deletions lib/global-styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand All @@ -223,11 +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();
$screen = ! $is_mobile && get_current_screen();
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_mobile ) ) &&
WP_Theme_JSON_Resolver::theme_has_support() &&
gutenberg_is_fse_theme()
) {
Expand Down
3 changes: 3 additions & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,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 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 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' );

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