| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\Components; |
| 4 |
|
| 5 |
use Elementor\Core\Utils\Collection; |
| 6 |
use Elementor\Modules\AtomicWidgets\Utils\Utils; |
| 7 |
use Elementor\Plugin; |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
class Non_Atomic_Widget_Validator { |
| 14 |
const ERROR_CODE = 'non_atomic_element_in_component'; |
| 15 |
const WIDGET_EL_TYPE = 'widget'; |
| 16 |
|
| 17 |
public static function make(): Non_Atomic_Widget_Validator { |
| 18 |
return new self(); |
| 19 |
} |
| 20 |
|
| 21 |
public function validate( array $elements ): array { |
| 22 |
$non_atomic_elements = $this->find_non_atomic_elements( $elements ); |
| 23 |
|
| 24 |
if ( ! empty( $non_atomic_elements ) ) { |
| 25 |
return $this->build_error_response( $non_atomic_elements ); |
| 26 |
} |
| 27 |
|
| 28 |
return [ |
| 29 |
'success' => true, |
| 30 |
'messages' => [], |
| 31 |
]; |
| 32 |
} |
| 33 |
|
| 34 |
public function validate_items( Collection $items ): array { |
| 35 |
foreach ( $items->all() as $item ) { |
| 36 |
$elements = $item['elements'] ?? []; |
| 37 |
$result = $this->validate( $elements ); |
| 38 |
|
| 39 |
if ( ! $result['success'] ) { |
| 40 |
return $result; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
return [ |
| 45 |
'success' => true, |
| 46 |
'messages' => [], |
| 47 |
]; |
| 48 |
} |
| 49 |
|
| 50 |
private function find_non_atomic_elements( array $elements ): array { |
| 51 |
$non_atomic = []; |
| 52 |
|
| 53 |
foreach ( $elements as $element ) { |
| 54 |
$el_type = $element['elType'] ?? null; |
| 55 |
$widget_type = $element['widgetType'] ?? null; |
| 56 |
$element_type = $this->get_element_type( $el_type, $widget_type ); |
| 57 |
|
| 58 |
if ( $element_type && ! $this->is_element_atomic( $el_type, $widget_type ) ) { |
| 59 |
$non_atomic[] = $element_type; |
| 60 |
} |
| 61 |
|
| 62 |
if ( ! empty( $element['elements'] ) ) { |
| 63 |
$nested_non_atomic = $this->find_non_atomic_elements( $element['elements'] ); |
| 64 |
$non_atomic = array_merge( $non_atomic, $nested_non_atomic ); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
return array_unique( $non_atomic ); |
| 69 |
} |
| 70 |
|
| 71 |
private function get_element_type( ?string $el_type, ?string $widget_type ): ?string { |
| 72 |
return $widget_type ?? $el_type; |
| 73 |
} |
| 74 |
|
| 75 |
private function is_element_atomic( ?string $el_type, ?string $widget_type ): bool { |
| 76 |
if ( ! $el_type ) { |
| 77 |
return false; |
| 78 |
} |
| 79 |
|
| 80 |
$element_instance = Plugin::$instance->elements_manager->get_element( $el_type, $widget_type ); |
| 81 |
|
| 82 |
if ( ! $element_instance ) { |
| 83 |
return false; |
| 84 |
} |
| 85 |
|
| 86 |
return Utils::is_atomic( $element_instance ); |
| 87 |
} |
| 88 |
|
| 89 |
private function build_error_response( array $non_atomic_elements ): array { |
| 90 |
$message = sprintf( |
| 91 |
// translators: %s: Comma-separated list of non-atomic element types. |
| 92 |
esc_html__( 'Component contains non-supported elements: %s. Only atomic elements are allowed inside components.', 'elementor' ), |
| 93 |
implode( ', ', $non_atomic_elements ) |
| 94 |
); |
| 95 |
|
| 96 |
return [ |
| 97 |
'success' => false, |
| 98 |
'code' => self::ERROR_CODE, |
| 99 |
'messages' => [ $message ], |
| 100 |
'non_atomic_elements' => $non_atomic_elements, |
| 101 |
]; |
| 102 |
} |
| 103 |
} |
| 104 |
|