Skip to content

Commit

Permalink
Return styles in REST Request (#31762)
Browse files Browse the repository at this point in the history
  • Loading branch information
nosolosw authored May 13, 2021
1 parent 305cf6f commit 1bc8d5f
Showing 1 changed file with 37 additions and 37 deletions.
74 changes: 37 additions & 37 deletions lib/global-styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,60 +76,60 @@ function gutenberg_experimental_global_styles_enqueue_assets() {
* @return array New block editor settings
*/
function gutenberg_experimental_global_styles_settings( $settings ) {
$origin = 'theme';
// Set what is the context for this data request.
$context = 'all';
if (
is_callable( 'get_current_screen' ) &&
function_exists( 'gutenberg_is_edit_site_page' ) &&
gutenberg_is_edit_site_page( get_current_screen()->id ) &&
WP_Theme_JSON_Resolver::theme_has_support() &&
gutenberg_supports_block_templates()
) {
// Only lookup for the user data if we need it.
$origin = 'user';
$context = 'site-editor';
}
$tree = WP_Theme_JSON_Resolver::get_merged_data( $settings, $origin );

// STEP 1: ADD FEATURES
//
// These need to be always added to the editor settings,
// even for themes that don't support theme.json.
// An example of this is that the presets are configured
// from the theme support data.
$settings['__experimentalFeatures'] = $tree->get_settings();

// STEP 2 - IF EDIT-SITE, ADD DATA REQUIRED FOR GLOBAL STYLES SIDEBAR
//
// 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.

if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
$context = 'rest-request';
}

$origin = 'theme';
if (
is_callable( 'get_current_screen' ) &&
function_exists( 'gutenberg_is_edit_site_page' ) &&
gutenberg_is_edit_site_page( get_current_screen()->id ) &&
WP_Theme_JSON_Resolver::theme_has_support() &&
gutenberg_supports_block_templates()
) {
// Only lookup for the user data if we need it.
$origin = 'user';
}
$consolidated = WP_Theme_JSON_Resolver::get_merged_data( $settings, $origin );

if ( 'rest-request' === $context ) {
$settings['__experimentalStyles'] = $consolidated->get_raw_data()['styles'];
}

if ( 'site-editor' === $context ) {
$theme = WP_Theme_JSON_Resolver::get_merged_data( $settings, 'theme' );
$user_cpt_id = WP_Theme_JSON_Resolver::get_user_custom_post_type_id();
$base_styles = WP_Theme_JSON_Resolver::get_merged_data( $settings, 'theme' )->get_raw_data();

$settings['__experimentalGlobalStylesUserEntityId'] = $user_cpt_id;
$settings['__experimentalGlobalStylesBaseStyles'] = $base_styles;
} elseif (
WP_Theme_JSON_Resolver::theme_has_support() ||
get_theme_support( 'experimental-link-color' ) // link color support needs the presets CSS variables regardless of the presence of theme.json file.
$settings['__experimentalGlobalStylesBaseStyles'] = $theme->get_raw_data();
}

if (
'site-editor' != $context &&
'rest-request' != $context &&
( WP_Theme_JSON_Resolver::theme_has_support() || get_theme_support( 'experimental-link-color' ) )
) {
// STEP 3 - ADD STYLES IF THEME HAS SUPPORT
//
// If we are in a block editor context, but not in edit-site,
// we add the styles via the settings, so the editor knows that
// some of these should be added the wrapper class,
// as if they were added via add_editor_styles.
$settings['styles'][] = array(
'css' => gutenberg_experimental_global_styles_get_stylesheet( $tree, 'css_variables' ),
$block_styles = array( 'css' => gutenberg_experimental_global_styles_get_stylesheet( $consolidated, 'block_styles' ) );
$css_variables = array(
'css' => gutenberg_experimental_global_styles_get_stylesheet( $consolidated, 'css_variables' ),
'__experimentalNoWrapper' => true,
);
$settings['styles'][] = array(
'css' => gutenberg_experimental_global_styles_get_stylesheet( $tree, 'block_styles' ),
);

$settings['styles'][] = $css_variables;
$settings['styles'][] = $block_styles;
}

$settings['__experimentalFeatures'] = $consolidated->get_settings();
unset( $settings['colors'] );
unset( $settings['disableCustomColors'] );
unset( $settings['disableCustomFontSizes'] );
Expand Down

0 comments on commit 1bc8d5f

Please sign in to comment.