elementor
/
modules
/
atomic-widgets
/
css-converter
/
converters
/
background-position-property-converter.php
background-position-property-converter.php in Elementor Website Builder – more than just a page builder 4.3.2, at modules/atomic-widgets/css-converter/converters/background-position-property-converter.php
| 1 | <?php |
| 2 | |
| 3 | namespace Elementor\Modules\AtomicWidgets\CssConverter\Converters; |
| 4 | |
| 5 | use Elementor\Modules\AtomicWidgets\CssConverter\ValueParsers\Css_Token_Splitter; |
| 6 | use Elementor\Modules\AtomicWidgets\PropTypes\Background_Image_Position_Offset_Prop_Type; |
| 7 | use Elementor\Modules\AtomicWidgets\PropTypes\Position_Prop_Type; |
| 8 | |
| 9 | if ( ! defined( 'ABSPATH' ) ) { |
| 10 | exit; // Exit if accessed directly. |
| 11 | } |
| 12 | |
| 13 | /** |
| 14 | * Converter for `background-position`. |
| 15 | * |
| 16 | * The schema accepts only two shapes for a position value: |
| 17 | * - String enum (one of Position_Prop_Type::get_position_enum_values()) |
| 18 | * - Background_Image_Position_Offset_Prop_Type { x: Size, y: Size } |
| 19 | * |
| 20 | * This subclass adds keyword normalization to the generic Background_Layer_Field_Converter |
| 21 | * so common LLM-emitted inputs reach the enum branch instead of declining: |
| 22 | * - single keyword: center -> "center center", top -> "top center", left -> "center left", ... |
| 23 | * - swapped pair: left top -> "top left" (enum is y-then-x ordered) |
| 24 | * |
| 25 | * Anything that cannot be normalized to an enum string or to two parseable Sizes |
| 26 | * (e.g. center 20%, bottom 4px, anything containing calc()) declines to custom_css. |
| 27 | */ |
| 28 | class Background_Position_Property_Converter extends Background_Layer_Field_Converter { |
| 29 | const SINGLE_KEYWORD_TO_PAIR = [ |
| 30 | 'center' => 'center center', |
| 31 | 'top' => 'top center', |
| 32 | 'bottom' => 'bottom center', |
| 33 | 'left' => 'center left', |
| 34 | 'right' => 'center right', |
| 35 | ]; |
| 36 | |
| 37 | const X_ONLY_KEYWORDS = [ 'left', 'right' ]; |
| 38 | const Y_ONLY_KEYWORDS = [ 'top', 'bottom' ]; |
| 39 | |
| 40 | public function __construct() { |
| 41 | parent::__construct( |
| 42 | 'background-position', |
| 43 | 'position', |
| 44 | Position_Prop_Type::get_position_enum_values(), |
| 45 | Background_Image_Position_Offset_Prop_Type::class, |
| 46 | [ 'x', 'y' ] |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | protected function parse_token( string $token ): ?array { |
| 51 | return parent::parse_token( $this->normalize_keywords( trim( $token ) ) ); |
| 52 | } |
| 53 | |
| 54 | private function normalize_keywords( string $token ): string { |
| 55 | $parts = Css_Token_Splitter::split_by_whitespace( $token ); |
| 56 | |
| 57 | if ( 1 === count( $parts ) ) { |
| 58 | $lower = strtolower( $parts[0] ); |
| 59 | |
| 60 | return self::SINGLE_KEYWORD_TO_PAIR[ $lower ] ?? $token; |
| 61 | } |
| 62 | |
| 63 | if ( 2 === count( $parts ) ) { |
| 64 | $first = strtolower( $parts[0] ); |
| 65 | $second = strtolower( $parts[1] ); |
| 66 | |
| 67 | if ( in_array( $first, self::X_ONLY_KEYWORDS, true ) && in_array( $second, self::Y_ONLY_KEYWORDS, true ) ) { |
| 68 | return $second . ' ' . $first; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | return $token; |
| 73 | } |
| 74 | } |
| 75 |