PluginProbe
Elementor Website Builder – more than just a page builder / 3.27.4
Elementor Website Builder – more than just a page builder v3.27.4
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.27.4, at modules/atomic-widgets/parsers/style-parser.php

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