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

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