PluginProbe
Elementor Website Builder – more than just a page builder / 4.3.0-beta3
Elementor Website Builder – more than just a page builder v4.3.0-beta3
4.3.0-beta3 4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 All 452 releases
elementor / modules / atomic-widgets / props-resolver / props-resolver.php

props-resolver.php in Elementor Website Builder – more than just a page builder 4.3.0-beta3, at modules/atomic-widgets/props-resolver/props-resolver.php

119 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Elementor\Modules\AtomicWidgets\PropsResolver;
4
5 use Elementor\Modules\AtomicWidgets\PropTypes\Base\Array_Prop_Type;
6 use Elementor\Modules\AtomicWidgets\PropTypes\Base\Object_Prop_Type;
7 use Elementor\Modules\AtomicWidgets\PropTypes\Contracts\Prop_Type;
8 use Elementor\Modules\AtomicWidgets\PropTypes\Union_Prop_Type;
9 use Exception;
10
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit; // Exit if accessed directly.
13 }
14
15 abstract class Props_Resolver {
16 protected static array $instances = [];
17
18 protected Transformers_Registry $transformers_registry;
19
20 protected function __construct( Transformers_Registry $transformers_registry ) {
21 $this->transformers_registry = $transformers_registry;
22 }
23
24 protected static function instance( string $context ) {
25 if ( ! isset( static::$instances[ $context ] ) ) {
26 $instance = new static( new Transformers_Registry() );
27
28 static::$instances[ $context ] = $instance;
29
30 do_action(
31 "elementor/atomic-widgets/$context/transformers/register",
32 $instance->get_transformers_registry(),
33 $instance
34 );
35 }
36
37 return static::$instances[ $context ];
38 }
39
40 public static function reset(): void {
41 static::$instances = [];
42 }
43
44 public function get_transformers_registry(): Transformers_Registry {
45 return $this->transformers_registry;
46 }
47
48 protected function transform( $value, $key, Prop_Type $prop_type ) {
49 if ( $prop_type instanceof Union_Prop_Type ) {
50 $prop_type = $prop_type->get_prop_type( $value['$$type'] );
51
52 if ( ! $prop_type ) {
53 return null;
54 }
55 }
56
57 if ( $value['$$type'] !== $prop_type::get_key() ) {
58 return null;
59 }
60
61 if ( $prop_type instanceof Object_Prop_Type ) {
62 if ( ! is_array( $value['value'] ) ) {
63 return null;
64 }
65
66 $value['value'] = $this->resolve(
67 $prop_type->get_shape(),
68 $value['value']
69 );
70 }
71
72 if ( $prop_type instanceof Array_Prop_Type ) {
73 if ( ! is_array( $value['value'] ) ) {
74 return null;
75 }
76
77 $resolved_items = [];
78
79 foreach ( $value['value'] as $item ) {
80 $resolved = $this->resolve_item( $item, null, $prop_type->get_item_type() );
81
82 if ( null !== $resolved ) {
83 $resolved_items[] = $resolved;
84 }
85 }
86
87 $value['value'] = $resolved_items;
88 }
89
90 $transformer = $this->transformers_registry->get( $value['$$type'] );
91
92 if ( ! ( $transformer instanceof Transformer_Base ) ) {
93 return null;
94 }
95
96 try {
97 $context = Props_Resolver_Context::make()
98 ->set_key( $key )
99 ->set_disabled( (bool) ( $value['disabled'] ?? false ) )
100 ->set_prop_type( $prop_type );
101
102 return $transformer->transform( $value['value'], $context );
103 } catch ( Exception $e ) {
104 return null;
105 }
106 }
107
108 protected function is_transformable( $value ): bool {
109 return (
110 ! empty( $value['$$type'] ) &&
111 array_key_exists( 'value', $value )
112 );
113 }
114
115 abstract public function resolve( array $schema, array $props ): array;
116
117 abstract protected function resolve_item( $value, $key, Prop_Type $prop_type );
118 }
119