PluginProbe
Elementor Website Builder – more than just a page builder / 3.0.4
Elementor Website Builder – more than just a page builder v3.0.4
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / core / files / css / base.php

base.php in Elementor Website Builder – more than just a page builder 3.0.4, at core/files/css/base.php

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