| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\Mcp\Abilities\Appliers; |
| 4 |
|
| 5 |
use Elementor\Modules\AtomicWidgets\Module as Atomic_Widgets_Module; |
| 6 |
use Elementor\Modules\AtomicWidgets\PlainResolvers\Plain_Values_Resolver; |
| 7 |
use Elementor\Modules\Interactions\Props\Interaction_Item_Prop_Type; |
| 8 |
use Elementor\Modules\Interactions\Schema\Interactions_Schema; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
class Interactions_Applier { |
| 15 |
|
| 16 |
private Plain_Values_Resolver $plain_values_resolver; |
| 17 |
|
| 18 |
public function __construct( ?Plain_Values_Resolver $plain_values_resolver = null ) { |
| 19 |
$this->plain_values_resolver = $plain_values_resolver ?? Atomic_Widgets_Module::instance()->get_settings_plain_values_resolver(); |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* @param array<string, array&> $index Index of subtree refs. |
| 24 |
* @param array<string, array<int, array>> $interactions Per-config-id list of native-shape interaction items. |
| 25 |
* |
| 26 |
* @return array{error: \WP_Error|null, warnings: string[]} |
| 27 |
*/ |
| 28 |
public function apply( array &$index, array $interactions ): array { |
| 29 |
if ( empty( $interactions ) ) { |
| 30 |
return [ |
| 31 |
'error' => null, |
| 32 |
'warnings' => [], |
| 33 |
]; |
| 34 |
} |
| 35 |
|
| 36 |
$errors = []; |
| 37 |
|
| 38 |
foreach ( $interactions as $config_id => $items ) { |
| 39 |
if ( ! isset( $index[ $config_id ] ) ) { |
| 40 |
continue; |
| 41 |
} |
| 42 |
|
| 43 |
if ( ! is_array( $items ) ) { |
| 44 |
$errors[] = sprintf( '[%s] Interactions must be an array.', $config_id ); |
| 45 |
continue; |
| 46 |
} |
| 47 |
|
| 48 |
if ( empty( $items ) ) { |
| 49 |
$index[ $config_id ]['interactions'] = [ |
| 50 |
'items' => [], |
| 51 |
'version' => Interactions_Schema::get_interactions_schema()['version'], |
| 52 |
]; |
| 53 |
continue; |
| 54 |
} |
| 55 |
|
| 56 |
$built_items = $this->resolve_items( $items, $config_id, $errors ); |
| 57 |
|
| 58 |
if ( empty( $built_items ) ) { |
| 59 |
continue; |
| 60 |
} |
| 61 |
|
| 62 |
$index[ $config_id ]['interactions'] = [ |
| 63 |
'items' => $built_items, |
| 64 |
'version' => Interactions_Schema::get_interactions_schema()['version'], |
| 65 |
]; |
| 66 |
} |
| 67 |
|
| 68 |
if ( ! empty( $errors ) ) { |
| 69 |
return [ |
| 70 |
'error' => new \WP_Error( |
| 71 |
'elementor_invalid_interactions', |
| 72 |
implode( ' ', $errors ), |
| 73 |
[ 'status' => \WP_Http::BAD_REQUEST ] |
| 74 |
), |
| 75 |
'warnings' => [], |
| 76 |
]; |
| 77 |
} |
| 78 |
|
| 79 |
return [ |
| 80 |
'error' => null, |
| 81 |
'warnings' => [], |
| 82 |
]; |
| 83 |
} |
| 84 |
|
| 85 |
private function resolve_items( array $items, string $config_id, array &$errors ): array { |
| 86 |
$prop_type = Interaction_Item_Prop_Type::make(); |
| 87 |
$built = []; |
| 88 |
|
| 89 |
foreach ( $items as $item_index => $plain_item ) { |
| 90 |
if ( ! is_array( $plain_item ) ) { |
| 91 |
$errors[] = sprintf( '[%s] Interaction at index %d must be an object.', $config_id, $item_index ); |
| 92 |
continue; |
| 93 |
} |
| 94 |
|
| 95 |
$resolved = $this->plain_values_resolver->resolve( $plain_item, $prop_type ); |
| 96 |
|
| 97 |
if ( null === $resolved || ! $prop_type->validate( $resolved ) ) { |
| 98 |
$errors[] = sprintf( |
| 99 |
'[%s] Interaction at index %d could not be resolved. See elementor://interactions/schema.', |
| 100 |
$config_id, |
| 101 |
$item_index |
| 102 |
); |
| 103 |
continue; |
| 104 |
} |
| 105 |
|
| 106 |
$built[] = $resolved; |
| 107 |
} |
| 108 |
|
| 109 |
return $built; |
| 110 |
} |
| 111 |
} |
| 112 |
|