| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\Components; |
| 4 |
|
| 5 |
use Elementor\Core\Utils\Collection; |
| 6 |
use Elementor\Modules\Components\Documents\Component; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; // Exit if accessed directly. |
| 10 |
} |
| 11 |
|
| 12 |
class Save_Components_Validator { |
| 13 |
private Collection $components; |
| 14 |
|
| 15 |
public function __construct( Collection $components ) { |
| 16 |
$this->components = $components; |
| 17 |
} |
| 18 |
|
| 19 |
public static function make( Collection $components ) { |
| 20 |
return new static( $components ); |
| 21 |
} |
| 22 |
|
| 23 |
public function validate( Collection $data ) { |
| 24 |
$errors = Collection::make( [ |
| 25 |
$this->validate_count( $data ), |
| 26 |
$this->validate_duplicated_values( $data ), |
| 27 |
] )->flatten(); |
| 28 |
|
| 29 |
if ( $errors->is_empty() ) { |
| 30 |
return [ |
| 31 |
'success' => true, |
| 32 |
'messages' => [], |
| 33 |
]; |
| 34 |
} |
| 35 |
|
| 36 |
return [ |
| 37 |
'success' => false, |
| 38 |
'messages' => $errors->values(), |
| 39 |
]; |
| 40 |
} |
| 41 |
|
| 42 |
private function validate_count( Collection $data ): array { |
| 43 |
$non_archived_components = $this->components->filter( fn ( $component ) => ! $component['is_archived'] ); |
| 44 |
$count = $non_archived_components->count() + $data->count(); |
| 45 |
|
| 46 |
if ( $count > Components_REST_API::MAX_COMPONENTS ) { |
| 47 |
return [ esc_html__( 'Maximum number of components exceeded.', 'elementor' ) ]; |
| 48 |
} |
| 49 |
|
| 50 |
return []; |
| 51 |
} |
| 52 |
|
| 53 |
private function validate_duplicated_values( Collection $data ): array { |
| 54 |
return $data |
| 55 |
->map( function ( $component ) use ( $data ) { |
| 56 |
$errors = []; |
| 57 |
|
| 58 |
$title = $component['title']; |
| 59 |
$uid = $component['uid']; |
| 60 |
|
| 61 |
$is_title_exists = $this->components->some( |
| 62 |
fn ( $component ) => ! $component['is_archived'] && $component['title'] === $title |
| 63 |
) || $data->filter( |
| 64 |
fn ( $component ) => ! $component['title'] === $title |
| 65 |
)->count() > 1; |
| 66 |
|
| 67 |
if ( $is_title_exists ) { |
| 68 |
$errors[] = [ |
| 69 |
sprintf( |
| 70 |
// translators: %s Component title. |
| 71 |
esc_html__( "Component title '%s' is duplicated.", 'elementor' ), |
| 72 |
$title |
| 73 |
), |
| 74 |
]; |
| 75 |
} |
| 76 |
|
| 77 |
$is_uid_exists = $this->components->some( |
| 78 |
fn ( $component ) => $component['uid'] === $uid |
| 79 |
) || $data->filter( |
| 80 |
fn ( $component ) => $component['uid'] === $uid |
| 81 |
)->count() > 1; |
| 82 |
|
| 83 |
if ( $is_uid_exists ) { |
| 84 |
$errors[] = [ |
| 85 |
sprintf( |
| 86 |
// translators: %s Component uid. |
| 87 |
esc_html__( "Component uid '%s' is duplicated.", 'elementor' ), |
| 88 |
$uid |
| 89 |
), |
| 90 |
]; |
| 91 |
} |
| 92 |
|
| 93 |
return $errors; |
| 94 |
} ) |
| 95 |
->flatten() |
| 96 |
->flatten() |
| 97 |
->unique() |
| 98 |
->values(); |
| 99 |
} |
| 100 |
} |
| 101 |
|