| 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\Background_Image_Value_Parser; |
| 8 |
use Elementor\Modules\AtomicWidgets\PropTypes\Background_Overlay_Prop_Type; |
| 9 |
use Elementor\Modules\AtomicWidgets\PropTypes\Background_Prop_Type; |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; // Exit if accessed directly. |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Converter for `background-image`. Delegates parsing to Background_Image_Value_Parser which returns |
| 17 |
* fully-constructed overlay PropValues (image overlays for url(), gradient overlays for linear/radial- |
| 18 |
* gradient()). The resulting ordered list is stored as `background-overlay` in the `background` |
| 19 |
* aggregate context object, preserving any existing scalar fields (color, clip). |
| 20 |
* |
| 21 |
* Sibling background longhands processed afterwards (background-repeat, background-size, etc.) read |
| 22 |
* and update the image-overlay items via Background_Layer_Field_Converter. |
| 23 |
*/ |
| 24 |
class Background_Image_Converter extends Property_Converter_Base { |
| 25 |
protected function get_supported_properties(): array { |
| 26 |
return [ 'background-image' ]; |
| 27 |
} |
| 28 |
|
| 29 |
protected function convert_null( Conversion_Context $context, array $rule ): bool { |
| 30 |
$fields = $this->current_background_fields( $context->get_prop( 'background' ) ); |
| 31 |
$fields['background-overlay'] = null; |
| 32 |
|
| 33 |
$context->set_prop( 'background', Background_Prop_Type::generate( $fields ) ); |
| 34 |
|
| 35 |
return true; |
| 36 |
} |
| 37 |
|
| 38 |
protected function do_convert( Conversion_Context $context, array $rule ): bool { |
| 39 |
$overlays = Background_Image_Value_Parser::parse( trim( $rule['value'] ) ); |
| 40 |
|
| 41 |
if ( null === $overlays ) { |
| 42 |
return false; |
| 43 |
} |
| 44 |
|
| 45 |
$fields = $this->current_background_fields( $context->get_prop( 'background' ) ); |
| 46 |
$fields['background-overlay'] = Background_Overlay_Prop_Type::generate( $overlays ); |
| 47 |
|
| 48 |
$context->set_prop( 'background', Background_Prop_Type::generate( $fields ) ); |
| 49 |
|
| 50 |
return true; |
| 51 |
} |
| 52 |
|
| 53 |
private function current_background_fields( $existing ): array { |
| 54 |
if ( ! is_array( $existing ) ) { |
| 55 |
return []; |
| 56 |
} |
| 57 |
|
| 58 |
$type = $existing['$$type'] ?? null; |
| 59 |
|
| 60 |
if ( Background_Prop_Type::get_key() === $type && is_array( $existing['value'] ?? null ) ) { |
| 61 |
return $existing['value']; |
| 62 |
} |
| 63 |
|
| 64 |
return []; |
| 65 |
} |
| 66 |
} |
| 67 |
|