PluginProbe
Elementor Website Builder – more than just a page builder / 3.28.0-dev1
Elementor Website Builder – more than just a page builder v3.28.0-dev1
4.3.0-beta3 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 All 452 releases
elementor / modules / atomic-widgets / parsers / style-parser.php

style-parser.php in Elementor Website Builder – more than just a page builder 3.28.0-dev1, at modules/atomic-widgets/parsers/style-parser.php

162 lines 3.5 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\AtomicWidgets\Parsers;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit; // Exit if accessed directly.
7 }
8
9 class Style_Parser {
10 const VALID_TYPES = [
11 'class',
12 ];
13
14 const VALID_STATES = [
15 'hover',
16 'active',
17 'focus',
18 null,
19 ];
20
21 private array $schema;
22 private array $errors_bag = [];
23 private $should_validate_id = true;
24
25 public function __construct( array $schema ) {
26 $this->schema = $schema;
27 }
28
29 public static function make( array $schema ): self {
30 return new static( $schema );
31 }
32
33 public function without_id() {
34 $this->should_validate_id = false;
35
36 return $this;
37 }
38
39 /**
40 * @param array $style
41 * the style object to validate
42 *
43 * @return array{
44 * 0: bool,
45 * 1: array<string, mixed>,
46 * 2: array<string>
47 * }
48 */
49 public function validate( array $style ): array {
50 $validated_style = $style;
51
52 if ( $this->should_validate_id && ( ! isset( $style['id'] ) || ! is_string( $style['id'] ) ) ) {
53 $this->errors_bag[] = 'id';
54 }
55
56 if ( ! isset( $style['type'] ) || ! in_array( $style['type'], self::VALID_TYPES, true ) ) {
57 $this->errors_bag[] = 'type';
58 }
59
60 if ( ! isset( $style['label'] ) || ! is_string( $style['label'] ) ) {
61 $this->errors_bag[] = 'label';
62 }
63
64 if ( ! isset( $style['variants'] ) || ! is_array( $style['variants'] ) ) {
65 $this->errors_bag[] = 'variants';
66 unset( $validated_style['variants'] );
67
68 return [
69 false,
70 $validated_style,
71 $this->errors_bag,
72 ];
73 }
74
75 $props_parser = Props_Parser::make( $this->schema );
76
77 foreach ( $style['variants'] as $variant_index => $variant ) {
78 if ( ! isset( $variant['meta'] ) ) {
79 $this->errors_bag[] = 'meta';
80 continue;
81 }
82
83 $is_variant_meta_valid = $this->validate_meta( $variant['meta'] );
84
85 if ( $is_variant_meta_valid ) {
86 [, $validated_props, $variant_errors] = $props_parser->validate( $variant['props'] );
87 $this->errors_bag = array_merge( $this->errors_bag, $variant_errors );
88
89 $validated_style['variants'][ $variant_index ]['props'] = $validated_props;
90 } else {
91 unset( $validated_style['variants'][ $variant_index ] );
92 }
93 }
94
95 $is_valid = empty( $this->errors_bag );
96
97 return [
98 $is_valid,
99 $validated_style,
100 $this->errors_bag,
101 ];
102 }
103
104 public function validate_meta( $meta ): bool {
105 if ( ! is_array( $meta ) ) {
106 $this->errors_bag[] = 'meta';
107 return false;
108 }
109
110 if ( ! array_key_exists( 'state', $meta ) || ! in_array( $meta['state'], self::VALID_STATES, true ) ) {
111 $this->errors_bag[] = 'meta';
112 return false;
113 }
114
115 // TODO: Validate breakpoint based on the existing breakpoints in the system [EDS-528]
116 if ( ! isset( $meta['breakpoint'] ) || ! is_string( $meta['breakpoint'] ) ) {
117 $this->errors_bag[] = 'meta';
118 return false;
119 }
120
121 return true;
122 }
123
124 /**
125 * @param array $style
126 * the style object to sanitize
127 *
128 * @return array<string, mixed>
129 */
130 public function sanitize( array $style ): array {
131 $props_parser = Props_Parser::make( $this->schema );
132
133 if ( ! empty( $style['variants'] ) ) {
134 foreach ( $style['variants'] as $variant_index => $variant ) {
135 $style['variants'][ $variant_index ]['props'] = $props_parser->sanitize( $variant['props'] );
136 }
137 }
138
139 return $style;
140 }
141
142 /**
143 * @param array $style
144 * the style object to parse
145 *
146 * @return array{
147 * 0: bool,
148 * 1: array<string, mixed>,
149 * 2: array<string>
150 * }
151 */
152 public function parse( array $style ): array {
153 [ $is_valid, $validated, $errors_bag ] = $this->validate( $style );
154
155 return [
156 $is_valid,
157 $this->sanitize( $validated ),
158 $errors_bag,
159 ];
160 }
161 }
162