| 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\Size_Value_Parser; |
| 8 |
use Elementor\Modules\AtomicWidgets\PropTypes\Size_Prop_Type; |
| 9 |
use Elementor\Modules\AtomicWidgets\Styles\Size_Constants; |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; // Exit if accessed directly. |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Reusable converter for properties backed by a Size_Prop_Type. One instance per property. |
| 17 |
* Delegates value parsing to Size_Value_Parser; a null parse declines (-> custom_css). On success |
| 18 |
* it emits the canonical Size PropValue from generate(). $allow_unitless opts a property into |
| 19 |
* keeping unitless multipliers (e.g. line-height: 1.1) instead of declining them. |
| 20 |
*/ |
| 21 |
class Size_Property_Converter extends Property_Converter_Base { |
| 22 |
private string $property; |
| 23 |
|
| 24 |
private bool $allow_unitless; |
| 25 |
|
| 26 |
public function __construct( string $property, bool $allow_unitless = false ) { |
| 27 |
$this->property = $property; |
| 28 |
$this->allow_unitless = $allow_unitless; |
| 29 |
} |
| 30 |
|
| 31 |
protected function get_supported_properties(): array { |
| 32 |
return [ $this->property ]; |
| 33 |
} |
| 34 |
|
| 35 |
protected function get_custom_converter( Conversion_Context $context, array $rule ): ?callable { |
| 36 |
if ( 'opacity' !== $rule['property'] || ! preg_match( '/^-?\d*\.?\d+$/', $rule['value'] ?? '' ) ) { |
| 37 |
return null; |
| 38 |
} |
| 39 |
|
| 40 |
return function() use ( $context, $rule ) { |
| 41 |
$constrained = max( 0.0, min( (float) $rule['value'], 1.0 ) ); |
| 42 |
$size = round( $constrained * 100, 4 ); |
| 43 |
|
| 44 |
$context->set_prop( 'opacity', Size_Prop_Type::generate( [ |
| 45 |
'size' => 0.0 === $size ? 0 : $size, |
| 46 |
'unit' => Size_Constants::UNIT_PERCENT, |
| 47 |
] ) ); |
| 48 |
|
| 49 |
return true; |
| 50 |
}; |
| 51 |
} |
| 52 |
|
| 53 |
protected function do_convert( Conversion_Context $context, array $rule ): bool { |
| 54 |
$parsed = Size_Value_Parser::parse( $rule['value'], $this->allow_unitless ); |
| 55 |
|
| 56 |
if ( null === $parsed ) { |
| 57 |
return false; |
| 58 |
} |
| 59 |
|
| 60 |
$context->set_prop( $this->property, Size_Prop_Type::generate( $parsed ) ); |
| 61 |
|
| 62 |
return true; |
| 63 |
} |
| 64 |
} |
| 65 |
|