Skip to content

Commit b771f32

Browse files
committed
Global Styles: Lift classic block restrictions
1 parent 48b2b65 commit b771f32

File tree

5 files changed

+68
-103
lines changed

5 files changed

+68
-103
lines changed

src/wp-includes/block-editor.php

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -525,41 +525,34 @@ function get_block_editor_settings( array $custom_settings, $block_editor_contex
525525
}
526526
}
527527

528-
if ( wp_theme_has_theme_json() ) {
529-
$block_classes = array(
530-
'css' => 'styles',
531-
'__unstableType' => 'theme',
532-
'isGlobalStyles' => true,
533-
);
534-
$actual_css = wp_get_global_stylesheet( array( $block_classes['css'] ) );
535-
if ( '' !== $actual_css ) {
536-
$block_classes['css'] = $actual_css;
537-
$global_styles[] = $block_classes;
538-
}
539-
540-
/*
541-
* Add the custom CSS as a separate stylesheet so any invalid CSS
542-
* entered by users does not break other global styles.
543-
*/
544-
$global_styles[] = array(
545-
'css' => wp_get_global_stylesheet( array( 'custom-css' ) ),
546-
'__unstableType' => 'user',
547-
'isGlobalStyles' => true,
548-
);
549-
} else {
550-
// If there is no `theme.json` file, ensure base layout styles are still available.
551-
$block_classes = array(
552-
'css' => 'base-layout-styles',
553-
'__unstableType' => 'base-layout',
554-
'isGlobalStyles' => true,
555-
);
556-
$actual_css = wp_get_global_stylesheet( array( $block_classes['css'] ) );
557-
if ( '' !== $actual_css ) {
558-
$block_classes['css'] = $actual_css;
559-
$global_styles[] = $block_classes;
560-
}
528+
$block_classes = array(
529+
'css' => 'styles',
530+
'__unstableType' => 'theme',
531+
'isGlobalStyles' => true,
532+
);
533+
$actual_css = wp_get_global_stylesheet( array( $block_classes['css'] ) );
534+
if ( '' !== $actual_css ) {
535+
$block_classes['css'] = $actual_css;
536+
$global_styles[] = $block_classes;
561537
}
562538

539+
// Get any additional css from the customizer and add it before global styles custom CSS.
540+
$global_styles[] = array(
541+
'css' => wp_get_custom_css(),
542+
'__unstableType' => 'user',
543+
'isGlobalStyles' => false,
544+
);
545+
546+
/*
547+
* Add the custom CSS as a separate stylesheet so any invalid CSS
548+
* entered by users does not break other global styles.
549+
*/
550+
$global_styles[] = array(
551+
'css' => wp_get_global_stylesheet( array( 'custom-css' ) ),
552+
'__unstableType' => 'user',
553+
'isGlobalStyles' => true,
554+
);
555+
563556
$editor_settings['styles'] = array_merge( $global_styles, get_block_editor_theme_styles() );
564557

565558
$editor_settings['__experimentalFeatures'] = wp_get_global_settings();

src/wp-includes/class-wp-theme-json-resolver.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -480,17 +480,6 @@ public static function get_user_data_from_wp_global_styles( $theme, $create_post
480480
$theme = wp_get_theme();
481481
}
482482

483-
/*
484-
* Bail early if the theme does not support a theme.json.
485-
*
486-
* Since wp_theme_has_theme_json() only supports the active
487-
* theme, the extra condition for whether $theme is the active theme is
488-
* present here.
489-
*/
490-
if ( $theme->get_stylesheet() === get_stylesheet() && ! wp_theme_has_theme_json() ) {
491-
return array();
492-
}
493-
494483
$user_cpt = array();
495484
$post_type_filter = 'wp_global_styles';
496485
$stylesheet = $theme->get_stylesheet();

src/wp-includes/global-styles-and-settings.php

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@ function wp_get_global_settings( $path = array(), $context = array() ) {
3939
* for clearing the cache appropriately.
4040
*/
4141
$origin = 'custom';
42-
if (
43-
! wp_theme_has_theme_json() ||
44-
( isset( $context['origin'] ) && 'base' === $context['origin'] )
45-
) {
42+
if ( isset( $context['origin'] ) && 'base' === $context['origin'] ) {
4643
$origin = 'theme';
4744
}
4845

@@ -180,12 +177,9 @@ function wp_get_global_stylesheet( $types = array() ) {
180177
}
181178
}
182179

183-
$tree = WP_Theme_JSON_Resolver::resolve_theme_file_uris( WP_Theme_JSON_Resolver::get_merged_data() );
184-
$supports_theme_json = wp_theme_has_theme_json();
180+
$tree = WP_Theme_JSON_Resolver::resolve_theme_file_uris( WP_Theme_JSON_Resolver::get_merged_data() );
185181

186-
if ( empty( $types ) && ! $supports_theme_json ) {
187-
$types = array( 'variables', 'presets', 'base-layout-styles' );
188-
} elseif ( empty( $types ) ) {
182+
if ( empty( $types ) ) {
189183
$types = array( 'variables', 'styles', 'presets' );
190184
}
191185

@@ -222,16 +216,7 @@ function wp_get_global_stylesheet( $types = array() ) {
222216
* (i.e. in the render cycle). Here, only the ones in use are rendered.
223217
* @see wp_add_global_styles_for_blocks
224218
*/
225-
$origins = array( 'default', 'theme', 'custom' );
226-
/*
227-
* If the theme doesn't have theme.json but supports both appearance tools and color palette,
228-
* the 'theme' origin should be included so color palette presets are also output.
229-
*/
230-
if ( ! $supports_theme_json && ( current_theme_supports( 'appearance-tools' ) || current_theme_supports( 'border' ) ) && current_theme_supports( 'editor-color-palette' ) ) {
231-
$origins = array( 'default', 'theme' );
232-
} elseif ( ! $supports_theme_json ) {
233-
$origins = array( 'default' );
234-
}
219+
$origins = array( 'default', 'theme', 'custom' );
235220
$styles_rest = $tree->get_stylesheet( $types, $origins );
236221
}
237222

src/wp-includes/script-loader.php

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2555,38 +2555,36 @@ function wp_enqueue_global_styles() {
25552555

25562556
$stylesheet = wp_get_global_stylesheet();
25572557

2558-
if ( $is_block_theme ) {
2559-
/*
2560-
* Dequeue the Customizer's custom CSS
2561-
* and add it before the global styles custom CSS.
2562-
*/
2563-
remove_action( 'wp_head', 'wp_custom_css_cb', 101 );
2558+
/*
2559+
* Dequeue the Customizer's custom CSS
2560+
* and add it before the global styles custom CSS.
2561+
*/
2562+
remove_action( 'wp_head', 'wp_custom_css_cb', 101 );
25642563

2565-
/*
2566-
* Get the custom CSS from the Customizer and add it to the global stylesheet.
2567-
* Always do this in Customizer preview for the sake of live preview since it be empty.
2568-
*/
2569-
$custom_css = trim( wp_get_custom_css() );
2570-
if ( $custom_css || is_customize_preview() ) {
2571-
if ( is_customize_preview() ) {
2572-
/*
2573-
* When in the Customizer preview, wrap the Custom CSS in milestone comments to allow customize-preview.js
2574-
* to locate the CSS to replace for live previewing. Make sure that the milestone comments are omitted from
2575-
* the stored Custom CSS if by chance someone tried to add them, which would be highly unlikely, but it
2576-
* would break live previewing.
2577-
*/
2578-
$before_milestone = '/*BEGIN_CUSTOMIZER_CUSTOM_CSS*/';
2579-
$after_milestone = '/*END_CUSTOMIZER_CUSTOM_CSS*/';
2580-
$custom_css = str_replace( array( $before_milestone, $after_milestone ), '', $custom_css );
2581-
$custom_css = $before_milestone . "\n" . $custom_css . "\n" . $after_milestone;
2582-
}
2583-
$custom_css = "\n" . $custom_css;
2564+
/*
2565+
* Get the custom CSS from the Customizer and add it to the global stylesheet.
2566+
* Always do this in Customizer preview for the sake of live preview since it be empty.
2567+
*/
2568+
$custom_css = trim( wp_get_custom_css() );
2569+
if ( $custom_css || is_customize_preview() ) {
2570+
if ( is_customize_preview() ) {
2571+
/*
2572+
* When in the Customizer preview, wrap the Custom CSS in milestone comments to allow customize-preview.js
2573+
* to locate the CSS to replace for live previewing. Make sure that the milestone comments are omitted from
2574+
* the stored Custom CSS if by chance someone tried to add them, which would be highly unlikely, but it
2575+
* would break live previewing.
2576+
*/
2577+
$before_milestone = '/*BEGIN_CUSTOMIZER_CUSTOM_CSS*/';
2578+
$after_milestone = '/*END_CUSTOMIZER_CUSTOM_CSS*/';
2579+
$custom_css = str_replace( array( $before_milestone, $after_milestone ), '', $custom_css );
2580+
$custom_css = $before_milestone . "\n" . $custom_css . "\n" . $after_milestone;
25842581
}
2585-
$stylesheet .= $custom_css;
2586-
2587-
// Add the global styles custom CSS at the end.
2588-
$stylesheet .= wp_get_global_stylesheet( array( 'custom-css' ) );
2582+
$custom_css = "\n" . $custom_css;
25892583
}
2584+
$stylesheet .= $custom_css;
2585+
2586+
// Add the global styles custom CSS at the end.
2587+
$stylesheet .= wp_get_global_stylesheet( array( 'custom-css' ) );
25902588

25912589
if ( empty( $stylesheet ) ) {
25922590
return;

tests/phpunit/tests/theme/wpThemeJsonResolver.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -734,22 +734,22 @@ public function test_get_user_data_from_wp_global_styles_does_not_use_uncached_q
734734
* @ticket 56945
735735
* @covers WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles
736736
*/
737-
public function test_get_user_data_from_wp_global_styles_does_not_run_for_theme_without_support() {
738-
// The 'default' theme does not support theme.json.
737+
public function test_get_user_data_from_wp_global_styles_runs_for_classic_themes() {
738+
// The 'default' theme does not support theme.json (classic theme).
739739
switch_theme( 'default' );
740740
wp_set_current_user( self::$administrator_id );
741741
$theme = wp_get_theme();
742742

743-
$start_queries = get_num_queries();
744-
745-
// When theme.json is not supported, the method should not run a query and always return an empty result.
746-
$user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
747-
$this->assertEmpty( $user_cpt, 'User CPT is expected to be empty.' );
748-
$this->assertSame( 0, get_num_queries() - $start_queries, 'Unexpected SQL query detected for theme without theme.json support.' );
749-
743+
// Classic themes should now be able to access user global styles data.
744+
// When should_create_post is true, it should create a post.
750745
$user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme, true );
751-
$this->assertEmpty( $user_cpt, 'User CPT is expected to be empty.' );
752-
$this->assertSame( 0, get_num_queries() - $start_queries, 'Unexpected SQL query detected for theme without theme.json support.' );
746+
$this->assertIsArray( $user_cpt, 'User CPT should be an array for classic themes.' );
747+
$this->assertArrayHasKey( 'ID', $user_cpt, 'User CPT should have an ID for classic themes.' );
748+
749+
// Clean up the created post.
750+
if ( isset( $user_cpt['ID'] ) ) {
751+
wp_delete_post( $user_cpt['ID'], true );
752+
}
753753
}
754754

755755
/**

0 commit comments

Comments
 (0)