PluginProbe
Elementor Website Builder – more than just a page builder / 3.7.4
Elementor Website Builder – more than just a page builder v3.7.4
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.7.4, at core/files/css/base.php

975 lines 25.6 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\Breakpoints\Manager as Breakpoints_Manager;
9 use Elementor\Core\Files\Base as Base_File;
10 use Elementor\Core\DynamicTags\Manager;
11 use Elementor\Core\DynamicTags\Tag;
12 use Elementor\Core\Kits\Documents\Tabs\Global_Typography;
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 ( Breakpoints_Manager::BREAKPOINT_KEY_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'] ) && Breakpoints_Manager::BREAKPOINT_KEY_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 the control value is empty, check for global default. `0` (integer, string) are falsy but are valid values.
445 if ( empty( $value ) && '0' !== $value && 0 !== $value ) {
446 $value = $this->get_control_global_default_value( $control );
447 }
448
449 if ( Controls_Manager::FONT === $control['type'] ) {
450 $this->fonts[] = $value;
451 }
452
453 /** @var Base_Data_Control $control_obj */
454 $control_obj = Plugin::$instance->controls_manager->get_control( $control['type'] );
455
456 return (string) $control_obj->get_style_value( $placeholder, $value, $control );
457 }
458
459 /**
460 * Get the fonts.
461 *
462 * Retrieve the list of fonts.
463 *
464 * @since 1.9.0
465 * @access public
466 *
467 * @return array Fonts.
468 */
469 public function get_fonts() {
470 return $this->fonts;
471 }
472
473 /**
474 * Get stylesheet.
475 *
476 * Retrieve the CSS file stylesheet instance.
477 *
478 * @since 1.2.0
479 * @access public
480 *
481 * @return Stylesheet The stylesheet object.
482 */
483 public function get_stylesheet() {
484 if ( ! $this->stylesheet_obj ) {
485 $this->init_stylesheet();
486 }
487
488 return $this->stylesheet_obj;
489 }
490
491 /**
492 * Add controls stack style rules.
493 *
494 * Parse the CSS for all the elements inside any given controls stack.
495 *
496 * This method recursively renders the CSS for all the child elements in the stack.
497 *
498 * @since 1.6.0
499 * @access public
500 *
501 * @param Controls_Stack $controls_stack The controls stack.
502 * @param array $controls Controls array.
503 * @param array $values Values array.
504 * @param array $placeholders Placeholders.
505 * @param array $replacements Replacements.
506 * @param array $all_controls All controls.
507 */
508 public function add_controls_stack_style_rules( Controls_Stack $controls_stack, array $controls, array $values, array $placeholders, array $replacements, array $all_controls = null ) {
509 if ( ! $all_controls ) {
510 $all_controls = $controls_stack->get_controls();
511 }
512
513 $parsed_dynamic_settings = $controls_stack->parse_dynamic_settings( $values, $controls );
514
515 foreach ( $controls as $control ) {
516 if ( ! empty( $control['style_fields'] ) ) {
517 $this->add_repeater_control_style_rules( $controls_stack, $control, $values[ $control['name'] ], $placeholders, $replacements );
518 }
519
520 if ( ! empty( $control[ Manager::DYNAMIC_SETTING_KEY ][ $control['name'] ] ) ) {
521 $this->add_dynamic_control_style_rules( $control, $control[ Manager::DYNAMIC_SETTING_KEY ][ $control['name'] ] );
522 }
523
524 if ( Controls_Manager::ICONS === $control['type'] ) {
525 $this->icons_fonts[] = $values[ $control['name'] ]['library'];
526 }
527
528 if ( ! empty( $parsed_dynamic_settings[ Manager::DYNAMIC_SETTING_KEY ][ $control['name'] ] ) ) {
529 // Dynamic CSS should not be added to the CSS files.
530 // Instead it's handled by \Elementor\Core\DynamicTags\Dynamic_CSS
531 // and printed in a style tag.
532 unset( $parsed_dynamic_settings[ $control['name'] ] );
533
534 $this->dynamic_elements_ids[] = $controls_stack->get_id();
535
536 continue;
537 }
538
539 if ( empty( $control['selectors'] ) ) {
540 continue;
541 }
542
543 $this->add_control_style_rules( $control, $parsed_dynamic_settings, $all_controls, $placeholders, $replacements );
544 }
545 }
546
547 /**
548 * Get file handle ID.
549 *
550 * Retrieve the file handle ID.
551 *
552 * @since 1.2.0
553 * @access protected
554 * @abstract
555 *
556 * @return string CSS file handle ID.
557 */
558 abstract protected function get_file_handle_id();
559
560 /**
561 * Render CSS.
562 *
563 * Parse the CSS.
564 *
565 * @since 1.2.0
566 * @access protected
567 * @abstract
568 */
569 abstract protected function render_css();
570
571 protected function get_default_meta() {
572 return array_merge( parent::get_default_meta(), [
573 'fonts' => array_unique( $this->fonts ),
574 'icons' => array_unique( $this->icons_fonts ),
575 'dynamic_elements_ids' => [],
576 'status' => '',
577 ] );
578 }
579
580 /**
581 * Get enqueue dependencies.
582 *
583 * Retrieve the name of the stylesheet used by `wp_enqueue_style()`.
584 *
585 * @since 1.2.0
586 * @access protected
587 *
588 * @return array Name of the stylesheet.
589 */
590 protected function get_enqueue_dependencies() {
591 return [];
592 }
593
594 /**
595 * Get inline dependency.
596 *
597 * Retrieve the name of the stylesheet used by `wp_add_inline_style()`.
598 *
599 * @since 1.2.0
600 * @access protected
601 *
602 * @return string Name of the stylesheet.
603 */
604 protected function get_inline_dependency() {
605 return '';
606 }
607
608 /**
609 * Is update required.
610 *
611 * Whether the CSS requires an update. When there are new schemes or settings
612 * updates.
613 *
614 * @since 1.2.0
615 * @access protected
616 *
617 * @return bool True if the CSS requires an update, False otherwise.
618 */
619 protected function is_update_required() {
620 return false;
621 }
622
623 /**
624 * Parse CSS.
625 *
626 * Parsing the CSS file.
627 *
628 * @since 1.2.0
629 * @access protected
630 */
631 protected function parse_content() {
632 $initial_responsive_controls_duplication_mode = Plugin::$instance->breakpoints->get_responsive_control_duplication_mode();
633
634 Plugin::$instance->breakpoints->set_responsive_control_duplication_mode( $this->get_responsive_control_duplication_mode() );
635
636 $this->render_css();
637
638 $name = $this->get_name();
639
640 /**
641 * Parse CSS file.
642 *
643 * Fires when CSS file is parsed on Elementor.
644 *
645 * The dynamic portion of the hook name, `$name`, refers to the CSS file name.
646 *
647 * @since 2.0.0
648 *
649 * @param Base $this The current CSS file.
650 */
651 do_action( "elementor/css-file/{$name}/parse", $this );
652
653 Plugin::$instance->breakpoints->set_responsive_control_duplication_mode( $initial_responsive_controls_duplication_mode );
654
655 return $this->get_stylesheet()->__toString();
656 }
657
658 /**
659 * Add control style rules.
660 *
661 * Register new style rules for the control.
662 *
663 * @since 1.6.0
664 * @access private
665 *
666 * @param array $control The control.
667 * @param array $values Values array.
668 * @param array $controls The controls stack.
669 * @param array $placeholders Placeholders.
670 * @param array $replacements Replacements.
671 */
672 protected function add_control_style_rules( array $control, array $values, array $controls, array $placeholders, array $replacements ) {
673 $this->add_control_rules(
674 $control, $controls, function( $control ) use ( $values ) {
675 return $this->get_style_control_value( $control, $values );
676 }, $placeholders, $replacements, $values
677 );
678 }
679
680 /**
681 * Get Control Global Default Value
682 *
683 * If the control has a global default value, and the corresponding global default setting is enabled, this method
684 * fetches and returns the global default value. Otherwise, it returns null.
685 *
686 * @since 3.7.0
687 * @access private
688 *
689 * @param $control
690 * @return string|null
691 */
692 private function get_control_global_default_value( $control ) {
693 if ( empty( $control['global']['default'] ) ) {
694 return null;
695 }
696
697 // If the control value is empty, and the control has a global default set, fetch the global value and use it.
698 $global_enabled = false;
699
700 if ( 'color' === $control['type'] ) {
701 $global_enabled = Plugin::$instance->kits_manager->is_custom_colors_enabled();
702 } elseif ( isset( $control['groupType'] ) && 'typography' === $control['groupType'] ) {
703 $global_enabled = Plugin::$instance->kits_manager->is_custom_typography_enabled();
704 }
705
706 $value = null;
707
708 // Only apply the global default if Global Colors are enabled.
709 if ( $global_enabled ) {
710 $value = $this->get_selector_global_value( $control, $control['global']['default'] );
711 }
712
713 return $value;
714 }
715
716 /**
717 * Get style control value.
718 *
719 * Retrieve the value of the style control for any give control and values.
720 *
721 * It will retrieve the control name and return the style value.
722 *
723 * @since 1.6.0
724 * @access private
725 *
726 * @param array $control The control.
727 * @param array $values Values array.
728 *
729 * @return mixed Style control value.
730 */
731 private function get_style_control_value( array $control, array $values ) {
732 if ( ! empty( $values['__globals__'][ $control['name'] ] ) ) {
733 // When the control itself has no global value, but it refers to another control global value
734 return $this->get_selector_global_value( $control, $values['__globals__'][ $control['name'] ] );
735 }
736
737 $value = $values[ $control['name'] ];
738
739 if ( isset( $control['selectors_dictionary'][ $value ] ) ) {
740 $value = $control['selectors_dictionary'][ $value ];
741 }
742
743 if ( ! is_numeric( $value ) && ! is_float( $value ) && empty( $value ) ) {
744 return null;
745 }
746
747 return $value;
748 }
749
750 /**
751 * Init stylesheet.
752 *
753 * Initialize CSS file stylesheet by creating a new `Stylesheet` object and register new
754 * breakpoints for the stylesheet.
755 *
756 * @since 1.2.0
757 * @access private
758 */
759 private function init_stylesheet() {
760 $this->stylesheet_obj = new Stylesheet();
761
762 $active_breakpoints = Plugin::$instance->breakpoints->get_active_breakpoints();
763
764 foreach ( $active_breakpoints as $breakpoint_name => $breakpoint ) {
765 $this->stylesheet_obj->add_device( $breakpoint_name, $breakpoint->get_value() );
766 }
767 }
768
769 /**
770 * Add repeater control style rules.
771 *
772 * Register new style rules for the repeater control.
773 *
774 * @since 2.0.0
775 * @access private
776 *
777 * @param Controls_Stack $controls_stack The control stack.
778 * @param array $repeater_control The repeater control.
779 * @param array $repeater_values Repeater values array.
780 * @param array $placeholders Placeholders.
781 * @param array $replacements Replacements.
782 */
783 protected function add_repeater_control_style_rules( Controls_Stack $controls_stack, array $repeater_control, array $repeater_values, array $placeholders, array $replacements ) {
784 $placeholders = array_merge( $placeholders, [ '{{CURRENT_ITEM}}' ] );
785
786 foreach ( $repeater_control['style_fields'] as $index => $item ) {
787 $this->add_controls_stack_style_rules(
788 $controls_stack,
789 $item,
790 $repeater_values[ $index ],
791 $placeholders,
792 array_merge( $replacements, [ '.elementor-repeater-item-' . $repeater_values[ $index ]['_id'] ] ),
793 $repeater_control['fields']
794 );
795 }
796 }
797
798 /**
799 * Add dynamic control style rules.
800 *
801 * Register new style rules for the dynamic control.
802 *
803 * @since 2.0.0
804 * @access private
805 *
806 * @param array $control The control.
807 * @param string $value The value.
808 */
809 protected function add_dynamic_control_style_rules( array $control, $value ) {
810 Plugin::$instance->dynamic_tags->parse_tags_text( $value, $control, function( $id, $name, $settings ) {
811 $tag = Plugin::$instance->dynamic_tags->create_tag( $id, $name, $settings );
812
813 if ( ! $tag instanceof Tag ) {
814 return;
815 }
816
817 $this->add_controls_stack_style_rules( $tag, $this->get_style_controls( $tag ), $tag->get_active_settings(), [ '{{WRAPPER}}' ], [ '#elementor-tag-' . $id ] );
818 } );
819 }
820
821 private function get_selector_global_value( $control, $global_key ) {
822 $data = Plugin::$instance->data_manager_v2->run( $global_key );
823
824 if ( empty( $data['value'] ) ) {
825 return null;
826 }
827
828 $global_args = explode( '?id=', $global_key );
829
830 $id = $global_args[1];
831
832 if ( ! empty( $control['groupType'] ) ) {
833 $strings_to_replace = [ $control['groupPrefix'] ];
834
835 $active_breakpoint_keys = array_keys( Plugin::$instance->breakpoints->get_active_breakpoints() );
836
837 foreach ( $active_breakpoint_keys as $breakpoint ) {
838 $strings_to_replace[] = '_' . $breakpoint;
839 }
840
841 $property_name = str_replace( $strings_to_replace, '', $control['name'] );
842
843 // TODO: This check won't retrieve the proper answer for array values (multiple controls).
844 if ( empty( $data['value'][ Global_Typography::TYPOGRAPHY_GROUP_PREFIX . $property_name ] ) ) {
845 return null;
846 }
847
848 $property_name = str_replace( '_', '-', $property_name );
849
850 $value = "var( --e-global-$control[groupType]-$id-$property_name )";
851
852 if ( $control['groupPrefix'] . 'font_family' === $control['name'] ) {
853 $default_generic_fonts = Plugin::$instance->kits_manager->get_current_settings( 'default_generic_fonts' );
854
855 if ( $default_generic_fonts ) {
856 $value .= ", $default_generic_fonts";
857 }
858 }
859 } else {
860 $value = "var( --e-global-$control[type]-$id )";
861 }
862
863 return $value;
864 }
865
866 final protected function get_active_controls( Controls_Stack $controls_stack, array $controls = null, array $settings = null ) {
867 if ( ! $controls ) {
868 $controls = $controls_stack->get_controls();
869 }
870
871 if ( ! $settings ) {
872 $settings = $controls_stack->get_controls_settings();
873 }
874
875 if ( $this->is_global_parsing_supported() ) {
876 $settings = $this->parse_global_settings( $settings, $controls );
877 }
878
879 $active_controls = array_reduce(
880 array_keys( $controls ), function( $active_controls, $control_key ) use ( $controls_stack, $controls, $settings ) {
881 $control = $controls[ $control_key ];
882
883 if ( $controls_stack->is_control_visible( $control, $settings ) ) {
884 $active_controls[ $control_key ] = $control;
885 }
886
887 return $active_controls;
888 }, []
889 );
890
891 return $active_controls;
892 }
893
894 final public function get_style_controls( Controls_Stack $controls_stack, array $controls = null, array $settings = null ) {
895 $controls = $this->get_active_controls( $controls_stack, $controls, $settings );
896
897 $style_controls = [];
898
899 foreach ( $controls as $control_name => $control ) {
900 $control_obj = Plugin::$instance->controls_manager->get_control( $control['type'] );
901
902 if ( ! $control_obj instanceof Base_Data_Control ) {
903 continue;
904 }
905
906 $control = array_merge( $control_obj->get_settings(), $control );
907
908 if ( $control_obj instanceof Control_Repeater ) {
909 $style_fields = [];
910
911 foreach ( $controls_stack->get_settings( $control_name ) as $item ) {
912 $style_fields[] = $this->get_style_controls( $controls_stack, $control['fields'], $item );
913 }
914
915 $control['style_fields'] = $style_fields;
916 }
917
918 if ( ! empty( $control['selectors'] ) || ! empty( $control['dynamic'] ) || $this->is_global_control( $controls_stack, $control_name, $controls ) || ! empty( $control['style_fields'] ) ) {
919 $style_controls[ $control_name ] = $control;
920 }
921 }
922
923 return $style_controls;
924 }
925
926 private function parse_global_settings( array $settings, array $controls ) {
927 foreach ( $controls as $control ) {
928 $control_name = $control['name'];
929 $control_obj = Plugin::$instance->controls_manager->get_control( $control['type'] );
930
931 if ( ! $control_obj instanceof Base_Data_Control ) {
932 continue;
933 }
934
935 if ( $control_obj instanceof Control_Repeater ) {
936 foreach ( $settings[ $control_name ] as & $field ) {
937 $field = $this->parse_global_settings( $field, $control['fields'] );
938 }
939
940 continue;
941 }
942
943 if ( empty( $control['global']['active'] ) ) {
944 continue;
945 }
946
947 if ( empty( $settings['__globals__'][ $control_name ] ) ) {
948 continue;
949 }
950
951 $settings[ $control_name ] = 'global';
952 }
953
954 return $settings;
955 }
956
957 private function is_global_control( Controls_Stack $controls_stack, $control_name, $controls ) {
958 $control = $controls[ $control_name ];
959
960 $control_global_key = $control_name;
961
962 if ( ! empty( $control['groupType'] ) ) {
963 $control_global_key = $control['groupPrefix'] . $control['groupType'];
964 }
965
966 if ( empty( $controls[ $control_global_key ]['global']['active'] ) ) {
967 return false;
968 }
969
970 $globals = $controls_stack->get_settings( '__globals__' );
971
972 return ! empty( $globals[ $control_global_key ] );
973 }
974 }
975