PluginProbe
Elementor Website Builder – more than just a page builder / 4.3.0-beta2
Elementor Website Builder – more than just a page builder v4.3.0-beta2
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 / components / circular-dependency-validator.php

circular-dependency-validator.php in Elementor Website Builder – more than just a page builder 4.3.0-beta2, at modules/components/circular-dependency-validator.php

174 lines 4.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\Components;
4
5 use Elementor\Core\Utils\Collection;
6 use Elementor\Modules\Components\Documents\Component as Component_Document;
7 use Elementor\Modules\Components\PropTypes\Component_Instance_Prop_Type;
8 use Elementor\Plugin;
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit;
12 }
13
14 class Circular_Dependency_Validator {
15 const COMPONENT_WIDGET_TYPE = 'e-component';
16 const MAX_RECURSION_DEPTH = 50;
17
18 private array $components_cache = [];
19
20 public static function make(): Circular_Dependency_Validator {
21 return new self();
22 }
23
24 public function validate( $component_id, array $elements, array $unsaved_components = [] ): array {
25 $inner_components_ids = $this->get_inner_component_ids( $elements );
26
27 if ( in_array( $component_id, $inner_components_ids, false ) ) {
28 return $this->build_error_response( $component_id );
29 }
30
31 foreach ( $inner_components_ids as $ref_id ) {
32 if ( $this->is_component_eventually_contains( $ref_id, $component_id, $unsaved_components, [] ) ) {
33 return $this->build_error_response( $component_id, $ref_id );
34 }
35 }
36
37 return [
38 'success' => true,
39 'messages' => [],
40 ];
41 }
42
43 public function validate_new_components( Collection $items ): array {
44 $unsaved_components = [];
45
46 foreach ( $items->all() as $item ) {
47 $unsaved_components[ $item['uid'] ] = $item['elements'] ?? [];
48 }
49
50 foreach ( $unsaved_components as $uid => $elements ) {
51 $result = $this->validate( $uid, $elements, $unsaved_components );
52
53 if ( ! $result['success'] ) {
54 return $result;
55 }
56 }
57
58 return [
59 'success' => true,
60 'messages' => [],
61 ];
62 }
63
64 private function is_component_eventually_contains( $component_id, $forbidden_id, array $unsaved_components, array $visited_path ): bool {
65 if ( in_array( $component_id, $visited_path, false ) ) {
66 return false;
67 }
68
69 if ( count( $visited_path ) >= self::MAX_RECURSION_DEPTH ) {
70 return false;
71 }
72
73 $elements = $this->get_elements_for_component( $component_id, $unsaved_components );
74
75 if ( empty( $elements ) ) {
76 return false;
77 }
78
79 $nested_ids = $this->get_inner_component_ids( $elements );
80
81 if ( in_array( $forbidden_id, $nested_ids, false ) ) {
82 return true;
83 }
84
85 $visited_path[] = $component_id;
86
87 foreach ( $nested_ids as $nested_id ) {
88 if ( $this->is_component_eventually_contains( $nested_id, $forbidden_id, $unsaved_components, $visited_path ) ) {
89 return true;
90 }
91 }
92
93 return false;
94 }
95
96 private function get_elements_for_component( $component_id, array $unsaved_components ): array {
97 if ( isset( $unsaved_components[ $component_id ] ) ) {
98 return $unsaved_components[ $component_id ];
99 }
100
101 return $this->get_component_elements( $component_id );
102 }
103
104 private function get_component_elements( $component_id ): array {
105 if ( ! is_int( $component_id ) ) {
106 return [];
107 }
108
109 if ( isset( $this->components_cache[ $component_id ] ) ) {
110 return $this->components_cache[ $component_id ];
111 }
112
113 $doc = Plugin::$instance->documents->get( $component_id );
114
115 if ( ! $doc instanceof Component_Document ) {
116 $this->components_cache[ $component_id ] = [];
117 return [];
118 }
119
120 $elements = $doc->get_elements_data();
121 $this->components_cache[ $component_id ] = $elements;
122
123 return $elements;
124 }
125
126 private function get_inner_component_ids( array $elements ): array {
127 $ids = [];
128
129 foreach ( $elements as $element ) {
130 $widget_type = $element['widgetType'] ?? null;
131
132 if ( self::COMPONENT_WIDGET_TYPE === $widget_type ) {
133 $component_id = $this->extract_component_id_from_settings( $element['settings'] ?? [] );
134
135 if ( null !== $component_id ) {
136 $ids[] = $component_id;
137 }
138 }
139
140 if ( ! empty( $element['elements'] ) ) {
141 $ids = array_merge( $ids, $this->get_inner_component_ids( $element['elements'] ) );
142 }
143 }
144
145 return array_unique( $ids, SORT_REGULAR );
146 }
147
148 private function extract_component_id_from_settings( array $settings ) {
149 return Component_Instance_Prop_Type::extract_component_id( $settings );
150 }
151
152 private function build_error_response( $component_id, $via_component_id = null ): array {
153 if ( null === $via_component_id ) {
154 $message = sprintf(
155 // translators: %s: Component ID that references itself.
156 esc_html__( 'Circular dependency detected: Component "%s" references itself.', 'elementor' ),
157 $component_id
158 );
159 } else {
160 $message = sprintf(
161 // translators: %1$s: Component ID, %2$s: Component ID that creates the cycle.
162 esc_html__( 'Circular dependency detected: Component "%1$s" would create a cycle via component "%2$s".', 'elementor' ),
163 $component_id,
164 $via_component_id
165 );
166 }
167
168 return [
169 'success' => false,
170 'messages' => [ $message ],
171 ];
172 }
173 }
174