PluginProbe
Elementor Website Builder – more than just a page builder / 3.35.0-dev4
Elementor Website Builder – more than just a page builder v3.35.0-dev4
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 4.0.7 All 451 releases
elementor / modules / atomic-widgets / prop-dependencies / manager.php

manager.php in Elementor Website Builder – more than just a page builder 3.35.0-dev4, at modules/atomic-widgets/prop-dependencies/manager.php

257 lines 7.1 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\PropDependencies;
4
5 use Elementor\Modules\AtomicWidgets\PropTypes\Contracts\Prop_Type;
6 use Elementor\Utils;
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit; // Exit if accessed directly.
10 }
11
12 class Manager {
13
14 const RELATION_OR = 'or';
15 const RELATION_AND = 'and';
16
17 const OPERATORS = [
18 'lt',
19 'lte',
20 'eq',
21 'ne',
22 'gte',
23 'gt',
24 'exists',
25 'not_exist',
26 'in',
27 'nin',
28 'contains',
29 'ncontains',
30 ];
31
32 /**
33 * @var ?array{
34 * relation: self::RELATION_OR|self::RELATION_AND,
35 * terms: array{
36 * operator: string,
37 * path: array<string>,
38 * value?: mixed,
39 * newValue?: array
40 * }
41 * }
42 */
43 private ?array $dependencies;
44
45 public function __construct( string $relation = self::RELATION_OR ) {
46 $this->new( $relation );
47
48 return $this;
49 }
50
51 public static function make( string $relation = self::RELATION_OR ): self {
52 return new self( $relation );
53 }
54
55 /**
56 * @param array<string, Prop_Type> $props_schema
57 * @return array<string, array<string>> Returns source prop path => array of dependent prop paths
58 */
59 public static function get_source_to_dependents( array $props_schema ): array {
60 $dependency_graph = self::build_dependency_graph( $props_schema );
61
62 if ( self::has_circular_dependencies( $dependency_graph ) ) {
63 Utils::safe_throw( 'Circular prop dependencies detected' );
64 }
65
66 return $dependency_graph;
67 }
68
69 /**
70 * @param $config array{
71 * operator: string,
72 * path: array<string>,
73 * value?: mixed,
74 * newValue?: array,
75 * }
76 * @return self
77 */
78 public function where( array $config, $new_value = null ): self {
79 if ( isset( $config['terms'] ) ) {
80 if ( empty( $this->dependencies ) ) {
81 $this->new();
82 }
83
84 $term = [
85 'terms' => $config['terms'],
86 'relation' => $config['relation'] ?? self::RELATION_OR,
87 'newValue' => $new_value ?? null,
88 ];
89 $this->dependencies['terms'][] = $term;
90
91 return $this;
92 }
93
94 if ( ! isset( $config['operator'] ) || ! isset( $config['path'] ) ) {
95 Utils::safe_throw( 'Term missing mandatory configurations' );
96 }
97
98 if ( ! in_array( $config['operator'], self::OPERATORS, true ) ) {
99 Utils::safe_throw( "Invalid operator: {$config['operator']}." );
100 }
101
102 $term = [
103 'operator' => $config['operator'],
104 'path' => $config['path'],
105 'nestedPath' => $config['nestedPath'] ?? null,
106 'value' => $config['value'] ?? null,
107 'newValue' => $config['newValue'] ?? null,
108 ];
109
110 if ( empty( $this->dependencies ) ) {
111 $this->new();
112 }
113
114 $this->dependencies['terms'][] = $term;
115
116 return $this;
117 }
118
119 private function new( string $relation = self::RELATION_OR ): self {
120 if ( ! in_array( $relation, [ self::RELATION_OR, self::RELATION_AND ], true ) ) {
121 Utils::safe_throw( "Invalid relation: $relation. Must be one of: " . implode( ', ', [ self::RELATION_OR, self::RELATION_AND ] ) );
122 }
123
124 $this->dependencies = [
125 'relation' => $relation,
126 'terms' => [],
127 ];
128
129 return $this;
130 }
131
132 public function get(): ?array {
133 return empty( $this->dependencies['terms'] ?? [] ) ? null : $this->dependencies;
134 }
135
136 /**
137 * @param array<string, Prop_Type> $props_schema The props schema to analyze, where keys are prop names
138 * @param ?array<string> $current_path The current property path being processed
139 * @param ?array<string, array<string>> $dependency_graph The dependency graph to build
140 */
141 private static function build_dependency_graph( array $props_schema, ?array $current_path = [], ?array $dependency_graph = [] ): array {
142 foreach ( $props_schema as $prop_name => $prop_type ) {
143 $dependency_graph = self::build_nested_prop_dependency_graph( $prop_name, $prop_type, $current_path, $dependency_graph );
144 $dependencies = $prop_type->get_dependencies();
145
146 if ( ! $dependencies ) {
147 continue;
148 }
149
150 foreach ( $dependencies['terms'] as $term ) {
151 $dependency_graph = self::process_dependency_term( $term, $current_path, $prop_name, $dependency_graph );
152 }
153 }
154
155 return $dependency_graph;
156 }
157
158 private static function build_nested_prop_dependency_graph( string $prop_name, Prop_Type $prop_type, array $current_path, array $dependency_graph ): array {
159 $nested_prop_path = array_merge( $current_path, [ $prop_name ] );
160
161 switch ( $prop_type->get_type() ) {
162 case 'object':
163 foreach ( $prop_type->get_shape() as $nested_prop_name => $nested_prop_type ) {
164 $dependency_graph = self::build_dependency_graph( [ $nested_prop_name => $nested_prop_type ], $nested_prop_path, $dependency_graph );
165 }
166 break;
167
168 case 'array':
169 $item_prop_type = $prop_type->get_item_type();
170 $dependency_graph = self::build_dependency_graph( [ $prop_name => $item_prop_type ], $current_path, $dependency_graph );
171 break;
172
173 case 'union':
174 foreach ( $prop_type->get_prop_types() as $nested_prop_type ) {
175 $dependency_graph = self::build_dependency_graph( [ $prop_name => $nested_prop_type ], $current_path, $dependency_graph );
176 }
177 break;
178 }
179
180 return $dependency_graph;
181 }
182
183 private static function process_dependency_term( array $term, array $current_path, string $prop_name, array $dependency_graph ): array {
184 if ( self::is_term_nested( $term ) ) {
185 foreach ( $term['terms'] as $nested_term ) {
186 $dependency_graph = self::process_dependency_term( $nested_term, $current_path, $prop_name, $dependency_graph );
187 }
188
189 return $dependency_graph;
190 }
191
192 if ( ! isset( $term['path'] ) || empty( $term['path'] ) ) {
193 Utils::safe_throw( 'Invalid term path in dependency.' );
194 }
195
196 $target_path = implode( '.', $term['path'] );
197 $source = array_merge( $current_path, [ $prop_name ] );
198 $source_path = implode( '.', $source );
199
200 if ( ! isset( $dependency_graph[ $target_path ] ) ) {
201 $dependency_graph[ $target_path ] = [];
202 }
203
204 if ( ! in_array( $source_path, $dependency_graph[ $target_path ] ) ) {
205 $dependency_graph[ $target_path ][] = $source_path;
206 }
207
208 return $dependency_graph;
209 }
210
211 private static function has_circular_dependencies( array $dependency_graph ): bool {
212 $visited_nodes = [];
213 $current_path_stack = [];
214
215 foreach ( array_keys( $dependency_graph ) as $node ) {
216 if ( isset( $visited_nodes[ $node ] ) ) {
217 continue;
218 }
219
220 if ( self::detect_cycle_from_node( $dependency_graph, $node, $visited_nodes, $current_path_stack ) ) {
221 return true;
222 }
223 }
224
225 return false;
226 }
227
228 private static function detect_cycle_from_node( array $dependency_graph, string $current_node, array &$visited_nodes, array &$current_path_stack ): bool {
229 if ( isset( $current_path_stack[ $current_node ] ) ) {
230 return true;
231 }
232
233 if ( isset( $visited_nodes[ $current_node ] ) ) {
234 return false;
235 }
236
237 $visited_nodes[ $current_node ] = true;
238 $current_path_stack[ $current_node ] = true;
239
240 foreach ( $dependency_graph[ $current_node ] ?? [] as $dependent_node ) {
241 $is_circular = self::detect_cycle_from_node( $dependency_graph, $dependent_node, $visited_nodes, $current_path_stack );
242
243 if ( $is_circular ) {
244 return true;
245 }
246 }
247
248 unset( $current_path_stack[ $current_node ] );
249
250 return false;
251 }
252
253 private static function is_term_nested( $term ): bool {
254 return isset( $term['terms'] ) && is_array( $term['terms'] );
255 }
256 }
257