Skip to content
Closed
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
33 changes: 24 additions & 9 deletions src/wp-includes/class-wp-theme-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -2194,13 +2194,20 @@ protected static function compute_style_properties( $styles, $settings = array()
}

$root_variable_duplicates = array();
$update_cache = false;
$str_start_with_cache = (array) wp_cache_get( 'str_start_with', 'theme_files' );
$str_len_root = strlen( '--wp--style--root--' );

foreach ( $properties as $css_property => $value_path ) {
$value = static::get_property_value( $styles, $value_path, $theme_json );

if ( str_starts_with( $css_property, '--wp--style--root--' ) && ( static::ROOT_BLOCK_SELECTOR !== $selector || ! $use_root_padding ) ) {
if ( ! isset( $str_start_with_cache[ $css_property ] ) ) {
$str_start_with_cache[ $css_property ] = str_starts_with( $css_property, '--wp--style--root--' );
$update_cache = true;
}
if ( $str_start_with_cache[ $css_property ] && ( static::ROOT_BLOCK_SELECTOR !== $selector || ! $use_root_padding ) ) {
continue;
}

$value = static::get_property_value( $styles, $value_path, $theme_json );
/*
* Root-level padding styles don't currently support strings with CSS shorthand values.
* This may change: https://github.com/WordPress/gutenberg/issues/40132.
Expand All @@ -2209,16 +2216,22 @@ protected static function compute_style_properties( $styles, $settings = array()
continue;
}

if ( str_starts_with( $css_property, '--wp--style--root--' ) && $use_root_padding ) {
$root_variable_duplicates[] = substr( $css_property, strlen( '--wp--style--root--' ) );
if ( $str_start_with_cache[ $css_property ] && $use_root_padding ) {
$root_variable_duplicates[] = substr( $css_property, $str_len_root );
}

/*
* Look up protected properties, keyed by value path.
* Skip protected properties that are explicitly set to `null`.
*/
if ( is_array( $value_path ) ) {
$path_string = implode( '.', $value_path );
$path_string = '';
for ( $i = 0; $i < count( $value_path ); $i++ ) {
Copy link
Member

Choose a reason for hiding this comment

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

is this bit faster than the call to implode?

if ( $i > 0 ) {
$path_string .= '.';
}
$path_string .= $value_path[ $i ];
}
if (
isset( static::PROTECTED_PROPERTIES[ $path_string ] ) &&
_wp_array_get( $settings, static::PROTECTED_PROPERTIES[ $path_string ], null ) === null
Expand All @@ -2233,9 +2246,7 @@ protected static function compute_style_properties( $styles, $settings = array()
$value = isset( $background_styles['declarations'][ $css_property ] ) ? $background_styles['declarations'][ $css_property ] : $value;
}

// Skip if empty and not "0" or value represents array of longhand values.
$has_missing_value = empty( $value ) && ! is_numeric( $value );
if ( $has_missing_value || is_array( $value ) ) {
if ( '' === $value || is_array( $value ) ) {
continue;
}

Expand Down Expand Up @@ -2267,6 +2278,10 @@ protected static function compute_style_properties( $styles, $settings = array()
);
}

if ( $update_cache ) {
wp_cache_set( 'str_start_with', $str_start_with_cache, 'theme_files' );
}

// If a variable value is added to the root, the corresponding property should be removed.
foreach ( $root_variable_duplicates as $duplicate ) {
$discard = array_search( $duplicate, array_column( $declarations, 'name' ), true );
Expand Down