| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\Mcp\Abilities\Appliers\V3; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
/** |
| 10 |
* Detects LLM-facing dynamic-tag input shapes on V3 widget controls. |
| 11 |
*/ |
| 12 |
class V3_Dynamic_Resolver { |
| 13 |
|
| 14 |
public static function is_dynamic_capable( array $control ): bool { |
| 15 |
$control_dynamic = is_array( $control['dynamic'] ?? null ) ? $control['dynamic'] : []; |
| 16 |
|
| 17 |
if ( true === ( $control_dynamic['active'] ?? false ) ) { |
| 18 |
return true; |
| 19 |
} |
| 20 |
|
| 21 |
return is_string( $control_dynamic['default'] ?? null ) && '' !== $control_dynamic['default']; |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* @return array{name: string, settings: array<string, mixed>}|null |
| 26 |
*/ |
| 27 |
public static function extract_input( $value, ?string $property ): ?array { |
| 28 |
if ( ! is_array( $value ) ) { |
| 29 |
return null; |
| 30 |
} |
| 31 |
|
| 32 |
$top_level = self::normalize_dynamic_input( $value ); |
| 33 |
if ( null !== $top_level ) { |
| 34 |
return $top_level; |
| 35 |
} |
| 36 |
|
| 37 |
if ( ! is_string( $property ) || '' === $property ) { |
| 38 |
return null; |
| 39 |
} |
| 40 |
|
| 41 |
$nested = $value[ $property ] ?? null; |
| 42 |
if ( ! is_array( $nested ) ) { |
| 43 |
return null; |
| 44 |
} |
| 45 |
|
| 46 |
return self::normalize_dynamic_input( $nested ); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* @param array<string, mixed> $value |
| 51 |
* |
| 52 |
* @return array<string, mixed> |
| 53 |
*/ |
| 54 |
public static function extract_primitive_remainder( array $value, ?string $property ): array { |
| 55 |
if ( ! is_string( $property ) || '' === $property || ! array_key_exists( $property, $value ) ) { |
| 56 |
return []; |
| 57 |
} |
| 58 |
|
| 59 |
$remainder = $value; |
| 60 |
unset( $remainder[ $property ] ); |
| 61 |
|
| 62 |
return $remainder; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* @return array{name: string, settings: array<string, mixed>}|null |
| 67 |
*/ |
| 68 |
private static function normalize_dynamic_input( array $candidate ): ?array { |
| 69 |
$name = $candidate['name'] ?? null; |
| 70 |
if ( ! is_string( $name ) || '' === $name ) { |
| 71 |
return null; |
| 72 |
} |
| 73 |
|
| 74 |
$settings = $candidate['settings'] ?? []; |
| 75 |
if ( ! is_array( $settings ) ) { |
| 76 |
$settings = []; |
| 77 |
} |
| 78 |
|
| 79 |
return [ |
| 80 |
'name' => $name, |
| 81 |
'settings' => $settings, |
| 82 |
]; |
| 83 |
} |
| 84 |
} |
| 85 |
|