PluginProbe
Elementor Website Builder – more than just a page builder / 3.28.0-dev1
Elementor Website Builder – more than just a page builder v3.28.0-dev1
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 3.28.0-dev1, at modules/atomic-widgets/props-resolver/props-resolver.php

172 lines 4.0 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 class Props_Resolver {
16 /**
17 * Each transformer can return a value that is also a transformable value,
18 * which means that it can be transformed again by another transformer.
19 * This constant defines the maximum depth of transformations to avoid infinite loops.
20 */
21 const TRANSFORM_DEPTH_LIMIT = 3;
22
23 const CONTEXT_SETTINGS = 'settings';
24 const CONTEXT_STYLES = 'styles';
25
26 /**
27 * @var array<string, Props_Resolver>
28 */
29 private static array $instances = [];
30
31 private Transformers_Registry $transformers_registry;
32
33 private function __construct( Transformers_Registry $transformers_registry ) {
34 $this->transformers_registry = $transformers_registry;
35 }
36
37 public static function for_styles(): self {
38 return self::instance( self::CONTEXT_STYLES );
39 }
40
41 public static function for_settings(): self {
42 return self::instance( self::CONTEXT_SETTINGS );
43 }
44
45 private static function instance( string $context ): self {
46 if ( ! isset( self::$instances[ $context ] ) ) {
47 $instance = new self( new Transformers_Registry() );
48
49 self::$instances[ $context ] = $instance;
50
51 do_action(
52 "elementor/atomic-widgets/$context/transformers/register",
53 $instance->get_transformers_registry(),
54 $instance
55 );
56 }
57
58 return self::$instances[ $context ];
59 }
60
61 public static function reset(): void {
62 self::$instances = [];
63 }
64
65 public function get_transformers_registry(): Transformers_Registry {
66 return $this->transformers_registry;
67 }
68
69 public function resolve( array $schema, array $props ): array {
70 $resolved = [];
71
72 foreach ( $schema as $key => $prop_type ) {
73 if ( ! ( $prop_type instanceof Prop_Type ) ) {
74 continue;
75 }
76
77 $resolved[ $key ] = $props[ $key ] ?? $prop_type->get_default();
78 }
79
80 return $this->assign_values( $resolved, $schema );
81 }
82
83 private function transform( $value, $key, Prop_Type $prop_type, int $depth = 0 ) {
84 if ( null === $value ) {
85 return null;
86 }
87
88 if ( ! $this->is_transformable( $value ) ) {
89 return $value;
90 }
91
92 if ( $depth >= self::TRANSFORM_DEPTH_LIMIT ) {
93 return null;
94 }
95
96 if ( isset( $value['disabled'] ) && true === $value['disabled'] ) {
97 return null;
98 }
99
100 if ( $prop_type instanceof Union_Prop_Type ) {
101 $prop_type = $prop_type->get_prop_type( $value['$$type'] );
102
103 if ( ! $prop_type ) {
104 return null;
105 }
106 }
107
108 if ( $prop_type instanceof Object_Prop_Type ) {
109 if ( ! is_array( $value['value'] ) ) {
110 return null;
111 }
112
113 $value['value'] = $this->resolve(
114 $prop_type->get_shape(),
115 $value['value']
116 );
117 }
118
119 if ( $prop_type instanceof Array_Prop_Type ) {
120 if ( ! is_array( $value['value'] ) ) {
121 return null;
122 }
123
124 $value['value'] = $this->assign_values(
125 $value['value'],
126 $prop_type->get_item_type()
127 );
128 }
129
130 $transformer = $this->transformers_registry->get( $value['$$type'] );
131
132 if ( ! ( $transformer instanceof Transformer_Base ) ) {
133 return null;
134 }
135
136 try {
137 $transformed_value = $transformer->transform( $value['value'], $key );
138
139 return $this->transform( $transformed_value, $key, $prop_type, $depth + 1 );
140 } catch ( Exception $e ) {
141 return null;
142 }
143 }
144
145 private function is_transformable( $value ): bool {
146 return (
147 ! empty( $value['$$type'] ) &&
148 array_key_exists( 'value', $value )
149 );
150 }
151
152 private function assign_values( $values, $schema ) {
153 $assigned = [];
154
155 foreach ( $values as $key => $value ) {
156 $prop_type = $schema instanceof Prop_Type ? $schema : $schema[ $key ];
157
158 $transformed = $this->transform( $value, $key, $prop_type );
159
160 if ( Multi_Props::is( $transformed ) ) {
161 $assigned = array_merge( $assigned, Multi_Props::get_value( $transformed ) );
162
163 continue;
164 }
165
166 $assigned[ $key ] = $transformed;
167 }
168
169 return $assigned;
170 }
171 }
172