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

Update/simplify case transforms #28171

Merged
merged 7 commits into from
Jan 14, 2021
Merged
Changes from 5 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
53 changes: 34 additions & 19 deletions lib/class-wp-theme-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,43 +346,60 @@ public function __construct( $contexts = array(), $should_escape_styles = false
}
}

/**
* Returns the kebab-cased name of a given property.
*
* @param string $property Property name to convert.
* @return string kebab-cased name of the property
*/
private static function to_kebab_case( $property ) {
$mappings = get_case_mappings();
return $mappings[ 'to_kebab_case' ][ $property ];
}

/**
* Returns the property name of a kebab-cased property.
*
* @param string $property Property name to convert in kebab-case.
* @return string Name of the property
*/
private static function to_property( $property ) {
$mappings = get_case_mappings();
return $mappings[ 'to_property' ][ $property ];
}

/**
* Returns a mapping on metadata properties to avoid having to constantly
* transforms properties between camel case and kebab.
*
* @return array Containing three mappings
* "to_kebab_case" mapping properties in camel case to
* properties in kebab case e.g: "paddingTop" to "padding-top".
* "to_camel_case" mapping properties in kebab case to
* properties in camel case e.g: "padding-top" to "paddingTop".
* "to_property" mapping properties in kebab case to
* the main properties in camel case e.g: "padding-top" to "padding".
*/
private static function get_properties_metadata_case_mappings() {
static $properties_metadata_case_mappings;
if ( null === $properties_metadata_case_mappings ) {
$properties_metadata_case_mappings = array(
private static function get_case_mappings() {
static $case_mappings;
if ( null === $case_mappings ) {
$case_mappings = array(
'to_kebab_case' => array(),
'to_camel_case' => array(),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We weren't using the to_camel_case anywhere, so I've deleted it.

'to_property' => array(),
);
foreach ( self::PROPERTIES_METADATA as $key => $metadata ) {
$kebab_case = strtolower( preg_replace( '/(?<!^)[A-Z]/', '-$0', $key ) );
$properties_metadata_case_mappings['to_kebab_case'][ $key ] = $kebab_case;
$properties_metadata_case_mappings['to_camel_case'][ $kebab_case ] = $key;
$properties_metadata_case_mappings['to_property'][ $kebab_case ] = $key;
$case_mappings['to_kebab_case'][ $key ] = $kebab_case;
$case_mappings['to_property'][ $kebab_case ] = $key;
if ( self::has_properties( $metadata ) ) {
foreach ( $metadata['properties'] as $property ) {
$camel_case = $key . ucfirst( $property );
$kebab_case = strtolower( preg_replace( '/(?<!^)[A-Z]/', '-$0', $camel_case ) );
$properties_metadata_case_mappings['to_kebab_case'][ $camel_case ] = $kebab_case;
$properties_metadata_case_mappings['to_camel_case'][ $kebab_case ] = $camel_case;
$properties_metadata_case_mappings['to_property'][ $kebab_case ] = $key;
$case_mappings['to_kebab_case'][ $camel_case ] = $kebab_case;
$case_mappings['to_property'][ $kebab_case ] = $key;
}
}
}
}
return $properties_metadata_case_mappings;
return $case_mappings;
}

/**
Expand Down Expand Up @@ -708,8 +725,7 @@ private static function compute_style_properties( &$declarations, $context, $con
if ( empty( $context['styles'] ) ) {
return;
}
$metadata_mappings = self::get_properties_metadata_case_mappings();
$properties = array();
$properties = array();
foreach ( self::PROPERTIES_METADATA as $name => $metadata ) {
if ( ! in_array( $name, $context_supports, true ) ) {
continue;
Expand All @@ -735,7 +751,7 @@ private static function compute_style_properties( &$declarations, $context, $con
foreach ( $properties as $prop ) {
$value = self::get_property_value( $context['styles'], $prop['value'] );
if ( ! empty( $value ) ) {
$kebab_cased_name = $metadata_mappings['to_kebab_case'][ $prop['name'] ];
$kebab_cased_name = self::to_kebab_case( $prop['name'] );
$declarations[] = array(
'name' => $kebab_cased_name,
'value' => $value,
Expand Down Expand Up @@ -1059,7 +1075,6 @@ public function merge( $theme_json ) {
*/
public function remove_insecure_properties() {
$blocks_metadata = self::get_blocks_metadata();
$metadata_mappings = self::get_properties_metadata_case_mappings();
foreach ( $this->contexts as $context_name => &$context ) {
// Escape the context key.
if ( empty( $blocks_metadata[ $context_name ] ) ) {
Expand All @@ -1081,7 +1096,7 @@ public function remove_insecure_properties() {
if ( null === $escaped_styles ) {
$escaped_styles = array();
}
$property = $metadata_mappings['to_property'][ $declaration['name'] ];
$property = self::to_property( $declaration['name'] );
$path = self::PROPERTIES_METADATA[ $property ]['value'];
if ( self::has_properties( self::PROPERTIES_METADATA[ $property ] ) ) {
$declaration_divided = explode( '-', $declaration['name'] );
Expand Down