| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\DesignSystemSync\Classes; |
| 4 |
|
| 5 |
use Elementor\Controls_Manager; |
| 6 |
use Elementor\Core\Breakpoints\Manager as Breakpoints_Manager; |
| 7 |
use Elementor\Core\Kits\Documents\Tabs\Global_Typography; |
| 8 |
use Elementor\Modules\DesignSystemSync\Controls\V4_Typography_List; |
| 9 |
use Elementor\Modules\DesignSystemSync\Module; |
| 10 |
use Elementor\Plugin; |
| 11 |
|
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit; |
| 14 |
} |
| 15 |
|
| 16 |
class Global_Typography_Extension { |
| 17 |
public function register_hooks() { |
| 18 |
add_action( 'elementor/kit/global-typography/register_controls', [ $this, 'add_v4_classes_section' ] ); |
| 19 |
add_filter( 'elementor/globals/typography/items', [ $this, 'add_v4_classes_to_typography_selector' ] ); |
| 20 |
} |
| 21 |
|
| 22 |
public function add_v4_classes_section( Global_Typography $tab ) { |
| 23 |
if ( ! Plugin::$instance->editor->is_edit_mode() ) { |
| 24 |
return; |
| 25 |
} |
| 26 |
|
| 27 |
$v4_typography_classes = Classes_Provider::get_typography_classes(); |
| 28 |
|
| 29 |
if ( empty( $v4_typography_classes ) ) { |
| 30 |
return; |
| 31 |
} |
| 32 |
|
| 33 |
$tab->add_control( |
| 34 |
'heading_v4_typography_classes', |
| 35 |
[ |
| 36 |
'type' => Controls_Manager::HEADING, |
| 37 |
'label' => esc_html__( 'Atomic Classes', 'elementor' ), |
| 38 |
'separator' => 'before', |
| 39 |
] |
| 40 |
); |
| 41 |
|
| 42 |
$tab->add_control( |
| 43 |
'heading_v4_typography_classes_description', |
| 44 |
[ |
| 45 |
'type' => Controls_Manager::RAW_HTML, |
| 46 |
'raw' => esc_html__( 'V4 classes can only be edited when applied on an atom.', 'elementor' ), |
| 47 |
'content_classes' => 'elementor-descriptor', |
| 48 |
] |
| 49 |
); |
| 50 |
|
| 51 |
$items = []; |
| 52 |
|
| 53 |
foreach ( $v4_typography_classes as $class ) { |
| 54 |
$label = sanitize_text_field( $class['label'] ?? '' ); |
| 55 |
|
| 56 |
if ( empty( $label ) ) { |
| 57 |
continue; |
| 58 |
} |
| 59 |
|
| 60 |
$items[] = [ 'title' => $label ]; |
| 61 |
} |
| 62 |
|
| 63 |
$tab->add_control( |
| 64 |
'v4_typography_classes_display', |
| 65 |
[ |
| 66 |
'type' => V4_Typography_List::TYPE, |
| 67 |
'items' => $items, |
| 68 |
] |
| 69 |
); |
| 70 |
} |
| 71 |
|
| 72 |
public function add_v4_classes_to_typography_selector( array $items ): array { |
| 73 |
$v4_classes = Classes_Provider::get_typography_classes(); |
| 74 |
|
| 75 |
if ( empty( $v4_classes ) ) { |
| 76 |
return $items; |
| 77 |
} |
| 78 |
|
| 79 |
$v4_items = []; |
| 80 |
|
| 81 |
foreach ( $v4_classes as $class ) { |
| 82 |
$label = sanitize_text_field( $class['label'] ?? '' ); |
| 83 |
|
| 84 |
if ( empty( $label ) ) { |
| 85 |
continue; |
| 86 |
} |
| 87 |
|
| 88 |
$id = Module::get_v3_sync_id( $label ); |
| 89 |
$props = $class['props'] ?? []; |
| 90 |
|
| 91 |
if ( empty( $props ) ) { |
| 92 |
continue; |
| 93 |
} |
| 94 |
|
| 95 |
$value = $this->convert_v4_props_to_v3_format( $props ); |
| 96 |
|
| 97 |
$variants_props = $class['variants_props'] ?? []; |
| 98 |
|
| 99 |
foreach ( $variants_props as $breakpoint => $bp_props ) { |
| 100 |
if ( Breakpoints_Manager::BREAKPOINT_KEY_DESKTOP === $breakpoint ) { |
| 101 |
continue; |
| 102 |
} |
| 103 |
|
| 104 |
if ( empty( $bp_props ) ) { |
| 105 |
continue; |
| 106 |
} |
| 107 |
|
| 108 |
$bp_values = $this->convert_v4_props_to_v3_format( $bp_props ); |
| 109 |
|
| 110 |
foreach ( $bp_values as $key => $val ) { |
| 111 |
if ( $this->is_responsive_prop( $key ) ) { |
| 112 |
$value[ $key . '_' . $breakpoint ] = $val; |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
$v4_items[ $id ] = [ |
| 118 |
'id' => $id, |
| 119 |
'title' => $label, |
| 120 |
'value' => $value, |
| 121 |
'group' => 'v4', |
| 122 |
]; |
| 123 |
} |
| 124 |
|
| 125 |
return array_merge( $v4_items, $items ); |
| 126 |
} |
| 127 |
|
| 128 |
private function is_responsive_prop( string $v3_key ): bool { |
| 129 |
return in_array( $v3_key, Sync_Typography_Props::RESPONSIVE_V3_PROPS, true ); |
| 130 |
} |
| 131 |
|
| 132 |
private function convert_v4_props_to_v3_format( array $v4_props ): array { |
| 133 |
$v3_format = []; |
| 134 |
|
| 135 |
foreach ( Sync_Typography_Props::PROP_MAP as $v4_prop => $v3_prop ) { |
| 136 |
if ( ! isset( $v4_props[ $v4_prop ] ) || empty( $v4_props[ $v4_prop ] ) ) { |
| 137 |
continue; |
| 138 |
} |
| 139 |
|
| 140 |
$v3_format[ $v3_prop ] = $this->extract_v4_prop_value( $v4_props[ $v4_prop ] ); |
| 141 |
} |
| 142 |
|
| 143 |
if ( ! empty( $v3_format ) ) { |
| 144 |
$v3_format['typography_typography'] = 'custom'; |
| 145 |
} |
| 146 |
|
| 147 |
return $v3_format; |
| 148 |
} |
| 149 |
|
| 150 |
private function extract_v4_prop_value( $prop ) { |
| 151 |
if ( ! empty( $prop['value'] ) ) { |
| 152 |
return $prop['value']; |
| 153 |
} |
| 154 |
|
| 155 |
return $prop; |
| 156 |
} |
| 157 |
} |
| 158 |
|