Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add _wp_array_set and _wp_to_kebab_case to 5.8 compat #36399

Merged
merged 1 commit into from
Nov 11, 2021
Merged
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
6 changes: 3 additions & 3 deletions lib/block-supports/colors.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] );
}
Expand All @@ -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'] );
}
Expand All @@ -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'] );
}
Expand Down
6 changes: 3 additions & 3 deletions lib/block-supports/typography.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] );
}
Expand All @@ -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 );
Expand Down
2 changes: 1 addition & 1 deletion lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ] );
}
}
Expand Down
16 changes: 8 additions & 8 deletions lib/class-wp-theme-json-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,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 ) );
}
}
}
Expand Down Expand Up @@ -704,7 +704,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'] ) ) {
Expand Down Expand Up @@ -745,7 +745,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;
Expand Down Expand Up @@ -1315,7 +1315,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 );
}
}
}
Expand Down Expand Up @@ -1369,7 +1369,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 );
}
}

Expand All @@ -1396,7 +1396,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 );
}
}
}
Expand Down Expand Up @@ -1442,7 +1442,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 );
}
}

Expand All @@ -1455,7 +1455,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 );
}
}

Expand Down
14 changes: 7 additions & 7 deletions lib/class-wp-theme-json-schema-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}

Expand Down Expand Up @@ -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 );
}
}
}
Expand Down Expand Up @@ -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 ] );
Expand All @@ -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 ] );

}
Expand Down Expand Up @@ -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 );
}
}
Expand Down
156 changes: 156 additions & 0 deletions lib/compat/wordpress-5.8/utils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php
/**
* General utilities.
*
* @package gutenberg
*/

if ( ! function_exists( '_wp_array_set' ) ) {
/**
* Sets an array in depth based on a path of keys.
*
* It is the PHP equivalent of JavaScript's `lodash.set()` and mirroring it may help other components
* retain some symmetry between client and server implementations.
*
* Example usage:
*
* $array = array();
* _wp_array_set( $array, array( 'a', 'b', 'c', 1 ) );
*
* $array becomes:
* array(
* 'a' => 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
}
}
2 changes: 1 addition & 1 deletion lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@ 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/blocks.php';
require __DIR__ . '/compat/wordpress-5.9/block-template-utils.php';
require __DIR__ . '/compat/wordpress-5.9/default-editor-styles.php';
require __DIR__ . '/compat/wordpress-5.9/get-global-styles-and-settings.php';
require __DIR__ . '/compat/wordpress-5.9/json-file-decode.php';
require __DIR__ . '/compat/wordpress-5.9/translate-settings-using-i18n-schema.php';
require __DIR__ . '/utils.php';
require __DIR__ . '/editor-settings.php';

if ( ! class_exists( 'WP_Block_Template' ) ) {
Expand Down
Loading