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

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