| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\DesignSystemSync\Classes; |
| 4 |
|
| 5 |
use Elementor\Modules\DesignSystemSync\Module; |
| 6 |
use Elementor\Modules\Variables\Services\Batch_Operations\Batch_Processor; |
| 7 |
use Elementor\Modules\Variables\Services\Variables_Service; |
| 8 |
use Elementor\Modules\Variables\Storage\Variables_Repository; |
| 9 |
use Elementor\Plugin; |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
class Variables_Provider { |
| 16 |
private static $cached_variables = null; |
| 17 |
|
| 18 |
public static function get_all_variables(): array { |
| 19 |
if ( null !== self::$cached_variables ) { |
| 20 |
return self::$cached_variables; |
| 21 |
} |
| 22 |
|
| 23 |
$kit = Plugin::$instance->kits_manager->get_active_kit(); |
| 24 |
|
| 25 |
if ( ! $kit ) { |
| 26 |
return []; |
| 27 |
} |
| 28 |
|
| 29 |
$repository = new Variables_Repository( $kit ); |
| 30 |
$service = new Variables_Service( $repository, new Batch_Processor() ); |
| 31 |
self::$cached_variables = $service->get_variables_list(); |
| 32 |
|
| 33 |
return self::$cached_variables; |
| 34 |
} |
| 35 |
|
| 36 |
public static function get_synced_color_variables(): array { |
| 37 |
$all_variables = self::get_all_variables(); |
| 38 |
$color_variables = []; |
| 39 |
|
| 40 |
foreach ( $all_variables as $id => $variable ) { |
| 41 |
if ( isset( $variable['deleted'] ) && $variable['deleted'] ) { |
| 42 |
continue; |
| 43 |
} |
| 44 |
|
| 45 |
if ( empty( $variable['type'] ) || 'global-color-variable' !== $variable['type'] ) { |
| 46 |
continue; |
| 47 |
} |
| 48 |
|
| 49 |
if ( empty( $variable['sync_to_v3'] ) ) { |
| 50 |
continue; |
| 51 |
} |
| 52 |
|
| 53 |
$color_variables[ $id ] = $variable; |
| 54 |
} |
| 55 |
return $color_variables; |
| 56 |
} |
| 57 |
|
| 58 |
public static function clear_cache() { |
| 59 |
self::$cached_variables = null; |
| 60 |
} |
| 61 |
|
| 62 |
public static function get_synced_color_css_entries(): array { |
| 63 |
$synced_variables = self::get_synced_color_variables(); |
| 64 |
$css_entries = []; |
| 65 |
|
| 66 |
foreach ( $synced_variables as $id => $variable ) { |
| 67 |
$label = sanitize_text_field( $variable['label'] ?? '' ); |
| 68 |
|
| 69 |
if ( empty( $label ) ) { |
| 70 |
continue; |
| 71 |
} |
| 72 |
|
| 73 |
$v3_id = Module::get_v3_sync_id( $label ); |
| 74 |
|
| 75 |
$css_entries[] = "--e-global-color-{$v3_id}:var(--{$label});"; |
| 76 |
} |
| 77 |
|
| 78 |
return $css_entries; |
| 79 |
} |
| 80 |
} |
| 81 |
|