PluginProbe
Elementor Website Builder – more than just a page builder / 3.35.3
Elementor Website Builder – more than just a page builder v3.35.3
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 / global-classes / global-classes-parser.php

global-classes-parser.php in Elementor Website Builder – more than just a page builder 3.35.3, at modules/global-classes/global-classes-parser.php

141 lines 3.3 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\GlobalClasses;
4
5 use Elementor\Core\Utils\Collection;
6 use Elementor\Modules\AtomicWidgets\Module;
7 use Elementor\Modules\AtomicWidgets\OptIn\Opt_In;
8 use Elementor\Core\Utils\Api\Parse_Result;
9 use Elementor\Modules\AtomicWidgets\Parsers\Style_Parser;
10 use Elementor\Modules\AtomicWidgets\Styles\Style_Schema;
11
12 class Global_Classes_Parser {
13 public static function make() {
14 return new static();
15 }
16
17 public function parse( $data ): Parse_Result {
18 $result = Parse_Result::make();
19
20 if ( ! isset( $data['items'] ) ) {
21 $result->errors()->add( 'items', 'missing' );
22
23 return $result;
24 }
25
26 if ( ! isset( $data['order'] ) ) {
27 $result->errors()->add( 'order', 'missing' );
28
29 return $result;
30 }
31
32 $items = $data['items'];
33 $order = $data['order'];
34
35 if ( ! is_array( $items ) ) {
36 $result->errors()->add( 'items', 'invalid' );
37
38 return $result;
39 }
40
41 if ( ! is_array( $order ) ) {
42 $result->errors()->add( 'order', 'invalid' );
43
44 return $result;
45 }
46
47 $items_result = $this->parse_items( $items );
48
49 if ( ! $items_result->is_valid() ) {
50 $result->errors()->merge( $items_result->errors(), 'items' );
51
52 return $result;
53 }
54
55 $order_result = $this->parse_order( $order, $items_result->unwrap() );
56
57 if ( ! $order_result->is_valid() ) {
58 $result->errors()->merge( $order_result->errors(), 'order' );
59
60 return $result;
61 }
62
63 return $result->wrap( [
64 'items' => $items_result->unwrap(),
65 'order' => $order_result->unwrap(),
66 ] );
67 }
68
69 public function parse_items( array $items ) {
70 $sanitized_items = [];
71 $result = Parse_Result::make();
72 $style_parser = Style_Parser::make( Style_Schema::get() );
73 $existing_labels = [];
74
75 foreach ( $items as $item_id => $item ) {
76 $item_result = $style_parser->parse( $item );
77
78 if ( ! $item_result->is_valid() ) {
79 $result->errors()->merge( $item_result->errors(), $item_id );
80
81 continue;
82 }
83
84 $sanitized_item = $item_result->unwrap();
85
86 if ( $item_id !== $sanitized_item['id'] ) {
87 $result->errors()->add( "$item_id.id", 'mismatching_value' );
88
89 continue;
90 }
91
92 $sanitized_items[ $sanitized_item['id'] ] = $sanitized_item;
93 $existing_labels[] = $sanitized_item['label'];
94 }
95
96 return $result->wrap( $sanitized_items );
97 }
98
99 public function parse_order( array $order, array $items ): Parse_Result {
100 $result = Parse_Result::make();
101
102 $items = Collection::make( $items );
103
104 $order = Collection::make( $order )
105 ->filter( fn( $item ) => is_string( $item ) )
106 ->unique();
107
108 $existing_ids = $items->keys();
109
110 $excess_ids = $order->diff( $existing_ids );
111 $missing_ids = $existing_ids->diff( $order );
112
113 $excess_ids->each( fn( $id ) => $result->errors()->add( $id, 'excess' ) );
114 $missing_ids->each( fn( $id ) => $result->errors()->add( $id, 'missing' ) );
115
116 return $result->is_valid()
117 ? $result->wrap( $order->values() )
118 : $result;
119 }
120
121 public static function check_for_duplicate_labels( array $existing_labels, array $items, array $new_items_ids ) {
122
123 if ( empty( $new_items_ids ) ) {
124 return false;
125 }
126 $new_added_items = array_filter( $items, fn( $item ) => in_array( $item['id'], $new_items_ids, true ) );
127
128 $duplicates = [];
129
130 foreach ( $new_added_items as $item_id => $item ) {
131 if ( in_array( $item['label'], $existing_labels, true ) ) {
132 $duplicates[] = [
133 'item_id' => $item_id,
134 'label' => $item['label'],
135 ];
136 }
137 }
138 return $duplicates;
139 }
140 }
141