Skip to content
Closed
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
89 changes: 48 additions & 41 deletions src/wp-includes/class-wp-theme-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -3715,22 +3715,45 @@ private static function get_block_nodes( $theme_json, $selectors = array(), $opt
}
}
}
if ( isset( $theme_json['styles']['blocks'][ $name ]['elements'] ) ) {
foreach ( $theme_json['styles']['blocks'][ $name ]['elements'] as $element => $node ) {
/*
* Elements can be styled outside any breakpoint, inside one, or both,
* so collect the names from all of those places before looping. An
* element styled only inside a breakpoint still needs a node.
*/
$block_node = $theme_json['styles']['blocks'][ $name ] ?? array();
$element_names = array_keys( $block_node['elements'] ?? array() );
foreach ( array_keys( $responsive_media_queries ) as $breakpoint ) {
$element_names = array_merge(
$element_names,
array_keys( $block_node[ $breakpoint ]['elements'] ?? array() )
);
}
$element_names = array_unique( $element_names );

if ( ! empty( $element_names ) ) {
foreach ( $element_names as $element ) {
$element_path = array( 'styles', 'blocks', $name, 'elements', $element );
if ( $include_node_paths_only ) {
$nodes[] = array(
'path' => $element_path,
);
if ( isset( $block_node['elements'][ $element ] ) ) {
$nodes[] = array(
'path' => $element_path,
);
}
continue;
}

if ( ! isset( $selectors[ $name ]['elements'][ $element ] ) ) {
continue;
}

$element_selector = $selectors[ $name ]['elements'][ $element ];

$nodes[] = array(
'path' => $element_path,
'selector' => $element_selector,
);
if ( isset( $block_node['elements'][ $element ] ) ) {
$nodes[] = array(
'path' => $element_path,
'selector' => $element_selector,
);
}

// Responsive element nodes: one node per breakpoint that has
// styles for this element. Cascade: a{} → @media{a{}}
Expand All @@ -3747,42 +3770,26 @@ private static function get_block_nodes( $theme_json, $selectors = array(), $opt
// Handle any pseudo selectors for the element.
if ( isset( static::VALID_ELEMENT_PSEUDO_SELECTORS[ $element ] ) ) {
foreach ( static::VALID_ELEMENT_PSEUDO_SELECTORS[ $element ] as $pseudo_selector ) {
// Create element pseudo node if default or any responsive breakpoint has the pseudo.
$has_element_pseudo = isset( $theme_json['styles']['blocks'][ $name ]['elements'][ $element ][ $pseudo_selector ] );
if ( ! $has_element_pseudo ) {
foreach ( array_keys( $responsive_media_queries ) as $bp ) {
if ( isset( $theme_json['styles']['blocks'][ $name ][ $bp ]['elements'][ $element ][ $pseudo_selector ] ) ) {
$has_element_pseudo = true;
break;
}
}
}

if ( $has_element_pseudo ) {
$element_pseudo_path = array( 'styles', 'blocks', $name, 'elements', $element );
if ( $include_node_paths_only ) {
$nodes[] = array(
'path' => $element_pseudo_path,
);
continue;
}

// Emit the default pseudo node only when the default state styles
// the pseudo. Otherwise get_styles_for_block() falls back to the
// element's base styles, outputting a rule the theme never defined.
if ( isset( $theme_json['styles']['blocks'][ $name ]['elements'][ $element ][ $pseudo_selector ] ) ) {
$nodes[] = array(
'path' => $element_pseudo_path,
'path' => array( 'styles', 'blocks', $name, 'elements', $element ),
'selector' => static::append_to_selector( $element_selector, $pseudo_selector ),
);
}

// Responsive element pseudo nodes: one node per breakpoint
// that has this pseudo state for this element.
// Cascade: a:hover{} → @media{a:hover{}}
foreach ( array_keys( $responsive_media_queries ) as $breakpoint ) {
if ( isset( $theme_json['styles']['blocks'][ $name ][ $breakpoint ]['elements'][ $element ][ $pseudo_selector ] ) ) {
$nodes[] = array(
'path' => array( 'styles', 'blocks', $name, $breakpoint, 'elements', $element ),
'selector' => static::append_to_selector( $element_selector, $pseudo_selector ),
'media_query' => $responsive_media_queries[ $breakpoint ],
);
}
// Responsive element pseudo nodes: one node per breakpoint
// that has this pseudo state for this element.
// Cascade: a:hover{} → @media{a:hover{}}
foreach ( array_keys( $responsive_media_queries ) as $breakpoint ) {
if ( isset( $theme_json['styles']['blocks'][ $name ][ $breakpoint ]['elements'][ $element ][ $pseudo_selector ] ) ) {
$nodes[] = array(
'path' => array( 'styles', 'blocks', $name, $breakpoint, 'elements', $element ),
'selector' => static::append_to_selector( $element_selector, $pseudo_selector ),
'media_query' => $responsive_media_queries[ $breakpoint ],
);
}
}
}
Expand Down
5 changes: 0 additions & 5 deletions tests/phpstan/baselines/if.alwaysFalse.neon
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ parameters:
identifier: if.alwaysFalse
count: 2
path: ../../../src/wp-includes/class-wp-block-processor.php
-
message: '#^If condition is always false\.$#'
identifier: if.alwaysFalse
count: 1
path: ../../../src/wp-includes/class-wp-theme-json.php
-
message: '#^If condition is always false\.$#'
identifier: if.alwaysFalse
Expand Down
156 changes: 156 additions & 0 deletions tests/phpunit/tests/theme/wpThemeJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -1448,6 +1448,162 @@ public function test_get_styles_for_block_outputs_responsive_block_gap_after_def
$this->assertLessThan( strpos( $actual_styles, $mobile_gap ), strpos( $actual_styles, $default_gap ) );
}

/**
* @ticket 65827
*/
public function test_get_stylesheet_renders_element_styles_defined_only_in_a_breakpoint() {
$theme_json = new WP_Theme_JSON(
array(
'version' => WP_Theme_JSON::LATEST_SCHEMA,
'styles' => array(
'blocks' => array(
'core/group' => array(
'@mobile' => array(
'elements' => array(
'link' => array(
'color' => array(
'text' => 'red',
),
),
),
),
),
),
),
)
);

$expected = '@media (width <= 480px){:root :where(.wp-block-group a:where(:not(.wp-element-button))){color: red;}}';

$this->assertSame(
$expected,
$theme_json->get_stylesheet( array( 'styles' ), null, array( 'skip_root_layout_styles' => true ) )
);
}

/**
* @ticket 65827
*/
public function test_get_stylesheet_renders_element_pseudo_styles_defined_only_in_a_breakpoint() {
$theme_json = new WP_Theme_JSON(
array(
'version' => WP_Theme_JSON::LATEST_SCHEMA,
'styles' => array(
'blocks' => array(
'core/group' => array(
'@mobile' => array(
'elements' => array(
'link' => array(
':hover' => array(
'color' => array(
'text' => 'red',
),
),
),
),
),
),
),
),
)
);

$expected = '@media (width <= 480px){:root :where(.wp-block-group a:where(:not(.wp-element-button)):hover){color: red;}}';

$this->assertSame(
$expected,
$theme_json->get_stylesheet( array( 'styles' ), null, array( 'skip_root_layout_styles' => true ) )
);
}

/**
* @ticket 65827
*/
public function test_get_stylesheet_renders_element_styles_defined_only_in_separate_breakpoints() {
$theme_json = new WP_Theme_JSON(
array(
'version' => WP_Theme_JSON::LATEST_SCHEMA,
'styles' => array(
'blocks' => array(
'core/group' => array(
'@mobile' => array(
'elements' => array(
'link' => array(
'color' => array(
'text' => 'red',
),
),
),
),
'@tablet' => array(
'elements' => array(
'link' => array(
'color' => array(
'text' => 'blue',
),
),
),
),
),
),
),
)
);

$link_selector = ':root :where(.wp-block-group a:where(:not(.wp-element-button)))';
$expected = '@media (width <= 480px){' . $link_selector . '{color: red;}}' .
'@media (480px < width <= 782px){' . $link_selector . '{color: blue;}}';

$this->assertSame(
$expected,
$theme_json->get_stylesheet( array( 'styles' ), null, array( 'skip_root_layout_styles' => true ) )
);
}

/**
* @ticket 65827
*/
public function test_get_stylesheet_does_not_duplicate_base_element_styles_into_pseudo_rule() {
$theme_json = new WP_Theme_JSON(
array(
'version' => WP_Theme_JSON::LATEST_SCHEMA,
'styles' => array(
'blocks' => array(
'core/group' => array(
'elements' => array(
'link' => array(
'color' => array(
'text' => 'blue',
),
),
),
'@mobile' => array(
'elements' => array(
'link' => array(
':hover' => array(
'color' => array(
'text' => 'darkred',
),
),
),
),
),
),
),
),
)
);

$link_selector = ':root :where(.wp-block-group a:where(:not(.wp-element-button)))';
$expected = $link_selector . '{color: blue;}' .
'@media (width <= 480px){:root :where(.wp-block-group a:where(:not(.wp-element-button)):hover){color: darkred;}}';

$this->assertSame(
$expected,
$theme_json->get_stylesheet( array( 'styles' ), null, array( 'skip_root_layout_styles' => true ) )
);
}

/**
* @ticket 65164
*/
Expand Down
Loading