PluginProbe
Elementor Website Builder – more than just a page builder / 3.29.0-beta4
Elementor Website Builder – more than just a page builder v3.29.0-beta4
4.3.2 4.3.1 4.3.0 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 All 455 releases
elementor / modules / atomic-widgets / parsers / style-parser.php

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

162 lines 4.0 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
23 public function __construct( array $schema ) {
24 $this->schema = $schema;
25 }
26
27 public static function make( array $schema ): self {
28 return new static( $schema );
29 }
30
31 /**
32 * @param array $style
33 * the style object to validate
34 */
35 private function validate( array $style ): Parse_Result {
36 $validated_style = $style;
37 $result = Parse_Result::make();
38
39 if ( ! isset( $style['id'] ) || ! is_string( $style['id'] ) ) {
40 $result->errors()->add( 'id', 'missing_or_invalid' );
41 }
42
43 if ( ! isset( $style['type'] ) || ! in_array( $style['type'], self::VALID_TYPES, true ) ) {
44 $result->errors()->add( 'type', 'missing_or_invalid' );
45 }
46
47 if ( ! isset( $style['label'] ) || ! is_string( $style['label'] ) ) {
48 $result->errors()->add( 'label', 'missing_or_invalid' );
49 }
50
51 if ( ! isset( $style['variants'] ) || ! is_array( $style['variants'] ) ) {
52 $result->errors()->add( 'variants', 'missing_or_invalid' );
53
54 unset( $validated_style['variants'] );
55
56 return $result->wrap( $validated_style );
57 }
58
59 $props_parser = Props_Parser::make( $this->schema );
60
61 foreach ( $style['variants'] as $variant_index => $variant ) {
62 if ( ! isset( $variant['meta'] ) ) {
63 $result->errors()->add( 'meta', 'missing' );
64
65 continue;
66 }
67
68 $meta_result = $this->validate_meta( $variant['meta'] );
69
70 $result->errors()->merge( $meta_result->errors(), 'meta' );
71
72 if ( $meta_result->is_valid() ) {
73 $variant_result = $props_parser->validate( $variant['props'] );
74
75 $result->errors()->merge( $variant_result->errors(), "variants[$variant_index]" );
76
77 $validated_style['variants'][ $variant_index ]['props'] = $variant_result->unwrap();
78 } else {
79 unset( $validated_style['variants'][ $variant_index ] );
80 }
81 }
82
83 return $result->wrap( $validated_style );
84 }
85
86 private function validate_meta( $meta ): Parse_Result {
87 $result = Parse_Result::make();
88
89 if ( ! is_array( $meta ) ) {
90 $result->errors()->add( 'meta', 'invalid_type' );
91
92 return $result;
93 }
94
95 if ( ! array_key_exists( 'state', $meta ) || ! in_array( $meta['state'], self::VALID_STATES, true ) ) {
96 $result->errors()->add( 'state', 'missing_or_invalid_value' );
97
98 return $result;
99 }
100
101 // TODO: Validate breakpoint based on the existing breakpoints in the system [EDS-528]
102 if ( ! isset( $meta['breakpoint'] ) || ! is_string( $meta['breakpoint'] ) ) {
103 $result->errors()->add( 'breakpoint', 'missing_or_invalid_value' );
104
105 return $result;
106 }
107
108 return $result;
109 }
110
111 private function sanitize_meta( $meta ) {
112 if ( ! is_array( $meta ) ) {
113 return [];
114 }
115
116 if ( isset( $meta['breakpoint'] ) ) {
117 $meta['breakpoint'] = sanitize_key( $meta['breakpoint'] );
118 }
119
120 return $meta;
121 }
122
123 /**
124 * @param array $style
125 * the style object to sanitize
126 */
127 private function sanitize( array $style ): Parse_Result {
128 $props_parser = Props_Parser::make( $this->schema );
129
130 if ( isset( $style['label'] ) ) {
131 $style['label'] = sanitize_text_field( $style['label'] );
132 }
133
134 if ( isset( $style['id'] ) ) {
135 $style['id'] = sanitize_key( $style['id'] );
136 }
137
138 if ( ! empty( $style['variants'] ) ) {
139 foreach ( $style['variants'] as $variant_index => $variant ) {
140 $style['variants'][ $variant_index ]['props'] = $props_parser->sanitize( $variant['props'] )->unwrap();
141 $style['variants'][ $variant_index ]['meta'] = $this->sanitize_meta( $variant['meta'] );
142 }
143 }
144
145 return Parse_Result::make()->wrap( $style );
146 }
147
148 /**
149 * @param array $style
150 * the style object to parse
151 */
152 public function parse( array $style ): Parse_Result {
153 $validate_result = $this->validate( $style );
154
155 $sanitize_result = $this->sanitize( $validate_result->unwrap() );
156
157 $sanitize_result->errors()->merge( $validate_result->errors() );
158
159 return $sanitize_result;
160 }
161 }
162