| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\AtomicWidgets\Styles; |
| 4 |
|
| 5 |
use Elementor\Utils; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; |
| 9 |
} |
| 10 |
|
| 11 |
/** |
| 12 |
* A single stored variant on a local style (breakpoint + optional pseudo-state) |
| 13 |
* that knows how to render itself into the MCP round-trip CSS format. |
| 14 |
* |
| 15 |
* Wrapping in `@media(--<breakpoint>) { ... }` is intentionally not this |
| 16 |
* object's responsibility — a variant has no visibility into its siblings, so |
| 17 |
* merging by breakpoint lives on `Local_Style`. |
| 18 |
*/ |
| 19 |
class Local_Style_Variant { |
| 20 |
|
| 21 |
const PSEUDO_STATES = [ 'hover', 'focus', 'active' ]; |
| 22 |
|
| 23 |
private array $meta; |
| 24 |
private array $props; |
| 25 |
private string $custom_css; |
| 26 |
|
| 27 |
private function __construct( array $meta, array $props, string $custom_css ) { |
| 28 |
$this->meta = $meta; |
| 29 |
$this->props = $props; |
| 30 |
$this->custom_css = $custom_css; |
| 31 |
} |
| 32 |
|
| 33 |
public static function from_array( array $variant ): self { |
| 34 |
return new self( |
| 35 |
is_array( $variant['meta'] ?? null ) ? $variant['meta'] : [], |
| 36 |
is_array( $variant['props'] ?? null ) ? $variant['props'] : [], |
| 37 |
Utils::decode_string( $variant['custom_css']['raw'] ?? '', '' ) |
| 38 |
); |
| 39 |
} |
| 40 |
|
| 41 |
public function breakpoint(): string { |
| 42 |
return $this->meta['breakpoint'] ?? Local_Style::DESKTOP_BREAKPOINT; |
| 43 |
} |
| 44 |
|
| 45 |
public function to_css_fragment(): string { |
| 46 |
$declarations = $this->render_declarations(); |
| 47 |
|
| 48 |
if ( '' === $declarations ) { |
| 49 |
return ''; |
| 50 |
} |
| 51 |
|
| 52 |
$state = $this->meta['state'] ?? null; |
| 53 |
|
| 54 |
if ( null === $state ) { |
| 55 |
return $declarations; |
| 56 |
} |
| 57 |
|
| 58 |
if ( ! in_array( $state, self::PSEUDO_STATES, true ) ) { |
| 59 |
return ''; |
| 60 |
} |
| 61 |
|
| 62 |
return sprintf( '&:%s { %s }', $state, $declarations ); |
| 63 |
} |
| 64 |
|
| 65 |
private function render_declarations(): string { |
| 66 |
$parts = []; |
| 67 |
|
| 68 |
foreach ( Style_Props_To_Css::to_map( $this->props ) as $prop => $value ) { |
| 69 |
$parts[] = $prop . ': ' . $value . ';'; |
| 70 |
} |
| 71 |
|
| 72 |
if ( '' !== $this->custom_css ) { |
| 73 |
$parts[] = trim( $this->custom_css ); |
| 74 |
} |
| 75 |
|
| 76 |
return implode( ' ', $parts ); |
| 77 |
} |
| 78 |
} |
| 79 |
|