Make WordPress Core


Ignore:
Timestamp:
03/02/2024 01:39:43 PM (2 years ago)
Author:
swissspidy
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-theme-json.php

    r57717 r57751  
    10661066                        }
    10671067
    1068                         // Check if the value is an array and requires further processing.
    1069                         if ( is_array( $value ) && is_array( $schema[ $key ] ) ) {
    1070                                 // Determine if it is an associative or indexed array.
    1071                                 $schema_is_assoc = self::is_assoc( $value );
    1072 
    1073                                 if ( $schema_is_assoc ) {
    1074                                         // If associative, process as a single object.
    1075                                         $tree[ $key ] = self::remove_keys_not_in_schema( $value, $schema[ $key ] );
    1076 
    1077                                         if ( empty( $tree[ $key ] ) ) {
    1078                                                 unset( $tree[ $key ] );
    1079                                         }
    1080                                 } else {
     1068                        if ( is_array( $schema[ $key ] ) ) {
     1069                                if ( ! is_array( $value ) ) {
     1070                                        unset( $tree[ $key ] );
     1071                                } elseif ( wp_is_numeric_array( $value ) ) {
    10811072                                        // If indexed, process each item in the array.
    10821073                                        foreach ( $value as $item_key => $item_value ) {
     
    10881079                                                }
    10891080                                        }
    1090                                 }
    1091                         } elseif ( is_array( $schema[ $key ] ) && ! is_array( $tree[ $key ] ) ) {
    1092                                 unset( $tree[ $key ] );
    1093                         }
    1094                 }
    1095 
     1081                                } else {
     1082                                        // If associative, process as a single object.
     1083                                        $tree[ $key ] = self::remove_keys_not_in_schema( $value, $schema[ $key ] );
     1084
     1085                                        if ( empty( $tree[ $key ] ) ) {
     1086                                                unset( $tree[ $key ] );
     1087                                        }
     1088                                }
     1089                        }
     1090                }
    10961091                return $tree;
    1097         }
    1098 
    1099         /**
    1100          * Checks if the given array is associative.
    1101          *
    1102          * @since 6.5.0
    1103          * @param array $data The array to check.
    1104          * @return bool True if the array is associative, false otherwise.
    1105          */
    1106         protected static function is_assoc( $data ) {
    1107                 if ( array() === $data ) {
    1108                         return false;
    1109                 }
    1110                 return array_keys( $data ) !== range( 0, count( $data ) - 1 );
    11111092        }
    11121093
Note: See TracChangeset for help on using the changeset viewer.