| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\Mcp\Abilities\Appliers\V3; |
| 4 |
|
| 5 |
use Elementor\Modules\Mcp\Abilities\Utils\V3_Json_Schema_Builder; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; |
| 9 |
} |
| 10 |
|
| 11 |
/** |
| 12 |
* Shallow shape guard for the primitive remainder of a V3 element_config entry. |
| 13 |
* |
| 14 |
* Called after `V3_Non_Style_Allowlist` (key gate) and `V3_Dynamic_Hoister` |
| 15 |
* (splits dynamic shortcodes from primitives). Delegates to |
| 16 |
* `V3_Json_Schema_Builder::check_settings_shape()` — enforces only `type`, |
| 17 |
* `enum`, and one-level nested `properties.type` so the applier never merges |
| 18 |
* an array into a scalar field. |
| 19 |
*/ |
| 20 |
class V3_Settings_Validator { |
| 21 |
|
| 22 |
/** |
| 23 |
* @param string $widget_type V3 widget type (e.g. `theme-post-title`). |
| 24 |
* @param array<string, mixed> $primitives Primitive settings (after hoisting removed __dynamic__ entries). |
| 25 |
* @param array<string, mixed> $widget_config Widget config from `Widget_Type_Resolver::resolve_type_config()`. |
| 26 |
* |
| 27 |
* @return array{ |
| 28 |
* valid: array<string, mixed>, |
| 29 |
* error: \WP_Error|null, |
| 30 |
* } |
| 31 |
*/ |
| 32 |
public static function validate_shape( string $widget_type, array $primitives, array $widget_config ): array { |
| 33 |
$controls = is_array( $widget_config['controls'] ?? null ) ? $widget_config['controls'] : []; |
| 34 |
$schema = V3_Json_Schema_Builder::build( $controls, array_keys( $primitives ) ); |
| 35 |
$shape = V3_Json_Schema_Builder::check_settings_shape( $primitives, $schema ); |
| 36 |
|
| 37 |
if ( empty( $shape['errors'] ) ) { |
| 38 |
return [ |
| 39 |
'valid' => $shape['valid'], |
| 40 |
'error' => null, |
| 41 |
]; |
| 42 |
} |
| 43 |
|
| 44 |
$messages = []; |
| 45 |
foreach ( $shape['errors'] as $key => $reason ) { |
| 46 |
$messages[] = sprintf( 'V3 widget "%s" property "%s": %s', $widget_type, $key, $reason ); |
| 47 |
} |
| 48 |
|
| 49 |
return [ |
| 50 |
'valid' => $shape['valid'], |
| 51 |
'error' => new \WP_Error( |
| 52 |
'elementor_invalid_settings', |
| 53 |
implode( '; ', $messages ), |
| 54 |
[ 'status' => \WP_Http::BAD_REQUEST ] |
| 55 |
), |
| 56 |
]; |
| 57 |
} |
| 58 |
} |
| 59 |
|