| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\Mcp\Abilities\Utils; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
/** |
| 10 |
* Finds keys of a plain input value that did not survive plain → envelope resolution, |
| 11 |
* either because they are not fields of the prop or because their value could not be resolved. |
| 12 |
* Compares against the resolver output instead of the schema so unions, custom resolvers |
| 13 |
* and open slots are judged by what was actually stored. |
| 14 |
*/ |
| 15 |
class Dropped_Plain_Keys_Finder { |
| 16 |
|
| 17 |
/** |
| 18 |
* @return string[] Dotted paths relative to $path. |
| 19 |
*/ |
| 20 |
public static function find( $plain_value, $resolved_value, string $path ): array { |
| 21 |
$plain_fields = self::unwrap( $plain_value ); |
| 22 |
$resolved_fields = self::unwrap( $resolved_value ); |
| 23 |
|
| 24 |
if ( ! self::is_object_map( $plain_fields ) || ! is_array( $resolved_fields ) || ! self::is_envelope( $resolved_value ) ) { |
| 25 |
return []; |
| 26 |
} |
| 27 |
|
| 28 |
$dropped = []; |
| 29 |
|
| 30 |
foreach ( $plain_fields as $key => $sub_value ) { |
| 31 |
if ( null === $sub_value ) { |
| 32 |
continue; |
| 33 |
} |
| 34 |
|
| 35 |
$sub_path = $path . '.' . $key; |
| 36 |
|
| 37 |
if ( ! array_key_exists( $key, $resolved_fields ) ) { |
| 38 |
$dropped[] = $sub_path; |
| 39 |
continue; |
| 40 |
} |
| 41 |
|
| 42 |
$dropped = array_merge( $dropped, self::find( $sub_value, $resolved_fields[ $key ], $sub_path ) ); |
| 43 |
} |
| 44 |
|
| 45 |
return $dropped; |
| 46 |
} |
| 47 |
|
| 48 |
private static function unwrap( $value ) { |
| 49 |
return self::is_envelope( $value ) ? $value['value'] : $value; |
| 50 |
} |
| 51 |
|
| 52 |
private static function is_envelope( $value ): bool { |
| 53 |
return is_array( $value ) && isset( $value['$$type'] ) && array_key_exists( 'value', $value ); |
| 54 |
} |
| 55 |
|
| 56 |
private static function is_object_map( $value ): bool { |
| 57 |
return is_array( $value ) && ! empty( $value ) && ! wp_is_numeric_array( $value ); |
| 58 |
} |
| 59 |
} |
| 60 |
|