← All changes
|
modules/atomic-widgets/parsers/style-parser.php
+9
-38
4.2.0-beta2
→
3.31.4
View file →
| @@ -5,13 +5,11 @@ | ||
| 5 | 5 | if ( ! defined( 'ABSPATH' ) ) { |
| 6 | 6 | exit; // Exit if accessed directly. |
| 7 | 7 | } |
| 8 | 8 | |
| 9 | -use Elementor\Modules\AtomicWidgets\OptIn\Opt_In; | |
| 9 | +use Elementor\Modules\AtomicWidgets\Module; | |
| 10 | +use Elementor\Modules\AtomicWidgets\Opt_In; | |
| 10 | 11 | use Elementor\Plugin; |
| 11 | -use Elementor\Utils; | |
| 12 | -use Elementor\Core\Utils\Api\Parse_Result; | |
| 13 | -use Elementor\Modules\AtomicWidgets\Styles\Style_States; | |
| 14 | 12 | |
| 15 | 13 | class Style_Parser { |
| 16 | 14 | const VALID_TYPES = [ |
| 17 | 15 | 'class', |
| @@ -16,8 +14,14 @@ | ||
| 16 | 14 | const VALID_TYPES = [ |
| 17 | 15 | 'class', |
| 18 | 16 | ]; |
| 19 | 17 | |
| 18 | + const VALID_STATES = [ | |
| 19 | + 'hover', | |
| 20 | + 'active', | |
| 21 | + 'focus', | |
| 22 | + null, | |
| 23 | + ]; | |
| 20 | 24 | |
| 21 | 25 | private array $schema; |
| 22 | 26 | |
| 23 | 27 | public function __construct( array $schema ) { |
| @@ -71,12 +75,10 @@ | ||
| 71 | 75 | continue; |
| 72 | 76 | } |
| 73 | 77 | |
| 74 | 78 | $meta_result = $this->validate_meta( $variant['meta'] ); |
| 75 | - $custom_css_result = $this->validate_custom_css( $variant ); | |
| 76 | 79 | |
| 77 | 80 | $result->errors()->merge( $meta_result->errors(), 'meta' ); |
| 78 | - $result->errors()->merge( $custom_css_result->errors(), 'custom_css' ); | |
| 79 | 81 | |
| 80 | 82 | if ( $meta_result->is_valid() ) { |
| 81 | 83 | $variant_result = $props_parser->validate( $variant['props'] ); |
| 82 | 84 | |
| @@ -162,9 +164,9 @@ | ||
| 162 | 164 | |
| 163 | 165 | return $result; |
| 164 | 166 | } |
| 165 | 167 | |
| 166 | - if ( ! array_key_exists( 'state', $meta ) || ! Style_States::is_valid_state( $meta['state'] ) ) { | |
| 168 | + if ( ! array_key_exists( 'state', $meta ) || ! in_array( $meta['state'], self::VALID_STATES, true ) ) { | |
| 167 | 169 | $result->errors()->add( 'state', 'missing_or_invalid_value' ); |
| 168 | 170 | |
| 169 | 171 | return $result; |
| 170 | 172 | } |
| @@ -178,22 +180,8 @@ | ||
| 178 | 180 | |
| 179 | 181 | return $result; |
| 180 | 182 | } |
| 181 | 183 | |
| 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 | 184 | private function sanitize_meta( $meta ) { |
| 197 | 185 | if ( ! is_array( $meta ) ) { |
| 198 | 186 | return []; |
| 199 | 187 | } |
| @@ -204,20 +192,8 @@ | ||
| 204 | 192 | |
| 205 | 193 | return $meta; |
| 206 | 194 | } |
| 207 | 195 | |
| 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 | 196 | /** |
| 221 | 197 | * @param array $style |
| 222 | 198 | * the style object to sanitize |
| 223 | 199 | */ |
| @@ -231,17 +207,12 @@ | ||
| 231 | 207 | if ( isset( $style['id'] ) ) { |
| 232 | 208 | $style['id'] = sanitize_key( $style['id'] ); |
| 233 | 209 | } |
| 234 | 210 | |
| 235 | - if ( isset( $style['sync_to_v3'] ) ) { | |
| 236 | - $style['sync_to_v3'] = (bool) $style['sync_to_v3']; | |
| 237 | - } | |
| 238 | - | |
| 239 | 211 | if ( ! empty( $style['variants'] ) ) { |
| 240 | 212 | foreach ( $style['variants'] as $variant_index => $variant ) { |
| 241 | 213 | $style['variants'][ $variant_index ]['props'] = $props_parser->sanitize( $variant['props'] )->unwrap(); |
| 242 | 214 | $style['variants'][ $variant_index ]['meta'] = $this->sanitize_meta( $variant['meta'] ); |
| 243 | - $style['variants'][ $variant_index ]['custom_css'] = $this->sanitize_custom_css( $variant ); | |
| 244 | 215 | } |
| 245 | 216 | } |
| 246 | 217 | |
| 247 | 218 | return Parse_Result::make()->wrap( $style ); |