| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\Variables\Classes; |
| 4 |
|
| 5 |
use Elementor\Modules\Variables\Services\Variables_Service; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; // Exit if accessed directly. |
| 9 |
} |
| 10 |
|
| 11 |
class CSS_Renderer { |
| 12 |
private Variables_Service $service; |
| 13 |
|
| 14 |
public function __construct( Variables_Service $service ) { |
| 15 |
$this->service = $service; |
| 16 |
} |
| 17 |
|
| 18 |
private function global_variables(): array { |
| 19 |
return $this->service->get_variables_list(); |
| 20 |
} |
| 21 |
|
| 22 |
public function raw_css(): string { |
| 23 |
$list_of_variables = $this->global_variables(); |
| 24 |
|
| 25 |
if ( empty( $list_of_variables ) ) { |
| 26 |
return ''; |
| 27 |
} |
| 28 |
|
| 29 |
$css_entries = $this->css_entries_for( $list_of_variables ); |
| 30 |
|
| 31 |
if ( empty( $css_entries ) ) { |
| 32 |
return ''; |
| 33 |
} |
| 34 |
|
| 35 |
return $this->wrap_with_root( $css_entries ); |
| 36 |
} |
| 37 |
|
| 38 |
private function css_entries_for( array $list_of_variables ): array { |
| 39 |
$entries = []; |
| 40 |
|
| 41 |
foreach ( $list_of_variables as $variable_id => $variable ) { |
| 42 |
$entry = $this->build_css_variable_entry( $variable_id, $variable ); |
| 43 |
|
| 44 |
if ( empty( $entry ) ) { |
| 45 |
continue; |
| 46 |
} |
| 47 |
|
| 48 |
$entries[] = $entry; |
| 49 |
} |
| 50 |
|
| 51 |
return $entries; |
| 52 |
} |
| 53 |
|
| 54 |
private function build_css_variable_entry( string $id, array $variable ): ?string { |
| 55 |
$variable_name = sanitize_text_field( $id ); |
| 56 |
|
| 57 |
if ( ! array_key_exists( 'deleted_at', $variable ) ) { |
| 58 |
$variable_name = sanitize_text_field( $variable['label'] ?? '' ); |
| 59 |
} |
| 60 |
|
| 61 |
$value = sanitize_text_field( $variable['value'] ?? '' ); |
| 62 |
|
| 63 |
if ( empty( $value ) || empty( $variable_name ) ) { |
| 64 |
return null; |
| 65 |
} |
| 66 |
|
| 67 |
$entry = "--{$variable_name}:{$value};"; |
| 68 |
|
| 69 |
$additional = apply_filters( 'elementor/variables/css_entry_additional', '', $variable, $id ); |
| 70 |
|
| 71 |
return $entry . $additional; |
| 72 |
} |
| 73 |
|
| 74 |
private function wrap_with_root( array $css_entries ): string { |
| 75 |
return ':root { ' . implode( ' ', $css_entries ) . ' }'; |
| 76 |
} |
| 77 |
} |
| 78 |
|