| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\DesignSystemSync\Classes; |
| 4 |
|
| 5 |
use Elementor\Core\Breakpoints\Manager as Breakpoints_Manager; |
| 6 |
use Elementor\Core\Files\Base as Base_File; |
| 7 |
use Elementor\Plugin; |
| 8 |
use Elementor\Stylesheet; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
class Stylesheet_Manager extends Base_File { |
| 15 |
const FILE_NAME = 'design-system-sync.css'; |
| 16 |
const DEFAULT_FILES_DIR = 'design-system-sync/'; |
| 17 |
const META_KEY = '_elementor_design_system_sync_css_meta'; |
| 18 |
|
| 19 |
public function __construct() { |
| 20 |
parent::__construct( self::FILE_NAME ); |
| 21 |
} |
| 22 |
|
| 23 |
public function generate(): ?array { |
| 24 |
$this->update(); |
| 25 |
|
| 26 |
if ( ! file_exists( $this->get_path() ) ) { |
| 27 |
return null; |
| 28 |
} |
| 29 |
|
| 30 |
return [ |
| 31 |
'url' => $this->get_url(), |
| 32 |
'version' => $this->get_meta( 'time' ), |
| 33 |
]; |
| 34 |
} |
| 35 |
|
| 36 |
public function enqueue(): void { |
| 37 |
if ( ! file_exists( $this->get_path() ) ) { |
| 38 |
$this->generate(); |
| 39 |
} |
| 40 |
|
| 41 |
if ( ! file_exists( $this->get_path() ) ) { |
| 42 |
return; |
| 43 |
} |
| 44 |
|
| 45 |
wp_enqueue_style( |
| 46 |
'elementor-design-system-sync', |
| 47 |
$this->get_url(), |
| 48 |
[], |
| 49 |
$this->get_meta( 'time' ) |
| 50 |
); |
| 51 |
} |
| 52 |
|
| 53 |
protected function parse_content(): string { |
| 54 |
$stylesheet = new Stylesheet(); |
| 55 |
|
| 56 |
$breakpoints = Plugin::$instance->breakpoints->get_active_breakpoints(); |
| 57 |
|
| 58 |
foreach ( $breakpoints as $breakpoint_name => $breakpoint ) { |
| 59 |
$stylesheet->add_device( $breakpoint_name, $breakpoint->get_value() ); |
| 60 |
} |
| 61 |
|
| 62 |
$color_entries = Variables_Provider::get_synced_color_css_entries(); |
| 63 |
|
| 64 |
if ( ! empty( $color_entries ) ) { |
| 65 |
$stylesheet->add_raw_css( ':root { ' . implode( ' ', $color_entries ) . ' }' ); |
| 66 |
} |
| 67 |
|
| 68 |
$typography_entries = Classes_Provider::get_synced_typography_css_entries(); |
| 69 |
|
| 70 |
foreach ( $typography_entries as $device => $entries ) { |
| 71 |
$css = ':root { ' . implode( ' ', $entries ) . ' }'; |
| 72 |
$device_key = ( Breakpoints_Manager::BREAKPOINT_KEY_DESKTOP === $device ) ? '' : $device; |
| 73 |
|
| 74 |
$stylesheet->add_raw_css( $css, $device_key ); |
| 75 |
} |
| 76 |
|
| 77 |
return (string) $stylesheet; |
| 78 |
} |
| 79 |
} |
| 80 |
|