PluginProbe
Elementor Website Builder – more than just a page builder / 4.0.3
Elementor Website Builder – more than just a page builder v4.0.3
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 4.0.3, at modules/atomic-widgets/prop-dependencies/manager.php

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