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

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