Skip to content

Commit ea13827

Browse files
committed
removing unnecessary sanitization and improving class name generation
1 parent 9de97f5 commit ea13827

File tree

2 files changed

+11
-56
lines changed

2 files changed

+11
-56
lines changed

src/wp-includes/block-supports/block-visibility.php

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -83,41 +83,27 @@ function wp_render_block_visibility_support( $block_content, $block ) {
8383
return $block_content;
8484
}
8585

86-
// If the block is hidden on all breakpoints, return empty string.
86+
// If the block is hidden on all breakpoints, do not render the block.
8787
if ( count( $hidden_on ) === count( $breakpoint_queries ) ) {
8888
return '';
8989
}
9090

91-
// Generate a unique class name based on which breakpoints are hidden.
91+
// Maintain consistent order of breakpoints for class name generation.
9292
sort( $hidden_on );
9393

94-
// Sanitize breakpoint names for use in HTML class attribute.
95-
$sanitized_hidden_on = array_map( 'sanitize_html_class', $hidden_on );
96-
$sanitized_hidden_on = array_filter( $sanitized_hidden_on );
97-
98-
// If all breakpoint names were invalid after sanitization, return unchanged.
99-
if ( empty( $sanitized_hidden_on ) ) {
100-
return $block_content;
101-
}
102-
103-
$visibility_class = 'wp-block-hidden-' . implode( '-', $sanitized_hidden_on );
104-
105-
// Generate CSS rules for each hidden breakpoint.
106-
$css_rules = array();
94+
$visibility_class = 'wp-block-hidden-' . implode( '-', $hidden_on );
95+
$css_rules = array();
10796

10897
foreach ( $hidden_on as $breakpoint ) {
109-
if ( isset( $breakpoint_queries[ $breakpoint ] ) ) {
110-
$css_rules[] = array(
111-
'selector' => '.' . $visibility_class,
112-
'declarations' => array(
113-
'display' => 'none !important',
114-
),
115-
'rules_group' => $breakpoint_queries[ $breakpoint ],
116-
);
117-
}
98+
$css_rules[] = array(
99+
'selector' => '.' . $visibility_class,
100+
'declarations' => array(
101+
'display' => 'none !important',
102+
),
103+
'rules_group' => $breakpoint_queries[ $breakpoint ],
104+
);
118105
}
119106

120-
// Use the style engine to enqueue the CSS.
121107
if ( ! empty( $css_rules ) ) {
122108
wp_style_engine_get_stylesheet_from_css_rules(
123109
$css_rules,
@@ -127,7 +113,6 @@ function wp_render_block_visibility_support( $block_content, $block ) {
127113
)
128114
);
129115

130-
// Add the visibility class to the block content.
131116
if ( ! empty( $block_content ) ) {
132117
$processor = new WP_HTML_Tag_Processor( $block_content );
133118
if ( $processor->next_tag() ) {

tests/phpunit/tests/block-supports/block-visibility.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -128,36 +128,6 @@ public function test_block_visibility_support_no_visibility_attribute() {
128128
$this->assertSame( $block_content, $result );
129129
}
130130

131-
/*
132-
* @ticket 64414
133-
*/
134-
public function test_block_visibility_support_generated_css_with_display_none() {
135-
$this->register_visibility_block_with_support(
136-
'test/css-generation',
137-
array( 'visibility' => true )
138-
);
139-
140-
$block = array(
141-
'blockName' => 'test/css-generation',
142-
'attrs' => array(
143-
'metadata' => array(
144-
'blockVisibility' => array(
145-
'mobile' => false,
146-
),
147-
),
148-
),
149-
);
150-
151-
$block_content = '<div>Test content</div>';
152-
wp_render_block_visibility_support( $block_content, $block );
153-
154-
$stylesheet = wp_style_engine_get_stylesheet_from_context( 'block-supports' );
155-
156-
$this->assertStringContainsString( 'display:none!important', str_replace( ' ', '', $stylesheet ), 'display:none!important should be in the CSS' );
157-
$this->assertStringContainsString( '.wp-block-hidden-mobile', $stylesheet, 'Stylesheet should contain the visibility class' );
158-
$this->assertStringContainsString( '@media', $stylesheet, 'Stylesheet should contain media query' );
159-
}
160-
161131
/*
162132
* @ticket 64414
163133
*/

0 commit comments

Comments
 (0)