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

[Global Styles]: Add support for nameless font sizes #37410

Merged
Merged
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
103 changes: 71 additions & 32 deletions lib/compat/wordpress-5.9/class-wp-theme-json-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,48 +95,53 @@ class WP_Theme_JSON_Gutenberg {
*/
const PRESETS_METADATA = array(
array(
'path' => array( 'color', 'palette' ),
'override' => array( 'color', 'defaultPalette' ),
'value_key' => 'color',
'css_vars' => '--wp--preset--color--$slug',
'classes' => array(
'path' => array( 'color', 'palette' ),
'override' => array( 'color', 'defaultPalette' ),
'use_default_names' => false,
'value_key' => 'color',
'css_vars' => '--wp--preset--color--$slug',
'classes' => array(
'.has-$slug-color' => 'color',
'.has-$slug-background-color' => 'background-color',
'.has-$slug-border-color' => 'border-color',
),
'properties' => array( 'color', 'background-color', 'border-color' ),
'properties' => array( 'color', 'background-color', 'border-color' ),
),
array(
'path' => array( 'color', 'gradients' ),
'override' => array( 'color', 'defaultGradients' ),
'value_key' => 'gradient',
'css_vars' => '--wp--preset--gradient--$slug',
'classes' => array( '.has-$slug-gradient-background' => 'background' ),
'properties' => array( 'background' ),
'path' => array( 'color', 'gradients' ),
'override' => array( 'color', 'defaultGradients' ),
'use_default_names' => false,
'value_key' => 'gradient',
'css_vars' => '--wp--preset--gradient--$slug',
'classes' => array( '.has-$slug-gradient-background' => 'background' ),
'properties' => array( 'background' ),
),
array(
'path' => array( 'color', 'duotone' ),
'override' => true,
'value_func' => 'gutenberg_render_duotone_filter_preset',
'css_vars' => '--wp--preset--duotone--$slug',
'classes' => array(),
'properties' => array( 'filter' ),
'path' => array( 'color', 'duotone' ),
'override' => true,
'use_default_names' => false,
'value_func' => 'gutenberg_render_duotone_filter_preset',
'css_vars' => '--wp--preset--duotone--$slug',
'classes' => array(),
'properties' => array( 'filter' ),
),
array(
'path' => array( 'typography', 'fontSizes' ),
'override' => true,
'value_key' => 'size',
'css_vars' => '--wp--preset--font-size--$slug',
'classes' => array( '.has-$slug-font-size' => 'font-size' ),
'properties' => array( 'font-size' ),
'path' => array( 'typography', 'fontSizes' ),
'override' => true,
'use_default_names' => true,
'value_key' => 'size',
'css_vars' => '--wp--preset--font-size--$slug',
'classes' => array( '.has-$slug-font-size' => 'font-size' ),
'properties' => array( 'font-size' ),
),
array(
'path' => array( 'typography', 'fontFamilies' ),
'override' => true,
'value_key' => 'fontFamily',
'css_vars' => '--wp--preset--font-family--$slug',
'classes' => array( '.has-$slug-font-family' => 'font-family' ),
'properties' => array( 'font-family' ),
'path' => array( 'typography', 'fontFamilies' ),
'override' => true,
'use_default_names' => false,
'value_key' => 'fontFamily',
'css_vars' => '--wp--preset--font-family--$slug',
'classes' => array( '.has-$slug-font-family' => 'font-family' ),
'properties' => array( 'font-family' ),
),
);

Expand Down Expand Up @@ -1445,12 +1450,24 @@ public function merge( $incoming ) {
$override_preset = self::should_override_preset( $this->theme_json, $node['path'], $preset['override'] );

foreach ( self::VALID_ORIGINS as $origin ) {
$path = array_merge( $node['path'], $preset['path'], array( $origin ) );
$content = _wp_array_get( $incoming_data, $path, null );
$base_path = array_merge( $node['path'], $preset['path'] );
$path = array_merge( $base_path, array( $origin ) );
$content = _wp_array_get( $incoming_data, $path, null );
if ( ! isset( $content ) ) {
continue;
}

if ( 'theme' === $origin && $preset['use_default_names'] ) {
foreach ( $content as &$item ) {
if ( ! array_key_exists( 'name', $item ) ) {
$name = self::get_name_from_defaults( $item['slug'], $base_path );
if ( null !== $name ) {
$item['name'] = $name;
}
}
}
}

if (
( 'theme' !== $origin ) ||
( 'theme' === $origin && $override_preset )
Expand Down Expand Up @@ -1546,6 +1563,28 @@ function( $value ) {
return $slugs;
}

/**
* Get a `default`'s preset name by a provided slug.
*
* @param string $slug The slug we want to find a match from default presets.
* @param array $base_path The path to inspect. It's 'settings' by default.
*
* @return string|null
*/
private function get_name_from_defaults( $slug, $base_path ) {
$path = array_merge( $base_path, array( 'default' ) );
$default_content = _wp_array_get( $this->theme_json, $path, null );
if ( ! $default_content ) {
return null;
}
foreach ( $default_content as $item ) {
if ( $slug === $item['slug'] ) {
return $item['name'];
}
}
return null;
}

/**
* Removes the preset values whose slug is equal to any of given slugs.
*
Expand Down
91 changes: 91 additions & 0 deletions phpunit/class-wp-theme-json-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1431,6 +1431,97 @@ public function test_merge_incoming_data_color_presets_with_same_slugs_as_defaul
$this->assertEqualSetsWithIndex( $expected, $actual );
}

public function test_merge_incoming_data_presets_use_default_names() {
$defaults = new WP_Theme_JSON_Gutenberg(
array(
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
'settings' => array(
'typography' => array(
'fontSizes' => array(
array(
'name' => 'Small',
'slug' => 'small',
'size' => '12px',
),
array(
'name' => 'Large',
'slug' => 'large',
'size' => '20px',
),
),
),
),
),
'default'
);
$theme_json = new WP_Theme_JSON_Gutenberg(
array(
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
'settings' => array(
'typography' => array(
'fontSizes' => array(
array(
'slug' => 'small',
'size' => '1.1rem',
),
array(
'slug' => 'large',
'size' => '1.75rem',
),
array(
'name' => 'Huge',
'slug' => 'huge',
'size' => '3rem',
),
),
),
),
),
'theme'
);
$expected = array(
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
'settings' => array(
'typography' => array(
'fontSizes' => array(
'theme' => array(
array(
'name' => 'Small',
'slug' => 'small',
'size' => '1.1rem',
),
array(
'name' => 'Large',
'slug' => 'large',
'size' => '1.75rem',
),
array(
'name' => 'Huge',
'slug' => 'huge',
'size' => '3rem',
),
),
'default' => array(
array(
'name' => 'Small',
'slug' => 'small',
'size' => '12px',
),
array(
'name' => 'Large',
'slug' => 'large',
'size' => '20px',
),
),
),
),
),
);
$defaults->merge( $theme_json );
$actual = $defaults->get_raw_data();
$this->assertEqualSetsWithIndex( $expected, $actual );
}

function test_remove_insecure_properties_removes_unsafe_styles() {
$actual = WP_Theme_JSON_Gutenberg::remove_insecure_properties(
array(
Expand Down