Skip to content
Closed
Changes from 1 commit
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
Next Next commit
Improve theme file caching in class-wp-theme-json.php
Minor changes have been made to class-wp-theme-json.php to better cache the theme files. This includes adding an 'update_cache' functionality that sets 'str_start_with' and 'str_start_with_cache', and removing redundant checks for empty or non-numerical values. These improvements aim to optimize performance by updating cache when necessary.
  • Loading branch information
spacedmonkey committed Jun 2, 2024
commit bde7afe3e228c493c8c9ba8712e7dd1d08b67571
25 changes: 17 additions & 8 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,8 +2216,8 @@ 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 );
}

/*
Expand All @@ -2233,9 +2240,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 +2272,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