Skip to content

Commit

Permalink
Fix generation of presets classes per block (#32190)
Browse files Browse the repository at this point in the history
  • Loading branch information
nosolosw authored May 26, 2021
1 parent 20dab8e commit a10584d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
24 changes: 23 additions & 1 deletion lib/class-wp-theme-json-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,28 @@ private static function compute_style_properties( $styles ) {
return $declarations;
}

/**
* Function that appends a sub-selector to a existing one.
*
* Given the compounded $selector "h1, h2, h3"
* and the $to_append selector ".some-class" the result will be
* "h1.some-class, h2.some-class, h3.some-class".
*
* @param string $selector Original selector.
* @param string $to_append Selector to append.
*
* @return string
*/
private static function append_to_selector( $selector, $to_append ) {
$new_selectors = array();
$selectors = explode( ',', $selector );
foreach ( $selectors as $sel ) {
$new_selectors[] = $sel . $to_append;
}

return implode( ',', $new_selectors );
}

/**
* Given a settings array, it returns the generated rulesets
* for the preset classes.
Expand All @@ -641,7 +663,7 @@ private static function compute_preset_classes( $settings, $selector ) {
foreach ( $values as $value ) {
foreach ( $preset['classes'] as $class ) {
$stylesheet .= self::to_ruleset(
$selector . '.has-' . $value['slug'] . '-' . $class['class_suffix'],
self::append_to_selector( $selector, '.has-' . $value['slug'] . '-' . $class['class_suffix'] ),
array(
array(
'name' => $class['property_name'],
Expand Down
27 changes: 27 additions & 0 deletions phpunit/class-wp-theme-json-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,33 @@ function test_get_stylesheet() {
);
}

function test_get_stylesheet_preset_classes_work_with_compounded_selectors() {
$theme_json = new WP_Theme_JSON_Gutenberg(
array(
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
'settings' => array(
'blocks' => array(
'core/heading' => array(
'color' => array(
'palette' => array(
array(
'slug' => 'white',
'color' => '#fff',
),
),
),
),
),
),
)
);

$this->assertEquals(
'h1.has-white-color,h2.has-white-color,h3.has-white-color,h4.has-white-color,h5.has-white-color,h6.has-white-color{color: #fff !important;}h1.has-white-background-color,h2.has-white-background-color,h3.has-white-background-color,h4.has-white-background-color,h5.has-white-background-color,h6.has-white-background-color{background-color: #fff !important;}h1.has-white-border-color,h2.has-white-border-color,h3.has-white-border-color,h4.has-white-border-color,h5.has-white-border-color,h6.has-white-border-color{border-color: #fff !important;}',
$theme_json->get_stylesheet( 'block_styles' )
);
}

function test_get_stylesheet_preset_rules_come_after_block_rules() {
$theme_json = new WP_Theme_JSON_Gutenberg(
array(
Expand Down

0 comments on commit a10584d

Please sign in to comment.