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

Return consolidated global styles in REST Request #31762

Merged
merged 1 commit into from
May 13, 2021
Merged
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
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 ) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$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'];
geriux marked this conversation as resolved.
Show resolved Hide resolved
}

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