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