diff --git a/phpstan-strict.neon.dist b/phpstan-strict.neon.dist index ffa58a226d576..79caf4a5f8004 100644 --- a/phpstan-strict.neon.dist +++ b/phpstan-strict.neon.dist @@ -68,6 +68,7 @@ parameters: - src/wp-includes/sitemaps.php - src/wp-includes/speculative-loading.php - src/wp-includes/style-engine + - src/wp-includes/style-engine.php - tests/phpstan # The code outside the strict paths that they can use. The bundled themes are left out, since nothing in core uses diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index ef81a42106e88..e38470d6ca9b0 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -3635,7 +3635,7 @@ static function () use ( $style ) { * * @since 6.1.0 * - * @param array $options { + * @param array $options { * Optional. An array of options to pass to wp_style_engine_get_stylesheet_from_context(). * Default empty array. * @@ -3646,6 +3646,14 @@ static function () use ( $style ) { * } */ function wp_enqueue_stored_styles( $options = array() ) { + if ( '' === $options ) { + // Handle being called as an action callback. + $options = array( + 'optimize' => false, + 'prettify' => false, + ); + } + // Note: Styles printed at wp_footer for classic themes may still end up in the head due to wp_load_classic_theme_block_styles_on_demand(). $is_block_theme = wp_is_block_theme(); $is_classic_theme = ! $is_block_theme; diff --git a/src/wp-includes/style-engine.php b/src/wp-includes/style-engine.php index 835f1c3f1b723..962e2ce8ba37c 100644 --- a/src/wp-includes/style-engine.php +++ b/src/wp-includes/style-engine.php @@ -35,8 +35,8 @@ * @link https://developer.wordpress.org/block-editor/reference-guides/theme-json-reference/theme-json-living/#styles * @link https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/ * - * @param array $block_styles The style object. - * @param array $options { + * @param array $block_styles The style object. + * @param array|null $options { * Optional. An array of options. Default empty array. * * @type string|null $context An identifier describing the origin of the style object, @@ -59,15 +59,21 @@ * e.g. `array( "$property" => "$value", "$property" => "$value" )`. * @type string $classnames Classnames separated by a space. * } + * + * @phpstan-return array{ + * css?: non-empty-string, + * declarations?: non-empty-array, + * classnames?: non-empty-string, + * } */ function wp_style_engine_get_styles( $block_styles, $options = array() ) { - $options = wp_parse_args( - $options, + $options = array_merge( array( - 'selector' => null, + 'selector' => '', 'context' => null, 'convert_vars_to_classnames' => false, - ) + ), + $options ?? array() ); $parsed_styles = WP_Style_Engine::parse_block_styles( $block_styles, $options ); @@ -120,12 +126,12 @@ function wp_style_engine_get_styles( $block_styles, $options = array() ) { * Required. A collection of CSS rules. * * @type array ...$0 { - * @type string $rules_group A parent CSS selector in the case of nested CSS, - * or a CSS nested @rule, such as `@media (min-width: 80rem)` or `@layer module`. - * @type string $selector A CSS selector. - * @type string[]|WP_Style_Engine_CSS_Declarations $declarations An associative array of CSS definitions, - * e.g. `array( "$property" => "$value", "$property" => "$value" )`, - * or a WP_Style_Engine_CSS_Declarations object. + * @type string $rules_group A parent CSS selector in the case of nested CSS, + * or a CSS nested @rule, such as `@media (min-width: 80rem)` or `@layer module`. + * @type string $selector A CSS selector. + * @type array|WP_Style_Engine_CSS_Declarations $declarations An associative array of CSS definitions, + * e.g. `array( "$property" => "$value", "$property" => "$value" )`, + * or a WP_Style_Engine_CSS_Declarations object. * } * } * @param array $options { @@ -146,11 +152,11 @@ function wp_style_engine_get_stylesheet_from_css_rules( $css_rules, $options = a return ''; } - $options = wp_parse_args( - $options, + $options = array_merge( array( 'context' => null, - ) + ), + $options ); $css_rule_objects = array(); @@ -164,7 +170,7 @@ function wp_style_engine_get_stylesheet_from_css_rules( $css_rules, $options = a continue; } - $rules_group = $css_rule['rules_group'] ?? null; + $rules_group = $css_rule['rules_group'] ?? ''; if ( ! empty( $options['context'] ) ) { WP_Style_Engine::store_css_rule( $options['context'], $css_rule['selector'], $declarations, $rules_group ); } @@ -194,7 +200,14 @@ function wp_style_engine_get_stylesheet_from_css_rules( $css_rules, $options = a * Defaults to whether the `SCRIPT_DEBUG` constant is defined. * } * @return string A compiled CSS string. + * + * @phpstan-param array{ optimize?: bool, prettify?: bool } $options */ function wp_style_engine_get_stylesheet_from_context( $context, $options = array() ) { - return WP_Style_Engine::compile_stylesheet_from_css_rules( WP_Style_Engine::get_store( $context )->get_all_rules(), $options ); + $store = WP_Style_Engine::get_store( $context ); + if ( ! $store ) { + return ''; + } + + return WP_Style_Engine::compile_stylesheet_from_css_rules( $store->get_all_rules(), $options ); } diff --git a/src/wp-includes/style-engine/class-wp-style-engine-css-declarations.php b/src/wp-includes/style-engine/class-wp-style-engine-css-declarations.php index 797947d1e9e75..aec6dc273d3a5 100644 --- a/src/wp-includes/style-engine/class-wp-style-engine-css-declarations.php +++ b/src/wp-includes/style-engine/class-wp-style-engine-css-declarations.php @@ -22,7 +22,8 @@ class WP_Style_Engine_CSS_Declarations { * * @since 6.1.0 * - * @var string[] + * @var array + * @phpstan-var array */ protected $declarations = array(); @@ -31,7 +32,8 @@ class WP_Style_Engine_CSS_Declarations { * * @since 7.1.0 * - * @var array + * @var array> + * @phpstan-var array */ protected $declaration_options = array(); @@ -43,9 +45,9 @@ class WP_Style_Engine_CSS_Declarations { * * @since 6.1.0 * - * @param string[] $declarations Optional. An associative array of CSS definitions, - * e.g. `array( "$property" => "$value", "$property" => "$value" )`. - * Default empty array. + * @param array $declarations Optional. An associative array of CSS definitions, + * e.g. `array( "$property" => "$value", "$property" => "$value" )`. + * Default empty array. */ public function __construct( $declarations = array() ) { $this->add_declarations( $declarations ); @@ -85,12 +87,7 @@ public function add_declaration( $property, $value, $options = array() ) { return $this; } - $options = wp_parse_args( - $options, - array( - 'important' => false, - ) - ); + // Only options that are set are stored, so the default of `important` is left out. $options = array_filter( $options ); // Adds the declaration property/value pair. @@ -123,7 +120,7 @@ public function remove_declaration( $property ) { * * @since 6.1.0 * - * @param string[] $declarations An array of declarations. + * @param array $declarations An array of declarations (property => value pairs). * @return WP_Style_Engine_CSS_Declarations Returns the object to allow chaining methods. */ public function add_declarations( $declarations ) { @@ -153,7 +150,9 @@ public function remove_declarations( $properties = array() ) { * * @since 6.1.0 * - * @return string[] The declarations array. + * @return array The declarations array. + * + * @phpstan-return array */ public function get_declarations() { return $this->declarations; @@ -164,7 +163,9 @@ public function get_declarations() { * * @since 7.1.0 * - * @return array Declaration options keyed by property name. + * @return array> Declaration options keyed by property name. + * + * @phpstan-return array */ public function get_declaration_options() { return $this->declaration_options; @@ -190,17 +191,10 @@ public function get_declaration_options() { protected static function filter_declaration( $property, $value, $spacer = '', $options = array() ) { $filtered_value = wp_strip_all_tags( $value, true ); if ( '' !== $filtered_value ) { - $options = wp_parse_args( - $options, - array( - 'important' => false, - ) - ); - $filtered_declaration = safecss_filter_attr( "{$property}:{$spacer}{$filtered_value}" ); // Only append !important in the presence of an option value and when sanitization returns a single declaration. - if ( true === $options['important'] && '' !== $filtered_declaration && ! str_contains( $filtered_declaration, ';' ) ) { + if ( true === ( $options['important'] ?? false ) && '' !== $filtered_declaration && ! str_contains( $filtered_declaration, ';' ) ) { return "$filtered_declaration !important"; } diff --git a/src/wp-includes/style-engine/class-wp-style-engine-css-rule.php b/src/wp-includes/style-engine/class-wp-style-engine-css-rule.php index a483911335ad9..f921d558b18f8 100644 --- a/src/wp-includes/style-engine/class-wp-style-engine-css-rule.php +++ b/src/wp-includes/style-engine/class-wp-style-engine-css-rule.php @@ -50,13 +50,13 @@ class WP_Style_Engine_CSS_Rule { * @since 6.1.0 * @since 6.6.0 Added the `$rules_group` parameter. * - * @param string $selector Optional. The CSS selector. Default empty string. - * @param string[]|WP_Style_Engine_CSS_Declarations $declarations Optional. An associative array of CSS definitions, - * e.g. `array( "$property" => "$value", "$property" => "$value" )`, - * or a WP_Style_Engine_CSS_Declarations object. - * Default empty array. - * @param string $rules_group A parent CSS selector in the case of nested CSS, or a CSS nested @rule, - * such as `@media (min-width: 80rem)` or `@layer module`. + * @param string $selector Optional. The CSS selector. Default empty string. + * @param array|WP_Style_Engine_CSS_Declarations $declarations Optional. An associative array of CSS definitions, + * e.g. `array( "$property" => "$value", "$property" => "$value" )`, + * or a WP_Style_Engine_CSS_Declarations object. + * Default empty array. + * @param string $rules_group A parent CSS selector in the case of nested CSS, or a CSS nested @rule, + * such as `@media (min-width: 80rem)` or `@layer module`. */ public function __construct( $selector = '', $declarations = array(), $rules_group = '' ) { $this->set_selector( $selector ); @@ -83,8 +83,8 @@ public function set_selector( $selector ) { * @since 6.1.0 * @since 7.1.0 Preserves declaration options when declarations are provided as a `WP_Style_Engine_CSS_Declarations` object. * - * @param string[]|WP_Style_Engine_CSS_Declarations $declarations An array of declarations (property => value pairs), - * or a WP_Style_Engine_CSS_Declarations object. + * @param array|WP_Style_Engine_CSS_Declarations $declarations An array of declarations (property => value pairs), + * or a WP_Style_Engine_CSS_Declarations object. * @return WP_Style_Engine_CSS_Rule Returns the object to allow chaining of methods. */ public function add_declarations( $declarations ) { diff --git a/src/wp-includes/style-engine/class-wp-style-engine-processor.php b/src/wp-includes/style-engine/class-wp-style-engine-processor.php index a896ea675975e..c25c8020f0152 100644 --- a/src/wp-includes/style-engine/class-wp-style-engine-processor.php +++ b/src/wp-includes/style-engine/class-wp-style-engine-processor.php @@ -120,7 +120,7 @@ public function get_css( $options = array() ) { 'optimize' => false, 'prettify' => defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG, ); - $options = wp_parse_args( $options, $defaults ); + $options = array_merge( $defaults, $options ); // If we have stores, get the rules from them. foreach ( $this->stores as $store ) { diff --git a/src/wp-includes/style-engine/class-wp-style-engine.php b/src/wp-includes/style-engine/class-wp-style-engine.php index 2ccfdefa95e0b..151180d1a44c0 100644 --- a/src/wp-includes/style-engine/class-wp-style-engine.php +++ b/src/wp-includes/style-engine/class-wp-style-engine.php @@ -30,11 +30,11 @@ * @since 7.2.0 Added support for typography.textShadow. * * @phpstan-type Style_Definition array{ - * property_keys?: array{ default: string, individual?: string }, - * path: non-empty-list, - * css_vars?: array, - * classnames?: array, - * value_func?: callable( mixed, array, array ): array, + * property_keys?: array{ default: non-falsy-string, individual?: non-falsy-string }, + * path: non-empty-list, + * css_vars?: array, + * classnames?: array, + * value_func?: callable( mixed, array, array ): array, * } */ #[AllowDynamicProperties] @@ -421,7 +421,8 @@ protected static function get_slug_from_preset_value( $style_value, $property_ke if ( is_string( $style_value ) && is_string( $property_key ) && str_contains( $style_value, "var:preset|{$property_key}|" ) ) { - $index_to_splice = strrpos( $style_value, '|' ) + 1; + // The string contains a pipe, as checked above, so strrpos() does not return false. + $index_to_splice = (int) strrpos( $style_value, '|' ) + 1; return _wp_to_kebab_case( substr( $style_value, $index_to_splice ) ); } return ''; @@ -459,6 +460,8 @@ protected static function get_css_var_value( $style_value, $css_vars ) { * * @param mixed $style_value A single raw style value. * @return bool + * + * @phpstan-assert-if-true !'' $style_value */ protected static function is_valid_style_value( $style_value ) { return '0' === $style_value || ! empty( $style_value ); @@ -471,16 +474,16 @@ protected static function is_valid_style_value( $style_value ) { * @since 6.6.0 Added the `$rules_group` parameter. * @since 7.1.0 Extended `$css_declarations` parameter to accept `WP_Style_Engine_CSS_Declarations` object. * - * @param string $store_name A valid store key. - * @param string $css_selector When a selector is passed, the function will return - * a full CSS rule `$selector { ...rules }` - * otherwise a concatenated string of properties and values. - * @param string[]|WP_Style_Engine_CSS_Declarations $css_declarations An associative array of CSS definitions, - * e.g. `array( "$property" => "$value", "$property" => "$value" )`, - * or a WP_Style_Engine_CSS_Declarations object. - * @param string $rules_group Optional. A parent CSS selector in the case of nested CSS, - * or a CSS nested @rule, such as `@media (min-width: 80rem)` - * or `@layer module`. + * @param string $store_name A valid store key. + * @param string $css_selector When a selector is passed, the function will return + * a full CSS rule `$selector { ...rules }` + * otherwise a concatenated string of properties and values. + * @param array|WP_Style_Engine_CSS_Declarations $css_declarations An associative array of CSS definitions, + * e.g. `array( "$property" => "$value", "$property" => "$value" )`, + * or a WP_Style_Engine_CSS_Declarations object. + * @param string $rules_group Optional. A parent CSS selector in the case of nested CSS, + * or a CSS nested @rule, such as `@media (min-width: 80rem)` + * or `@layer module`. */ public static function store_css_rule( $store_name, $css_selector, $css_declarations, $rules_group = '' ): void { if ( empty( $store_name ) || empty( $css_selector ) || empty( $css_declarations ) ) { @@ -536,6 +539,8 @@ public static function get_store( $store_name ) { * @type string[] $declarations An associative array of CSS definitions, * e.g. `array( "$property" => "$value", "$property" => "$value" )`. * } + * + * @phpstan-return array{ classnames: list, declarations: array } */ public static function parse_block_styles( $block_styles, $options ) { $parsed_styles = array( @@ -638,9 +643,10 @@ protected static function get_classnames( $style_value, $style_definition ) { * e.g. `var:preset||`, * to `var( --wp--preset--* )` values. Default false. * } - * @return string[] An associative array of CSS definitions, e.g. `array( "$property" => "$value", "$property" => "$value" )`. + * @return array An associative array of CSS definitions, e.g. `array( "$property" => "$value", "$property" => "$value" )`. * * @phpstan-param Style_Definition $style_definition + * @phpstan-return array */ protected static function get_css_declarations( $style_value, $style_definition, $options = array() ) { if ( isset( $style_definition['value_func'] ) && is_callable( $style_definition['value_func'] ) ) { @@ -695,8 +701,8 @@ protected static function get_css_declarations( $style_value, $style_definition, return $css_declarations; } - // CSS declarations only accept string values. - if ( is_string( $style_value ) ) { + // CSS declarations only accept non-empty string values. + if ( is_string( $style_value ) && static::is_valid_style_value( $style_value ) ) { $css_declarations[ $style_property_keys['default'] ] = $style_value; } return $css_declarations; @@ -731,9 +737,10 @@ protected static function get_css_declarations( $style_value, $style_definition, * e.g. `var:preset||`, * to `var( --wp--preset--* )` values. Default false. * } - * @return string[] An associative array of CSS definitions, e.g. `array( "$property" => "$value", "$property" => "$value" )`. + * @return array An associative array of CSS definitions, e.g. `array( "$property" => "$value", "$property" => "$value" )`. * * @phpstan-param Style_Definition $individual_property_definition + * @phpstan-return array */ protected static function get_individual_property_css_declarations( $style_value, $individual_property_definition, $options = array() ) { if ( ! is_array( $style_value ) || empty( $style_value ) || empty( $individual_property_definition['path'] ) ) { @@ -768,6 +775,10 @@ protected static function get_individual_property_css_declarations( $style_value && ! $should_skip_css_vars && ! empty( $individual_property_definition['css_vars'] ) ) { $value = static::get_css_var_value( $value, $individual_property_definition['css_vars'] ); + + if ( ! static::is_valid_style_value( $value ) ) { + continue; + } } $individual_css_property = sprintf( $style_definition['property_keys']['individual'], $individual_property_key ); @@ -788,16 +799,12 @@ protected static function get_individual_property_css_declarations( $style_value * * @param mixed $style_value A single raw style value from $block_styles array. * @param array $style_definition A single style definition from {@see self::BLOCK_STYLE_DEFINITIONS_METADATA}. - * @return string[] An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ). + * @return array An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ). * * @phpstan-param Style_Definition $style_definition - * @phpstan-return array + * @phpstan-return array */ protected static function get_url_or_value_css_declaration( $style_value, $style_definition ) { - if ( empty( $style_value ) ) { - return array(); - } - $css_declarations = array(); if ( isset( $style_definition['property_keys']['default'] ) ) { @@ -805,7 +812,7 @@ protected static function get_url_or_value_css_declaration( $style_value, $style if ( is_array( $style_value ) && ! empty( $style_value['url'] ) && is_string( $style_value['url'] ) ) { $value = "url('" . $style_value['url'] . "')"; - } elseif ( is_string( $style_value ) ) { + } elseif ( is_string( $style_value ) && ! empty( $style_value ) ) { $value = $style_value; } @@ -863,11 +870,11 @@ protected static function get_background_clip_css_declarations( $style_value, $s * * @since 6.1.0 * - * @param string[] $css_declarations An associative array of CSS definitions, - * e.g. `array( "$property" => "$value", "$property" => "$value" )`. - * @param string $css_selector When a selector is passed, the function will return - * a full CSS rule `$selector { ...rules }`, - * otherwise a concatenated string of properties and values. + * @param array $css_declarations An associative array of CSS definitions, + * e.g. `array( "$property" => "$value", "$property" => "$value" )`. + * @param string $css_selector When a selector is passed, the function will return + * a full CSS rule `$selector { ...rules }`, + * otherwise a concatenated string of properties and values. * @return string A compiled CSS string. */ public static function compile_css( $css_declarations, $css_selector ) { diff --git a/tests/phpstan/baselines-strict/argument.type.neon b/tests/phpstan/baselines-strict/argument.type.neon index dd83b950de430..7915abd17a20d 100644 --- a/tests/phpstan/baselines-strict/argument.type.neon +++ b/tests/phpstan/baselines-strict/argument.type.neon @@ -43,18 +43,3 @@ parameters: identifier: argument.type count: 1 path: ../../../src/wp-includes/ai-client/adapters/class-wp-ai-client-http-client.php - - - message: '#^Parameter \#4 \$options of static method WP_Style_Engine_CSS_Declarations\:\:filter_declaration\(\) expects array\{important\?\: bool, \.\.\.\}, mixed given\.$#' - identifier: argument.type - count: 1 - path: ../../../src/wp-includes/style-engine/class-wp-style-engine-css-declarations.php - - - message: '#^Parameter \#3 \$options of method WP_Style_Engine_CSS_Declarations\:\:add_declaration\(\) expects array\{important\?\: bool, \.\.\.\}, mixed given\.$#' - identifier: argument.type - count: 1 - path: ../../../src/wp-includes/style-engine/class-wp-style-engine-css-rule.php - - - message: '#^Parameter \#1 \$should_prettify of method WP_Style_Engine_CSS_Rule\:\:get_css\(\) expects bool, mixed given\.$#' - identifier: argument.type - count: 1 - path: ../../../src/wp-includes/style-engine/class-wp-style-engine-processor.php diff --git a/tests/phpstan/baselines-strict/missingType.iterableValue.neon b/tests/phpstan/baselines-strict/missingType.iterableValue.neon index 862bff081da41..5f4c28dc8d01a 100644 --- a/tests/phpstan/baselines-strict/missingType.iterableValue.neon +++ b/tests/phpstan/baselines-strict/missingType.iterableValue.neon @@ -43,13 +43,3 @@ parameters: identifier: missingType.iterableValue count: 1 path: ../../../src/wp-includes/ai-client.php - - - message: '#^Method WP_Style_Engine_CSS_Declarations\:\:get_declaration_options\(\) return type has no value type specified in iterable type array\.$#' - identifier: missingType.iterableValue - count: 1 - path: ../../../src/wp-includes/style-engine/class-wp-style-engine-css-declarations.php - - - message: '#^Property WP_Style_Engine_CSS_Declarations\:\:\$declaration_options type has no value type specified in iterable type array\.$#' - identifier: missingType.iterableValue - count: 1 - path: ../../../src/wp-includes/style-engine/class-wp-style-engine-css-declarations.php diff --git a/tests/phpunit/tests/style-engine/styleEngine.php b/tests/phpunit/tests/style-engine/styleEngine.php index 504f069f715ce..7b060be9bc907 100644 --- a/tests/phpunit/tests/style-engine/styleEngine.php +++ b/tests/phpunit/tests/style-engine/styleEngine.php @@ -1058,4 +1058,45 @@ public function test_should_omit_non_string_values_from_declarations() { wp_style_engine_get_styles( $block_styles ) ); } + + /** + * Tests that a preset for an individual border side that matches none of its + * CSS var patterns is left out of the returned declarations, not returned as empty. + * + * @ticket 66739 + * + * @covers ::wp_style_engine_get_styles + */ + public function test_should_omit_unmatched_individual_preset_from_declarations(): void { + $block_styles = array( + 'border' => array( + 'top' => array( + 'color' => 'var:preset|font-size|large', + 'style' => 'solid', + ), + ), + ); + + $this->assertSame( + array( + 'css' => 'border-top-style:solid;', + 'declarations' => array( + 'border-top-style' => 'solid', + ), + ), + wp_style_engine_get_styles( $block_styles ) + ); + } + + /** + * Tests that a context with no store returns an empty stylesheet + * instead of calling {@see WP_Style_Engine_CSS_Rules_Store::get_all_rules()} on null. + * + * @ticket 66739 + * + * @covers ::wp_style_engine_get_stylesheet_from_context + */ + public function test_should_return_empty_stylesheet_for_context_without_store(): void { + $this->assertSame( '', wp_style_engine_get_stylesheet_from_context( '' ) ); + } }