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

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