From 2a334ede98e6822eb7dca04b96e30ad83ac2bc01 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Sat, 2 Mar 2024 13:41:05 +0000 Subject: [PATCH] Editor: Simplify sanitization code path in `WP_Theme_JSON` after [57496] Removes the custom `WP_Theme_JSON::is_assoc()` method again in favor of the existing `wp_is_numeric_array()` helper function. Props mmaattiiaass, costdev, swissspidy, spacedmonkey. Fixes #60360. Built from https://develop.svn.wordpress.org/trunk@57751 git-svn-id: http://core.svn.wordpress.org/trunk@57252 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/class-wp-theme-json.php | 41 ++++++++--------------------- wp-includes/version.php | 2 +- 2 files changed, 12 insertions(+), 31 deletions(-) diff --git a/wp-includes/class-wp-theme-json.php b/wp-includes/class-wp-theme-json.php index ba2a2dfa38..2a4303b5a3 100644 --- a/wp-includes/class-wp-theme-json.php +++ b/wp-includes/class-wp-theme-json.php @@ -1065,19 +1065,10 @@ class WP_Theme_JSON { continue; } - // Check if the value is an array and requires further processing. - if ( is_array( $value ) && is_array( $schema[ $key ] ) ) { - // Determine if it is an associative or indexed array. - $schema_is_assoc = self::is_assoc( $value ); - - if ( $schema_is_assoc ) { - // If associative, process as a single object. - $tree[ $key ] = self::remove_keys_not_in_schema( $value, $schema[ $key ] ); - - if ( empty( $tree[ $key ] ) ) { - unset( $tree[ $key ] ); - } - } else { + if ( is_array( $schema[ $key ] ) ) { + if ( ! is_array( $value ) ) { + unset( $tree[ $key ] ); + } elseif ( wp_is_numeric_array( $value ) ) { // If indexed, process each item in the array. foreach ( $value as $item_key => $item_value ) { if ( isset( $schema[ $key ][0] ) && is_array( $schema[ $key ][0] ) ) { @@ -1087,29 +1078,19 @@ class WP_Theme_JSON { $tree[ $key ][ $item_key ] = $item_value; } } + } else { + // If associative, process as a single object. + $tree[ $key ] = self::remove_keys_not_in_schema( $value, $schema[ $key ] ); + + if ( empty( $tree[ $key ] ) ) { + unset( $tree[ $key ] ); + } } - } elseif ( is_array( $schema[ $key ] ) && ! is_array( $tree[ $key ] ) ) { - unset( $tree[ $key ] ); } } - return $tree; } - /** - * Checks if the given array is associative. - * - * @since 6.5.0 - * @param array $data The array to check. - * @return bool True if the array is associative, false otherwise. - */ - protected static function is_assoc( $data ) { - if ( array() === $data ) { - return false; - } - return array_keys( $data ) !== range( 0, count( $data ) - 1 ); - } - /** * Returns the existing settings for each block. * diff --git a/wp-includes/version.php b/wp-includes/version.php index 71e059594d..cd74b468ea 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.5-beta3-57750'; +$wp_version = '6.5-beta3-57751'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.