From 1ff920f9a691cd3dd67575b16f6215069678bce0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petter=20Walb=C3=B8=20Johnsg=C3=A5rd?= Date: Thu, 11 Nov 2021 15:20:37 +0100 Subject: [PATCH] Add `_wp_array_set` and `_wp_to_kebab_case` to 5.8 compat (#36399) Replace `gutenberg_experimental_set` with `_wp_array_set` and `gutenberg_experimental_to_kebab_case` with `_wp_to_kebab_case`. These functions was added in WordPress 5.8. This fixes a issue with the page list block where `gutenberg_experimental_to_kebab_case` is used. WordPress doesn't have this function and the block will fail when used. --- lib/block-supports/colors.php | 6 +- lib/block-supports/typography.php | 6 +- lib/blocks.php | 2 +- lib/class-wp-theme-json-gutenberg.php | 16 +- lib/class-wp-theme-json-schema-gutenberg.php | 14 +- lib/compat/wordpress-5.8/utils.php | 156 ++++++++++++++++++ lib/load.php | 2 +- lib/utils.php | 139 ---------------- .../block-library/src/page-list/index.php | 8 +- phpunit/class-gutenberg-utils-test.php | 152 ----------------- 10 files changed, 183 insertions(+), 318 deletions(-) create mode 100644 lib/compat/wordpress-5.8/utils.php delete mode 100644 lib/utils.php delete mode 100644 phpunit/class-gutenberg-utils-test.php diff --git a/lib/block-supports/colors.php b/lib/block-supports/colors.php index 8e914e42aff2bd..bd278f9b4f651b 100644 --- a/lib/block-supports/colors.php +++ b/lib/block-supports/colors.php @@ -92,7 +92,7 @@ function gutenberg_apply_colors_support( $block_type, $block_attributes ) { } // Apply color class or inline style. if ( $has_named_text_color ) { - $classes[] = sprintf( 'has-%s-color', gutenberg_experimental_to_kebab_case( $block_attributes['textColor'] ) ); + $classes[] = sprintf( 'has-%s-color', _wp_to_kebab_case( $block_attributes['textColor'] ) ); } elseif ( $has_custom_text_color ) { $styles[] = sprintf( 'color: %s;', $block_attributes['style']['color']['text'] ); } @@ -109,7 +109,7 @@ function gutenberg_apply_colors_support( $block_type, $block_attributes ) { } // Apply background color classes or styles. if ( $has_named_background_color ) { - $classes[] = sprintf( 'has-%s-background-color', gutenberg_experimental_to_kebab_case( $block_attributes['backgroundColor'] ) ); + $classes[] = sprintf( 'has-%s-background-color', _wp_to_kebab_case( $block_attributes['backgroundColor'] ) ); } elseif ( $has_custom_background_color ) { $styles[] = sprintf( 'background-color: %s;', $block_attributes['style']['color']['background'] ); } @@ -125,7 +125,7 @@ function gutenberg_apply_colors_support( $block_type, $block_attributes ) { } // Apply required background class. if ( $has_named_gradient ) { - $classes[] = sprintf( 'has-%s-gradient-background', gutenberg_experimental_to_kebab_case( $block_attributes['gradient'] ) ); + $classes[] = sprintf( 'has-%s-gradient-background', _wp_to_kebab_case( $block_attributes['gradient'] ) ); } elseif ( $has_custom_gradient ) { $styles[] = sprintf( 'background: %s;', $block_attributes['style']['color']['gradient'] ); } diff --git a/lib/block-supports/typography.php b/lib/block-supports/typography.php index 5b60c96a6b2da1..ffa515cd8bae42 100644 --- a/lib/block-supports/typography.php +++ b/lib/block-supports/typography.php @@ -98,7 +98,7 @@ function gutenberg_apply_typography_support( $block_type, $block_attributes ) { $has_custom_font_size = isset( $block_attributes['style']['typography']['fontSize'] ); if ( $has_named_font_size ) { - $classes[] = sprintf( 'has-%s-font-size', gutenberg_experimental_to_kebab_case( $block_attributes['fontSize'] ) ); + $classes[] = sprintf( 'has-%s-font-size', _wp_to_kebab_case( $block_attributes['fontSize'] ) ); } elseif ( $has_custom_font_size ) { $styles[] = sprintf( 'font-size: %s;', $block_attributes['style']['typography']['fontSize'] ); } @@ -109,14 +109,14 @@ function gutenberg_apply_typography_support( $block_type, $block_attributes ) { $has_custom_font_family = isset( $block_attributes['style']['typography']['fontFamily'] ); if ( $has_named_font_family ) { - $classes[] = sprintf( 'has-%s-font-family', gutenberg_experimental_to_kebab_case( $block_attributes['fontFamily'] ) ); + $classes[] = sprintf( 'has-%s-font-family', _wp_to_kebab_case( $block_attributes['fontFamily'] ) ); } elseif ( $has_custom_font_family ) { // Before using classes, the value was serialized as a CSS Custom Property. // We don't need this code path when it lands in core. $font_family_custom = $block_attributes['style']['typography']['fontFamily']; if ( strpos( $font_family_custom, 'var:preset|font-family' ) !== false ) { $index_to_splice = strrpos( $font_family_custom, '|' ) + 1; - $font_family_slug = gutenberg_experimental_to_kebab_case( substr( $font_family_custom, $index_to_splice ) ); + $font_family_slug = _wp_to_kebab_case( substr( $font_family_custom, $index_to_splice ) ); $font_family_custom = sprintf( 'var(--wp--preset--font-family--%s)', $font_family_slug ); } $styles[] = sprintf( 'font-family: %s;', $font_family_custom ); diff --git a/lib/blocks.php b/lib/blocks.php index 9aa111dfff61a4..4fa8b5027b0a2b 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -557,7 +557,7 @@ function gutenberg_migrate_old_typography_shape( $metadata ) { sprintf( __( 'Block %1$s is declaring %2$s support on block.json under supports.%2$s. %2$s support is now declared under supports.typography.%2$s.', 'gutenberg' ), $metadata['name'], $typography_key ), headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE ); - gutenberg_experimental_set( $metadata['supports'], array( 'typography', $typography_key ), $support_for_key ); + _wp_array_set( $metadata['supports'], array( 'typography', $typography_key ), $support_for_key ); unset( $metadata['supports'][ $typography_key ] ); } } diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index cc32441d708a30..ccd151dd330c94 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -301,7 +301,7 @@ public function __construct( $theme_json = array(), $origin = 'theme' ) { $path = array_merge( $node['path'], $preset_metadata['path'] ); $preset = _wp_array_get( $this->theme_json, $path, null ); if ( null !== $preset ) { - gutenberg_experimental_set( $this->theme_json, $path, array( $origin => $preset ) ); + _wp_array_set( $this->theme_json, $path, array( $origin => $preset ) ); } } } @@ -706,7 +706,7 @@ private static function get_settings_values_by_slug( $settings, $preset_metadata continue; } foreach ( $preset_per_origin[ $origin ] as $preset ) { - $slug = gutenberg_experimental_to_kebab_case( $preset['slug'] ); + $slug = _wp_to_kebab_case( $preset['slug'] ); $value = ''; if ( isset( $preset_metadata['value_key'] ) ) { @@ -747,7 +747,7 @@ private static function get_settings_slugs( $settings, $preset_metadata, $origin continue; } foreach ( $preset_per_origin[ $origin ] as $preset ) { - $slug = gutenberg_experimental_to_kebab_case( $preset['slug'] ); + $slug = _wp_to_kebab_case( $preset['slug'] ); // Use the array as a set so we don't get duplicates. $result[ $slug ] = $slug; @@ -1317,7 +1317,7 @@ public function merge( $incoming ) { $path = array_merge( $metadata['path'], $property_path ); $node = _wp_array_get( $incoming_data, $path, null ); if ( isset( $node ) ) { - gutenberg_experimental_set( $this->theme_json, $path, $node ); + _wp_array_set( $this->theme_json, $path, $node ); } } } @@ -1371,7 +1371,7 @@ private static function remove_insecure_settings( $input ) { } if ( ! empty( $escaped_preset ) ) { - gutenberg_experimental_set( $output, $preset_metadata['path'], $escaped_preset ); + _wp_array_set( $output, $preset_metadata['path'], $escaped_preset ); } } @@ -1398,7 +1398,7 @@ private static function remove_insecure_styles( $input ) { // double up shorthand and longhand styles. $value = _wp_array_get( $input, $path, array() ); if ( ! is_array( $value ) ) { - gutenberg_experimental_set( $output, $path, $value ); + _wp_array_set( $output, $path, $value ); } } } @@ -1444,7 +1444,7 @@ public static function remove_insecure_properties( $theme_json ) { $output = self::remove_insecure_styles( $input ); if ( ! empty( $output ) ) { - gutenberg_experimental_set( $sanitized, $metadata['path'], $output ); + _wp_array_set( $sanitized, $metadata['path'], $output ); } } @@ -1457,7 +1457,7 @@ public static function remove_insecure_properties( $theme_json ) { $output = self::remove_insecure_settings( $input ); if ( ! empty( $output ) ) { - gutenberg_experimental_set( $sanitized, $metadata['path'], $output ); + _wp_array_set( $sanitized, $metadata['path'], $output ); } } diff --git a/lib/class-wp-theme-json-schema-gutenberg.php b/lib/class-wp-theme-json-schema-gutenberg.php index 74d3737589d93d..85648b1c01a9cc 100644 --- a/lib/class-wp-theme-json-schema-gutenberg.php +++ b/lib/class-wp-theme-json-schema-gutenberg.php @@ -219,7 +219,7 @@ private static function migrate_v0_to_v1_process_settings( $settings ) { null ); if ( null !== $root_value ) { - gutenberg_experimental_set( $new, $path, $root_value ); + _wp_array_set( $new, $path, $root_value ); } } @@ -250,13 +250,13 @@ private static function migrate_v0_to_v1_process_settings( $settings ) { if ( null !== $block_settings ) { $new_path = array( 'blocks', $new_name ); $new_settings = array(); - gutenberg_experimental_set( $new_settings, $new_path, $block_settings ); + _wp_array_set( $new_settings, $new_path, $block_settings ); $new = array_replace_recursive( $new, $new_settings ); foreach ( $paths_to_override as $path ) { $block_value = _wp_array_get( $block_settings, $path, null ); if ( null !== $block_value ) { - gutenberg_experimental_set( $new, array_merge( $new_path, $path ), $block_value ); + _wp_array_set( $new, array_merge( $new_path, $path ), $block_value ); } } } @@ -343,10 +343,10 @@ private static function migrate_v0_to_v1_process_styles( $styles ) { continue; } - gutenberg_experimental_set( $new, array( 'elements', $new_name ), $new['blocks'][ $old_name ] ); + _wp_array_set( $new, array( 'elements', $new_name ), $new['blocks'][ $old_name ] ); if ( isset( $new['blocks'][ $old_name ]['elements'] ) ) { - gutenberg_experimental_set( $new, array( 'blocks', 'core/heading', 'elements' ), $new['blocks'][ $old_name ]['elements'] ); + _wp_array_set( $new, array( 'blocks', 'core/heading', 'elements' ), $new['blocks'][ $old_name ]['elements'] ); } unset( $new['blocks'][ $old_name ] ); @@ -362,7 +362,7 @@ private static function migrate_v0_to_v1_process_styles( $styles ) { continue; } - gutenberg_experimental_set( $new, array( 'blocks', $new_name ), $new['blocks'][ $old_name ] ); + _wp_array_set( $new, array( 'blocks', $new_name ), $new['blocks'][ $old_name ] ); unset( $new['blocks'][ $old_name ] ); } @@ -463,7 +463,7 @@ private static function rename_settings( &$settings, $paths_to_rename ) { $current_value = _wp_array_get( $settings, $original_path, null ); if ( null !== $current_value ) { - gutenberg_experimental_set( $settings, $renamed_path, $current_value ); + _wp_array_set( $settings, $renamed_path, $current_value ); self::unset_setting_by_path( $settings, $original_path ); } } diff --git a/lib/compat/wordpress-5.8/utils.php b/lib/compat/wordpress-5.8/utils.php new file mode 100644 index 00000000000000..9fa93b3819e154 --- /dev/null +++ b/lib/compat/wordpress-5.8/utils.php @@ -0,0 +1,156 @@ + array( + * 'b' => array( + * 'c' => 1, + * ), + * ), + * ); + * + * @internal + * + * @since 5.8.0 + * @access private + * + * @param array $array An array that we want to mutate to include a specific value in a path. + * @param array $path An array of keys describing the path that we want to mutate. + * @param mixed $value The value that will be set. + */ + function _wp_array_set( &$array, $path, $value = null ) { + // Confirm $array is valid. + if ( ! is_array( $array ) ) { + return; + } + + // Confirm $path is valid. + if ( ! is_array( $path ) ) { + return; + } + + $path_length = count( $path ); + + if ( 0 === $path_length ) { + return; + } + + foreach ( $path as $path_element ) { + if ( + ! is_string( $path_element ) && ! is_integer( $path_element ) && + ! is_null( $path_element ) + ) { + return; + } + } + + for ( $i = 0; $i < $path_length - 1; ++$i ) { + $path_element = $path[ $i ]; + if ( + ! array_key_exists( $path_element, $array ) || + ! is_array( $array[ $path_element ] ) + ) { + $array[ $path_element ] = array(); + } + $array = &$array[ $path_element ]; // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.VariableRedeclaration + } + + $array[ $path[ $i ] ] = $value; + } +} + +if ( ! function_exists( '_wp_to_kebab_case' ) ) { + /** + * This function is trying to replicate what + * lodash's kebabCase (JS library) does in the client. + * + * The reason we need this function is that we do some processing + * in both the client and the server (e.g.: we generate + * preset classes from preset slugs) that needs to + * create the same output. + * + * We can't remove or update the client's library due to backward compatibility + * (some of the output of lodash's kebabCase is saved in the post content). + * We have to make the server behave like the client. + * + * Changes to this function should follow updates in the client + * with the same logic. + * + * @link https://github.com/lodash/lodash/blob/4.17/dist/lodash.js#L14369 + * @link https://github.com/lodash/lodash/blob/4.17/dist/lodash.js#L278 + * @link https://github.com/lodash-php/lodash-php/blob/master/src/String/kebabCase.php + * @link https://github.com/lodash-php/lodash-php/blob/master/src/internal/unicodeWords.php + * + * @param string $string The string to kebab-case. + * + * @return string kebab-cased-string. + */ + function _wp_to_kebab_case( $string ) { + //phpcs:disable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase + // ignore the camelCase names for variables so the names are the same as lodash + // so comparing and porting new changes is easier. + + /* + * Some notable things we've removed compared to the lodash version are: + * + * - non-alphanumeric characters: rsAstralRange, rsEmoji, etc + * - the groups that processed the apostrophe, as it's removed before passing the string to preg_match: rsApos, rsOptContrLower, and rsOptContrUpper + * + */ + + /** Used to compose unicode character classes. */ + $rsLowerRange = 'a-z\\xdf-\\xf6\\xf8-\\xff'; + $rsNonCharRange = '\\x00-\\x2f\\x3a-\\x40\\x5b-\\x60\\x7b-\\xbf'; + $rsPunctuationRange = '\\x{2000}-\\x{206f}'; + $rsSpaceRange = ' \\t\\x0b\\f\\xa0\\x{feff}\\n\\r\\x{2028}\\x{2029}\\x{1680}\\x{180e}\\x{2000}\\x{2001}\\x{2002}\\x{2003}\\x{2004}\\x{2005}\\x{2006}\\x{2007}\\x{2008}\\x{2009}\\x{200a}\\x{202f}\\x{205f}\\x{3000}'; + $rsUpperRange = 'A-Z\\xc0-\\xd6\\xd8-\\xde'; + $rsBreakRange = $rsNonCharRange . $rsPunctuationRange . $rsSpaceRange; + + /** Used to compose unicode capture groups. */ + $rsBreak = '[' . $rsBreakRange . ']'; + $rsDigits = '\\d+'; // The last lodash version in GitHub uses a single digit here and expands it when in use. + $rsLower = '[' . $rsLowerRange . ']'; + $rsMisc = '[^' . $rsBreakRange . $rsDigits . $rsLowerRange . $rsUpperRange . ']'; + $rsUpper = '[' . $rsUpperRange . ']'; + + /** Used to compose unicode regexes. */ + $rsMiscLower = '(?:' . $rsLower . '|' . $rsMisc . ')'; + $rsMiscUpper = '(?:' . $rsUpper . '|' . $rsMisc . ')'; + $rsOrdLower = '\\d*(?:1st|2nd|3rd|(?![123])\\dth)(?=\\b|[A-Z_])'; + $rsOrdUpper = '\\d*(?:1ST|2ND|3RD|(?![123])\\dTH)(?=\\b|[a-z_])'; + + $regexp = '/' . implode( + '|', + array( + $rsUpper . '?' . $rsLower . '+' . '(?=' . implode( '|', array( $rsBreak, $rsUpper, '$' ) ) . ')', + $rsMiscUpper . '+' . '(?=' . implode( '|', array( $rsBreak, $rsUpper . $rsMiscLower, '$' ) ) . ')', + $rsUpper . '?' . $rsMiscLower . '+', + $rsUpper . '+', + $rsOrdUpper, + $rsOrdLower, + $rsDigits, + ) + ) . '/u'; + + preg_match_all( $regexp, str_replace( "'", '', $string ), $matches ); + return strtolower( implode( '-', $matches[0] ) ); + //phpcs:enable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase + } +} diff --git a/lib/load.php b/lib/load.php index ce15c863c0fbcf..2f9d0a6093ba53 100644 --- a/lib/load.php +++ b/lib/load.php @@ -89,6 +89,7 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/compat.php'; require __DIR__ . '/compat/wordpress-5.8/index.php'; +require __DIR__ . '/compat/wordpress-5.8/utils.php'; require __DIR__ . '/compat/wordpress-5.8.1/index.php'; require __DIR__ . '/compat/wordpress-5.9/block-template-utils.php'; require __DIR__ . '/compat/wordpress-5.9/default-editor-styles.php'; @@ -96,7 +97,6 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/compat/wordpress-5.9/json-file-decode.php'; require __DIR__ . '/compat/wordpress-5.9/translate-settings-using-i18n-schema.php'; require __DIR__ . '/compat/wordpress-5.9/edit-site-export.php'; -require __DIR__ . '/utils.php'; require __DIR__ . '/editor-settings.php'; if ( ! class_exists( 'WP_Block_Template' ) ) { diff --git a/lib/utils.php b/lib/utils.php deleted file mode 100644 index 12ee435e88f5b0..00000000000000 --- a/lib/utils.php +++ /dev/null @@ -1,139 +0,0 @@ - array( - * 'b' => array( - * 'c' => 1, - * ), - * ), - * ); - * - * @param array $array An array that we want to mutate to include a specific value in a path. - * @param array $path An array of keys describing the path that we want to mutate. - * @param mixed $value The value that will be set. - */ -function gutenberg_experimental_set( &$array, $path, $value = null ) { - // Confirm $array is valid. - if ( ! is_array( $array ) ) { - return; - } - - // Confirm $path is valid. - if ( ! is_array( $path ) ) { - return; - } - $path_length = count( $path ); - if ( 0 === $path_length ) { - return; - } - foreach ( $path as $path_element ) { - if ( - ! is_string( $path_element ) && ! is_integer( $path_element ) && - ! is_null( $path_element ) - ) { - return; - } - } - - for ( $i = 0; $i < $path_length - 1; ++$i ) { - $path_element = $path[ $i ]; - if ( - ! array_key_exists( $path_element, $array ) || - ! is_array( $array[ $path_element ] ) - ) { - $array[ $path_element ] = array(); - } - $array = &$array[ $path_element ]; // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.VariableRedeclaration - } - $array[ $path[ $i ] ] = $value; -} - -/** - * This function is trying to replicate what - * lodash's kebabCase (JS library) does in the client. - * - * The reason we need this function is that we do some processing - * in both the client and the server (e.g.: we generate - * preset classes from preset slugs) that needs to - * create the same output. - * - * We can't remove or update the client's library due to backward compatibility - * (some of the output of lodash's kebabCaseare saved in the post content). - * We have to make the server behave like the client. - * - * @link https://github.com/lodash/lodash/blob/4.17/dist/lodash.js#L14369 - * @link https://github.com/lodash/lodash/blob/4.17/dist/lodash.js#L278 - * @link https://github.com/lodash-php/lodash-php/blob/master/src/String/kebabCase.php - * @link https://github.com/lodash-php/lodash-php/blob/master/src/internal/unicodeWords.php - * - * @param string $string The string to kebab-case. - * - * @return string kebab-cased-string. - */ -function gutenberg_experimental_to_kebab_case( $string ) { - //phpcs:disable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase - // ignore the camelCase names for variables so the names are the same as lodash - // so comparing and porting new changes is easier. - - /* - * Some notable things we've removed compared to the lodash version are: - * - * - non-alphanumeric characters: rsAstralRange, rsEmoji, etc - * - the groups that processed the apostrophe, as it's removed before passing the string to preg_match: rsApos, rsOptContrLower, and rsOptContrUpper - * - */ - - /** Used to compose unicode character classes. */ - $rsLowerRange = 'a-z\\xdf-\\xf6\\xf8-\\xff'; - $rsNonCharRange = '\\x00-\\x2f\\x3a-\\x40\\x5b-\\x60\\x7b-\\xbf'; - $rsPunctuationRange = '\\x{2000}-\\x{206f}'; - $rsSpaceRange = ' \\t\\x0b\\f\\xa0\\x{feff}\\n\\r\\x{2028}\\x{2029}\\x{1680}\\x{180e}\\x{2000}\\x{2001}\\x{2002}\\x{2003}\\x{2004}\\x{2005}\\x{2006}\\x{2007}\\x{2008}\\x{2009}\\x{200a}\\x{202f}\\x{205f}\\x{3000}'; - $rsUpperRange = 'A-Z\\xc0-\\xd6\\xd8-\\xde'; - $rsBreakRange = $rsNonCharRange . $rsPunctuationRange . $rsSpaceRange; - - /** Used to compose unicode capture groups. */ - $rsBreak = '[' . $rsBreakRange . ']'; - $rsDigits = '\\d+'; // The last lodash version in GitHub uses a single digit here and expands it when in use. - $rsLower = '[' . $rsLowerRange . ']'; - $rsMisc = '[^' . $rsBreakRange . $rsDigits . $rsLowerRange . $rsUpperRange . ']'; - $rsUpper = '[' . $rsUpperRange . ']'; - - /** Used to compose unicode regexes. */ - $rsMiscLower = '(?:' . $rsLower . '|' . $rsMisc . ')'; - $rsMiscUpper = '(?:' . $rsUpper . '|' . $rsMisc . ')'; - $rsOrdLower = '\\d*(?:1st|2nd|3rd|(?![123])\\dth)(?=\\b|[A-Z_])'; - $rsOrdUpper = '\\d*(?:1ST|2ND|3RD|(?![123])\\dTH)(?=\\b|[a-z_])'; - - $regexp = '/' . implode( - '|', - array( - $rsUpper . '?' . $rsLower . '+' . '(?=' . implode( '|', array( $rsBreak, $rsUpper, '$' ) ) . ')', - $rsMiscUpper . '+' . '(?=' . implode( '|', array( $rsBreak, $rsUpper . $rsMiscLower, '$' ) ) . ')', - $rsUpper . '?' . $rsMiscLower . '+', - $rsUpper . '+', - $rsOrdUpper, - $rsOrdLower, - $rsDigits, - ) - ) . '/u'; - - preg_match_all( $regexp, str_replace( "'", '', $string ), $matches ); - return strtolower( implode( '-', $matches[0] ) ); - //phpcs:enable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase -} diff --git a/packages/block-library/src/page-list/index.php b/packages/block-library/src/page-list/index.php index 4f91363cabb1fa..53dec1f1957e44 100644 --- a/packages/block-library/src/page-list/index.php +++ b/packages/block-library/src/page-list/index.php @@ -34,7 +34,7 @@ function block_core_page_list_build_css_colors( $attributes, $context ) { if ( $has_named_text_color ) { // Add the color class. - $colors['css_classes'][] = sprintf( 'has-%s-color', gutenberg_experimental_to_kebab_case( $context['textColor'] ) ); + $colors['css_classes'][] = sprintf( 'has-%s-color', _wp_to_kebab_case( $context['textColor'] ) ); } elseif ( $has_picked_text_color ) { $colors['inline_styles'] .= sprintf( 'color: %s;', $context['customTextColor'] ); } elseif ( $has_custom_text_color ) { @@ -55,7 +55,7 @@ function block_core_page_list_build_css_colors( $attributes, $context ) { if ( $has_named_background_color ) { // Add the background-color class. - $colors['css_classes'][] = sprintf( 'has-%s-background-color', gutenberg_experimental_to_kebab_case( $context['backgroundColor'] ) ); + $colors['css_classes'][] = sprintf( 'has-%s-background-color', _wp_to_kebab_case( $context['backgroundColor'] ) ); } elseif ( $has_picked_background_color ) { $colors['inline_styles'] .= sprintf( 'background-color: %s;', $context['customBackgroundColor'] ); } elseif ( $has_custom_background_color ) { @@ -74,7 +74,7 @@ function block_core_page_list_build_css_colors( $attributes, $context ) { // Give overlay colors priority, fall back to Navigation block colors, then global styles. if ( $has_named_overlay_text_color ) { - $colors['overlay_css_classes'][] = sprintf( 'has-%s-color', gutenberg_experimental_to_kebab_case( $context['overlayTextColor'] ) ); + $colors['overlay_css_classes'][] = sprintf( 'has-%s-color', _wp_to_kebab_case( $context['overlayTextColor'] ) ); } elseif ( $has_picked_overlay_text_color ) { $colors['overlay_inline_styles'] .= sprintf( 'color: %s;', $context['customOverlayTextColor'] ); } @@ -89,7 +89,7 @@ function block_core_page_list_build_css_colors( $attributes, $context ) { } if ( $has_named_overlay_background_color ) { - $colors['overlay_css_classes'][] = sprintf( 'has-%s-background-color', gutenberg_experimental_to_kebab_case( $context['overlayBackgroundColor'] ) ); + $colors['overlay_css_classes'][] = sprintf( 'has-%s-background-color', _wp_to_kebab_case( $context['overlayBackgroundColor'] ) ); } elseif ( $has_picked_overlay_background_color ) { $colors['overlay_inline_styles'] .= sprintf( 'background-color: %s;', $context['customOverlayBackgroundColor'] ); } diff --git a/phpunit/class-gutenberg-utils-test.php b/phpunit/class-gutenberg-utils-test.php deleted file mode 100644 index ccfc252a38e8c9..00000000000000 --- a/phpunit/class-gutenberg-utils-test.php +++ /dev/null @@ -1,152 +0,0 @@ -assertSame( - $test_array, - array( 'a' => 1 ) - ); - - $test_array = array( 'a' => 2 ); - gutenberg_experimental_set( $test_array, array( 'a' ), 3 ); - $this->assertSame( - $test_array, - array( 'a' => 3 ) - ); - - $test_array = array( 'b' => 1 ); - gutenberg_experimental_set( $test_array, array( 'a' ), 3 ); - $this->assertSame( - $test_array, - array( - 'b' => 1, - 'a' => 3, - ) - ); - } - - /** - * Test gutenberg_experimental_set() with subtree paths. - */ - public function test_subtree_set() { - $test_array = array(); - gutenberg_experimental_set( $test_array, array( 'a', 'b', 'c' ), 1 ); - $this->assertSame( - $test_array, - array( 'a' => array( 'b' => array( 'c' => 1 ) ) ) - ); - - $test_array = array( 'b' => 3 ); - gutenberg_experimental_set( $test_array, array( 'a', 'b', 'c' ), 1 ); - $this->assertSame( - $test_array, - array( - 'b' => 3, - 'a' => array( 'b' => array( 'c' => 1 ) ), - ) - ); - - $test_array = array( - 'b' => 3, - 'a' => 1, - ); - gutenberg_experimental_set( $test_array, array( 'a', 'b', 'c' ), 1 ); - $this->assertSame( - $test_array, - array( - 'b' => 3, - 'a' => array( 'b' => array( 'c' => 1 ) ), - ) - ); - - $test_array = array( - 'b' => 3, - 'a' => array(), - ); - gutenberg_experimental_set( $test_array, array( 'a', 'b', 'c' ), 1 ); - $this->assertSame( - $test_array, - array( - 'b' => 3, - 'a' => array( 'b' => array( 'c' => 1 ) ), - ) - ); - } - - /** - * Test gutenberg_experimental_set() with invalid parameters. - */ - public function test_invalid_parameters_set() { - $test = 3; - gutenberg_experimental_set( $test, array( 'a' ), 1 ); - $this->assertSame( - $test, - 3 - ); - - $test_array = array( 'a' => 2 ); - gutenberg_experimental_set( $test_array, 'a', 3 ); - $this->assertSame( - $test_array, - array( 'a' => 2 ) - ); - - $test_array = array( 'a' => 2 ); - gutenberg_experimental_set( $test_array, null, 3 ); - $this->assertSame( - $test_array, - array( 'a' => 2 ) - ); - - $test_array = array( 'a' => 2 ); - gutenberg_experimental_set( $test_array, array(), 3 ); - $this->assertSame( - $test_array, - array( 'a' => 2 ) - ); - - $test_array = array( 'a' => 2 ); - gutenberg_experimental_set( $test_array, array( 'a', array() ), 3 ); - $this->assertSame( - $test_array, - array( 'a' => 2 ) - ); - } - - public function test_gutenberg_experimental_to_kebab_case() { - $this->assertEquals( 'white', gutenberg_experimental_to_kebab_case( 'white' ) ); - $this->assertEquals( 'white-black', gutenberg_experimental_to_kebab_case( 'white+black' ) ); - $this->assertEquals( 'white-black', gutenberg_experimental_to_kebab_case( 'white:black' ) ); - $this->assertEquals( 'white-black', gutenberg_experimental_to_kebab_case( 'white*black' ) ); - $this->assertEquals( 'white-black', gutenberg_experimental_to_kebab_case( 'white.black' ) ); - $this->assertEquals( 'white-black', gutenberg_experimental_to_kebab_case( 'white black' ) ); - $this->assertEquals( 'white-black', gutenberg_experimental_to_kebab_case( 'white black' ) ); - $this->assertEquals( 'white-to-black', gutenberg_experimental_to_kebab_case( 'white-to-black' ) ); - $this->assertEquals( 'white-2-white', gutenberg_experimental_to_kebab_case( 'white2white' ) ); - $this->assertEquals( 'white-2nd', gutenberg_experimental_to_kebab_case( 'white2nd' ) ); - $this->assertEquals( 'white-2-ndcolor', gutenberg_experimental_to_kebab_case( 'white2ndcolor' ) ); - $this->assertEquals( 'white-2nd-color', gutenberg_experimental_to_kebab_case( 'white2ndColor' ) ); - $this->assertEquals( 'white-2nd-color', gutenberg_experimental_to_kebab_case( 'white2nd_color' ) ); - $this->assertEquals( 'white-23-color', gutenberg_experimental_to_kebab_case( 'white23color' ) ); - $this->assertEquals( 'white-23', gutenberg_experimental_to_kebab_case( 'white23' ) ); - $this->assertEquals( '23-color', gutenberg_experimental_to_kebab_case( '23color' ) ); - $this->assertEquals( 'white-4th', gutenberg_experimental_to_kebab_case( 'white4th' ) ); - $this->assertEquals( 'font-2-xl', gutenberg_experimental_to_kebab_case( 'font2xl' ) ); - $this->assertEquals( 'white-to-white', gutenberg_experimental_to_kebab_case( 'whiteToWhite' ) ); - $this->assertEquals( 'white-t-owhite', gutenberg_experimental_to_kebab_case( 'whiteTOwhite' ) ); - $this->assertEquals( 'whit-eto-white', gutenberg_experimental_to_kebab_case( 'WHITEtoWHITE' ) ); - $this->assertEquals( '42', gutenberg_experimental_to_kebab_case( 42 ) ); - $this->assertEquals( 'ive-done', gutenberg_experimental_to_kebab_case( "i've done" ) ); - $this->assertEquals( 'ffffff', gutenberg_experimental_to_kebab_case( '#ffffff' ) ); - $this->assertEquals( 'ffffff', gutenberg_experimental_to_kebab_case( '$ffffff' ) ); - } -}