Skip to content

Commit

Permalink
Simplify sanitize_variables and update usage
Browse files Browse the repository at this point in the history
Since the data is already checked against the schema
sanitize_variables doesn't need to know about the schema
anymore
  • Loading branch information
samnajian committed May 9, 2023
1 parent 1250ec9 commit 5833401
Showing 1 changed file with 7 additions and 31 deletions.
38 changes: 7 additions & 31 deletions lib/class-wp-theme-json-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ protected static function sanitize( $input, $valid_block_names, $valid_element_n
if ( empty( $result ) ) {
unset( $output[ $subtree ] );
} else {
$output[ $subtree ] = static::sanitize_variables( $result, $schema[ $subtree ] );
$output[ $subtree ] = static::sanitize_variables( $result );
}
}

Expand Down Expand Up @@ -3596,41 +3596,17 @@ private static function convert_custom_properties( $value ) {
*
* @since 6.3.0
* @param array $tree Input to process.
* @param array $schema Schema to adhere to.
* @return array The modified $tree.
*/
private static function sanitize_variables( $tree, $schema ) {
$tree = array_intersect_key( $tree, $schema );
private static function sanitize_variables( $tree ) {
$prefix = 'var:';
foreach ( $schema as $key => $data ) {
if ( ! isset( $tree[ $key ] ) ) {
continue;
}
$values = $tree[ $key ];
if ( is_array( $values ) ) {
foreach ( $values as $name => $value ) {
// if value is an array, do recursion.
if ( is_array( $value ) ) {
// make sure variations are included in the schema for recursion.
if ( isset( $value['variations'] ) ) {
$schema = $schema + array( 'variations' => null );
}
$values[ $name ] = array_merge( $value, self::sanitize_variables( $value, $schema ) );
continue;
}
if ( ! is_string( $value ) || 0 !== strpos( $value, $prefix ) ) {
continue;
}

$values[ $name ] = self::convert_custom_properties( $value );
}
} elseif ( ! is_string( $values ) || 0 !== strpos( $values, $prefix ) ) {
continue;
} else {
$values = self::convert_custom_properties( $values );
foreach ( $tree as $key => $data ) {
if ( is_string( $data ) && 0 === strpos( $data, $prefix ) ) {
$tree[ $key ] = self::convert_custom_properties( $data );
} elseif ( is_array( $data ) ) {
$tree[ $key ] = self::sanitize_variables( $data );
}

$tree[ $key ] = $values;
}

return $tree;
Expand Down

0 comments on commit 5833401

Please sign in to comment.