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
← All changes | modules/atomic-widgets/parsers/style-parser.php +7 -109 4.2.0 → 3.29.0-beta4 View file →
@@ -5,19 +5,19 @@
5 5 if ( ! defined( 'ABSPATH' ) ) {
6 6 exit; // Exit if accessed directly.
7 7 }
8 8
9 -use Elementor\Modules\AtomicWidgets\OptIn\Opt_In;
10 -use Elementor\Plugin;
11 -use Elementor\Utils;
12 -use Elementor\Core\Utils\Api\Parse_Result;
13 -use Elementor\Modules\AtomicWidgets\Styles\Style_States;
14 -
15 9 class Style_Parser {
16 10 const VALID_TYPES = [
17 11 'class',
18 12 ];
19 13
14 + const VALID_STATES = [
15 + 'hover',
16 + 'active',
17 + 'focus',
18 + null,
19 + ];
20 20
21 21 private array $schema;
22 22
23 23 public function __construct( array $schema ) {
@@ -45,14 +45,8 @@
45 45 }
46 46
47 47 if ( ! isset( $style['label'] ) || ! is_string( $style['label'] ) ) {
48 48 $result->errors()->add( 'label', 'missing_or_invalid' );
49 - } elseif ( Plugin::$instance->experiments->is_feature_active( Opt_In::EXPERIMENT_NAME ) ) {
50 - $label_validation = $this->validate_style_label( $style['label'] );
51 -
52 - if ( ! $label_validation['is_valid'] ) {
53 - $result->errors()->add( 'label', $label_validation['error_message'] );
54 - }
55 49 }
56 50
57 51 if ( ! isset( $style['variants'] ) || ! is_array( $style['variants'] ) ) {
58 52 $result->errors()->add( 'variants', 'missing_or_invalid' );
@@ -71,12 +65,10 @@
71 65 continue;
72 66 }
73 67
74 68 $meta_result = $this->validate_meta( $variant['meta'] );
75 - $custom_css_result = $this->validate_custom_css( $variant );
76 69
77 70 $result->errors()->merge( $meta_result->errors(), 'meta' );
78 - $result->errors()->merge( $custom_css_result->errors(), 'custom_css' );
79 71
80 72 if ( $meta_result->is_valid() ) {
81 73 $variant_result = $props_parser->validate( $variant['props'] );
82 74
@@ -90,71 +82,8 @@
90 82
91 83 return $result->wrap( $validated_style );
92 84 }
93 85
94 - private function validate_style_label( string $label ): array {
95 - $label = strtolower( $label );
96 -
97 - $reserved_class_names = [ 'container' ];
98 -
99 - if ( strlen( $label ) > 50 ) {
100 - return [
101 - 'is_valid' => false,
102 - 'error_message' => 'class_name_too_long',
103 - ];
104 - }
105 -
106 - if ( strlen( $label ) < 2 ) {
107 - return [
108 - 'is_valid' => false,
109 - 'error_message' => 'class_name_too_short',
110 - ];
111 - }
112 -
113 - if ( in_array( $label, $reserved_class_names, true ) ) {
114 - return [
115 - 'is_valid' => false,
116 - 'error_message' => 'reserved_class_name',
117 - ];
118 - }
119 -
120 - $regexes = [
121 - [
122 - 'pattern' => '/^(|[^0-9].*)$/',
123 - 'message' => 'class_name_starts_with_digit',
124 - ],
125 - [
126 - 'pattern' => '/^\S*$/',
127 - 'message' => 'class_name_contains_spaces',
128 - ],
129 - [
130 - 'pattern' => '/^(|[a-zA-Z0-9_-]+)$/',
131 - 'message' => 'class_name_invalid_chars',
132 - ],
133 - [
134 - 'pattern' => '/^(?!--).*/',
135 - 'message' => 'class_name_double_hyphen',
136 - ],
137 - [
138 - 'pattern' => '/^(?!-[0-9])/',
139 - 'message' => 'class_name_starts_with_hyphen_digit',
140 - ],
141 - ];
142 -
143 - foreach ( $regexes as $rule ) {
144 - if ( ! preg_match( $rule['pattern'], $label ) ) {
145 - return [
146 - 'is_valid' => false,
147 - 'error_message' => $rule['message'],
148 - ];
149 - }
150 - }
151 - return [
152 - 'is_valid' => true,
153 - 'error_message' => null,
154 - ];
155 - }
156 -
157 86 private function validate_meta( $meta ): Parse_Result {
158 87 $result = Parse_Result::make();
159 88
160 89 if ( ! is_array( $meta ) ) {
@@ -162,9 +91,9 @@
162 91
163 92 return $result;
164 93 }
165 94
166 - if ( ! array_key_exists( 'state', $meta ) || ! Style_States::is_valid_state( $meta['state'] ) ) {
95 + if ( ! array_key_exists( 'state', $meta ) || ! in_array( $meta['state'], self::VALID_STATES, true ) ) {
167 96 $result->errors()->add( 'state', 'missing_or_invalid_value' );
168 97
169 98 return $result;
170 99 }
@@ -178,22 +107,8 @@
178 107
179 108 return $result;
180 109 }
181 110
182 - private function validate_custom_css( array $variant ): Parse_Result {
183 - $result = Parse_Result::make();
184 -
185 - if ( ! empty( $variant['custom_css']['raw'] ) && (
186 - ! is_string( $variant['custom_css']['raw'] ) ||
187 - null === Utils::decode_string( $variant['custom_css']['raw'], null )
188 - )
189 - ) {
190 - $result->errors()->add( 'custom_css', 'invalid_type' );
191 - }
192 -
193 - return $result;
194 - }
195 -
196 111 private function sanitize_meta( $meta ) {
197 112 if ( ! is_array( $meta ) ) {
198 113 return [];
199 114 }
@@ -204,20 +119,8 @@
204 119
205 120 return $meta;
206 121 }
207 122
208 - private function sanitize_custom_css( array $variant ) {
209 - if ( empty( $variant['custom_css']['raw'] ) ) {
210 - return null;
211 - }
212 -
213 - $custom_css = Utils::decode_string( $variant['custom_css']['raw'] );
214 - $custom_css = sanitize_textarea_field( $custom_css );
215 - $custom_css = [ 'raw' => Utils::encode_string( $custom_css ) ];
216 -
217 - return empty( $custom_css['raw'] ) ? null : $custom_css;
218 - }
219 -
220 123 /**
221 124 * @param array $style
222 125 * the style object to sanitize
223 126 */
@@ -231,17 +134,12 @@
231 134 if ( isset( $style['id'] ) ) {
232 135 $style['id'] = sanitize_key( $style['id'] );
233 136 }
234 137
235 - if ( isset( $style['sync_to_v3'] ) ) {
236 - $style['sync_to_v3'] = (bool) $style['sync_to_v3'];
237 - }
238 -
239 138 if ( ! empty( $style['variants'] ) ) {
240 139 foreach ( $style['variants'] as $variant_index => $variant ) {
241 140 $style['variants'][ $variant_index ]['props'] = $props_parser->sanitize( $variant['props'] )->unwrap();
242 141 $style['variants'][ $variant_index ]['meta'] = $this->sanitize_meta( $variant['meta'] );
243 - $style['variants'][ $variant_index ]['custom_css'] = $this->sanitize_custom_css( $variant );
244 142 }
245 143 }
246 144
247 145 return Parse_Result::make()->wrap( $style );