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

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