PluginProbe
Elementor Website Builder – more than just a page builder / 3.34.1
Elementor Website Builder – more than just a page builder v3.34.1
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 / save-components-validator.php

save-components-validator.php in Elementor Website Builder – more than just a page builder 3.34.1, at modules/components/save-components-validator.php

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