| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\Mcp\Abilities\Appliers\V3; |
| 4 |
|
| 5 |
use Elementor\Core\DynamicTags\Manager; |
| 6 |
use Elementor\Plugin; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
class V3_Dynamic_Hoister { |
| 13 |
|
| 14 |
/** @var Manager */ |
| 15 |
private $manager; |
| 16 |
|
| 17 |
public function __construct( ?Manager $manager = null ) { |
| 18 |
$this->manager = $manager ?? Plugin::$instance->dynamic_tags; |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* @param string $widget_type |
| 23 |
* @param array<string, mixed> $allowed |
| 24 |
* @param array<string, array<string, mixed>> $controls |
| 25 |
* |
| 26 |
* @return array{primitives: array<string, mixed>, shortcodes: array<string, string>, errors: string[]} |
| 27 |
*/ |
| 28 |
public function hoist( string $widget_type, array $allowed, array $controls ): array { |
| 29 |
$primitives = []; |
| 30 |
$shortcodes = []; |
| 31 |
$errors = []; |
| 32 |
|
| 33 |
foreach ( $allowed as $key => $value ) { |
| 34 |
$control = is_array( $controls[ $key ] ?? null ) ? $controls[ $key ] : []; |
| 35 |
|
| 36 |
if ( ! V3_Dynamic_Resolver::is_dynamic_capable( $control ) ) { |
| 37 |
$primitives[ $key ] = $value; |
| 38 |
continue; |
| 39 |
} |
| 40 |
|
| 41 |
$control_dynamic = is_array( $control['dynamic'] ?? null ) ? $control['dynamic'] : []; |
| 42 |
$property = is_string( $control_dynamic['property'] ?? null ) ? $control_dynamic['property'] : null; |
| 43 |
|
| 44 |
$input = V3_Dynamic_Resolver::extract_input( $value, $property ); |
| 45 |
if ( null === $input ) { |
| 46 |
$primitives[ $key ] = $value; |
| 47 |
continue; |
| 48 |
} |
| 49 |
|
| 50 |
$tag = $this->manager->create_tag( |
| 51 |
$this->generate_tag_id( $widget_type, $key, $input ), |
| 52 |
$input['name'], |
| 53 |
$input['settings'] |
| 54 |
); |
| 55 |
if ( ! $tag ) { |
| 56 |
$errors[] = sprintf( |
| 57 |
'V3 widget "%s" property "%s": dynamic tag "%s" is not registered.', |
| 58 |
$widget_type, |
| 59 |
$key, |
| 60 |
$input['name'] |
| 61 |
); |
| 62 |
continue; |
| 63 |
} |
| 64 |
|
| 65 |
$control_categories = $control_dynamic['categories'] ?? []; |
| 66 |
if ( ! empty( $control_categories ) && empty( array_intersect( $tag->get_categories(), $control_categories ) ) ) { |
| 67 |
$errors[] = sprintf( |
| 68 |
'V3 widget "%s" property "%s": dynamic tag "%s" (categories: [%s]) is not compatible with field "%s" (allowed categories: [%s]).', |
| 69 |
$widget_type, |
| 70 |
$key, |
| 71 |
$input['name'], |
| 72 |
implode( ', ', $tag->get_categories() ), |
| 73 |
$key, |
| 74 |
implode( ', ', $control_categories ) |
| 75 |
); |
| 76 |
continue; |
| 77 |
} |
| 78 |
|
| 79 |
$shortcode = $this->manager->tag_to_text( $tag ); |
| 80 |
if ( '' === $shortcode ) { |
| 81 |
$errors[] = sprintf( |
| 82 |
'V3 widget "%s" property "%s": failed to serialize dynamic tag "%s".', |
| 83 |
$widget_type, |
| 84 |
$key, |
| 85 |
$input['name'] |
| 86 |
); |
| 87 |
continue; |
| 88 |
} |
| 89 |
|
| 90 |
$shortcodes[ $key ] = $shortcode; |
| 91 |
|
| 92 |
if ( is_array( $value ) ) { |
| 93 |
$remainder = V3_Dynamic_Resolver::extract_primitive_remainder( $value, $property ); |
| 94 |
if ( ! empty( $remainder ) ) { |
| 95 |
$primitives[ $key ] = $remainder; |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
return [ |
| 101 |
'primitives' => $primitives, |
| 102 |
'shortcodes' => $shortcodes, |
| 103 |
'errors' => $errors, |
| 104 |
]; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* @param string $widget_type |
| 109 |
* @param string $key |
| 110 |
* @param array $input |
| 111 |
*/ |
| 112 |
private function generate_tag_id( string $widget_type, string $key, array $input ): string { |
| 113 |
$encoded_settings = wp_json_encode( $input['settings'] ); |
| 114 |
|
| 115 |
return substr( |
| 116 |
md5( $widget_type . ':' . $key . ':' . $input['name'] . ':' . $encoded_settings ), |
| 117 |
0, |
| 118 |
7 |
| 119 |
); |
| 120 |
} |
| 121 |
} |
| 122 |
|