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

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