PluginProbe
Elementor Website Builder – more than just a page builder / 3.4.0-dev9
Elementor Website Builder – more than just a page builder v3.4.0-dev9
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 / core / files / css / base.php

base.php in Elementor Website Builder – more than just a page builder 3.4.0-dev9, at core/files/css/base.php

934 lines 24.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Core\Files\CSS;
3
4 use Elementor\Base_Data_Control;
5 use Elementor\Control_Repeater;
6 use Elementor\Controls_Manager;
7 use Elementor\Controls_Stack;
8 use Elementor\Core\Files\Base as Base_File;
9 use Elementor\Core\DynamicTags\Manager;
10 use Elementor\Core\DynamicTags\Tag;
11 use Elementor\Core\Kits\Documents\Tabs\Global_Typography;
12 use Elementor\Element_Base;
13 use Elementor\Plugin;
14 use Elementor\Stylesheet;
15 use Elementor\Icons_Manager;
16
17 if ( ! defined( 'ABSPATH' ) ) {
18 exit; // Exit if accessed directly.
19 }
20
21 /**
22 * Elementor CSS file.
23 *
24 * Elementor CSS file handler class is responsible for generating CSS files.
25 *
26 * @since 1.2.0
27 * @abstract
28 */
29 abstract class Base extends Base_File {
30
31 /**
32 * Elementor CSS file generated status.
33 *
34 * The parsing result after generating CSS file.
35 */
36 const CSS_STATUS_FILE = 'file';
37
38 /**
39 * Elementor inline CSS status.
40 *
41 * The parsing result after generating inline CSS.
42 */
43 const CSS_STATUS_INLINE = 'inline';
44
45 /**
46 * Elementor CSS empty status.
47 *
48 * The parsing result when an empty CSS returned.
49 */
50 const CSS_STATUS_EMPTY = 'empty';
51
52 /**
53 * Fonts.
54 *
55 * Holds the list of fonts.
56 *
57 * @access private
58 *
59 * @var array
60 */
61 private $fonts = [];
62
63 private $icons_fonts = [];
64
65 private $dynamic_elements_ids = [];
66
67 /**
68 * Stylesheet object.
69 *
70 * Holds the CSS file stylesheet instance.
71 *
72 * @access protected
73 *
74 * @var Stylesheet
75 */
76 protected $stylesheet_obj;
77
78 /**
79 * Printed.
80 *
81 * Holds the list of printed files.
82 *
83 * @access protected
84 *
85 * @var array
86 */
87 private static $printed = [];
88
89 /**
90 * Get CSS file name.
91 *
92 * Retrieve the CSS file name.
93 *
94 * @since 1.6.0
95 * @access public
96 * @abstract
97 */
98 abstract public function get_name();
99
100 protected function is_global_parsing_supported() {
101 return false;
102 }
103
104 /**
105 * Use external file.
106 *
107 * Whether to use external CSS file of not. When there are new schemes or settings
108 * updates.
109 *
110 * @since 1.9.0
111 * @access protected
112 *
113 * @return bool True if the CSS requires an update, False otherwise.
114 */
115 protected function use_external_file() {
116 return 'internal' !== get_option( 'elementor_css_print_method' );
117 }
118
119 /**
120 * Update the CSS file.
121 *
122 * Delete old CSS, parse the CSS, save the new file and update the database.
123 *
124 * This method also sets the CSS status to be used later on in the render posses.
125 *
126 * @since 1.2.0
127 * @access public
128 */
129 public function update() {
130 $this->update_file();
131
132 $meta = $this->get_meta();
133
134 $meta['time'] = time();
135
136 $content = $this->get_content();
137
138 if ( empty( $content ) ) {
139 $meta['status'] = self::CSS_STATUS_EMPTY;
140 $meta['css'] = '';
141 } else {
142 $use_external_file = $this->use_external_file();
143
144 if ( $use_external_file ) {
145 $meta['status'] = self::CSS_STATUS_FILE;
146 } else {
147 $meta['status'] = self::CSS_STATUS_INLINE;
148 $meta['css'] = $content;
149 }
150 }
151
152 $meta['dynamic_elements_ids'] = $this->dynamic_elements_ids;
153
154 $this->update_meta( $meta );
155 }
156
157 /**
158 * @since 2.1.0
159 * @access public
160 */
161 public function write() {
162 if ( $this->use_external_file() ) {
163 parent::write();
164 }
165 }
166
167 /**
168 * @since 3.0.0
169 * @access public
170 */
171 public function delete() {
172 if ( $this->use_external_file() ) {
173 parent::delete();
174 } else {
175 $this->delete_meta();
176 }
177 }
178
179 /**
180 * Get Responsive Control Duplication Mode
181 *
182 * @since 3.4.0
183 *
184 * @return string
185 */
186 protected function get_responsive_control_duplication_mode() {
187 return 'on';
188 }
189
190 /**
191 * Enqueue CSS.
192 *
193 * Either enqueue the CSS file in Elementor or add inline style.
194 *
195 * This method is also responsible for loading the fonts.
196 *
197 * @since 1.2.0
198 * @access public
199 */
200 public function enqueue() {
201 $handle_id = $this->get_file_handle_id();
202
203 if ( isset( self::$printed[ $handle_id ] ) ) {
204 return;
205 }
206
207 self::$printed[ $handle_id ] = true;
208
209 $meta = $this->get_meta();
210
211 if ( self::CSS_STATUS_EMPTY === $meta['status'] ) {
212 return;
213 }
214
215 // First time after clear cache and etc.
216 if ( '' === $meta['status'] || $this->is_update_required() ) {
217 $this->update();
218
219 $meta = $this->get_meta();
220 }
221
222 if ( self::CSS_STATUS_INLINE === $meta['status'] ) {
223 $dep = $this->get_inline_dependency();
224 // If the dependency has already been printed ( like a template in footer )
225 if ( wp_styles()->query( $dep, 'done' ) ) {
226 printf( '<style id="%1$s">%2$s</style>', $this->get_file_handle_id(), $meta['css'] ); // XSS ok.
227 } else {
228 wp_add_inline_style( $dep, $meta['css'] );
229 }
230 } elseif ( self::CSS_STATUS_FILE === $meta['status'] ) { // Re-check if it's not empty after CSS update.
231 wp_enqueue_style( $this->get_file_handle_id(), $this->get_url(), $this->get_enqueue_dependencies(), null ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
232 }
233
234 // Handle fonts.
235 if ( ! empty( $meta['fonts'] ) ) {
236 foreach ( $meta['fonts'] as $font ) {
237 Plugin::$instance->frontend->enqueue_font( $font );
238 }
239 }
240
241 if ( ! empty( $meta['icons'] ) ) {
242 $icons_types = Icons_Manager::get_icon_manager_tabs();
243 foreach ( $meta['icons'] as $icon_font ) {
244 if ( ! isset( $icons_types[ $icon_font ] ) ) {
245 continue;
246 }
247 Plugin::$instance->frontend->enqueue_font( $icon_font );
248 }
249 }
250
251 $name = $this->get_name();
252
253 /**
254 * Enqueue CSS file.
255 *
256 * Fires when CSS file is enqueued on Elementor.
257 *
258 * The dynamic portion of the hook name, `$name`, refers to the CSS file name.
259 *
260 * @since 2.0.0
261 *
262 * @param Base $this The current CSS file.
263 */
264 do_action( "elementor/css-file/{$name}/enqueue", $this );
265 }
266
267 /**
268 * Print CSS.
269 *
270 * Output the final CSS inside the `<style>` tags and all the frontend fonts in
271 * use.
272 *
273 * @since 1.9.4
274 * @access public
275 */
276 public function print_css() {
277 echo '<style>' . $this->get_content() . '</style>'; // XSS ok.
278 Plugin::$instance->frontend->print_fonts_links();
279 }
280
281 /**
282 * Add control rules.
283 *
284 * Parse the CSS for all the elements inside any given control.
285 *
286 * This method recursively renders the CSS for all the selectors in the control.
287 *
288 * @since 1.2.0
289 * @access public
290 *
291 * @param array $control The controls.
292 * @param array $controls_stack The controls stack.
293 * @param callable $value_callback Callback function for the value.
294 * @param array $placeholders Placeholders.
295 * @param array $replacements Replacements.
296 * @param array $values Global Values.
297 */
298 public function add_control_rules( array $control, array $controls_stack, callable $value_callback, array $placeholders, array $replacements, array $values = [] ) {
299 if ( empty( $control['selectors'] ) ) {
300 return;
301 }
302
303 $control_global_key = $control['name'];
304
305 if ( ! empty( $control['groupType'] ) ) {
306 $control_global_key = $control['groupPrefix'] . $control['groupType'];
307 }
308
309 $global_values = [];
310 $global_key = '';
311
312 if ( ! empty( $values['__globals__'] ) ) {
313 $global_values = $values['__globals__'];
314 }
315
316 if ( ! empty( $global_values[ $control_global_key ] ) ) {
317 $global_key = $global_values[ $control_global_key ];
318 }
319
320 if ( ! $global_key ) {
321 $value = call_user_func( $value_callback, $control );
322
323 if ( null === $value ) {
324 return;
325 }
326 }
327
328 $stylesheet = $this->get_stylesheet();
329
330 foreach ( $control['selectors'] as $selector => $css_property ) {
331 $output_css_property = '';
332
333 if ( $global_key ) {
334 $selector_global_value = $this->get_selector_global_value( $control, $global_key );
335
336 if ( $selector_global_value ) {
337 $output_css_property = preg_replace( '/(:)[^;]+(;?)/', '$1' . $selector_global_value . '$2', $css_property );
338 }
339 } else {
340 try {
341 $output_css_property = preg_replace_callback( '/{{(?:([^.}]+)\.)?([^}| ]*)(?: *\|\| *(?:([^.}]+)\.)?([^}| ]*) *)*}}/', function( $matches ) use ( $control, $value_callback, $controls_stack, $value, $css_property ) {
342 $external_control_missing = $matches[1] && ! isset( $controls_stack[ $matches[1] ] );
343
344 $parsed_value = '';
345
346 if ( ! $external_control_missing ) {
347 $parsed_value = $this->parse_property_placeholder( $control, $value, $controls_stack, $value_callback, $matches[2], $matches[1] );
348 }
349
350 if ( '' === $parsed_value ) {
351 if ( isset( $matches[4] ) ) {
352 $parsed_value = $matches[4];
353
354 $is_string_value = preg_match( '/^([\'"])(.*)\1$/', $parsed_value, $string_matches );
355
356 if ( $is_string_value ) {
357 $parsed_value = $string_matches[2];
358 } elseif ( ! is_numeric( $parsed_value ) ) {
359 if ( $matches[3] && ! isset( $controls_stack[ $matches[3] ] ) ) {
360 return '';
361 }
362
363 $parsed_value = $this->parse_property_placeholder( $control, $value, $controls_stack, $value_callback, $matches[4], $matches[3] );
364 }
365 }
366
367 if ( '' === $parsed_value ) {
368 if ( $external_control_missing ) {
369 return '';
370 }
371
372 throw new \Exception();
373 }
374 }
375
376 return $parsed_value;
377 }, $css_property );
378 } catch ( \Exception $e ) {
379 return;
380 }
381 }
382
383 if ( ! $output_css_property ) {
384 continue;
385 }
386
387 $device_pattern = '/^(?:\([^\)]+\)){1,2}/';
388
389 preg_match( $device_pattern, $selector, $device_rules );
390
391 $query = [];
392
393 if ( $device_rules ) {
394 $selector = preg_replace( $device_pattern, '', $selector );
395
396 preg_match_all( '/\(([^)]+)\)/', $device_rules[0], $pure_device_rules );
397
398 $pure_device_rules = $pure_device_rules[1];
399
400 foreach ( $pure_device_rules as $device_rule ) {
401 if ( Element_Base::RESPONSIVE_DESKTOP === $device_rule ) {
402 continue;
403 }
404
405 $device = preg_replace( '/\+$/', '', $device_rule );
406
407 $endpoint = $device === $device_rule ? 'max' : 'min';
408
409 $query[ $endpoint ] = $device;
410 }
411 }
412
413 $parsed_selector = str_replace( $placeholders, $replacements, $selector );
414
415 if ( ! $query && ! empty( $control['responsive'] ) ) {
416 $query = array_intersect_key( $control['responsive'], array_flip( [ 'min', 'max' ] ) );
417
418 if ( ! empty( $query['max'] ) && Element_Base::RESPONSIVE_DESKTOP === $query['max'] ) {
419 unset( $query['max'] );
420 }
421 }
422
423 $stylesheet->add_rules( $parsed_selector, $output_css_property, $query );
424 }
425 }
426
427 /**
428 * @param array $control
429 * @param mixed $value
430 * @param array $controls_stack
431 * @param callable $value_callback
432 * @param string $placeholder
433 * @param string $parser_control_name
434 *
435 * @return string
436 */
437 public function parse_property_placeholder( array $control, $value, array $controls_stack, $value_callback, $placeholder, $parser_control_name = null ) {
438 if ( $parser_control_name ) {
439 $control = $controls_stack[ $parser_control_name ];
440
441 $value = call_user_func( $value_callback, $control );
442 }
443
444 if ( Controls_Manager::FONT === $control['type'] ) {
445 $this->fonts[] = $value;
446 }
447
448 /** @var Base_Data_Control $control_obj */
449 $control_obj = Plugin::$instance->controls_manager->get_control( $control['type'] );
450
451 return (string) $control_obj->get_style_value( $placeholder, $value, $control );
452 }
453
454 /**
455 * Get the fonts.
456 *
457 * Retrieve the list of fonts.
458 *
459 * @since 1.9.0
460 * @access public
461 *
462 * @return array Fonts.
463 */
464 public function get_fonts() {
465 return $this->fonts;
466 }
467
468 /**
469 * Get stylesheet.
470 *
471 * Retrieve the CSS file stylesheet instance.
472 *
473 * @since 1.2.0
474 * @access public
475 *
476 * @return Stylesheet The stylesheet object.
477 */
478 public function get_stylesheet() {
479 if ( ! $this->stylesheet_obj ) {
480 $this->init_stylesheet();
481 }
482
483 return $this->stylesheet_obj;
484 }
485
486 /**
487 * Add controls stack style rules.
488 *
489 * Parse the CSS for all the elements inside any given controls stack.
490 *
491 * This method recursively renders the CSS for all the child elements in the stack.
492 *
493 * @since 1.6.0
494 * @access public
495 *
496 * @param Controls_Stack $controls_stack The controls stack.
497 * @param array $controls Controls array.
498 * @param array $values Values array.
499 * @param array $placeholders Placeholders.
500 * @param array $replacements Replacements.
501 * @param array $all_controls All controls.
502 */
503 public function add_controls_stack_style_rules( Controls_Stack $controls_stack, array $controls, array $values, array $placeholders, array $replacements, array $all_controls = null ) {
504 if ( ! $all_controls ) {
505 $all_controls = $controls_stack->get_controls();
506 }
507
508 $parsed_dynamic_settings = $controls_stack->parse_dynamic_settings( $values, $controls );
509
510 foreach ( $controls as $control ) {
511 if ( ! empty( $control['style_fields'] ) ) {
512 $this->add_repeater_control_style_rules( $controls_stack, $control, $values[ $control['name'] ], $placeholders, $replacements );
513 }
514
515 if ( ! empty( $control[ Manager::DYNAMIC_SETTING_KEY ][ $control['name'] ] ) ) {
516 $this->add_dynamic_control_style_rules( $control, $control[ Manager::DYNAMIC_SETTING_KEY ][ $control['name'] ] );
517 }
518
519 if ( Controls_Manager::ICONS === $control['type'] ) {
520 $this->icons_fonts[] = $values[ $control['name'] ]['library'];
521 }
522
523 if ( ! empty( $parsed_dynamic_settings[ Manager::DYNAMIC_SETTING_KEY ][ $control['name'] ] ) ) {
524 // Dynamic CSS should not be added to the CSS files.
525 // Instead it's handled by \Elementor\Core\DynamicTags\Dynamic_CSS
526 // and printed in a style tag.
527 unset( $parsed_dynamic_settings[ $control['name'] ] );
528
529 $this->dynamic_elements_ids[] = $controls_stack->get_id();
530
531 continue;
532 }
533
534 if ( empty( $control['selectors'] ) ) {
535 continue;
536 }
537
538 $this->add_control_style_rules( $control, $parsed_dynamic_settings, $all_controls, $placeholders, $replacements );
539 }
540 }
541
542 /**
543 * Get file handle ID.
544 *
545 * Retrieve the file handle ID.
546 *
547 * @since 1.2.0
548 * @access protected
549 * @abstract
550 *
551 * @return string CSS file handle ID.
552 */
553 abstract protected function get_file_handle_id();
554
555 /**
556 * Render CSS.
557 *
558 * Parse the CSS.
559 *
560 * @since 1.2.0
561 * @access protected
562 * @abstract
563 */
564 abstract protected function render_css();
565
566 protected function get_default_meta() {
567 return array_merge( parent::get_default_meta(), [
568 'fonts' => array_unique( $this->fonts ),
569 'icons' => array_unique( $this->icons_fonts ),
570 'dynamic_elements_ids' => [],
571 'status' => '',
572 ] );
573 }
574
575 /**
576 * Get enqueue dependencies.
577 *
578 * Retrieve the name of the stylesheet used by `wp_enqueue_style()`.
579 *
580 * @since 1.2.0
581 * @access protected
582 *
583 * @return array Name of the stylesheet.
584 */
585 protected function get_enqueue_dependencies() {
586 return [];
587 }
588
589 /**
590 * Get inline dependency.
591 *
592 * Retrieve the name of the stylesheet used by `wp_add_inline_style()`.
593 *
594 * @since 1.2.0
595 * @access protected
596 *
597 * @return string Name of the stylesheet.
598 */
599 protected function get_inline_dependency() {
600 return '';
601 }
602
603 /**
604 * Is update required.
605 *
606 * Whether the CSS requires an update. When there are new schemes or settings
607 * updates.
608 *
609 * @since 1.2.0
610 * @access protected
611 *
612 * @return bool True if the CSS requires an update, False otherwise.
613 */
614 protected function is_update_required() {
615 return false;
616 }
617
618 /**
619 * Parse CSS.
620 *
621 * Parsing the CSS file.
622 *
623 * @since 1.2.0
624 * @access protected
625 */
626 protected function parse_content() {
627 $initial_responsive_controls_duplication_mode = Plugin::$instance->breakpoints->get_responsive_control_duplication_mode();
628
629 Plugin::$instance->breakpoints->set_responsive_control_duplication_mode( $this->get_responsive_control_duplication_mode() );
630
631 $this->render_css();
632
633 $name = $this->get_name();
634
635 /**
636 * Parse CSS file.
637 *
638 * Fires when CSS file is parsed on Elementor.
639 *
640 * The dynamic portion of the hook name, `$name`, refers to the CSS file name.
641 *
642 * @since 2.0.0
643 *
644 * @param Base $this The current CSS file.
645 */
646 do_action( "elementor/css-file/{$name}/parse", $this );
647
648 Plugin::$instance->breakpoints->set_responsive_control_duplication_mode( $initial_responsive_controls_duplication_mode );
649
650 return $this->get_stylesheet()->__toString();
651 }
652
653 /**
654 * Add control style rules.
655 *
656 * Register new style rules for the control.
657 *
658 * @since 1.6.0
659 * @access private
660 *
661 * @param array $control The control.
662 * @param array $values Values array.
663 * @param array $controls The controls stack.
664 * @param array $placeholders Placeholders.
665 * @param array $replacements Replacements.
666 */
667 protected function add_control_style_rules( array $control, array $values, array $controls, array $placeholders, array $replacements ) {
668 $this->add_control_rules(
669 $control, $controls, function( $control ) use ( $values ) {
670 return $this->get_style_control_value( $control, $values );
671 }, $placeholders, $replacements, $values
672 );
673 }
674
675 /**
676 * Get style control value.
677 *
678 * Retrieve the value of the style control for any give control and values.
679 *
680 * It will retrieve the control name and return the style value.
681 *
682 * @since 1.6.0
683 * @access private
684 *
685 * @param array $control The control.
686 * @param array $values Values array.
687 *
688 * @return mixed Style control value.
689 */
690 private function get_style_control_value( array $control, array $values ) {
691 if ( ! empty( $values['__globals__'][ $control['name'] ] ) ) {
692 // When the control itself has no global value, but it refers to another control global value
693 return $this->get_selector_global_value( $control, $values['__globals__'][ $control['name'] ] );
694 }
695
696 $value = $values[ $control['name'] ];
697
698 if ( isset( $control['selectors_dictionary'][ $value ] ) ) {
699 $value = $control['selectors_dictionary'][ $value ];
700 }
701
702 if ( ! is_numeric( $value ) && ! is_float( $value ) && empty( $value ) ) {
703 return null;
704 }
705
706 return $value;
707 }
708
709 /**
710 * Init stylesheet.
711 *
712 * Initialize CSS file stylesheet by creating a new `Stylesheet` object and register new
713 * breakpoints for the stylesheet.
714 *
715 * @since 1.2.0
716 * @access private
717 */
718 private function init_stylesheet() {
719 $this->stylesheet_obj = new Stylesheet();
720
721 $active_breakpoints = Plugin::$instance->breakpoints->get_active_breakpoints();
722
723 foreach ( $active_breakpoints as $breakpoint_name => $breakpoint ) {
724 $this->stylesheet_obj->add_device( $breakpoint_name, $breakpoint->get_value() );
725 }
726 }
727
728 /**
729 * Add repeater control style rules.
730 *
731 * Register new style rules for the repeater control.
732 *
733 * @since 2.0.0
734 * @access private
735 *
736 * @param Controls_Stack $controls_stack The control stack.
737 * @param array $repeater_control The repeater control.
738 * @param array $repeater_values Repeater values array.
739 * @param array $placeholders Placeholders.
740 * @param array $replacements Replacements.
741 */
742 protected function add_repeater_control_style_rules( Controls_Stack $controls_stack, array $repeater_control, array $repeater_values, array $placeholders, array $replacements ) {
743 $placeholders = array_merge( $placeholders, [ '{{CURRENT_ITEM}}' ] );
744
745 foreach ( $repeater_control['style_fields'] as $index => $item ) {
746 $this->add_controls_stack_style_rules(
747 $controls_stack,
748 $item,
749 $repeater_values[ $index ],
750 $placeholders,
751 array_merge( $replacements, [ '.elementor-repeater-item-' . $repeater_values[ $index ]['_id'] ] ),
752 $repeater_control['fields']
753 );
754 }
755 }
756
757 /**
758 * Add dynamic control style rules.
759 *
760 * Register new style rules for the dynamic control.
761 *
762 * @since 2.0.0
763 * @access private
764 *
765 * @param array $control The control.
766 * @param string $value The value.
767 */
768 protected function add_dynamic_control_style_rules( array $control, $value ) {
769 Plugin::$instance->dynamic_tags->parse_tags_text( $value, $control, function( $id, $name, $settings ) {
770 $tag = Plugin::$instance->dynamic_tags->create_tag( $id, $name, $settings );
771
772 if ( ! $tag instanceof Tag ) {
773 return;
774 }
775
776 $this->add_controls_stack_style_rules( $tag, $this->get_style_controls( $tag ), $tag->get_active_settings(), [ '{{WRAPPER}}' ], [ '#elementor-tag-' . $id ] );
777 } );
778 }
779
780 private function get_selector_global_value( $control, $global_key ) {
781 $data = Plugin::$instance->data_manager->run( $global_key );
782
783 if ( empty( $data['value'] ) ) {
784 return null;
785 }
786
787 $global_args = explode( '?id=', $global_key );
788
789 $id = $global_args[1];
790
791 if ( ! empty( $control['groupType'] ) ) {
792 $strings_to_replace = [ $control['groupPrefix'] ];
793
794 $active_breakpoint_keys = array_keys( Plugin::$instance->breakpoints->get_active_breakpoints() );
795
796 foreach ( $active_breakpoint_keys as $breakpoint ) {
797 $strings_to_replace[] = '_' . $breakpoint;
798 }
799
800 $property_name = str_replace( $strings_to_replace, '', $control['name'] );
801
802 // TODO: This check won't retrieve the proper answer for array values (multiple controls).
803 if ( empty( $data['value'][ Global_Typography::TYPOGRAPHY_GROUP_PREFIX . $property_name ] ) ) {
804 return null;
805 }
806
807 $property_name = str_replace( '_', '-', $property_name );
808
809 $value = "var( --e-global-$control[groupType]-$id-$property_name )";
810
811 if ( $control['groupPrefix'] . 'font_family' === $control['name'] ) {
812 $default_generic_fonts = Plugin::$instance->kits_manager->get_current_settings( 'default_generic_fonts' );
813
814 if ( $default_generic_fonts ) {
815 $value .= ", $default_generic_fonts";
816 }
817 }
818 } else {
819 $value = "var( --e-global-$control[type]-$id )";
820 }
821
822 return $value;
823 }
824
825 final protected function get_active_controls( Controls_Stack $controls_stack, array $controls = null, array $settings = null ) {
826 if ( ! $controls ) {
827 $controls = $controls_stack->get_controls();
828 }
829
830 if ( ! $settings ) {
831 $settings = $controls_stack->get_controls_settings();
832 }
833
834 if ( $this->is_global_parsing_supported() ) {
835 $settings = $this->parse_global_settings( $settings, $controls );
836 }
837
838 $active_controls = array_reduce(
839 array_keys( $controls ), function( $active_controls, $control_key ) use ( $controls_stack, $controls, $settings ) {
840 $control = $controls[ $control_key ];
841
842 if ( $controls_stack->is_control_visible( $control, $settings ) ) {
843 $active_controls[ $control_key ] = $control;
844 }
845
846 return $active_controls;
847 }, []
848 );
849
850 return $active_controls;
851 }
852
853 final public function get_style_controls( Controls_Stack $controls_stack, array $controls = null, array $settings = null ) {
854 $controls = $this->get_active_controls( $controls_stack, $controls, $settings );
855
856 $style_controls = [];
857
858 foreach ( $controls as $control_name => $control ) {
859 $control_obj = Plugin::$instance->controls_manager->get_control( $control['type'] );
860
861 if ( ! $control_obj instanceof Base_Data_Control ) {
862 continue;
863 }
864
865 $control = array_merge( $control_obj->get_settings(), $control );
866
867 if ( $control_obj instanceof Control_Repeater ) {
868 $style_fields = [];
869
870 foreach ( $controls_stack->get_settings( $control_name ) as $item ) {
871 $style_fields[] = $this->get_style_controls( $controls_stack, $control['fields'], $item );
872 }
873
874 $control['style_fields'] = $style_fields;
875 }
876
877 if ( ! empty( $control['selectors'] ) || ! empty( $control['dynamic'] ) || $this->is_global_control( $controls_stack, $control_name, $controls ) || ! empty( $control['style_fields'] ) ) {
878 $style_controls[ $control_name ] = $control;
879 }
880 }
881
882 return $style_controls;
883 }
884
885 private function parse_global_settings( array $settings, array $controls ) {
886 foreach ( $controls as $control ) {
887 $control_name = $control['name'];
888 $control_obj = Plugin::$instance->controls_manager->get_control( $control['type'] );
889
890 if ( ! $control_obj instanceof Base_Data_Control ) {
891 continue;
892 }
893
894 if ( $control_obj instanceof Control_Repeater ) {
895 foreach ( $settings[ $control_name ] as & $field ) {
896 $field = $this->parse_global_settings( $field, $control['fields'] );
897 }
898
899 continue;
900 }
901
902 if ( empty( $control['global']['active'] ) ) {
903 continue;
904 }
905
906 if ( empty( $settings['__globals__'][ $control_name ] ) ) {
907 continue;
908 }
909
910 $settings[ $control_name ] = 'global';
911 }
912
913 return $settings;
914 }
915
916 private function is_global_control( Controls_Stack $controls_stack, $control_name, $controls ) {
917 $control = $controls[ $control_name ];
918
919 $control_global_key = $control_name;
920
921 if ( ! empty( $control['groupType'] ) ) {
922 $control_global_key = $control['groupPrefix'] . $control['groupType'];
923 }
924
925 if ( empty( $controls[ $control_global_key ]['global']['active'] ) ) {
926 return false;
927 }
928
929 $globals = $controls_stack->get_settings( '__globals__' );
930
931 return ! empty( $globals[ $control_global_key ] );
932 }
933 }
934