elementor
/
modules
/
atomic-widgets
/
css-converter
/
converters
/
dimensions-property-converter.php
dimensions-property-converter.php in Elementor Website Builder – more than just a page builder 4.3.1, at modules/atomic-widgets/css-converter/converters/dimensions-property-converter.php
| 1 | <?php |
| 2 | |
| 3 | namespace Elementor\Modules\AtomicWidgets\CssConverter\Converters; |
| 4 | |
| 5 | use Elementor\Modules\AtomicWidgets\CssConverter\Conversion_Context; |
| 6 | use Elementor\Modules\AtomicWidgets\CssConverter\Property_Converter_Base; |
| 7 | use Elementor\Modules\AtomicWidgets\CssConverter\ValueParsers\Box_Shorthand_Parser; |
| 8 | use Elementor\Modules\AtomicWidgets\PropTypes\Dimensions_Prop_Type; |
| 9 | use Elementor\Modules\AtomicWidgets\PropTypes\Size_Prop_Type; |
| 10 | |
| 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | exit; // Exit if accessed directly. |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * Reusable converter for box shorthands backed by Union(<sides object> | Size): padding/margin |
| 17 | * (Dimensions) and border-width (Border_Width) all share the four logical sides, differing only by the |
| 18 | * wrapping object prop type, which is injected. One instance per property. Delegates |
| 19 | * tokenizing/expansion to Box_Shorthand_Parser; a null parse declines (-> custom_css). |
| 20 | * |
| 21 | * A single token emits the Size member (the union accepts it). Two-to-four tokens expand via the CSS |
| 22 | * box rule and map physical->logical (top=block-start, right=inline-end, bottom=block-end, |
| 23 | * left=inline-start) into the injected object's PropValue. |
| 24 | */ |
| 25 | class Dimensions_Property_Converter extends Property_Converter_Base { |
| 26 | private string $property; |
| 27 | |
| 28 | private string $object_prop_type; |
| 29 | |
| 30 | /** |
| 31 | * @param string $property The CSS property this instance owns. |
| 32 | * @param string $object_prop_type Object_Prop_Type class used to wrap the four expanded sides. |
| 33 | */ |
| 34 | public function __construct( string $property, string $object_prop_type = Dimensions_Prop_Type::class ) { |
| 35 | $this->property = $property; |
| 36 | $this->object_prop_type = $object_prop_type; |
| 37 | } |
| 38 | |
| 39 | protected function get_supported_properties(): array { |
| 40 | return [ $this->property ]; |
| 41 | } |
| 42 | |
| 43 | protected function do_convert( Conversion_Context $context, array $rule ): bool { |
| 44 | $parsed = Box_Shorthand_Parser::parse( $rule['value'] ); |
| 45 | |
| 46 | if ( null === $parsed ) { |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | if ( isset( $parsed['single'] ) ) { |
| 51 | $context->set_prop( $this->property, Size_Prop_Type::generate( $parsed['single'] ) ); |
| 52 | |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | [ $top, $right, $bottom, $left ] = $parsed['sides']; |
| 57 | |
| 58 | $context->set_prop( $this->property, ( $this->object_prop_type )::generate( [ |
| 59 | 'block-start' => Size_Prop_Type::generate( $top ), |
| 60 | 'inline-end' => Size_Prop_Type::generate( $right ), |
| 61 | 'block-end' => Size_Prop_Type::generate( $bottom ), |
| 62 | 'inline-start' => Size_Prop_Type::generate( $left ), |
| 63 | ] ) ); |
| 64 | |
| 65 | return true; |
| 66 | } |
| 67 | } |
| 68 |