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

Use the style-engine in global styles #42893

Open
wants to merge 13 commits into
base: trunk
Choose a base branch
from
10 changes: 9 additions & 1 deletion lib/compat/wordpress-6.2/get-global-styles-and-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,15 @@ function gutenberg_get_global_stylesheet( $types = array() ) {
if ( $can_use_cached ) {
wp_cache_set( $cache_key, $stylesheet, $cache_group );
}
return $stylesheet;

$processor = new WP_Style_Engine_Processor_Gutenberg();
$processor->add_css_string( $stylesheet );

return $processor->get_css(
array(
'prettify' => defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG,
)
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,16 @@ public function get_declarations_string( $should_prettify = false, $indent_count
$spacer = $should_prettify ? ' ' : '';

foreach ( $declarations_array as $property => $value ) {
if ( 0 === strpos( $property, '--' ) ) { // Account for CSS variables.
$declarations_output .= "{$indent}{$property}:{$spacer}{$value};$suffix";
continue;
}
$filtered_declaration = static::filter_declaration( $property, $value, $spacer );
if ( $filtered_declaration ) {
$declarations_output .= "{$indent}{$filtered_declaration};$suffix";
}
}

return rtrim( $declarations_output );
}

Expand Down
45 changes: 45 additions & 0 deletions packages/style-engine/class-wp-style-engine-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,51 @@ public function add_rules( $css_rules ) {
return $this;
}

/**
* Adds rules from a plain CSS string.
*
* @param string $css_string The CSS string to add.
*/
public function add_css_string( $css_string ) {
$css_array = array();
// Split CSS by rules.
$css_rules = explode( '}', $css_string );

foreach ( $css_rules as $css_rule ) {
$rule_parts = explode( '{', $css_rule );
if ( 2 !== count( $rule_parts ) ) {
continue;
}
// Get the selector.
$selector = trim( $rule_parts[0] );
if ( empty( $selector ) ) {
continue;
}

// Create the rule object.
if ( ! isset( $css_array[ $selector ] ) ) {
$css_array[ $selector ] = new WP_Style_Engine_CSS_Rule( $selector );
}

// Get the declarations.
$declarations = explode( ';', $rule_parts[1] );
$declarations_array = array();
foreach ( $declarations as $declaration ) {
$declaration_parts = explode( ':', $declaration );
if ( 2 !== count( $declaration_parts ) ) {
continue;
}
$declarations_array[ $declaration_parts[0] ] = $declaration_parts[1];
}

// Add the declarations to the rule.
$css_array[ $selector ]->add_declarations( $declarations_array );
}

// Add the rules to the processor.
$this->add_rules( $css_array );
}

/**
* Get the CSS rules as a string.
*
Expand Down