PluginProbe
Elementor Website Builder – more than just a page builder / 4.0.0-dev4
Elementor Website Builder – more than just a page builder v4.0.0-dev4
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / modules / design-system-sync / classes / classes-provider.php

classes-provider.php in Elementor Website Builder – more than just a page builder 4.0.0-dev4, at modules/design-system-sync/classes/classes-provider.php

181 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 ->context( Global_Classes_Repository::CONTEXT_FRONTEND )
25 ->all()
26 ->get();
27
28 self::$cached_classes = $classes_data['items'] ?? [];
29
30 return self::$cached_classes;
31 }
32
33 public static function get_synced_classes(): array {
34 $all_classes = self::get_all_classes();
35 $synced_classes = [];
36
37 foreach ( $all_classes as $id => $class ) {
38 if ( empty( $class['sync_to_v3'] ) ) {
39 continue;
40 }
41
42 $synced_classes[ $id ] = $class;
43 }
44
45 return $synced_classes;
46 }
47
48 public static function clear_cache() {
49 self::$cached_classes = null;
50 }
51
52 public static function get_default_breakpoint_props( array $variants ): array {
53 $all = self::get_all_normal_state_variant_props( $variants );
54
55 return $all[ Breakpoints_Manager::BREAKPOINT_KEY_DESKTOP ] ?? [];
56 }
57
58 public static function get_all_normal_state_variant_props( array $variants ): array {
59 $result = [];
60
61 foreach ( $variants as $variant ) {
62 if ( ! isset( $variant['meta'] ) ) {
63 continue;
64 }
65
66 $meta = $variant['meta'];
67
68 if ( ! array_key_exists( 'breakpoint', $meta ) || ! array_key_exists( 'state', $meta ) ) {
69 continue;
70 }
71
72 $state = $meta['state'];
73
74 if ( ! in_array( $state, [ null, 'normal' ], true ) ) {
75 continue;
76 }
77
78 $breakpoint = $meta['breakpoint'];
79 $breakpoint_key = ( null === $breakpoint ) ? Breakpoints_Manager::BREAKPOINT_KEY_DESKTOP : $breakpoint;
80
81 $result[ $breakpoint_key ] = $variant['props'] ?? [];
82 }
83
84 return $result;
85 }
86
87 public static function has_typography_props( array $props ): bool {
88 foreach ( Sync_Typography_Props::get_css_props() as $key ) {
89 if ( isset( $props[ $key ] ) ) {
90 return true;
91 }
92 }
93
94 return false;
95 }
96
97 public static function get_typography_classes(): array {
98 $synced_classes = self::get_synced_classes();
99
100 if ( empty( $synced_classes ) ) {
101 return [];
102 }
103
104 $typography_classes = [];
105
106 foreach ( $synced_classes as $id => $class ) {
107 $variants = $class['variants'] ?? [];
108 $default_props = self::get_default_breakpoint_props( $variants );
109
110 if ( empty( $default_props ) ) {
111 continue;
112 }
113
114 if ( ! self::has_typography_props( $default_props ) ) {
115 continue;
116 }
117
118 $typography_classes[] = [
119 'id' => $id,
120 'label' => $class['label'] ?? '',
121 'props' => $default_props,
122 'variants_props' => self::get_all_normal_state_variant_props( $variants ),
123 ];
124 }
125
126 return $typography_classes;
127 }
128
129 public static function get_synced_typography_css_entries(): array {
130 $synced_classes = self::get_synced_classes();
131 $grouped_entries = [];
132
133 $schema = Style_Schema::get();
134 $props_resolver = Render_Props_Resolver::for_styles();
135
136 foreach ( $synced_classes as $id => $class ) {
137 $label = sanitize_text_field( $class['label'] ?? '' );
138
139 if ( empty( $label ) ) {
140 continue;
141 }
142
143 $all_variant_props = self::get_all_normal_state_variant_props( $class['variants'] ?? [] );
144
145 if ( empty( $all_variant_props ) ) {
146 continue;
147 }
148
149 $desktop_props = $all_variant_props[ Breakpoints_Manager::BREAKPOINT_KEY_DESKTOP ] ?? [];
150
151 if ( ! self::has_typography_props( $desktop_props ) ) {
152 continue;
153 }
154
155 $v3_id = Module::get_v3_sync_id( $label );
156
157 foreach ( $all_variant_props as $device => $props ) {
158 if ( empty( $props ) ) {
159 continue;
160 }
161
162 $resolved_props = $props_resolver->resolve( $schema, $props );
163
164 if ( ! isset( $grouped_entries[ $device ] ) ) {
165 $grouped_entries[ $device ] = [];
166 }
167
168 foreach ( Sync_Typography_Props::get_css_props() as $prop_name ) {
169 if ( empty( $resolved_props[ $prop_name ] ) ) {
170 continue;
171 }
172
173 $grouped_entries[ $device ][] = "--e-global-typography-{$v3_id}-{$prop_name}:{$resolved_props[ $prop_name ]};";
174 }
175 }
176 }
177
178 return $grouped_entries;
179 }
180 }
181