PluginProbe
Elementor Website Builder – more than just a page builder / 3.32.4
Elementor Website Builder – more than just a page builder v3.32.4
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.32.4, at modules/global-classes/global-classes-parser.php

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