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

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