PluginProbe
Core Framework / trunk
Core Framework vtrunk
2.0.2 2.0.1 2.0.0 1.10.4 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.3.10 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.1.1 1.5.2 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.8.0 All 32 releases
core-framework / wp / Helper.php

Helper.php in Core Framework trunk, at wp/Helper.php

1,498 lines 55.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace CoreFramework;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit;
7 }
8
9
10 /**
11 * Core Framework Helper class
12 * Can be used to retrieve an array of class names and variables.
13 * Class names and variables can be grouped into categories.
14 *
15 * @since 1.2.7
16 */
17 class Helper {
18 private $preset = null;
19 private $stylesheet_data_keys = array(
20 'colorStyles',
21 'typographyStyles',
22 'spacingStyles',
23 'layoutsStyles',
24 'designStyles',
25 'componentsStyles',
26 'otherStyles',
27 );
28
29 public function __construct() {
30 }
31
32 public function getPresetId(): ?string {
33 $options = get_option( 'core_framework_main', array() );
34 return $options['selected_id'] ?? null;
35 }
36
37 /**
38 * Check if fonts integration is disabled in plugin preferences
39 *
40 * @return bool
41 * @since 1.10.0
42 */
43 public function isFontsDisabled(): bool {
44 $option = get_option( 'core_framework_main', array() );
45 return isset( $option['disable_fonts'] ) && $option['disable_fonts'];
46 }
47
48 /**
49 * Get fonts that are enabled in the selected preset.
50 *
51 * @return array
52 */
53 public function getEnabledFonts(): array {
54 $preset = $this->loadPreset();
55 $fonts = isset( $preset['modulesData']['FONTS']['fonts'] )
56 ? $preset['modulesData']['FONTS']['fonts']
57 : array();
58
59 if ( ! is_array( $fonts ) ) {
60 return array();
61 }
62
63 return array_values(
64 array_filter(
65 $fonts,
66 static fn( $font ): bool => is_array( $font ) && true === ( $font['enable'] ?? false )
67 )
68 );
69 }
70
71 public function setPresetId( string $preset_id ): void {
72 $options = get_option( 'core_framework_main', array() );
73 $options['selected_id'] = $preset_id;
74 update_option( 'core_framework_main', $options );
75 }
76
77 /**
78 * Load selected preset from database
79 *
80 * @return array|null
81 */
82 public function loadPreset(): ?array {
83 if ( $this->preset ) {
84 return $this->preset;
85 }
86
87 $options = get_option( 'core_framework_main', array() );
88
89 if ( ! is_array( $options ) || ! isset( $options['selected_id'] ) || empty( $options ) ) {
90 return null;
91 }
92
93 global $wpdb;
94
95 $id = $options['selected_id'];
96 $table_name = \esc_sql( $wpdb->prefix . 'core_framework_presets' );
97 $row = $wpdb->get_row(
98 // The identifier is the WordPress-controlled table prefix plus a fixed plugin suffix.
99 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
100 $wpdb->prepare( "SELECT * FROM $table_name WHERE id = %s", $id )
101 );
102
103 if ( ! $row ) {
104 return null;
105 }
106
107 $preset = json_decode( $row->data, true );
108 $this->preset = $preset;
109 return $preset;
110 }
111
112 public function getPreset() {
113 return $this->preset;
114 }
115
116 /**
117 * Get the generated stylesheet directory details.
118 *
119 * WordPress provides a site-specific uploads directory on multisite, so the
120 * same relative path can safely be used for every site.
121 *
122 * @return array{path: string, url: string}
123 */
124 private function getStylesheetDirectory(): array {
125 $uploads = \wp_upload_dir();
126
127 return array(
128 'path' => \trailingslashit( $uploads['basedir'] ) . 'core-framework/css',
129 'url' => \trailingslashit( $uploads['baseurl'] ) . 'core-framework/css',
130 );
131 }
132
133 /**
134 * Get the stylesheet path used by versions through 1.10.4.
135 */
136 private function getLegacyStylesheetPath(): string {
137 $filename = \is_multisite()
138 ? 'core_framework_' . \get_current_blog_id() . '.css'
139 : 'core_framework.css';
140
141 return \plugin_dir_path( CORE_FRAMEWORK_ABSOLUTE ) . 'assets/public/css/' . $filename;
142 }
143
144 /**
145 * Initialize the WordPress filesystem API.
146 *
147 * @return \WP_Filesystem_Base|null
148 */
149 private function getFilesystem() {
150 global $wp_filesystem;
151
152 if ( ! \function_exists( 'WP_Filesystem' ) ) {
153 require_once ABSPATH . 'wp-admin/includes/file.php';
154 }
155
156 if ( ! \WP_Filesystem() || ! \is_object( $wp_filesystem ) ) {
157 return null;
158 }
159
160 return $wp_filesystem;
161 }
162
163 /**
164 * Get stylesheet URL.
165 *
166 * @return string
167 * @since 1.3.3
168 */
169 public function getStylesheetUrl(): string {
170 $directory = $this->getStylesheetDirectory();
171
172 return \trailingslashit( $directory['url'] ) . 'core_framework.css';
173 }
174
175 /**
176 * Get Stylesheet plain path
177 *
178 * @return string
179 * @since 1.3.3
180 */
181 public function getStylesheetPath(): string {
182 $directory = $this->getStylesheetDirectory();
183
184 return \trailingslashit( $directory['path'] ) . 'core_framework.css';
185 }
186
187 /**
188 * Persist the generated stylesheet in WordPress-managed storage.
189 *
190 * @return int|false Number of bytes saved, or false on failure.
191 */
192 public function writeStylesheet( string $css ) {
193 $filesystem = $this->getFilesystem();
194
195 if ( null === $filesystem ) {
196 return false;
197 }
198
199 $directory = $this->getStylesheetDirectory();
200
201 if ( ! $filesystem->is_dir( $directory['path'] ) && ! \wp_mkdir_p( $directory['path'] ) ) {
202 return false;
203 }
204
205 $written = $filesystem->put_contents(
206 $this->getStylesheetPath(),
207 $css,
208 defined( 'FS_CHMOD_FILE' ) ? FS_CHMOD_FILE : 0644
209 );
210
211 return $written ? \strlen( $css ) : false;
212 }
213
214 /**
215 * Read the generated stylesheet from WordPress-managed storage.
216 *
217 * @return string|false
218 */
219 public function readStylesheet() {
220 $filesystem = $this->getFilesystem();
221
222 if ( null === $filesystem ) {
223 return false;
224 }
225
226 return $filesystem->get_contents( $this->getStylesheetPath() );
227 }
228
229 /**
230 * Ensure a generated stylesheet exists, migrating legacy data if possible.
231 */
232 public function ensureStylesheet(): bool {
233 $filesystem = $this->getFilesystem();
234 $stylesheet_path = $this->getStylesheetPath();
235
236 if ( null === $filesystem ) {
237 return false;
238 }
239
240 $stylesheet_exists = $filesystem->is_file( $stylesheet_path );
241 $stylesheet_size = $stylesheet_exists ? $filesystem->size( $stylesheet_path ) : false;
242
243 if ( false !== $stylesheet_size && $stylesheet_size > 0 ) {
244 return true;
245 }
246
247 $css = '';
248 $legacy_path = $this->getLegacyStylesheetPath();
249
250 if ( $filesystem->is_file( $legacy_path ) && $filesystem->size( $legacy_path ) > 0 ) {
251 $legacy_css = $filesystem->get_contents( $legacy_path );
252 $css = false !== $legacy_css ? $legacy_css : '';
253 }
254
255 if ( '' === $css ) {
256 $css = (string) \get_option( 'core_framework_selected_preset_backup', '' );
257 }
258
259 if ( '' === $css && $stylesheet_exists ) {
260 return true;
261 }
262
263 return false !== $this->writeStylesheet( $css );
264 }
265
266 /**
267 * Get stylesheet version
268 *
269 * @return string
270 * @since 1.3.3
271 */
272 public function getStylesheetVersion(): string {
273 $stylesheet_path = $this->getStylesheetPath();
274
275 if ( \is_file( $stylesheet_path ) ) {
276 $version = \filemtime( $stylesheet_path );
277
278 if ( false !== $version ) {
279 return (string) $version;
280 }
281 }
282
283 return CORE_FRAMEWORK_VERSION;
284 }
285
286 /**
287 * Extract all classNames from selector. Prefix first className with class_prefix.
288 *
289 * @param string $selector
290 * @param string $class_prefix
291 * @return array
292 * @since 1.2.7
293 */
294 private function extractClassNames( string $selector, string $class_prefix ): array {
295 if ( ! $selector || $selector === '.' ) {
296 return array();
297 }
298
299 $get_first_selector = function ( $selector ) {
300 $arr = explode( ',', $selector );
301 $arr = array_map(
302 function ( $s ) {
303 return trim( $s );
304 },
305 $arr
306 );
307 $arr = array_filter(
308 $arr,
309 function ( $s ) {
310 return ! empty( $s );
311 }
312 );
313 $arr = array_filter(
314 $arr,
315 function ( $s ) {
316 return $s !== '.';
317 }
318 );
319
320 return $arr[0];
321 };
322
323 $class_accumulator = array();
324
325 $selector = strpos( $selector, ',' ) !== false
326 ? $get_first_selector( $selector )
327 : trim( $selector );
328
329 if ( strpos( $selector, '.' ) === 0 && strpos( $selector, ':' ) === false ) {
330 $dots = substr_count( $selector, '.' );
331
332 if ( $dots && $dots > 1 ) {
333 $split = explode( '.', $selector );
334 $last = end( $split );
335
336 if ( $last ) {
337 $class_accumulator[] = $last;
338 }
339
340 return $class_accumulator;
341 }
342
343 $class_accumulator[] = str_replace( '.', '', $class_prefix . $selector );
344 }
345
346 return $class_accumulator;
347 }
348
349 /**
350 * Get all class names from project
351 *
352 * @param array $options
353 * - group_by_category: bool (default: true) - if true, returns array of grouped class names, otherwise returns flat array
354 * - excluded_keys: array of ['colorStyles', 'typographyStyles', 'spacingStyles', 'layoutsStyles', 'designStyles', 'componentsStyles', 'otherStyles']
355 * @return array
356 * @since 1.2.7
357 */
358 public function getClassNames(array $options = array(
359 'group_by_category' => true,
360 'excluded_keys' => array(),
361 )): array {
362 $options = \wp_parse_args(
363 $options,
364 array(
365 'group_by_category' => true,
366 'excluded_keys' => array(),
367 )
368 );
369
370 $preset = $this->loadPreset();
371
372 if ( ! $preset ) {
373 return array();
374 }
375
376 $class_prefix = isset( $preset['classPrefix'] ) ? $preset['classPrefix'] : '';
377
378 $grouped_class_names = array(
379 'colorStyles' => array(),
380 'typographyStyles' => array(),
381 'spacingStyles' => array(),
382 'layoutsStyles' => array(),
383 'designStyles' => array(),
384 'componentsStyles' => array(),
385 'otherStyles' => array(),
386 );
387
388 if ( isset( $preset['styleSheetData'] ) && is_array( $preset['styleSheetData'] ) ) {
389 foreach ( $this->stylesheet_data_keys as $key ) {
390 if ( isset( $preset['styleSheetData'][ $key ] ) && is_array( $preset['styleSheetData'][ $key ] ) ) {
391 foreach ( $preset['styleSheetData'][ $key ] as $group ) {
392 $is_variable_group = isset( $group['type'] ) && $group['type'] === 'variable';
393 $is_group_disabled = isset( $group['isDisabled'] ) && $group['isDisabled'] === true;
394
395 if ( $is_variable_group || $is_group_disabled ) {
396 continue;
397 }
398
399 if ( isset( $group['cssObjects'] ) && is_array( $group['cssObjects'] ) ) {
400 foreach ( $group['cssObjects'] as $cssObject ) {
401 $is_disabled = isset( $cssObject['isDisabled'] ) && $cssObject['isDisabled'] === true;
402
403 if ( $is_disabled ) {
404 continue;
405 }
406
407 $selector = isset( $cssObject['selector'] ) ? $cssObject['selector'] : '';
408 $grouped_class_names[ $key ] = array_merge(
409 $grouped_class_names[ $key ],
410 $this->extractClassNames( $selector, $class_prefix )
411 );
412 }
413 }
414 }
415 }
416 }
417 }
418
419 if ( isset( $preset['modulesData'] ) && isset( $preset['modulesData']['COMPONENTS'] ) && isset( $preset['modulesData']['COMPONENTS']['components'] ) ) {
420 $is_disabled = isset( $preset['modulesData']['COMPONENTS']['isDisabled'] ) && $preset['modulesData']['COMPONENTS']['isDisabled'] === true;
421
422 if ( ! $is_disabled ) {
423 foreach ( $preset['modulesData']['COMPONENTS']['components'] as $component ) {
424 if ( strpos( $component['selector'], '.' ) === 0 ) {
425 $grouped_class_names['componentsStyles'] = array_merge(
426 $grouped_class_names['componentsStyles'],
427 $this->extractClassNames( $component['selector'], $class_prefix )
428 );
429
430 if ( isset( $component['variants'] ) ) {
431 foreach ( $component['variants'] as $variant ) {
432 if ( isset( $variant['variantSelector'] ) && strpos( $variant['variantSelector'], '.' ) === 0 ) {
433 $grouped_class_names['componentsStyles'] = array_merge(
434 $grouped_class_names['componentsStyles'],
435 $this->extractClassNames( $variant['variantSelector'], '' )
436 );
437 }
438 }
439 }
440 }
441 }
442 }
443 }
444
445 if ( isset( $preset['modulesData'] ) && isset( $preset['modulesData']['FLUID_SPACING'] ) ) {
446 $spacing_data = $preset['modulesData']['FLUID_SPACING'];
447 $is_disabled = isset( $spacing_data['isDisabled'] ) && $spacing_data['isDisabled'] === true;
448 $is_manual_fluid_spacing = isset( $spacing_data['mode'] ) && $spacing_data['mode'] === 'fluid_manual';
449 $is_gen_padding = isset( $spacing_data['genPadding'] ) && $spacing_data['genPadding'] === true;
450 $is_gen_margin = isset( $spacing_data['genMargin'] ) && $spacing_data['genMargin'] === true;
451 $is_gen_gap = isset( $spacing_data['genGap'] ) && $spacing_data['genGap'] === true;
452 $generate_any_classes = $is_gen_padding || $is_gen_margin || $is_gen_gap;
453
454 $key = 'spacingStyles';
455
456 if ( $generate_any_classes ) {
457 if ( ! $is_disabled && $is_manual_fluid_spacing && isset( $spacing_data['manualSizes'] ) ) {
458 foreach ( $spacing_data['manualSizes'] as $manual_size ) {
459 $name = $manual_size['name'];
460 $get_suffix = function ( $name ) {
461 $parts = explode( '-', $name );
462 return end( $parts );
463 };
464 $suffix = $get_suffix( $name );
465
466 if ( $is_gen_gap ) {
467 $grouped_class_names[ $key ][] = $class_prefix . 'gap-' . $suffix;
468 $grouped_class_names[ $key ][] = $class_prefix . 'gap-horizontal-' . $suffix;
469 $grouped_class_names[ $key ][] = $class_prefix . 'gap-vertical-' . $suffix;
470 }
471
472 if ( $is_gen_padding ) {
473 $grouped_class_names[ $key ][] = $class_prefix . 'padding-' . $suffix;
474 $grouped_class_names[ $key ][] = $class_prefix . 'padding-horizontal-' . $suffix;
475 $grouped_class_names[ $key ][] = $class_prefix . 'padding-vertical-' . $suffix;
476 $grouped_class_names[ $key ][] = $class_prefix . 'padding-top-' . $suffix;
477 $grouped_class_names[ $key ][] = $class_prefix . 'padding-right-' . $suffix;
478 $grouped_class_names[ $key ][] = $class_prefix . 'padding-bottom-' . $suffix;
479 $grouped_class_names[ $key ][] = $class_prefix . 'padding-left-' . $suffix;
480 }
481
482 if ( $is_gen_margin ) {
483 $grouped_class_names[ $key ][] = $class_prefix . 'margin-' . $suffix;
484 $grouped_class_names[ $key ][] = $class_prefix . 'margin-horizontal-' . $suffix;
485 $grouped_class_names[ $key ][] = $class_prefix . 'margin-vertical-' . $suffix;
486 $grouped_class_names[ $key ][] = $class_prefix . 'margin-top-' . $suffix;
487 $grouped_class_names[ $key ][] = $class_prefix . 'margin-right-' . $suffix;
488 $grouped_class_names[ $key ][] = $class_prefix . 'margin-bottom-' . $suffix;
489 $grouped_class_names[ $key ][] = $class_prefix . 'margin-left-' . $suffix;
490 }
491 }
492 }
493
494 if ( ! $is_disabled && ! $is_manual_fluid_spacing && isset( $spacing_data['steps'] ) && isset( $spacing_data['namingConvention'] ) ) {
495 $steps = $spacing_data['steps'];
496 $steps = explode( ',', $steps );
497
498 foreach ( $steps as $step ) {
499 if ( $is_gen_gap ) {
500 $grouped_class_names[ $key ][] = $class_prefix . 'gap-' . $step;
501 $grouped_class_names[ $key ][] = $class_prefix . 'gap-horizontal-' . $step;
502 $grouped_class_names[ $key ][] = $class_prefix . 'gap-vertical-' . $step;
503 }
504
505 if ( $is_gen_padding ) {
506 $grouped_class_names[ $key ][] = $class_prefix . 'padding-' . $step;
507 $grouped_class_names[ $key ][] = $class_prefix . 'padding-horizontal-' . $step;
508 $grouped_class_names[ $key ][] = $class_prefix . 'padding-vertical-' . $step;
509 $grouped_class_names[ $key ][] = $class_prefix . 'padding-top-' . $step;
510 $grouped_class_names[ $key ][] = $class_prefix . 'padding-right-' . $step;
511 $grouped_class_names[ $key ][] = $class_prefix . 'padding-bottom-' . $step;
512 $grouped_class_names[ $key ][] = $class_prefix . 'padding-left-' . $step;
513 }
514
515 if ( $is_gen_margin ) {
516 $grouped_class_names[ $key ][] = $class_prefix . 'margin-' . $step;
517 $grouped_class_names[ $key ][] = $class_prefix . 'margin-horizontal-' . $step;
518 $grouped_class_names[ $key ][] = $class_prefix . 'margin-vertical-' . $step;
519 $grouped_class_names[ $key ][] = $class_prefix . 'margin-top-' . $step;
520 $grouped_class_names[ $key ][] = $class_prefix . 'margin-right-' . $step;
521 $grouped_class_names[ $key ][] = $class_prefix . 'margin-bottom-' . $step;
522 $grouped_class_names[ $key ][] = $class_prefix . 'margin-left-' . $step;
523 }
524 }
525 }
526 }
527 }
528
529 if ( isset( $preset['modulesData'] ) && isset( $preset['modulesData']['FLUID_TYPOGRAPHY'] ) ) {
530 $typography_data = $preset['modulesData']['FLUID_TYPOGRAPHY'];
531 $is_disabled = isset( $typography_data['isDisabled'] ) && $typography_data['isDisabled'] === true;
532 $is_manual_fluid_spacing = isset( $typography_data['mode'] ) && $typography_data['mode'] === 'fluid_manual';
533 $is_gen_text_class = isset( $typography_data['genFontSizeClass'] ) && $typography_data['genFontSizeClass'] === true;
534 $key = 'typographyStyles';
535
536 if ( $is_gen_text_class ) {
537 if ( ! $is_disabled && $is_manual_fluid_spacing && isset( $typography_data['manualSizes'] ) ) {
538 foreach ( $typography_data['manualSizes'] as $manual_size ) {
539 $name = $manual_size['name'];
540 $get_suffix = function ( $name ) {
541 $parts = explode( '-', $name );
542 return end( $parts );
543 };
544 $suffix = $get_suffix( $name );
545 $naming_convention = $typography_data['namingConvention'];
546
547 $grouped_class_names[ $key ][] = $class_prefix . $naming_convention . '-' . str_replace( 'text-', '', $name );
548 }
549 }
550
551 if ( ! $is_disabled && ! $is_manual_fluid_spacing && isset( $typography_data['steps'] ) && isset( $typography_data['namingConvention'] ) ) {
552 $steps = $typography_data['steps'];
553 $steps = explode( ',', $steps );
554 $naming_convention = $typography_data['namingConvention'];
555
556 foreach ( $steps as $step ) {
557 $grouped_class_names[ $key ][] = $class_prefix . $naming_convention . '-' . str_replace( 'text-', '', $step );
558 }
559 }
560 }
561 }
562
563 if ( isset( $preset['modulesData'] ) && isset( $preset['modulesData']['COLOR_SYSTEM'] ) && isset( $preset['modulesData']['COLOR_SYSTEM']['groups'] ) ) {
564 foreach ( $preset['modulesData']['COLOR_SYSTEM']['groups'] as $group ) {
565 if ( isset( $group['isDisabled'] ) && $group['isDisabled'] ) {
566 continue;
567 }
568
569 if ( isset( $group['colors'] ) && is_array( $group['colors'] ) ) {
570 foreach ( $group['colors'] as $color ) {
571 $key = 'colorStyles';
572
573 $local_color_names = array();
574 $main_name = $color['name'];
575 $local_color_names[] = $main_name;
576
577 $shades = isset( $color['shades'] ) ? $color['shades'] : array();
578 foreach ( $shades as $shade ) {
579 $local_color_names[] = $shade['name'];
580 }
581
582 $tints = isset( $color['tints'] ) ? $color['tints'] : array();
583 foreach ( $tints as $tint ) {
584 $local_color_names[] = $tint['name'];
585 }
586
587 $process_name = function ( $name, $prefix ) {
588 if ( strpos( $name, $prefix ) === 0 ) {
589 $name = str_replace( $prefix, '', $name );
590 }
591
592 return $name;
593 };
594
595 $class_generation_array = isset( $color['gen'] ) ? $color['gen'] : array();
596 $is_gen_bg = in_array( 'bg', $class_generation_array );
597 $is_gen_text = in_array( 'text', $class_generation_array );
598 $is_gen_border = in_array( 'border', $class_generation_array );
599
600 $is_transparent = isset( $color['transparent'] ) && $color['transparent'] === true;
601 $transparent_variables = isset( $color['transparentVariables'] ) ? $color['transparentVariables'] : array();
602
603 foreach ( $local_color_names as $name ) {
604 if ( $is_gen_bg ) {
605 $prefix = 'bg-';
606 $grouped_class_names[ $key ][] = $class_prefix . $prefix . $process_name( $name, $prefix );
607
608 if ( $is_transparent ) {
609 foreach ( $transparent_variables as $transparent_value ) {
610 $grouped_class_names[ $key ][] = $class_prefix . $prefix . $main_name . '-' . $transparent_value;
611 }
612 }
613 }
614
615 if ( $is_gen_text ) {
616 $prefix = 'text-';
617 $grouped_class_names[ $key ][] = $class_prefix . $prefix . $process_name( $name, $prefix );
618
619 if ( $is_transparent ) {
620 foreach ( $transparent_variables as $transparent_value ) {
621 $grouped_class_names[ $key ][] = $class_prefix . $prefix . $main_name . '-' . $transparent_value;
622 }
623 }
624 }
625
626 if ( $is_gen_border ) {
627 $prefix = 'border-';
628 $grouped_class_names[ $key ][] = $class_prefix . $prefix . $process_name( $name, $prefix );
629
630 if ( $is_transparent ) {
631 foreach ( $transparent_variables as $transparent_value ) {
632 $grouped_class_names[ $key ][] = $class_prefix . $prefix . $main_name . '-' . $transparent_value;
633 }
634 }
635 }
636 }
637 }
638 }
639 }
640 }
641
642 $core_framework_main = \get_option( 'core_framework_main', array() );
643 $has_theme = isset( $core_framework_main['has_theme'] ) && $core_framework_main['has_theme'];
644 if ( $has_theme ) {
645 $grouped_class_names['otherStyles'][] = $class_prefix . 'theme-inverted';
646 }
647
648 if ( isset( $options['excluded_keys'] ) && is_array( $options['excluded_keys'] ) && ! empty( $options['excluded_keys'] ) ) {
649 foreach ( $options['excluded_keys'] as $excluded_key ) {
650 if ( isset( $grouped_class_names[ $excluded_key ] ) ) {
651 unset( $grouped_class_names[ $excluded_key ] );
652 }
653 }
654 }
655
656 foreach ( array_keys( $grouped_class_names ) as $key ) {
657 $grouped_class_names[ $key ] = array_values( array_unique( $grouped_class_names[ $key ] ) );
658 }
659
660 if ( $options['group_by_category'] === false ) {
661 $grouped_class_names = array_merge( ...array_values( $grouped_class_names ?? array() ) );
662 }
663
664 return $grouped_class_names;
665 }
666
667 /**
668 * @return array
669 * @since 1.2.7
670 */
671 public function getClassNamesGroupedByCategoriesAndGroups(): array {
672 $preset = $this->loadPreset();
673
674 if ( ! $preset ) {
675 return array();
676 }
677
678 $class_prefix = isset( $preset['classPrefix'] ) ? $preset['classPrefix'] : '';
679
680 $grouped_class_names = array(
681 'colorStyles' => array(),
682 'typographyStyles' => array(),
683 'spacingStyles' => array(),
684 'layoutsStyles' => array(),
685 'designStyles' => array(),
686 'componentsStyles' => array(),
687 'otherStyles' => array(),
688 );
689
690 if ( isset( $preset['styleSheetData'] ) && is_array( $preset['styleSheetData'] ) ) {
691 foreach ( $this->stylesheet_data_keys as $key ) {
692 if ( isset( $preset['styleSheetData'][ $key ] ) && is_array( $preset['styleSheetData'][ $key ] ) ) {
693 foreach ( $preset['styleSheetData'][ $key ] as $group ) {
694 $is_variable_group = isset( $group['type'] ) && $group['type'] === 'variable';
695 $is_group_disabled = isset( $group['isDisabled'] ) && $group['isDisabled'] === true;
696 $group_name = isset( $group['name'] ) ? $group['name'] : 'No name';
697
698 if ( $is_variable_group || $is_group_disabled ) {
699 continue;
700 }
701
702 if ( isset( $group['cssObjects'] ) && is_array( $group['cssObjects'] ) ) {
703 foreach ( $group['cssObjects'] as $cssObject ) {
704 $selector = isset( $cssObject['selector'] ) ? $cssObject['selector'] : '';
705 $grouped_class_names[ $key ][ $group_name ] = array_merge(
706 $grouped_class_names[ $key ][ $group_name ] ?? array(),
707 $this->extractClassNames( $selector, $class_prefix )
708 );
709 }
710 }
711 }
712 }
713 }
714 }
715
716 if ( isset( $preset['modulesData'] ) && isset( $preset['modulesData']['COMPONENTS'] ) && isset( $preset['modulesData']['COMPONENTS']['components'] ) ) {
717 $is_disabled = isset( $preset['modulesData']['COMPONENTS']['isDisabled'] ) && $preset['modulesData']['COMPONENTS']['isDisabled'] === true;
718 $group_name = 'Components';
719
720 if ( ! $is_disabled ) {
721 foreach ( $preset['modulesData']['COMPONENTS']['components'] as $component ) {
722 if ( strpos( $component['selector'], '.' ) === 0 ) {
723 $grouped_class_names['componentsStyles'][ $group_name ] = array_merge(
724 $grouped_class_names['componentsStyles'][ $group_name ] ?? array(),
725 $this->extractClassNames( $component['selector'], $class_prefix )
726 );
727
728 if ( isset( $component['variants'] ) ) {
729 foreach ( $component['variants'] as $variant ) {
730 if ( isset( $variant['variantSelector'] ) && strpos( $variant['variantSelector'], '.' ) === 0 ) {
731 $grouped_class_names['componentsStyles'][ $group_name ] = array_merge(
732 $grouped_class_names['componentsStyles'][ $group_name ] ?? array(),
733 $this->extractClassNames( $variant['variantSelector'], $class_prefix )
734 );
735 }
736 }
737 }
738 }
739 }
740 }
741 }
742
743 if ( isset( $preset['modulesData'] ) && isset( $preset['modulesData']['FLUID_SPACING'] ) ) {
744 $spacing_data = $preset['modulesData']['FLUID_SPACING'];
745 $is_disabled = isset( $spacing_data['isDisabled'] ) && $spacing_data['isDisabled'] === true;
746 $is_manual_fluid_spacing = isset( $spacing_data['mode'] ) && $spacing_data['mode'] === 'fluid_manual';
747 $is_gen_padding = isset( $spacing_data['genPadding'] ) && $spacing_data['genPadding'] === true;
748 $is_gen_margin = isset( $spacing_data['genMargin'] ) && $spacing_data['genMargin'] === true;
749 $is_gen_gap = isset( $spacing_data['genGap'] ) && $spacing_data['genGap'] === true;
750 $generate_any_classes = $is_gen_padding || $is_gen_margin || $is_gen_gap;
751
752 $key = 'spacingStyles';
753
754 if ( $generate_any_classes ) {
755 if ( ! $is_disabled && $is_manual_fluid_spacing && isset( $spacing_data['manualSizes'] ) ) {
756 foreach ( $spacing_data['manualSizes'] as $manual_size ) {
757 $name = $manual_size['name'];
758 $get_suffix = function ( $name ) {
759 $parts = explode( '-', $name );
760 return end( $parts );
761 };
762 $suffix = $get_suffix( $name );
763
764 if ( $is_gen_gap ) {
765 $group_name = 'Gaps';
766 $grouped_class_names[ $key ][ $group_name ][] = $class_prefix . 'gap-' . $suffix;
767 $grouped_class_names[ $key ][ $group_name ][] = $class_prefix . 'gap-horizontal-' . $suffix;
768 $grouped_class_names[ $key ][ $group_name ][] = $class_prefix . 'gap-vertical-' . $suffix;
769 }
770
771 if ( $is_gen_padding ) {
772 $group_name = 'Paddings';
773 $grouped_class_names[ $key ][ $group_name ][] = $class_prefix . 'padding-' . $suffix;
774 $grouped_class_names[ $key ][ $group_name ][] = $class_prefix . 'padding-horizontal-' . $suffix;
775 $grouped_class_names[ $key ][ $group_name ][] = $class_prefix . 'padding-vertical-' . $suffix;
776 $grouped_class_names[ $key ][ $group_name ][] = $class_prefix . 'padding-top-' . $suffix;
777 $grouped_class_names[ $key ][ $group_name ][] = $class_prefix . 'padding-right-' . $suffix;
778 $grouped_class_names[ $key ][ $group_name ][] = $class_prefix . 'padding-bottom-' . $suffix;
779 $grouped_class_names[ $key ][ $group_name ][] = $class_prefix . 'padding-left-' . $suffix;
780 }
781
782 if ( $is_gen_margin ) {
783 $group_name = 'Margins';
784 $grouped_class_names[ $key ][ $group_name ][] = $class_prefix . 'margin-' . $suffix;
785 $grouped_class_names[ $key ][ $group_name ][] = $class_prefix . 'margin-horizontal-' . $suffix;
786 $grouped_class_names[ $key ][ $group_name ][] = $class_prefix . 'margin-vertical-' . $suffix;
787 $grouped_class_names[ $key ][ $group_name ][] = $class_prefix . 'margin-top-' . $suffix;
788 $grouped_class_names[ $key ][ $group_name ][] = $class_prefix . 'margin-right-' . $suffix;
789 $grouped_class_names[ $key ][ $group_name ][] = $class_prefix . 'margin-bottom-' . $suffix;
790 $grouped_class_names[ $key ][ $group_name ][] = $class_prefix . 'margin-left-' . $suffix;
791 }
792 }
793 }
794
795 if ( ! $is_disabled && ! $is_manual_fluid_spacing && isset( $spacing_data['steps'] ) && isset( $spacing_data['namingConvention'] ) ) {
796 $steps = $spacing_data['steps'];
797 $steps = explode( ',', $steps );
798
799 foreach ( $steps as $step ) {
800 if ( $is_gen_gap ) {
801 $group_name = 'Gaps';
802 $grouped_class_names[ $key ][ $group_name ][] = $class_prefix . 'gap-' . $step;
803 $grouped_class_names[ $key ][ $group_name ][] = $class_prefix . 'gap-horizontal-' . $step;
804 $grouped_class_names[ $key ][ $group_name ][] = $class_prefix . 'gap-vertical-' . $step;
805 }
806
807 if ( $is_gen_padding ) {
808 $group_name = 'Paddings';
809 $grouped_class_names[ $key ][ $group_name ][] = $class_prefix . 'padding-' . $step;
810 $grouped_class_names[ $key ][ $group_name ][] = $class_prefix . 'padding-horizontal-' . $step;
811 $grouped_class_names[ $key ][ $group_name ][] = $class_prefix . 'padding-vertical-' . $step;
812 $grouped_class_names[ $key ][ $group_name ][] = $class_prefix . 'padding-top-' . $step;
813 $grouped_class_names[ $key ][ $group_name ][] = $class_prefix . 'padding-right-' . $step;
814 $grouped_class_names[ $key ][ $group_name ][] = $class_prefix . 'padding-bottom-' . $step;
815 $grouped_class_names[ $key ][ $group_name ][] = $class_prefix . 'padding-left-' . $step;
816 }
817
818 if ( $is_gen_margin ) {
819 $group_name = 'Margins';
820 $grouped_class_names[ $key ][ $group_name ][] = $class_prefix . 'margin-' . $step;
821 $grouped_class_names[ $key ][ $group_name ][] = $class_prefix . 'margin-horizontal-' . $step;
822 $grouped_class_names[ $key ][ $group_name ][] = $class_prefix . 'margin-vertical-' . $step;
823 $grouped_class_names[ $key ][ $group_name ][] = $class_prefix . 'margin-top-' . $step;
824 $grouped_class_names[ $key ][ $group_name ][] = $class_prefix . 'margin-right-' . $step;
825 $grouped_class_names[ $key ][ $group_name ][] = $class_prefix . 'margin-bottom-' . $step;
826 $grouped_class_names[ $key ][ $group_name ][] = $class_prefix . 'margin-left-' . $step;
827 }
828 }
829 }
830 }
831 }
832
833 if ( isset( $preset['modulesData'] ) && isset( $preset['modulesData']['FLUID_TYPOGRAPHY'] ) ) {
834 $typography_data = $preset['modulesData']['FLUID_TYPOGRAPHY'];
835 $is_disabled = isset( $typography_data['isDisabled'] ) && $typography_data['isDisabled'] === true;
836 $is_manual_fluid_spacing = isset( $typography_data['mode'] ) && $typography_data['mode'] === 'fluid_manual';
837 $is_gen_text_class = isset( $typography_data['genFontSizeClass'] ) && $typography_data['genFontSizeClass'] === true;
838 $key = 'typographyStyles';
839 $group_name = 'Fluid Typography';
840
841 if ( $is_gen_text_class ) {
842 if ( ! $is_disabled && $is_manual_fluid_spacing && isset( $typography_data['manualSizes'] ) ) {
843 foreach ( $typography_data['manualSizes'] as $manual_size ) {
844 $name = $manual_size['name'];
845 $get_suffix = function ( $name ) {
846 $parts = explode( '-', $name );
847 return end( $parts );
848 };
849 $suffix = $get_suffix( $name );
850 $naming_convention = $typography_data['namingConvention'];
851
852 $grouped_class_names[ $key ][ $group_name ][] = $class_prefix . $naming_convention . '-' . str_replace( 'text-', '', $name );
853 }
854 }
855
856 if ( ! $is_disabled && ! $is_manual_fluid_spacing && isset( $typography_data['steps'] ) && isset( $typography_data['namingConvention'] ) ) {
857 $steps = $typography_data['steps'];
858 $steps = explode( ',', $steps );
859 $naming_convention = $typography_data['namingConvention'];
860
861 foreach ( $steps as $step ) {
862 $grouped_class_names[ $key ][ $group_name ][] = $class_prefix . $naming_convention . '-' . str_replace( 'text-', '', $step );
863 }
864 }
865 }
866 }
867
868 if ( isset( $preset['modulesData'] ) && isset( $preset['modulesData']['COLOR_SYSTEM'] ) && isset( $preset['modulesData']['COLOR_SYSTEM']['groups'] ) ) {
869 foreach ( $preset['modulesData']['COLOR_SYSTEM']['groups'] as $group ) {
870 if ( isset( $group['isDisabled'] ) && $group['isDisabled'] ) {
871 continue;
872 }
873
874 if ( isset( $group['colors'] ) && is_array( $group['colors'] ) ) {
875 foreach ( $group['colors'] as $color ) {
876 $key = 'colorStyles';
877
878 $local_color_names = array();
879 $main_name = $color['name'];
880 $local_color_names[] = $main_name;
881
882 $shades = isset( $color['shades'] ) ? $color['shades'] : array();
883 foreach ( $shades as $shade ) {
884 $local_color_names[] = $shade['name'];
885 }
886
887 $tints = isset( $color['tints'] ) ? $color['tints'] : array();
888 foreach ( $tints as $tint ) {
889 $local_color_names[] = $tint['name'];
890 }
891
892 $process_name = function ( $name, $prefix ) {
893 if ( strpos( $name, $prefix ) === 0 ) {
894 $name = str_replace( $prefix, '', $name );
895 }
896
897 return $name;
898 };
899
900 $class_generation_array = isset( $color['gen'] ) ? $color['gen'] : array();
901 $is_gen_bg = in_array( 'bg', $class_generation_array );
902 $is_gen_text = in_array( 'text', $class_generation_array );
903 $is_gen_border = in_array( 'border', $class_generation_array );
904
905 $is_transparent = isset( $color['transparent'] ) && $color['transparent'] === true;
906 $transparent_variables = isset( $color['transparentVariables'] ) ? $color['transparentVariables'] : array();
907
908 foreach ( $local_color_names as $name ) {
909 if ( $is_gen_bg ) {
910 $prefix = 'bg-';
911 $group_name = 'Backgrounds';
912 $grouped_class_names[ $key ][ $group_name ][] = $class_prefix . $prefix . $process_name( $name, $prefix );
913
914 if ( $is_transparent ) {
915 foreach ( $transparent_variables as $transparent_value ) {
916 $grouped_class_names[ $key ][ $group_name ][] = $class_prefix . $prefix . $main_name . '-' . $transparent_value;
917 }
918 }
919 }
920
921 if ( $is_gen_text ) {
922 $prefix = 'text-';
923 $group_name = 'Text Colors';
924 $grouped_class_names[ $key ][ $group_name ][] = $class_prefix . $prefix . $process_name( $name, $prefix );
925
926 if ( $is_transparent ) {
927 foreach ( $transparent_variables as $transparent_value ) {
928 $grouped_class_names[ $key ][ $group_name ][] = $class_prefix . $prefix . $main_name . '-' . $transparent_value;
929 }
930 }
931 }
932
933 if ( $is_gen_border ) {
934 $prefix = 'border-';
935 $group_name = 'Border Colors';
936 $grouped_class_names[ $key ][ $group_name ][] = $class_prefix . $prefix . $process_name( $name, $prefix );
937
938 if ( $is_transparent ) {
939 foreach ( $transparent_variables as $transparent_value ) {
940 $grouped_class_names[ $key ][ $group_name ][] = $class_prefix . $prefix . $main_name . '-' . $transparent_value;
941 }
942 }
943 }
944 }
945 }
946 }
947 }
948 }
949
950 $core_framework_main = \get_option( 'core_framework_main', array() );
951 $has_theme = isset( $core_framework_main['has_theme'] ) && $core_framework_main['has_theme'];
952 if ( $has_theme ) {
953 $group_name = 'Theme';
954 $grouped_class_names['otherStyles'][ $group_name ][] = $class_prefix . 'theme-inverted';
955 }
956
957 foreach ( array_keys( $grouped_class_names ) as $key ) {
958 foreach ( array_keys( $grouped_class_names[ $key ] ) as $key2 ) {
959 $grouped_class_names[ $key ][ $key2 ] = array_values( array_unique( $grouped_class_names[ $key ][ $key2 ] ) );
960 if ( empty( $grouped_class_names[ $key ][ $key2 ] ) ) {
961 unset( $grouped_class_names[ $key ][ $key2 ] );
962 }
963 }
964 }
965
966 return $grouped_class_names;
967 }
968
969 /**
970 * @param array $options
971 * - group_by_category: bool (default: true) - if true, returns array of grouped variables, otherwise returns flat array
972 * - excluded_keys: array of ['colorStyles', 'typographyStyles', 'spacingStyles', 'layoutsStyles', 'designStyles', 'componentsStyles', 'otherStyles']
973 * @return array
974 * @since 1.2.7
975 */
976 public function getVariables(array $options = array(
977 'group_by_category' => true,
978 'excluded_keys' => array(),
979 )): array {
980 $options = \wp_parse_args(
981 $options,
982 array(
983 'group_by_category' => true,
984 'excluded_keys' => array(),
985 )
986 );
987
988 $preset = $this->loadPreset();
989
990 if ( ! $preset ) {
991 return array();
992 }
993
994 $variable_prefix = isset( $preset['variablePrefix'] ) ? $preset['variablePrefix'] : '';
995
996 $grouped_variables = array(
997 'colorStyles' => array(),
998 'typographyStyles' => array(),
999 'spacingStyles' => array(),
1000 'layoutsStyles' => array(),
1001 'designStyles' => array(),
1002 'componentsStyles' => array(),
1003 'otherStyles' => array(),
1004 );
1005
1006 if ( isset( $preset['styleSheetData'] ) && is_array( $preset['styleSheetData'] ) ) {
1007 foreach ( $this->stylesheet_data_keys as $key ) {
1008 if ( isset( $preset['styleSheetData'][ $key ] ) && is_array( $preset['styleSheetData'][ $key ] ) ) {
1009 $stylesheet_category = $preset['styleSheetData'][ $key ];
1010
1011 foreach ( $stylesheet_category as $group ) {
1012 $is_variable_group = isset( $group['type'] ) && $group['type'] === 'variable';
1013 $is_group_disabled = isset( $group['isDisabled'] ) && $group['isDisabled'] === true;
1014
1015 if ( ! $is_variable_group || $is_group_disabled ) {
1016 continue;
1017 }
1018
1019 if ( isset( $group['cssObjects'] ) && is_array( $group['cssObjects'] ) ) {
1020 foreach ( $group['cssObjects'] as $cssObject ) {
1021 $is_disabled = isset( $cssObject['isDisabled'] ) && $cssObject['isDisabled'] === true;
1022
1023 if ( $is_disabled ) {
1024 continue;
1025 }
1026
1027 $declarations = isset( $cssObject['declarations'] ) ? $cssObject['declarations'] : array();
1028
1029 if ( ! is_array( $declarations ) || empty( $declarations ) ) {
1030 continue;
1031 }
1032
1033 foreach ( $declarations as $declaration ) {
1034 $property = $declaration['property'];
1035 $grouped_variables[ $key ][] = $variable_prefix . CoreFramework()->str_replace_first( '--', '', $property );
1036 }
1037 }
1038 }
1039 }
1040 }
1041 }
1042 }
1043
1044 if ( isset( $preset['modulesData'] ) && isset( $preset['modulesData']['FLUID_SPACING'] ) ) {
1045 if ( isset( $preset['modulesData']['FLUID_SPACING']['namingConvention'] ) ) {
1046 $single_object = $preset['modulesData']['FLUID_SPACING'];
1047 $preset['modulesData']['FLUID_SPACING']['groups'][] = $single_object;
1048
1049 if ( isset( $preset['modulesData']['FLUID_SPACING']['isDisabled'] ) ) {
1050 $preset['modulesData']['FLUID_SPACING']['isDisabled'] = $single_object['isDisabled'] || false;
1051 }
1052 }
1053
1054 if ( isset( $preset['modulesData']['FLUID_SPACING']['groups'] ) ) {
1055 $is_disabled = isset( $preset['modulesData']['FLUID_SPACING']['isDisabled'] ) && $preset['modulesData']['FLUID_SPACING']['isDisabled'] === true;
1056
1057 foreach ( $preset['modulesData']['FLUID_SPACING']['groups'] as $group ) {
1058 $key = 'spacingStyles';
1059
1060 $is_manual_fluid_spacing = isset( $group['mode'] ) && $group['mode'] === 'fluid_manual';
1061 $is_gen_semantic_variables = isset( $group['genSemanticVariables'] ) && $group['genSemanticVariables'] === true;
1062
1063 if ( $is_gen_semantic_variables ) {
1064 $sematic_variables_css_object_array = isset( $group['semanticVariables'] ) ? $group['semanticVariables'] : array();
1065
1066 foreach ( $sematic_variables_css_object_array as $css_object ) {
1067 $declarations = isset( $css_object['declarations'] ) ? $css_object['declarations'] : array();
1068
1069 if ( ! is_array( $declarations ) || empty( $declarations ) ) {
1070 continue;
1071 }
1072
1073 foreach ( $declarations as $declaration ) {
1074 $property = $declaration['property'];
1075 $grouped_variables[ $key ][] = $variable_prefix . CoreFramework()->str_replace_first( '--', '', $property );
1076 }
1077 }
1078 }
1079
1080 if ( ! $is_disabled && $is_manual_fluid_spacing && isset( $group['manualSizes'] ) ) {
1081 $manual_sizes = $group['manualSizes'];
1082
1083 foreach ( $manual_sizes as $manual_size ) {
1084 $grouped_variables[ $key ][] = $variable_prefix . $manual_size['name'];
1085 }
1086 }
1087
1088 if ( ! $is_disabled && ! $is_manual_fluid_spacing && isset( $group['steps'] ) && isset( $group['namingConvention'] ) ) {
1089 $steps = $group['steps'];
1090 $steps = explode( ',', $steps );
1091 $naming_convention = $group['namingConvention'];
1092
1093 foreach ( $steps as $step ) {
1094 $grouped_variables[ $key ][] = $variable_prefix . $naming_convention . '-' . $step;
1095 }
1096 }
1097 }
1098 }
1099 }
1100
1101 if ( isset( $preset['modulesData'] ) && isset( $preset['modulesData']['FLUID_TYPOGRAPHY'] ) ) {
1102 if ( isset( $preset['modulesData']['FLUID_TYPOGRAPHY']['namingConvention'] ) ) {
1103 $single_object = $preset['modulesData']['FLUID_TYPOGRAPHY'];
1104 $preset['modulesData']['FLUID_TYPOGRAPHY']['groups'][] = $single_object;
1105
1106 if ( isset( $preset['modulesData']['FLUID_TYPOGRAPHY']['isDisabled'] ) ) {
1107 $preset['modulesData']['FLUID_TYPOGRAPHY']['isDisabled'] = $single_object['isDisabled'] || false;
1108 }
1109 }
1110
1111 if ( isset( $preset['modulesData']['FLUID_TYPOGRAPHY']['groups'] ) ) {
1112 $is_disabled = isset( $preset['modulesData']['FLUID_TYPOGRAPHY']['isDisabled'] ) && $preset['modulesData']['FLUID_TYPOGRAPHY']['isDisabled'] === true;
1113
1114 foreach ( $preset['modulesData']['FLUID_TYPOGRAPHY']['groups'] as $group ) {
1115 $key = 'typographyStyles';
1116
1117 $typography_data = $group;
1118 $is_manual_fluid_spacing = isset( $typography_data['mode'] ) && $typography_data['mode'] === 'fluid_manual';
1119 $is_gen_semantic_variables = isset( $typography_data['genSemanticVariables'] ) && $typography_data['genSemanticVariables'] === true;
1120
1121 if ( $is_gen_semantic_variables ) {
1122 $sematic_variables_css_object_array = isset( $typography_data['semanticVariables'] ) ? $typography_data['semanticVariables'] : array();
1123
1124 foreach ( $sematic_variables_css_object_array as $css_object ) {
1125 $declarations = isset( $css_object['declarations'] ) ? $css_object['declarations'] : array();
1126
1127 if ( ! is_array( $declarations ) || empty( $declarations ) ) {
1128 continue;
1129 }
1130
1131 foreach ( $declarations as $declaration ) {
1132 $property = $declaration['property'];
1133 $grouped_variables[ $key ][] = $variable_prefix . CoreFramework()->str_replace_first( '--', '', $property );
1134 }
1135 }
1136 }
1137
1138 if ( ! $is_disabled && $is_manual_fluid_spacing && isset( $typography_data['manualSizes'] ) ) {
1139 $manual_sizes = $typography_data['manualSizes'];
1140
1141 foreach ( $manual_sizes as $manual_size ) {
1142 $grouped_variables[ $key ][] = $variable_prefix . $manual_size['name'];
1143 }
1144 }
1145
1146 if ( ! $is_disabled && ! $is_manual_fluid_spacing && isset( $typography_data['steps'] ) && isset( $typography_data['namingConvention'] ) ) {
1147 $steps = $typography_data['steps'];
1148 $steps = explode( ',', $steps );
1149 $naming_convention = $typography_data['namingConvention'];
1150
1151 foreach ( $steps as $step ) {
1152 $grouped_variables[ $key ][] = $variable_prefix . $naming_convention . '-' . $step;
1153 }
1154 }
1155 }
1156 }
1157 }
1158
1159 if ( isset( $preset['modulesData'] ) && isset( $preset['modulesData']['COLOR_SYSTEM'] ) && isset( $preset['modulesData']['COLOR_SYSTEM']['groups'] ) ) {
1160 foreach ( $preset['modulesData']['COLOR_SYSTEM']['groups'] as $group ) {
1161 if ( isset( $group['isDisabled'] ) && $group['isDisabled'] ) {
1162 continue;
1163 }
1164
1165 if ( isset( $group['colors'] ) && is_array( $group['colors'] ) ) {
1166 foreach ( $group['colors'] as $color ) {
1167 $name = $color['name'];
1168 $grouped_variables['colorStyles'][] = $variable_prefix . $name;
1169
1170 $shades = isset( $color['shades'] ) ? $color['shades'] : array();
1171 foreach ( $shades as $shade ) {
1172 $grouped_variables['colorStyles'][] = $variable_prefix . $shade['name'];
1173 }
1174
1175 $tints = isset( $color['tints'] ) ? $color['tints'] : array();
1176 foreach ( $tints as $tint ) {
1177 $grouped_variables['colorStyles'][] = $variable_prefix . $tint['name'];
1178 }
1179
1180 $is_transparent = isset( $color['transparent'] ) && $color['transparent'] === true;
1181 $transparent_variables = isset( $color['transparentVariables'] ) ? $color['transparentVariables'] : array();
1182
1183 if ( $is_transparent ) {
1184 foreach ( $transparent_variables as $transparent_variable ) {
1185 $grouped_variables['colorStyles'][] = $variable_prefix . $name . '-' . $transparent_variable;
1186 }
1187 }
1188 }
1189 }
1190 }
1191 }
1192
1193 // Add screen width preference variables
1194 if ( isset( $preset['preferences'] ) ) {
1195 if ( isset( $preset['preferences']['min_screen_width'] ) && $preset['preferences']['min_screen_width'] ) {
1196 $grouped_variables['layoutsStyles'][] = 'min-screen-width';
1197 }
1198 if ( isset( $preset['preferences']['max_screen_width'] ) && $preset['preferences']['max_screen_width'] ) {
1199 $grouped_variables['layoutsStyles'][] = 'max-screen-width';
1200 }
1201 }
1202
1203 foreach ( array_keys( $grouped_variables ) as $key ) {
1204 $grouped_variables[ $key ] = array_unique( $grouped_variables[ $key ] );
1205 }
1206
1207 if ( isset( $options['excluded_keys'] ) && is_array( $options['excluded_keys'] ) && ! empty( $options['excluded_keys'] ) ) {
1208 foreach ( $options['excluded_keys'] as $excluded_key ) {
1209 if ( isset( $grouped_variables[ $excluded_key ] ) ) {
1210 unset( $grouped_variables[ $excluded_key ] );
1211 }
1212 }
1213 }
1214
1215 if ( $options['group_by_category'] === false ) {
1216 $grouped_variables = array_merge( ...array_values( $grouped_variables ?? array() ) );
1217 }
1218
1219 return $grouped_variables ?? array();
1220 }
1221
1222 public function getVariablesGroupedByCategoriesAndGroups(array $options = array(
1223 'group_by_category' => true,
1224 'excluded_keys' => array(),
1225 'exclude_color_system_variables' => false,
1226 )): array {
1227 $options = \wp_parse_args(
1228 $options,
1229 array(
1230 'group_by_category' => true,
1231 'excluded_keys' => array(),
1232 'exclude_color_system_variables' => false,
1233 )
1234 );
1235
1236 $preset = $this->loadPreset();
1237
1238 if ( ! $preset ) {
1239 return array();
1240 }
1241
1242 $variable_prefix = isset( $preset['variablePrefix'] ) ? $preset['variablePrefix'] : '';
1243
1244 $grouped_variables = array(
1245 'colorStyles' => array(),
1246 'typographyStyles' => array(),
1247 'spacingStyles' => array(),
1248 'layoutsStyles' => array(),
1249 'designStyles' => array(),
1250 'componentsStyles' => array(),
1251 'otherStyles' => array(),
1252 );
1253
1254 $protected_group_names = array( 'Fluid Layouts', 'Fluid Typography', 'Fluid Spacing', 'Color System', 'Components' );
1255
1256 if ( isset( $preset['styleSheetData'] ) && is_array( $preset['styleSheetData'] ) ) {
1257 foreach ( $this->stylesheet_data_keys as $key ) {
1258 if ( isset( $preset['styleSheetData'][ $key ] ) && is_array( $preset['styleSheetData'][ $key ] ) ) {
1259 $stylesheet_category = $preset['styleSheetData'][ $key ];
1260
1261 $i = 1;
1262
1263 foreach ( $stylesheet_category as $group ) {
1264 ++$i;
1265 $is_variable_group = isset( $group['type'] ) && $group['type'] === 'variable';
1266 $is_group_disabled = isset( $group['isDisabled'] ) && $group['isDisabled'] === true;
1267
1268 $group_name = isset( $group['name'] ) ? $group['name'] : 'No name';
1269
1270 if ( array_search( $group_name, $protected_group_names ) !== false ) {
1271 $group_name = $group_name . ' (' . $i . ')';
1272 }
1273
1274 if ( ! $is_variable_group || $is_group_disabled ) {
1275 continue;
1276 }
1277
1278 if ( isset( $group['cssObjects'] ) && is_array( $group['cssObjects'] ) ) {
1279 foreach ( $group['cssObjects'] as $cssObject ) {
1280 $is_disabled = isset( $cssObject['isDisabled'] ) && $cssObject['isDisabled'] === true;
1281
1282 if ( $is_disabled ) {
1283 continue;
1284 }
1285
1286 $declarations = isset( $cssObject['declarations'] ) ? $cssObject['declarations'] : array();
1287
1288 if ( ! is_array( $declarations ) || empty( $declarations ) ) {
1289 continue;
1290 }
1291
1292 foreach ( $declarations as $declaration ) {
1293 $property = $declaration['property'];
1294 $grouped_variables[ $key ][ $group_name ][] = $variable_prefix . CoreFramework()->str_replace_first( '--', '', $property );
1295 }
1296 }
1297 }
1298 }
1299 }
1300 }
1301 }
1302
1303 if ( isset( $preset['modulesData'] ) && isset( $preset['modulesData']['FLUID_SPACING'] ) ) {
1304 if ( isset( $preset['modulesData']['FLUID_SPACING']['namingConvention'] ) ) {
1305 $single_object = $preset['modulesData']['FLUID_SPACING'];
1306 $preset['modulesData']['FLUID_SPACING']['groups'][] = $single_object;
1307
1308 if ( isset( $preset['modulesData']['FLUID_SPACING']['isDisabled'] ) ) {
1309 $preset['modulesData']['FLUID_SPACING']['isDisabled'] = $single_object['isDisabled'] || false;
1310 }
1311 }
1312
1313 if ( isset( $preset['modulesData']['FLUID_SPACING']['groups'] ) ) {
1314 $is_disabled = isset( $preset['modulesData']['FLUID_SPACING']['isDisabled'] ) && $preset['modulesData']['FLUID_SPACING']['isDisabled'] === true;
1315
1316 foreach ( $preset['modulesData']['FLUID_SPACING']['groups'] as $group ) {
1317 $key = 'spacingStyles';
1318 $group_name = 'Fluid Spacing';
1319
1320 $spacing_data = $group;
1321 $is_manual_fluid_spacing = isset( $spacing_data['mode'] ) && $spacing_data['mode'] === 'fluid_manual';
1322 $is_gen_semantic_variables = isset( $spacing_data['genSemanticVariables'] ) && $spacing_data['genSemanticVariables'] === true;
1323
1324 if ( $is_gen_semantic_variables ) {
1325 $sematic_variables_group_name = 'Contextual Spacing Variables';
1326 $sematic_variables_css_object_array = isset( $spacing_data['semanticVariables'] ) ? $spacing_data['semanticVariables'] : array();
1327
1328 foreach ( $sematic_variables_css_object_array as $css_object ) {
1329 $declarations = isset( $css_object['declarations'] ) ? $css_object['declarations'] : array();
1330
1331 if ( ! is_array( $declarations ) || empty( $declarations ) ) {
1332 continue;
1333 }
1334
1335 foreach ( $declarations as $declaration ) {
1336 $property = $declaration['property'];
1337 $grouped_variables[ $key ][ $sematic_variables_group_name ][] = $variable_prefix . CoreFramework()->str_replace_first( '--', '', $property );
1338 }
1339 }
1340 }
1341
1342 if ( ! $is_disabled && $is_manual_fluid_spacing && isset( $spacing_data['manualSizes'] ) ) {
1343 $manual_sizes = $spacing_data['manualSizes'];
1344 foreach ( $manual_sizes as $manual_size ) {
1345 $grouped_variables[ $key ][ $group_name ][] = $variable_prefix . $manual_size['name'];
1346 }
1347 }
1348
1349 if ( ! $is_disabled && ! $is_manual_fluid_spacing && isset( $spacing_data['steps'] ) && isset( $spacing_data['namingConvention'] ) ) {
1350 $steps = $spacing_data['steps'];
1351 $steps = explode( ',', $steps );
1352 $naming_convention = $spacing_data['namingConvention'];
1353
1354 foreach ( $steps as $step ) {
1355 $grouped_variables[ $key ][ $group_name ][] = $variable_prefix . $naming_convention . '-' . $step;
1356 }
1357 }
1358 }
1359 }
1360 }
1361
1362 if ( isset( $preset['modulesData'] ) && isset( $preset['modulesData']['FLUID_TYPOGRAPHY'] ) ) {
1363 if ( isset( $preset['modulesData']['FLUID_TYPOGRAPHY']['namingConvention'] ) ) {
1364 $single_object = $preset['modulesData']['FLUID_TYPOGRAPHY'];
1365 $preset['modulesData']['FLUID_TYPOGRAPHY']['groups'][] = $single_object;
1366
1367 if ( isset( $preset['modulesData']['FLUID_TYPOGRAPHY']['isDisabled'] ) ) {
1368 $preset['modulesData']['FLUID_TYPOGRAPHY']['isDisabled'] = $single_object['isDisabled'] || false;
1369 }
1370 }
1371
1372 if ( isset( $preset['modulesData']['FLUID_TYPOGRAPHY']['groups'] ) ) {
1373 $is_disabled = isset( $preset['modulesData']['FLUID_TYPOGRAPHY']['isDisabled'] ) && $preset['modulesData']['FLUID_TYPOGRAPHY']['isDisabled'] === true;
1374
1375 foreach ( $preset['modulesData']['FLUID_TYPOGRAPHY']['groups'] as $group ) {
1376 $key = 'typographyStyles';
1377 $group_name = 'Fluid Typography';
1378
1379 $typography_data = $group;
1380 $is_manual_fluid_spacing = isset( $typography_data['mode'] ) && $typography_data['mode'] === 'fluid_manual';
1381 $is_gen_semantic_variables = isset( $typography_data['genSemanticVariables'] ) && $typography_data['genSemanticVariables'] === true;
1382
1383 if ( $is_gen_semantic_variables ) {
1384 $sematic_variables_group_name = 'Contextual Typography Variables';
1385 $sematic_variables_css_object_array = isset( $typography_data['semanticVariables'] ) ? $typography_data['semanticVariables'] : array();
1386
1387 foreach ( $sematic_variables_css_object_array as $css_object ) {
1388 $declarations = isset( $css_object['declarations'] ) ? $css_object['declarations'] : array();
1389
1390 if ( ! is_array( $declarations ) || empty( $declarations ) ) {
1391 continue;
1392 }
1393
1394 foreach ( $declarations as $declaration ) {
1395 $property = $declaration['property'];
1396 $grouped_variables[ $key ][ $sematic_variables_group_name ][] = $variable_prefix . CoreFramework()->str_replace_first( '--', '', $property );
1397 }
1398 }
1399 }
1400
1401 if ( ! $is_disabled && $is_manual_fluid_spacing && isset( $typography_data['manualSizes'] ) ) {
1402 $manual_sizes = $typography_data['manualSizes'];
1403
1404 foreach ( $manual_sizes as $manual_size ) {
1405 $grouped_variables[ $key ][ $group_name ][] = $variable_prefix . $manual_size['name'];
1406 }
1407 }
1408
1409 if ( ! $is_disabled && ! $is_manual_fluid_spacing && isset( $typography_data['steps'] ) && isset( $typography_data['namingConvention'] ) ) {
1410 $steps = $typography_data['steps'];
1411 $steps = explode( ',', $steps );
1412 $naming_convention = $typography_data['namingConvention'];
1413
1414 foreach ( $steps as $step ) {
1415 $grouped_variables[ $key ][ $group_name ][] = $variable_prefix . $naming_convention . '-' . $step;
1416 }
1417 }
1418 }
1419 }
1420 }
1421
1422 if ( $options['exclude_color_system_variables'] === false ) {
1423 if ( isset( $preset['modulesData'] ) && isset( $preset['modulesData']['COLOR_SYSTEM'] ) && isset( $preset['modulesData']['COLOR_SYSTEM']['groups'] ) ) {
1424 foreach ( $preset['modulesData']['COLOR_SYSTEM']['groups'] as $group ) {
1425 if ( isset( $group['isDisabled'] ) && $group['isDisabled'] ) {
1426 continue;
1427 }
1428
1429 if ( isset( $group['colors'] ) && is_array( $group['colors'] ) ) {
1430 $group_name = isset( $group['name'] ) ? $group['name'] : 'No name';
1431 foreach ( $group['colors'] as $color ) {
1432 $name = $color['name'];
1433 $grouped_variables['colorStyles'][ $group_name ][] = $variable_prefix . $name;
1434
1435 $shades = isset( $color['shades'] ) ? $color['shades'] : array();
1436 foreach ( $shades as $shade ) {
1437 $grouped_variables['colorStyles'][ $group_name ][] = $variable_prefix . $shade['name'];
1438 }
1439
1440 $tints = isset( $color['tints'] ) ? $color['tints'] : array();
1441 foreach ( $tints as $tint ) {
1442 $grouped_variables['colorStyles'][ $group_name ][] = $variable_prefix . $tint['name'];
1443 }
1444
1445 $is_transparent = isset( $color['transparent'] ) && $color['transparent'] === true;
1446 $transparent_variables = isset( $color['transparentVariables'] ) ? $color['transparentVariables'] : array();
1447
1448 if ( $is_transparent ) {
1449 foreach ( $transparent_variables as $transparent_variable ) {
1450 $grouped_variables['colorStyles'][ $group_name ][] = $variable_prefix . $name . '-' . $transparent_variable;
1451 }
1452 }
1453 }
1454 }
1455 }
1456 }
1457 }
1458
1459 // Add screen width preference variables
1460 if ( isset( $preset['preferences'] ) ) {
1461 $preferences_group_name = 'Preferences';
1462 if ( isset( $preset['preferences']['min_screen_width'] ) && $preset['preferences']['min_screen_width'] ) {
1463 $grouped_variables['layoutsStyles'][ $preferences_group_name ][] = 'min-screen-width';
1464 }
1465 if ( isset( $preset['preferences']['max_screen_width'] ) && $preset['preferences']['max_screen_width'] ) {
1466 $grouped_variables['layoutsStyles'][ $preferences_group_name ][] = 'max-screen-width';
1467 }
1468 }
1469
1470 if ( isset( $options['excluded_keys'] ) && is_array( $options['excluded_keys'] ) && ! empty( $options['excluded_keys'] ) ) {
1471 foreach ( $options['excluded_keys'] as $excluded_key ) {
1472 if ( isset( $grouped_variables[ $excluded_key ] ) ) {
1473 unset( $grouped_variables[ $excluded_key ] );
1474 }
1475 }
1476 }
1477
1478 if ( $options['group_by_category'] === false ) {
1479 $grouped_variables = array_merge( ...array_values( $grouped_variables ?? array() ) );
1480 }
1481
1482 return $grouped_variables ?? array();
1483 }
1484
1485 public function getVariableString(): string {
1486 $cssString = get_option( 'core_framework_selected_preset_backup', '' );
1487
1488 if ( ! $cssString ) {
1489 return '';
1490 }
1491
1492 $pattern = '/(:root[^{]*\{[^}]*--[^}]*\})/s';
1493 preg_match_all( $pattern, $cssString, $matches );
1494
1495 return implode( "\n\n", $matches[0] );
1496 }
1497 }
1498