| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\DesignSystemSync\Classes; |
| 4 |
|
| 5 |
use Elementor\Controls_Manager; |
| 6 |
use Elementor\Core\Kits\Documents\Tabs\Global_Colors; |
| 7 |
use Elementor\Modules\DesignSystemSync\Controls\V4_Color_Variable_List; |
| 8 |
use Elementor\Modules\DesignSystemSync\Module; |
| 9 |
use Elementor\Plugin; |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
class Global_Colors_Extension { |
| 16 |
public function register_hooks() { |
| 17 |
add_action( 'elementor/kit/global-colors/register_controls', [ $this, 'add_v4_variables_section' ] ); |
| 18 |
add_filter( 'elementor/globals/colors/items', [ $this, 'add_v4_variables_section_to_color_selector' ] ); |
| 19 |
} |
| 20 |
|
| 21 |
public function add_v4_variables_section( Global_Colors $tab ) { |
| 22 |
if ( ! Plugin::$instance->editor->is_edit_mode() ) { |
| 23 |
return; |
| 24 |
} |
| 25 |
|
| 26 |
$v4_colors = $this->get_v4_color_variables(); |
| 27 |
|
| 28 |
if ( empty( $v4_colors ) ) { |
| 29 |
return; |
| 30 |
} |
| 31 |
|
| 32 |
$items = []; |
| 33 |
foreach ( $v4_colors as $variable ) { |
| 34 |
$items[] = [ |
| 35 |
'_id' => $variable['id'], |
| 36 |
'title' => $variable['label'], |
| 37 |
'color' => strtoupper( $variable['value'] ), |
| 38 |
]; |
| 39 |
} |
| 40 |
|
| 41 |
$tab->add_control( |
| 42 |
'heading_v4_variables', |
| 43 |
[ |
| 44 |
'type' => Controls_Manager::HEADING, |
| 45 |
'label' => esc_html__( 'Atomic Variables', 'elementor' ), |
| 46 |
'separator' => 'before', |
| 47 |
] |
| 48 |
); |
| 49 |
|
| 50 |
$tab->add_control( |
| 51 |
'v4_color_variables_display', |
| 52 |
[ |
| 53 |
'type' => V4_Color_Variable_List::TYPE, |
| 54 |
'items' => $items, |
| 55 |
] |
| 56 |
); |
| 57 |
} |
| 58 |
|
| 59 |
public function add_v4_variables_section_to_color_selector( array $items ): array { |
| 60 |
$v4_colors = $this->get_v4_color_variables(); |
| 61 |
|
| 62 |
if ( empty( $v4_colors ) ) { |
| 63 |
return $items; |
| 64 |
} |
| 65 |
|
| 66 |
foreach ( $v4_colors as $color ) { |
| 67 |
$label = sanitize_text_field( $color['label'] ?? '' ); |
| 68 |
|
| 69 |
if ( empty( $label ) ) { |
| 70 |
continue; |
| 71 |
} |
| 72 |
|
| 73 |
$id = Module::get_v3_sync_id( $label ); |
| 74 |
|
| 75 |
$items[ $id ] = [ |
| 76 |
'id' => $id, |
| 77 |
'title' => $label, |
| 78 |
'value' => strtoupper( $color['value'] ), |
| 79 |
'group' => 'v4', |
| 80 |
]; |
| 81 |
} |
| 82 |
|
| 83 |
return $items; |
| 84 |
} |
| 85 |
|
| 86 |
private function get_v4_color_variables(): array { |
| 87 |
$synced_variables = Variables_Provider::get_synced_color_variables(); |
| 88 |
|
| 89 |
if ( empty( $synced_variables ) ) { |
| 90 |
return []; |
| 91 |
} |
| 92 |
|
| 93 |
$color_variables = []; |
| 94 |
|
| 95 |
foreach ( $synced_variables as $id => $variable ) { |
| 96 |
$value = $variable['value'] ?? ''; |
| 97 |
|
| 98 |
if ( is_array( $value ) && isset( $value['value'] ) ) { |
| 99 |
$value = $value['value']; |
| 100 |
} |
| 101 |
|
| 102 |
$color_variables[] = [ |
| 103 |
'id' => $id, |
| 104 |
'label' => $variable['label'] ?? '', |
| 105 |
'value' => $value, |
| 106 |
'order' => $variable['order'] ?? 0, |
| 107 |
]; |
| 108 |
} |
| 109 |
|
| 110 |
usort( $color_variables, function( $a, $b ) { |
| 111 |
return $a['order'] <=> $b['order']; |
| 112 |
} ); |
| 113 |
|
| 114 |
return $color_variables; |
| 115 |
} |
| 116 |
} |
| 117 |
|