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