← All changes
|
modules/atomic-widgets/parsers/style-parser.php
+9
-39
4.2.0-beta2
→
3.30.1
View file →
| @@ -5,13 +5,10 @@ | ||
| 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 | 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 | 11 | |
| 15 | 12 | class Style_Parser { |
| 16 | 13 | const VALID_TYPES = [ |
| 17 | 14 | 'class', |
| @@ -16,8 +13,14 @@ | ||
| 16 | 13 | const VALID_TYPES = [ |
| 17 | 14 | 'class', |
| 18 | 15 | ]; |
| 19 | 16 | |
| 17 | + const VALID_STATES = [ | |
| 18 | + 'hover', | |
| 19 | + 'active', | |
| 20 | + 'focus', | |
| 21 | + null, | |
| 22 | + ]; | |
| 20 | 23 | |
| 21 | 24 | private array $schema; |
| 22 | 25 | |
| 23 | 26 | public function __construct( array $schema ) { |
| @@ -45,9 +48,9 @@ | ||
| 45 | 48 | } |
| 46 | 49 | |
| 47 | 50 | if ( ! isset( $style['label'] ) || ! is_string( $style['label'] ) ) { |
| 48 | 51 | $result->errors()->add( 'label', 'missing_or_invalid' ); |
| 49 | - } elseif ( Plugin::$instance->experiments->is_feature_active( Opt_In::EXPERIMENT_NAME ) ) { | |
| 52 | + } elseif ( Plugin::$instance->experiments->is_feature_active( Module::EXPERIMENT_VERSION_3_30 ) ) { | |
| 50 | 53 | $label_validation = $this->validate_style_label( $style['label'] ); |
| 51 | 54 | |
| 52 | 55 | if ( ! $label_validation['is_valid'] ) { |
| 53 | 56 | $result->errors()->add( 'label', $label_validation['error_message'] ); |
| @@ -71,12 +74,10 @@ | ||
| 71 | 74 | continue; |
| 72 | 75 | } |
| 73 | 76 | |
| 74 | 77 | $meta_result = $this->validate_meta( $variant['meta'] ); |
| 75 | - $custom_css_result = $this->validate_custom_css( $variant ); | |
| 76 | 78 | |
| 77 | 79 | $result->errors()->merge( $meta_result->errors(), 'meta' ); |
| 78 | - $result->errors()->merge( $custom_css_result->errors(), 'custom_css' ); | |
| 79 | 80 | |
| 80 | 81 | if ( $meta_result->is_valid() ) { |
| 81 | 82 | $variant_result = $props_parser->validate( $variant['props'] ); |
| 82 | 83 | |
| @@ -162,9 +163,9 @@ | ||
| 162 | 163 | |
| 163 | 164 | return $result; |
| 164 | 165 | } |
| 165 | 166 | |
| 166 | - if ( ! array_key_exists( 'state', $meta ) || ! Style_States::is_valid_state( $meta['state'] ) ) { | |
| 167 | + if ( ! array_key_exists( 'state', $meta ) || ! in_array( $meta['state'], self::VALID_STATES, true ) ) { | |
| 167 | 168 | $result->errors()->add( 'state', 'missing_or_invalid_value' ); |
| 168 | 169 | |
| 169 | 170 | return $result; |
| 170 | 171 | } |
| @@ -178,22 +179,8 @@ | ||
| 178 | 179 | |
| 179 | 180 | return $result; |
| 180 | 181 | } |
| 181 | 182 | |
| 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 | 183 | private function sanitize_meta( $meta ) { |
| 197 | 184 | if ( ! is_array( $meta ) ) { |
| 198 | 185 | return []; |
| 199 | 186 | } |
| @@ -204,20 +191,8 @@ | ||
| 204 | 191 | |
| 205 | 192 | return $meta; |
| 206 | 193 | } |
| 207 | 194 | |
| 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 | 195 | /** |
| 221 | 196 | * @param array $style |
| 222 | 197 | * the style object to sanitize |
| 223 | 198 | */ |
| @@ -231,17 +206,12 @@ | ||
| 231 | 206 | if ( isset( $style['id'] ) ) { |
| 232 | 207 | $style['id'] = sanitize_key( $style['id'] ); |
| 233 | 208 | } |
| 234 | 209 | |
| 235 | - if ( isset( $style['sync_to_v3'] ) ) { | |
| 236 | - $style['sync_to_v3'] = (bool) $style['sync_to_v3']; | |
| 237 | - } | |
| 238 | - | |
| 239 | 210 | if ( ! empty( $style['variants'] ) ) { |
| 240 | 211 | foreach ( $style['variants'] as $variant_index => $variant ) { |
| 241 | 212 | $style['variants'][ $variant_index ]['props'] = $props_parser->sanitize( $variant['props'] )->unwrap(); |
| 242 | 213 | $style['variants'][ $variant_index ]['meta'] = $this->sanitize_meta( $variant['meta'] ); |
| 243 | - $style['variants'][ $variant_index ]['custom_css'] = $this->sanitize_custom_css( $variant ); | |
| 244 | 214 | } |
| 245 | 215 | } |
| 246 | 216 | |
| 247 | 217 | return Parse_Result::make()->wrap( $style ); |