Skip to content

Commit

Permalink
Use wp_unique_id() instead of uniqid() to generate CSS class names (
Browse files Browse the repository at this point in the history
#38891)

* Use wp_unique_id() instead of uniqid() to generate CSS class names

* Fix elements test by upadting pattern in make_unique_id_one
  • Loading branch information
westonruter authored and Mamaduka committed Mar 29, 2022
1 parent 0cddcb1 commit 198f97c
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/block-supports/duotone.php
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ function gutenberg_render_duotone_support( $block_content, $block ) {
}

$filter_preset = array(
'slug' => uniqid(),
'slug' => wp_unique_id( sanitize_key( implode( '-', $block['attrs']['style']['color']['duotone'] ) . '-' ) ),
'colors' => $block['attrs']['style']['color']['duotone'],
);
$filter_property = gutenberg_get_duotone_filter_property( $filter_preset );
Expand Down
2 changes: 1 addition & 1 deletion lib/block-supports/elements.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function gutenberg_render_elements_support( $block_content, $block ) {
return $block_content;
}

$class_name = 'wp-elements-' . uniqid();
$class_name = wp_unique_id( 'wp-elements-' );

if ( strpos( $link_color, 'var:preset|color|' ) !== false ) {
// Get the name from the string and add proper styles.
Expand Down
8 changes: 4 additions & 4 deletions lib/block-supports/layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,18 @@ function gutenberg_render_layout_support_flag( $block_content, $block ) {
$used_layout = $default_layout;
}

$id = uniqid();
$gap_value = _wp_array_get( $block, array( 'attrs', 'style', 'spacing', 'blockGap' ) );
$class_name = wp_unique_id( 'wp-container-' );
$gap_value = _wp_array_get( $block, array( 'attrs', 'style', 'spacing', 'blockGap' ) );
// Skip if gap value contains unsupported characters.
// Regex for CSS value borrowed from `safecss_filter_attr`, and used here
// because we only want to match against the value, not the CSS attribute.
$gap_value = preg_match( '%[\\\(&=}]|/\*%', $gap_value ) ? null : $gap_value;
$style = gutenberg_get_layout_style( ".wp-container-$id", $used_layout, $has_block_gap_support, $gap_value );
$style = gutenberg_get_layout_style( ".$class_name", $used_layout, $has_block_gap_support, $gap_value );
// This assumes the hook only applies to blocks with a single wrapper.
// I think this is a reasonable limitation for that particular hook.
$content = preg_replace(
'/' . preg_quote( 'class="', '/' ) . '/',
'class="wp-container-' . $id . ' ',
'class="' . esc_attr( $class_name ) . ' ',
$block_content,
1
);
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/archives/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function render_block_core_archives( $attributes ) {

$class .= ' wp-block-archives-dropdown';

$dropdown_id = esc_attr( uniqid( 'wp-block-archives-' ) );
$dropdown_id = wp_unique_id( 'wp-block-archives-' );
$title = __( 'Archives' );

/** This filter is documented in wp-includes/widgets/class-wp-widget-archives.php */
Expand Down
8 changes: 4 additions & 4 deletions packages/block-library/src/navigation/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ function render_block_core_navigation( $attributes, $content, $block ) {
)
);

$modal_unique_id = uniqid();
$modal_unique_id = wp_unique_id( 'modal-' );

// Determine whether or not navigation elements should be wrapped in the markup required to make it responsive,
// return early if they don't.
Expand All @@ -538,12 +538,12 @@ function render_block_core_navigation( $attributes, $content, $block ) {
);

$responsive_container_markup = sprintf(
'<button aria-haspopup="true" aria-label="%3$s" class="%6$s" data-micromodal-trigger="modal-%1$s"><svg width="24" height="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" role="img" aria-hidden="true" focusable="false"><rect x="4" y="7.5" width="16" height="1.5" /><rect x="4" y="15" width="16" height="1.5" /></svg></button>
<div class="%5$s" style="%7$s" id="modal-%1$s">
'<button aria-haspopup="true" aria-label="%3$s" class="%6$s" data-micromodal-trigger="%1$s"><svg width="24" height="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" role="img" aria-hidden="true" focusable="false"><rect x="4" y="7.5" width="16" height="1.5" /><rect x="4" y="15" width="16" height="1.5" /></svg></button>
<div class="%5$s" style="%7$s" id="%1$s">
<div class="wp-block-navigation__responsive-close" tabindex="-1" data-micromodal-close>
<div class="wp-block-navigation__responsive-dialog" aria-label="%8$s">
<button aria-label="%4$s" data-micromodal-close class="wp-block-navigation__responsive-container-close"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" role="img" aria-hidden="true" focusable="false"><path d="M13 11.8l6.1-6.3-1-1-6.1 6.2-6.1-6.2-1 1 6.1 6.3-6.5 6.7 1 1 6.5-6.6 6.5 6.6 1-1z"></path></svg></button>
<div class="wp-block-navigation__responsive-container-content" id="modal-%1$s-content">
<div class="wp-block-navigation__responsive-container-content" id="%1$s-content">
%2$s
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion phpunit/class-elements-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Gutenberg_Elements_Test extends WP_UnitTestCase {
* @return string String where the unique id classes were replaced with "wp-elements-1".
*/
private static function make_unique_id_one( $string ) {
return preg_replace( '/wp-elements-.{13}/', 'wp-elements-1', $string );
return preg_replace( '/wp-elements-\d+/', 'wp-elements-1', $string );
}

/**
Expand Down

0 comments on commit 198f97c

Please sign in to comment.