| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\DesignSystemSync\Classes; |
| 4 |
|
| 5 |
use Elementor\Core\Breakpoints\Manager as Breakpoints_Manager; |
| 6 |
use Elementor\Modules\AtomicWidgets\PropsResolver\Render_Props_Resolver; |
| 7 |
use Elementor\Modules\AtomicWidgets\Styles\Style_Schema; |
| 8 |
use Elementor\Modules\DesignSystemSync\Module; |
| 9 |
use Elementor\Modules\GlobalClasses\Global_Classes_Repository; |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
class Classes_Provider { |
| 16 |
private static $cached_classes = null; |
| 17 |
|
| 18 |
public static function get_all_classes(): array { |
| 19 |
if ( null !== self::$cached_classes ) { |
| 20 |
return self::$cached_classes; |
| 21 |
} |
| 22 |
|
| 23 |
$classes_data = Global_Classes_Repository::make() |
| 24 |
->all() |
| 25 |
->get(); |
| 26 |
|
| 27 |
self::$cached_classes = $classes_data['items'] ?? []; |
| 28 |
|
| 29 |
return self::$cached_classes; |
| 30 |
} |
| 31 |
|
| 32 |
public static function get_synced_classes(): array { |
| 33 |
$synced_ids = Global_Classes_Sync_Map::make()->get_synced_ids(); |
| 34 |
|
| 35 |
if ( empty( $synced_ids ) ) { |
| 36 |
return []; |
| 37 |
} |
| 38 |
|
| 39 |
return Global_Classes_Repository::make()->get_by_ids( $synced_ids ); |
| 40 |
} |
| 41 |
|
| 42 |
public static function clear_cache() { |
| 43 |
self::$cached_classes = null; |
| 44 |
} |
| 45 |
|
| 46 |
public static function get_default_breakpoint_props( array $variants ): array { |
| 47 |
$all = self::get_all_normal_state_variant_props( $variants ); |
| 48 |
|
| 49 |
return $all[ Breakpoints_Manager::BREAKPOINT_KEY_DESKTOP ] ?? []; |
| 50 |
} |
| 51 |
|
| 52 |
public static function get_all_normal_state_variant_props( array $variants ): array { |
| 53 |
$result = []; |
| 54 |
|
| 55 |
foreach ( $variants as $variant ) { |
| 56 |
if ( ! isset( $variant['meta'] ) ) { |
| 57 |
continue; |
| 58 |
} |
| 59 |
|
| 60 |
$meta = $variant['meta']; |
| 61 |
|
| 62 |
if ( ! array_key_exists( 'breakpoint', $meta ) || ! array_key_exists( 'state', $meta ) ) { |
| 63 |
continue; |
| 64 |
} |
| 65 |
|
| 66 |
$state = $meta['state']; |
| 67 |
|
| 68 |
if ( ! in_array( $state, [ null, 'normal' ], true ) ) { |
| 69 |
continue; |
| 70 |
} |
| 71 |
|
| 72 |
$breakpoint = $meta['breakpoint']; |
| 73 |
$breakpoint_key = ( null === $breakpoint ) ? Breakpoints_Manager::BREAKPOINT_KEY_DESKTOP : $breakpoint; |
| 74 |
|
| 75 |
$result[ $breakpoint_key ] = $variant['props'] ?? []; |
| 76 |
} |
| 77 |
|
| 78 |
return $result; |
| 79 |
} |
| 80 |
|
| 81 |
public static function has_typography_props( array $props ): bool { |
| 82 |
foreach ( Sync_Typography_Props::get_css_props() as $key ) { |
| 83 |
if ( isset( $props[ $key ] ) ) { |
| 84 |
return true; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
return false; |
| 89 |
} |
| 90 |
|
| 91 |
public static function get_typography_classes(): array { |
| 92 |
$synced_classes = self::get_synced_classes(); |
| 93 |
|
| 94 |
if ( empty( $synced_classes ) ) { |
| 95 |
return []; |
| 96 |
} |
| 97 |
|
| 98 |
$typography_classes = []; |
| 99 |
|
| 100 |
foreach ( $synced_classes as $id => $class ) { |
| 101 |
$variants = $class['variants'] ?? []; |
| 102 |
$default_props = self::get_default_breakpoint_props( $variants ); |
| 103 |
|
| 104 |
if ( empty( $default_props ) ) { |
| 105 |
continue; |
| 106 |
} |
| 107 |
|
| 108 |
if ( ! self::has_typography_props( $default_props ) ) { |
| 109 |
continue; |
| 110 |
} |
| 111 |
|
| 112 |
$typography_classes[] = [ |
| 113 |
'id' => $id, |
| 114 |
'label' => $class['label'] ?? '', |
| 115 |
'props' => $default_props, |
| 116 |
'variants_props' => self::get_all_normal_state_variant_props( $variants ), |
| 117 |
]; |
| 118 |
} |
| 119 |
|
| 120 |
return $typography_classes; |
| 121 |
} |
| 122 |
|
| 123 |
public static function get_synced_typography_css_entries(): array { |
| 124 |
$synced_classes = self::get_synced_classes(); |
| 125 |
$grouped_entries = []; |
| 126 |
|
| 127 |
$schema = Style_Schema::get(); |
| 128 |
$props_resolver = Render_Props_Resolver::for_styles(); |
| 129 |
|
| 130 |
foreach ( $synced_classes as $id => $class ) { |
| 131 |
$label = sanitize_text_field( $class['label'] ?? '' ); |
| 132 |
|
| 133 |
if ( empty( $label ) ) { |
| 134 |
continue; |
| 135 |
} |
| 136 |
|
| 137 |
$all_variant_props = self::get_all_normal_state_variant_props( $class['variants'] ?? [] ); |
| 138 |
|
| 139 |
if ( empty( $all_variant_props ) ) { |
| 140 |
continue; |
| 141 |
} |
| 142 |
|
| 143 |
$desktop_props = $all_variant_props[ Breakpoints_Manager::BREAKPOINT_KEY_DESKTOP ] ?? []; |
| 144 |
|
| 145 |
if ( ! self::has_typography_props( $desktop_props ) ) { |
| 146 |
continue; |
| 147 |
} |
| 148 |
|
| 149 |
$v3_id = Module::get_v3_sync_id( $label ); |
| 150 |
|
| 151 |
foreach ( $all_variant_props as $device => $props ) { |
| 152 |
if ( empty( $props ) ) { |
| 153 |
continue; |
| 154 |
} |
| 155 |
|
| 156 |
$resolved_props = $props_resolver->resolve( $schema, $props ); |
| 157 |
|
| 158 |
if ( ! isset( $grouped_entries[ $device ] ) ) { |
| 159 |
$grouped_entries[ $device ] = []; |
| 160 |
} |
| 161 |
|
| 162 |
foreach ( Sync_Typography_Props::get_css_props() as $prop_name ) { |
| 163 |
if ( empty( $resolved_props[ $prop_name ] ) ) { |
| 164 |
continue; |
| 165 |
} |
| 166 |
|
| 167 |
$grouped_entries[ $device ][] = "--e-global-typography-{$v3_id}-{$prop_name}:{$resolved_props[ $prop_name ]};"; |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
return $grouped_entries; |
| 173 |
} |
| 174 |
} |
| 175 |
|