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

Fix for classic themes using default presets #2233

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 14 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
36 changes: 25 additions & 11 deletions src/wp-includes/global-styles-and-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,26 +103,40 @@ function wp_get_global_stylesheet( $types = array() ) {
}
}

$tree = WP_Theme_JSON_Resolver::get_merged_data();

$supports_theme_json = WP_Theme_JSON_Resolver::theme_has_support();
$supports_link_color = get_theme_support( 'experimental-link-color' );
if ( empty( $types ) && ! $supports_theme_json ) {
$types = array( 'variables', 'presets' );
} elseif ( empty( $types ) ) {
$types = array( 'variables', 'styles', 'presets' );
}

$origins = array( 'default', 'theme', 'custom' );
if ( ! $supports_theme_json && ! $supports_link_color ) {
// In this case we only enqueue the core presets (CSS Custom Properties + the classes).
$origins = array( 'default' );
} elseif ( ! $supports_theme_json && $supports_link_color ) {
// For the legacy link color feature to work, the CSS Custom Properties
// should be in scope (either the core or the theme ones).
$origins = array( 'default', 'theme' );
// If variables are part of the stylesheet,
// we add them for all origins (default, theme, user).
// This is so themes without a theme.json still work as before 5.9:
// they can override the default presets.
// See https://core.trac.wordpress.org/ticket/54782
$styles_variables = '';
if ( in_array( 'variables', $types ) ) {
$styles_variables = $tree->get_stylesheet( array( 'variables' ) );
$types = array_diff( $types, array( 'variables' ) );
}

// For the remaining types (presets, styles), we do consider origins:
//
// - themes without theme.json: only the classes for the presets defined by core
// - themes with theme.json: the presets and styles classes, both from core and the theme
$styles_rest = '';
if ( ! empty( $types ) ) {
$origins = array( 'default', 'theme', 'custom' );
if ( ! $supports_theme_json ) {
$origins = array( 'default' );
}
$styles_rest = $tree->get_stylesheet( $types, $origins );
}

$tree = WP_Theme_JSON_Resolver::get_merged_data();
$stylesheet = $tree->get_stylesheet( $types, $origins );
$stylesheet = $styles_variables . $styles_rest;

if ( $can_use_cached ) {
// Cache for a minute.
Expand Down
20 changes: 20 additions & 0 deletions tests/phpunit/data/themedir1/classic-with-presets/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

function classic_with_presets_after_setup_theme() {
add_theme_support(
'editor-font-sizes',
array(
array(
'name' => 'Small',
'size' => 18,
'slug' => 'small',
),
array(
'name' => 'Large',
'size' => 26.25,
'slug' => 'large',
),
)
);
}
add_action( 'after_setup_theme', 'classic_with_presets_after_setup_theme' );
4 changes: 4 additions & 0 deletions tests/phpunit/data/themedir1/classic-with-presets/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
/**
* Dummy theme.
*/
7 changes: 7 additions & 0 deletions tests/phpunit/data/themedir1/classic-with-presets/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
Theme Name: Classic with Presets
Theme URI: https://wordpress.org/
Description: For testing purposes only.
Version: 1.0.0
Text Domain: classic-with-presets
*/
119 changes: 119 additions & 0 deletions tests/phpunit/tests/theme/globalStylesheet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

/**
* @group themes
*/
class Tests_Global_Stylesheet extends WP_UnitTestCase {
oandregal marked this conversation as resolved.
Show resolved Hide resolved

public function test_block_theme_using_variables() {
oandregal marked this conversation as resolved.
Show resolved Hide resolved
switch_theme( 'block-theme' );
oandregal marked this conversation as resolved.
Show resolved Hide resolved

$styles = wp_get_global_stylesheet( array( 'variables' ) );
$this->assertTrue( str_contains( $styles, '--wp--preset--font-size--small: 13px' ), 'small font size is 13px' );
$this->assertTrue( str_contains( $styles, '--wp--preset--font-size--medium: 20px' ), 'medium font size is 20px' );
$this->assertTrue( str_contains( $styles, '--wp--preset--font-size--large: 36px' ), 'large font size is 36px' );
$this->assertTrue( str_contains( $styles, '--wp--preset--font-size--x-large: 42px' ), 'x-large font size is 42px' );
$this->assertTrue( str_contains( $styles, '--wp--preset--font-size--custom: 100px;' ), 'custom font size is 100px' );

switch_theme( WP_DEFAULT_THEME );
}

public function test_block_theme_using_presets() {
switch_theme( 'block-theme' );

$styles = wp_get_global_stylesheet( array( 'presets' ) );
$this->assertFalse( str_contains( $styles, '--wp--preset--font-size--small: 13px' ), 'small font size is not present' );
$this->assertFalse( str_contains( $styles, '--wp--preset--font-size--medium: 20px' ), 'medium font size is not present' );
$this->assertFalse( str_contains( $styles, '--wp--preset--font-size--large: 36px' ), 'large font size is not present' );
$this->assertFalse( str_contains( $styles, '--wp--preset--font-size--x-large: 42px' ), 'x-large font size is not present' );
$this->assertFalse( str_contains( $styles, '--wp--preset--font-size--custom: 100px;' ), 'custom font size is not present' );

switch_theme( WP_DEFAULT_THEME );
}

public function test_block_theme_using_defaults() {
switch_theme( 'block-theme' );

$styles = wp_get_global_stylesheet();
$this->assertTrue( str_contains( $styles, '--wp--preset--font-size--small: 13px' ), 'small font size is 13px' );
$this->assertTrue( str_contains( $styles, '--wp--preset--font-size--medium: 20px' ), 'medium font size is 20px' );
$this->assertTrue( str_contains( $styles, '--wp--preset--font-size--large: 36px' ), 'large font size is 36px' );
$this->assertTrue( str_contains( $styles, '--wp--preset--font-size--x-large: 42px' ), 'x-large font size is 42px' );
$this->assertTrue( str_contains( $styles, '--wp--preset--font-size--custom: 100px;' ), 'custom font size is 100px' );

switch_theme( WP_DEFAULT_THEME );
}

public function test_variables_in_classic_theme_with_no_presets_using_variables() {
switch_theme( 'default' );

$styles = wp_get_global_stylesheet( array( 'variables' ) );
$this->assertTrue( str_contains( $styles, '--wp--preset--font-size--small: 13px' ), 'small font size is 13px' );
$this->assertTrue( str_contains( $styles, '--wp--preset--font-size--medium: 20px' ), 'medium font size is 20px' );
$this->assertTrue( str_contains( $styles, '--wp--preset--font-size--large: 36px' ), 'large font size is 36px' );
$this->assertTrue( str_contains( $styles, '--wp--preset--font-size--x-large: 42px' ), 'x-large font size is 42px' );

switch_theme( WP_DEFAULT_THEME );
}

public function test_variables_in_classic_theme_with_no_presets_using_presets() {
switch_theme( 'default' );

$styles = wp_get_global_stylesheet( array( 'presets' ) );
$this->assertFalse( str_contains( $styles, '--wp--preset--font-size--small: 13px' ), 'small font size is not present' );
$this->assertFalse( str_contains( $styles, '--wp--preset--font-size--medium: 20px' ), 'medium font size is not present' );
$this->assertFalse( str_contains( $styles, '--wp--preset--font-size--large: 36px' ), 'large font size is not present' );
$this->assertFalse( str_contains( $styles, '--wp--preset--font-size--x-large: 42px' ), 'x-large font size is not present' );

switch_theme( WP_DEFAULT_THEME );
}

public function test_variables_in_classic_theme_with_no_presets_using_defaults() {
switch_theme( 'default' );

$styles = wp_get_global_stylesheet();
$this->assertTrue( str_contains( $styles, '--wp--preset--font-size--small: 13px' ), 'small font size is 13px' );
$this->assertTrue( str_contains( $styles, '--wp--preset--font-size--medium: 20px' ), 'medium font size is 20px' );
$this->assertTrue( str_contains( $styles, '--wp--preset--font-size--large: 36px' ), 'large font size is 36px' );
$this->assertTrue( str_contains( $styles, '--wp--preset--font-size--x-large: 42px' ), 'x-large font size is 42px' );

switch_theme( WP_DEFAULT_THEME );
}

public function test_variables_in_classic_theme_with_presets_using_variables() {
switch_theme( 'classic-with-presets' );

$styles = wp_get_global_stylesheet( array( 'variables' ) );
$this->assertTrue( str_contains( $styles, '--wp--preset--font-size--small: 18px' ), 'small font size is 18px' );
$this->assertTrue( str_contains( $styles, '--wp--preset--font-size--medium: 20px' ), 'medium font size is 20px' );
$this->assertTrue( str_contains( $styles, '--wp--preset--font-size--large: 26.25px' ), 'large font size is 26.25px' );
$this->assertTrue( str_contains( $styles, '--wp--preset--font-size--x-large: 42px' ), 'x-large font size is 42px' );

switch_theme( WP_DEFAULT_THEME );
}

public function test_variables_in_classic_theme_with_presets_using_presets() {
switch_theme( 'classic-with-presets' );

$styles = wp_get_global_stylesheet( array( 'presets' ) );
$this->assertFalse( str_contains( $styles, '--wp--preset--font-size--small: 18px' ), 'small font size is not present' );
$this->assertFalse( str_contains( $styles, '--wp--preset--font-size--medium: 20px' ), 'medium font size is not present' );
$this->assertFalse( str_contains( $styles, '--wp--preset--font-size--large: 26.25px' ), 'large font size is not present' );
$this->assertFalse( str_contains( $styles, '--wp--preset--font-size--x-large: 42px' ), 'x-large font size is not present' );

switch_theme( WP_DEFAULT_THEME );
}

public function test_variables_in_classic_theme_with_presets_using_defaults() {
switch_theme( 'classic-with-presets' );

$styles = wp_get_global_stylesheet();
$this->assertTrue( str_contains( $styles, '--wp--preset--font-size--small: 18px' ), 'small font size is 18px' );
$this->assertTrue( str_contains( $styles, '--wp--preset--font-size--medium: 20px' ), 'medium font size is 20px' );
$this->assertTrue( str_contains( $styles, '--wp--preset--font-size--large: 26.25px' ), 'large font size is 26.25px' );
$this->assertTrue( str_contains( $styles, '--wp--preset--font-size--x-large: 42px' ), 'small font size is 42px' );

switch_theme( WP_DEFAULT_THEME );
}

}
1 change: 1 addition & 0 deletions tests/phpunit/tests/theme/themeDir.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ public function test_theme_list() {
'Page Template Theme', // Theme with page templates for other test code.
'Theme with Spaces in the Directory',
'Internationalized Theme',
'Classic with Presets',
'camelCase',
'REST Theme',
'Block Theme',
Expand Down