| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\AtomicWidgets\CssConverter; |
| 4 |
|
| 5 |
use Elementor\Modules\AtomicWidgets\CssConverter\ValueParsers\Css_Token_Splitter; |
| 6 |
use Elementor\Modules\Variables\Adapters\Prop_Type_Adapter; |
| 7 |
use Elementor\Modules\Variables\PropTypes\Size_Variable_Prop_Type; |
| 8 |
use Elementor\Modules\Variables\Services\Variables_Service; |
| 9 |
use Elementor\Modules\Variables\Utils\Variable_Type_Keys; |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; // Exit if accessed directly. |
| 13 |
} |
| 14 |
|
| 15 |
class Css_Var_Token_Resolver { |
| 16 |
public static function is_var_only_token( string $token ): bool { |
| 17 |
$token = trim( $token ); |
| 18 |
|
| 19 |
if ( null === Css_Var_Reference::parse( $token ) ) { |
| 20 |
return false; |
| 21 |
} |
| 22 |
|
| 23 |
return 1 === count( Css_Token_Splitter::split_by_whitespace( $token ) ); |
| 24 |
} |
| 25 |
|
| 26 |
public static function resolve_var_only_token_type( ?Variables_Service $service, string $token ): ?string { |
| 27 |
if ( ! self::is_var_only_token( $token ) || null === $service ) { |
| 28 |
return null; |
| 29 |
} |
| 30 |
|
| 31 |
$reference = Css_Var_Reference::parse( trim( $token ) ); |
| 32 |
|
| 33 |
if ( null === $reference ) { |
| 34 |
return null; |
| 35 |
} |
| 36 |
|
| 37 |
$variable = $service->find_by_label_or_id( $reference ); |
| 38 |
|
| 39 |
if ( null === $variable ) { |
| 40 |
return null; |
| 41 |
} |
| 42 |
|
| 43 |
return Variable_Type_Keys::get_resolved_type( $variable['type'] ?? '' ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* If $token is a var-only token that resolves to a known size variable, returns the ready-made |
| 48 |
* size-variable PropValue `['$$type' => ..., 'value' => $id]`. Returns null if the token is not |
| 49 |
* a var, the variable is unknown, or the variable type is not a size (caller should then fall |
| 50 |
* back to the raw Size leaf or decline to customCss based on context). |
| 51 |
* |
| 52 |
* @return array{$$type: string, value: string}|null |
| 53 |
*/ |
| 54 |
public static function resolve_size_var_prop_value( ?Variables_Service $service, string $token ): ?array { |
| 55 |
if ( 'size' !== self::resolve_var_only_token_type( $service, $token ) ) { |
| 56 |
return null; |
| 57 |
} |
| 58 |
|
| 59 |
$reference = Css_Var_Reference::parse( trim( $token ) ); |
| 60 |
$variable = $service->find_by_label_or_id( $reference ); |
| 61 |
$id = $variable['id'] ?? ''; |
| 62 |
|
| 63 |
if ( '' === $id ) { |
| 64 |
return null; |
| 65 |
} |
| 66 |
|
| 67 |
$prop_type_key = Prop_Type_Adapter::GLOBAL_CUSTOM_SIZE_VARIABLE_KEY === ( $variable['type'] ?? '' ) |
| 68 |
? Size_Variable_Prop_Type::get_key() |
| 69 |
: ( $variable['type'] ?? '' ); |
| 70 |
|
| 71 |
return [ |
| 72 |
'$$type' => $prop_type_key, |
| 73 |
'value' => $id, |
| 74 |
]; |
| 75 |
} |
| 76 |
} |
| 77 |
|