Skip to content
Draft
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
1 change: 1 addition & 0 deletions phpstan-strict.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion src/wp-includes/script-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -3635,7 +3635,7 @@ static function () use ( $style ) {
*
* @since 6.1.0
*
* @param array<string, bool> $options {
* @param array $options {
* Optional. An array of options to pass to wp_style_engine_get_stylesheet_from_context().
* Default empty array.
*
Expand All @@ -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;
Expand Down
47 changes: 30 additions & 17 deletions src/wp-includes/style-engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, mixed> $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,
Expand All @@ -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<non-falsy-string, non-empty-string>,
* 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 );
Expand Down Expand Up @@ -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<string, 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.
* }
* }
* @param array $options {
Expand All @@ -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();
Expand All @@ -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 );
}
Expand Down Expand Up @@ -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 );
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class WP_Style_Engine_CSS_Declarations {
*
* @since 6.1.0
*
* @var string[]
* @var array<string, string>
* @phpstan-var array<non-falsy-string, non-empty-string>
*/
protected $declarations = array();

Expand All @@ -31,7 +32,8 @@ class WP_Style_Engine_CSS_Declarations {
*
* @since 7.1.0
*
* @var array
* @var array<string, array<string, bool>>
* @phpstan-var array<non-falsy-string, array{ important?: bool }>
*/
protected $declaration_options = array();

Expand All @@ -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<string, string> $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 );
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -123,7 +120,7 @@ public function remove_declaration( $property ) {
*
* @since 6.1.0
*
* @param string[] $declarations An array of declarations.
* @param array<string, string> $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 ) {
Expand Down Expand Up @@ -153,7 +150,9 @@ public function remove_declarations( $properties = array() ) {
*
* @since 6.1.0
*
* @return string[] The declarations array.
* @return array<string, string> The declarations array.
*
* @phpstan-return array<non-falsy-string, non-empty-string>
*/
public function get_declarations() {
return $this->declarations;
Expand All @@ -164,7 +163,9 @@ public function get_declarations() {
*
* @since 7.1.0
*
* @return array Declaration options keyed by property name.
* @return array<string, array<string, bool>> Declaration options keyed by property name.
*
* @phpstan-return array<non-falsy-string, array{ important?: bool }>
*/
public function get_declaration_options() {
return $this->declaration_options;
Expand All @@ -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";
}

Expand Down
18 changes: 9 additions & 9 deletions src/wp-includes/style-engine/class-wp-style-engine-css-rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, 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`.
*/
public function __construct( $selector = '', $declarations = array(), $rules_group = '' ) {
$this->set_selector( $selector );
Expand All @@ -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<string, string>|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 ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
Loading
Loading