PluginProbe
Elementor Website Builder – more than just a page builder / 3.8.0-beta1
Elementor Website Builder – more than just a page builder v3.8.0-beta1
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 / includes / base / controls-stack.php

controls-stack.php in Elementor Website Builder – more than just a page builder 3.8.0-beta1, at includes/base/controls-stack.php

2,471 lines 66.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor;
3
4 use Elementor\Core\Base\Base_Object;
5 use Elementor\Core\DynamicTags\Manager;
6 use Elementor\Core\Schemes\Manager as Schemes_Manager;
7 use Elementor\Core\Breakpoints\Manager as Breakpoints_Manager;
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit; // Exit if accessed directly.
11 }
12
13 /**
14 * Elementor controls stack.
15 *
16 * An abstract class that provides the needed properties and methods to
17 * manage and handle controls in the editor panel to inheriting classes.
18 *
19 * @since 1.4.0
20 * @abstract
21 */
22 abstract class Controls_Stack extends Base_Object {
23
24 /**
25 * Responsive 'desktop' device name.
26 *
27 * @deprecated 3.4.0
28 */
29 const RESPONSIVE_DESKTOP = 'desktop';
30
31 /**
32 * Responsive 'tablet' device name.
33 *
34 * @deprecated 3.4.0
35 */
36 const RESPONSIVE_TABLET = 'tablet';
37
38 /**
39 * Responsive 'mobile' device name.
40 *
41 * @deprecated 3.4.0
42 */
43 const RESPONSIVE_MOBILE = 'mobile';
44
45 /**
46 * Generic ID.
47 *
48 * Holds the unique ID.
49 *
50 * @access private
51 *
52 * @var string
53 */
54 private $id;
55
56 private $active_settings;
57
58 private $parsed_active_settings;
59
60 /**
61 * Parsed Dynamic Settings.
62 *
63 * @access private
64 *
65 * @var null|array
66 */
67 private $parsed_dynamic_settings;
68
69 /**
70 * Raw Data.
71 *
72 * Holds all the raw data including the element type, the child elements,
73 * the user data.
74 *
75 * @access private
76 *
77 * @var null|array
78 */
79 private $data;
80
81 /**
82 * The configuration.
83 *
84 * Holds the configuration used to generate the Elementor editor. It includes
85 * the element name, icon, categories, etc.
86 *
87 * @access private
88 *
89 * @var null|array
90 */
91 private $config;
92
93 /**
94 * The additional configuration.
95 *
96 * Holds additional configuration that has been set using `set_config` method.
97 * The `config` property is not modified directly while using the method because
98 * it's used to check whether the initial config already loaded (in `get_config`).
99 * After the initial config loaded, the additional config is merged into it.
100 *
101 * @access private
102 *
103 * @var null|array
104 */
105 private $additional_config = [];
106
107 /**
108 * Current section.
109 *
110 * Holds the current section while inserting a set of controls sections.
111 *
112 * @access private
113 *
114 * @var null|array
115 */
116 private $current_section;
117
118 /**
119 * Current tab.
120 *
121 * Holds the current tab while inserting a set of controls tabs.
122 *
123 * @access private
124 *
125 * @var null|array
126 */
127 private $current_tab;
128
129 /**
130 * Current popover.
131 *
132 * Holds the current popover while inserting a set of controls.
133 *
134 * @access private
135 *
136 * @var null|array
137 */
138 private $current_popover;
139
140 /**
141 * Injection point.
142 *
143 * Holds the injection point in the stack where the control will be inserted.
144 *
145 * @access private
146 *
147 * @var null|array
148 */
149 private $injection_point;
150
151
152 /**
153 * Data sanitized.
154 *
155 * @access private
156 *
157 * @var bool
158 */
159 private $settings_sanitized = false;
160
161 /**
162 * Element render attributes.
163 *
164 * Holds all the render attributes of the element. Used to store data like
165 * the HTML class name and the class value, or HTML element ID name and value.
166 *
167 * @access private
168 *
169 * @var array
170 */
171 private $render_attributes = [];
172
173 /**
174 * Get element name.
175 *
176 * Retrieve the element name.
177 *
178 * @since 1.4.0
179 * @access public
180 * @abstract
181 *
182 * @return string The name.
183 */
184 abstract public function get_name();
185
186 /**
187 * Get unique name.
188 *
189 * Some classes need to use unique names, this method allows you to create
190 * them. By default it retrieves the regular name.
191 *
192 * @since 1.6.0
193 * @access public
194 *
195 * @return string Unique name.
196 */
197 public function get_unique_name() {
198 return $this->get_name();
199 }
200
201 /**
202 * Get element ID.
203 *
204 * Retrieve the element generic ID.
205 *
206 * @since 1.4.0
207 * @access public
208 *
209 * @return string The ID.
210 */
211 public function get_id() {
212 return $this->id;
213 }
214
215 /**
216 * Get element ID.
217 *
218 * Retrieve the element generic ID as integer.
219 *
220 * @since 1.8.0
221 * @access public
222 *
223 * @return string The converted ID.
224 */
225 public function get_id_int() {
226 /** We ignore possible notices, in order to support elements created prior to v1.8.0 and might include
227 * non-base 16 characters as part of their ID.
228 */
229 return @hexdec( $this->id );
230 }
231
232 /**
233 * Get the type.
234 *
235 * Retrieve the type, e.g. 'stack', 'section', 'widget' etc.
236 *
237 * @since 1.4.0
238 * @access public
239 * @static
240 *
241 * @return string The type.
242 */
243 public static function get_type() {
244 return 'stack';
245 }
246
247 /**
248 * @since 2.9.0
249 * @access public
250 *
251 * @return bool
252 */
253 public function is_editable() {
254 return true;
255 }
256
257 /**
258 * Get current section.
259 *
260 * When inserting new controls, this method will retrieve the current section.
261 *
262 * @since 1.7.1
263 * @access public
264 *
265 * @return null|array Current section.
266 */
267 public function get_current_section() {
268 return $this->current_section;
269 }
270
271 /**
272 * Get current tab.
273 *
274 * When inserting new controls, this method will retrieve the current tab.
275 *
276 * @since 1.7.1
277 * @access public
278 *
279 * @return null|array Current tab.
280 */
281 public function get_current_tab() {
282 return $this->current_tab;
283 }
284
285 /**
286 * Get controls.
287 *
288 * Retrieve all the controls or, when requested, a specific control.
289 *
290 * @since 1.4.0
291 * @access public
292 *
293 * @param string $control_id The ID of the requested control. Optional field,
294 * when set it will return a specific control.
295 * Default is null.
296 *
297 * @return mixed Controls list.
298 */
299 public function get_controls( $control_id = null ) {
300 return self::get_items( $this->get_stack()['controls'], $control_id );
301 }
302
303 /**
304 * Get active controls.
305 *
306 * Retrieve an array of active controls that meet the condition field.
307 *
308 * If specific controls was given as a parameter, retrieve active controls
309 * from that list, otherwise check for all the controls available.
310 *
311 * @since 1.4.0
312 * @since 2.0.9 Added the `controls` and the `settings` parameters.
313 * @access public
314 * @deprecated 3.0.0
315 *
316 * @param array $controls Optional. An array of controls. Default is null.
317 * @param array $settings Optional. Controls settings. Default is null.
318 *
319 * @return array Active controls.
320 */
321 public function get_active_controls( array $controls = null, array $settings = null ) {
322 Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( __METHOD__, '3.0.0' );
323
324 if ( ! $controls ) {
325 $controls = $this->get_controls();
326 }
327
328 if ( ! $settings ) {
329 $settings = $this->get_controls_settings();
330 }
331
332 $active_controls = array_reduce(
333 array_keys( $controls ), function( $active_controls, $control_key ) use ( $controls, $settings ) {
334 $control = $controls[ $control_key ];
335
336 if ( $this->is_control_visible( $control, $settings ) ) {
337 $active_controls[ $control_key ] = $control;
338 }
339
340 return $active_controls;
341 }, []
342 );
343
344 return $active_controls;
345 }
346
347 /**
348 * Get controls settings.
349 *
350 * Retrieve the settings for all the controls that represent them.
351 *
352 * @since 1.5.0
353 * @access public
354 *
355 * @return array Controls settings.
356 */
357 public function get_controls_settings() {
358 return array_intersect_key( $this->get_settings(), $this->get_controls() );
359 }
360
361 /**
362 * Add new control to stack.
363 *
364 * Register a single control to allow the user to set/update data.
365 *
366 * This method should be used inside `register_controls()`.
367 *
368 * @since 1.4.0
369 * @access public
370 *
371 * @param string $id Control ID.
372 * @param array $args Control arguments.
373 * @param array $options Optional. Control options. Default is an empty array.
374 *
375 * @return bool True if control added, False otherwise.
376 */
377 public function add_control( $id, array $args, $options = [] ) {
378 $default_options = [
379 'overwrite' => false,
380 'position' => null,
381 ];
382
383 if ( isset( $args['scheme'] ) ) {
384 $args['global'] = [
385 'default' => Plugin::$instance->kits_manager->convert_scheme_to_global( $args['scheme'] ),
386 ];
387
388 unset( $args['scheme'] );
389 }
390
391 $options = array_merge( $default_options, $options );
392
393 if ( $options['position'] ) {
394 $this->start_injection( $options['position'] );
395 }
396
397 if ( $this->injection_point ) {
398 $options['index'] = $this->injection_point['index']++;
399 }
400
401 if ( empty( $args['type'] ) || ! in_array( $args['type'], [ Controls_Manager::SECTION, Controls_Manager::WP_WIDGET ], true ) ) {
402 $args = $this->handle_control_position( $args, $id, $options['overwrite'] );
403 }
404
405 if ( $options['position'] ) {
406 $this->end_injection();
407 }
408
409 unset( $options['position'] );
410
411 if ( $this->current_popover ) {
412 $args['popover'] = [];
413
414 if ( ! $this->current_popover['initialized'] ) {
415 $args['popover']['start'] = true;
416
417 $this->current_popover['initialized'] = true;
418 }
419 }
420
421 return Plugin::$instance->controls_manager->add_control_to_stack( $this, $id, $args, $options );
422 }
423
424 /**
425 * Remove control from stack.
426 *
427 * Unregister an existing control and remove it from the stack.
428 *
429 * @since 1.4.0
430 * @access public
431 *
432 * @param string $control_id Control ID.
433 *
434 * @return bool|\WP_Error
435 */
436 public function remove_control( $control_id ) {
437 return Plugin::$instance->controls_manager->remove_control_from_stack( $this->get_unique_name(), $control_id );
438 }
439
440 /**
441 * Update control in stack.
442 *
443 * Change the value of an existing control in the stack. When you add new
444 * control you set the `$args` parameter, this method allows you to update
445 * the arguments by passing new data.
446 *
447 * @since 1.4.0
448 * @since 1.8.1 New `$options` parameter added.
449 *
450 * @access public
451 *
452 * @param string $control_id Control ID.
453 * @param array $args Control arguments. Only the new fields you want
454 * to update.
455 * @param array $options Optional. Some additional options. Default is
456 * an empty array.
457 *
458 * @return bool
459 */
460 public function update_control( $control_id, array $args, array $options = [] ) {
461 $is_updated = Plugin::$instance->controls_manager->update_control_in_stack( $this, $control_id, $args, $options );
462
463 if ( ! $is_updated ) {
464 return false;
465 }
466
467 $control = $this->get_controls( $control_id );
468
469 if ( Controls_Manager::SECTION === $control['type'] ) {
470 $section_args = $this->get_section_args( $control_id );
471
472 $section_controls = $this->get_section_controls( $control_id );
473
474 foreach ( $section_controls as $section_control_id => $section_control ) {
475 $this->update_control( $section_control_id, $section_args, $options );
476 }
477 }
478
479 return true;
480 }
481
482 /**
483 * Get stack.
484 *
485 * Retrieve the stack of controls.
486 *
487 * @since 1.9.2
488 * @access public
489 *
490 * @return array Stack of controls.
491 */
492 public function get_stack() {
493 $stack = Plugin::$instance->controls_manager->get_element_stack( $this );
494
495 if ( null === $stack ) {
496 $this->init_controls();
497
498 return Plugin::$instance->controls_manager->get_element_stack( $this );
499 }
500
501 return $stack;
502 }
503
504 /**
505 * Get position information.
506 *
507 * Retrieve the position while injecting data, based on the element type.
508 *
509 * @since 1.7.0
510 * @access public
511 *
512 * @param array $position {
513 * The injection position.
514 *
515 * @type string $type Injection type, either `control` or `section`.
516 * Default is `control`.
517 * @type string $at Where to inject. If `$type` is `control` accepts
518 * `before` and `after`. If `$type` is `section`
519 * accepts `start` and `end`. Default values based on
520 * the `type`.
521 * @type string $of Control/Section ID.
522 * @type array $fallback Fallback injection position. When the position is
523 * not found it will try to fetch the fallback
524 * position.
525 * }
526 *
527 * @return bool|array Position info.
528 */
529 final public function get_position_info( array $position ) {
530 $default_position = [
531 'type' => 'control',
532 'at' => 'after',
533 ];
534
535 if ( ! empty( $position['type'] ) && 'section' === $position['type'] ) {
536 $default_position['at'] = 'end';
537 }
538
539 $position = array_merge( $default_position, $position );
540
541 if (
542 'control' === $position['type'] && in_array( $position['at'], [ 'start', 'end' ], true ) ||
543 'section' === $position['type'] && in_array( $position['at'], [ 'before', 'after' ], true )
544 ) {
545 _doing_it_wrong( sprintf( '%s::%s', get_called_class(), __FUNCTION__ ), 'Invalid position arguments. Use `before` / `after` for control or `start` / `end` for section.', '1.7.0' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
546
547 return false;
548 }
549
550 $target_control_index = $this->get_control_index( $position['of'] );
551
552 if ( false === $target_control_index ) {
553 if ( ! empty( $position['fallback'] ) ) {
554 return $this->get_position_info( $position['fallback'] );
555 }
556
557 return false;
558 }
559
560 $target_section_index = $target_control_index;
561
562 $registered_controls = $this->get_controls();
563
564 $controls_keys = array_keys( $registered_controls );
565
566 while ( Controls_Manager::SECTION !== $registered_controls[ $controls_keys[ $target_section_index ] ]['type'] ) {
567 $target_section_index--;
568 }
569
570 if ( 'section' === $position['type'] ) {
571 $target_control_index++;
572
573 if ( 'end' === $position['at'] ) {
574 while ( Controls_Manager::SECTION !== $registered_controls[ $controls_keys[ $target_control_index ] ]['type'] ) {
575 if ( ++$target_control_index >= count( $registered_controls ) ) {
576 break;
577 }
578 }
579 }
580 }
581
582 $target_control = $registered_controls[ $controls_keys[ $target_control_index ] ];
583
584 if ( 'after' === $position['at'] ) {
585 $target_control_index++;
586 }
587
588 $section_id = $registered_controls[ $controls_keys[ $target_section_index ] ]['name'];
589
590 $position_info = [
591 'index' => $target_control_index,
592 'section' => $this->get_section_args( $section_id ),
593 ];
594
595 if ( ! empty( $target_control['tabs_wrapper'] ) ) {
596 $position_info['tab'] = [
597 'tabs_wrapper' => $target_control['tabs_wrapper'],
598 'inner_tab' => $target_control['inner_tab'],
599 ];
600 }
601
602 return $position_info;
603 }
604
605 /**
606 * Get control key.
607 *
608 * Retrieve the key of the control based on a given index of the control.
609 *
610 * @since 1.9.2
611 * @access public
612 *
613 * @param string $control_index Control index.
614 *
615 * @return int Control key.
616 */
617 final public function get_control_key( $control_index ) {
618 $registered_controls = $this->get_controls();
619
620 $controls_keys = array_keys( $registered_controls );
621
622 return $controls_keys[ $control_index ];
623 }
624
625 /**
626 * Get control index.
627 *
628 * Retrieve the index of the control based on a given key of the control.
629 *
630 * @since 1.7.6
631 * @access public
632 *
633 * @param string $control_key Control key.
634 *
635 * @return false|int Control index.
636 */
637 final public function get_control_index( $control_key ) {
638 $controls = $this->get_controls();
639
640 $controls_keys = array_keys( $controls );
641
642 return array_search( $control_key, $controls_keys );
643 }
644
645 /**
646 * Get section controls.
647 *
648 * Retrieve all controls under a specific section.
649 *
650 * @since 1.7.6
651 * @access public
652 *
653 * @param string $section_id Section ID.
654 *
655 * @return array Section controls
656 */
657 final public function get_section_controls( $section_id ) {
658 $section_index = $this->get_control_index( $section_id );
659
660 $section_controls = [];
661
662 $registered_controls = $this->get_controls();
663
664 $controls_keys = array_keys( $registered_controls );
665
666 while ( true ) {
667 $section_index++;
668
669 if ( ! isset( $controls_keys[ $section_index ] ) ) {
670 break;
671 }
672
673 $control_key = $controls_keys[ $section_index ];
674
675 if ( Controls_Manager::SECTION === $registered_controls[ $control_key ]['type'] ) {
676 break;
677 }
678
679 $section_controls[ $control_key ] = $registered_controls[ $control_key ];
680 };
681
682 return $section_controls;
683 }
684
685 /**
686 * Add new group control to stack.
687 *
688 * Register a set of related controls grouped together as a single unified
689 * control. For example grouping together like typography controls into a
690 * single, easy-to-use control.
691 *
692 * @since 1.4.0
693 * @access public
694 *
695 * @param string $group_name Group control name.
696 * @param array $args Group control arguments. Default is an empty array.
697 * @param array $options Optional. Group control options. Default is an
698 * empty array.
699 */
700 final public function add_group_control( $group_name, array $args = [], array $options = [] ) {
701 $group = Plugin::$instance->controls_manager->get_control_groups( $group_name );
702
703 if ( ! $group ) {
704 wp_die( sprintf( '%s::%s: Group "%s" not found.', get_called_class(), __FUNCTION__, $group_name ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
705 }
706
707 $group->add_controls( $this, $args, $options );
708 }
709
710 /**
711 * Get scheme controls.
712 *
713 * Retrieve all the controls that use schemes.
714 *
715 * @since 1.4.0
716 * @access public
717 * @deprecated 3.0.0
718 *
719 * @return array Scheme controls.
720 */
721 final public function get_scheme_controls() {
722
723 Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( __METHOD__, '3.0.0' );
724
725 $enabled_schemes = Schemes_Manager::get_enabled_schemes();
726
727 return array_filter(
728 $this->get_controls(), function ( $control ) use ( $enabled_schemes ) {
729 return ( ! empty( $control['scheme'] ) && in_array( $control['scheme']['type'], $enabled_schemes ) );
730 }
731 );
732 }
733
734 /**
735 * Get style controls.
736 *
737 * Retrieve style controls for all active controls or, when requested, from
738 * a specific set of controls.
739 *
740 * @since 1.4.0
741 * @since 2.0.9 Added the `settings` parameter.
742 * @access public
743 * @deprecated 3.0.0
744 *
745 * @param array $controls Optional. Controls list. Default is null.
746 * @param array $settings Optional. Controls settings. Default is null.
747 *
748 * @return array Style controls.
749 */
750 final public function get_style_controls( array $controls = null, array $settings = null ) {
751 Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( __METHOD__, '3.0.0' );
752
753 $controls = $this->get_active_controls( $controls, $settings );
754
755 $style_controls = [];
756
757 foreach ( $controls as $control_name => $control ) {
758 $control_obj = Plugin::$instance->controls_manager->get_control( $control['type'] );
759
760 if ( ! $control_obj instanceof Base_Data_Control ) {
761 continue;
762 }
763
764 $control = array_merge( $control_obj->get_settings(), $control );
765
766 if ( $control_obj instanceof Control_Repeater ) {
767 $style_fields = [];
768
769 foreach ( $this->get_settings( $control_name ) as $item ) {
770 $style_fields[] = $this->get_style_controls( $control['fields'], $item );
771 }
772
773 $control['style_fields'] = $style_fields;
774 }
775
776 if ( ! empty( $control['selectors'] ) || ! empty( $control['dynamic'] ) || ! empty( $control['style_fields'] ) ) {
777 $style_controls[ $control_name ] = $control;
778 }
779 }
780
781 return $style_controls;
782 }
783
784 /**
785 * Get tabs controls.
786 *
787 * Retrieve all the tabs assigned to the control.
788 *
789 * @since 1.4.0
790 * @access public
791 *
792 * @return array Tabs controls.
793 */
794 final public function get_tabs_controls() {
795 return $this->get_stack()['tabs'];
796 }
797
798 /**
799 * Add new responsive control to stack.
800 *
801 * Register a set of controls to allow editing based on user screen size.
802 * This method registers one or more controls per screen size/device, depending on the current Responsive Control
803 * Duplication Mode. There are 3 control duplication modes:
804 * * 'off' - Only a single control is generated. In the Editor, this control is duplicated in JS.
805 * * 'on' - Multiple controls are generated, one control per enabled device/breakpoint + a default/desktop control.
806 * * 'dynamic' - If the control includes the `'dynamic' => 'active' => true` property - the control is duplicated,
807 * once for each device/breakpoint + default/desktop.
808 * If the control doesn't include the `'dynamic' => 'active' => true` property - the control is not duplicated.
809 *
810 * @since 1.4.0
811 * @access public
812 *
813 * @param string $id Responsive control ID.
814 * @param array $args Responsive control arguments.
815 * @param array $options Optional. Responsive control options. Default is
816 * an empty array.
817 */
818 final public function add_responsive_control( $id, array $args, $options = [] ) {
819 $args['responsive'] = [];
820
821 $active_breakpoints = Plugin::$instance->breakpoints->get_active_breakpoints();
822
823 $devices = Plugin::$instance->breakpoints->get_active_devices_list( [
824 'reverse' => true,
825 'desktop_first' => true,
826 ] );
827
828 if ( isset( $args['devices'] ) ) {
829 $devices = array_intersect( $devices, $args['devices'] );
830
831 $args['responsive']['devices'] = $devices;
832
833 unset( $args['devices'] );
834 }
835
836 $control_to_check = $args;
837
838 if ( ! empty( $options['overwrite'] ) ) {
839 $existing_control = Plugin::$instance->controls_manager->get_control_from_stack( $this->get_unique_name(), $id );
840
841 if ( ! is_wp_error( $existing_control ) ) {
842 $control_to_check = $existing_control;
843 }
844 }
845
846 $responsive_duplication_mode = Plugin::$instance->breakpoints->get_responsive_control_duplication_mode();
847 $additional_breakpoints_active = Plugin::$instance->experiments->is_feature_active( 'additional_custom_breakpoints' );
848 $control_is_dynamic = ! empty( $control_to_check['dynamic']['active'] );
849 $is_frontend_available = ! empty( $control_to_check['frontend_available'] );
850 $has_prefix_class = ! empty( $control_to_check['prefix_class'] );
851
852 // If the new responsive controls experiment is active, create only one control - duplicates per device will
853 // be created in JS in the Editor.
854 if (
855 $additional_breakpoints_active
856 && ( 'off' === $responsive_duplication_mode || ( 'dynamic' === $responsive_duplication_mode && ! $control_is_dynamic ) )
857 // Some responsive controls need responsive settings to be available to the widget handler, even when empty.
858 && ! $is_frontend_available
859 && ! $has_prefix_class
860 ) {
861 $args['is_responsive'] = true;
862
863 if ( ! empty( $options['overwrite'] ) ) {
864 $this->update_control( $id, $args, [
865 'recursive' => ! empty( $options['recursive'] ),
866 ] );
867 } else {
868 $this->add_control( $id, $args, $options );
869 }
870
871 return;
872 }
873
874 if ( isset( $args['default'] ) ) {
875 $args['desktop_default'] = $args['default'];
876
877 unset( $args['default'] );
878 }
879
880 foreach ( $devices as $device_name ) {
881 $control_args = $args;
882
883 // Set parent using the name from previous iteration.
884 if ( isset( $control_name ) ) {
885 // If $control_name end with _widescreen use desktop name instead
886 $control_args['parent'] = '_widescreen' === substr( $control_name, -strlen( '_widescreen' ) ) ? $id : $control_name;
887 } else {
888 $control_args['parent'] = null;
889 }
890
891 if ( isset( $control_args['device_args'] ) ) {
892 if ( ! empty( $control_args['device_args'][ $device_name ] ) ) {
893 $control_args = array_merge( $control_args, $control_args['device_args'][ $device_name ] );
894 }
895
896 unset( $control_args['device_args'] );
897 }
898
899 if ( ! empty( $args['prefix_class'] ) ) {
900 $device_to_replace = Breakpoints_Manager::BREAKPOINT_KEY_DESKTOP === $device_name ? '' : '-' . $device_name;
901
902 $control_args['prefix_class'] = sprintf( $args['prefix_class'], $device_to_replace );
903 }
904
905 $direction = 'max';
906
907 if ( Breakpoints_Manager::BREAKPOINT_KEY_DESKTOP !== $device_name ) {
908 $direction = $active_breakpoints[ $device_name ]->get_direction();
909 }
910
911 $control_args['responsive'][ $direction ] = $device_name;
912
913 if ( isset( $control_args['min_affected_device'] ) ) {
914 if ( ! empty( $control_args['min_affected_device'][ $device_name ] ) ) {
915 $control_args['responsive']['min'] = $control_args['min_affected_device'][ $device_name ];
916 }
917
918 unset( $control_args['min_affected_device'] );
919 }
920
921 if ( isset( $control_args[ $device_name . '_default' ] ) ) {
922 $control_args['default'] = $control_args[ $device_name . '_default' ];
923 }
924
925 foreach ( $devices as $device ) {
926 unset( $control_args[ $device . '_default' ] );
927 }
928
929 $id_suffix = Breakpoints_Manager::BREAKPOINT_KEY_DESKTOP === $device_name ? '' : '_' . $device_name;
930 $control_name = $id . $id_suffix;
931
932 // Set this control as child of previous iteration control.
933 $this->update_control( $control_args['parent'], [ 'inheritors' => [ $control_name ] ] );
934
935 if ( ! empty( $options['overwrite'] ) ) {
936 $this->update_control( $control_name, $control_args, [
937 'recursive' => ! empty( $options['recursive'] ),
938 ] );
939 } else {
940 $this->add_control( $control_name, $control_args, $options );
941 }
942 }
943 }
944
945 /**
946 * Update responsive control in stack.
947 *
948 * Change the value of an existing responsive control in the stack. When you
949 * add new control you set the `$args` parameter, this method allows you to
950 * update the arguments by passing new data.
951 *
952 * @since 1.4.0
953 * @access public
954 *
955 * @param string $id Responsive control ID.
956 * @param array $args Responsive control arguments.
957 * @param array $options Optional. Additional options.
958 */
959 final public function update_responsive_control( $id, array $args, array $options = [] ) {
960 $this->add_responsive_control( $id, $args, [
961 'overwrite' => true,
962 'recursive' => ! empty( $options['recursive'] ),
963 ] );
964 }
965
966 /**
967 * Remove responsive control from stack.
968 *
969 * Unregister an existing responsive control and remove it from the stack.
970 *
971 * @since 1.4.0
972 * @access public
973 *
974 * @param string $id Responsive control ID.
975 */
976 final public function remove_responsive_control( $id ) {
977 $devices = Plugin::$instance->breakpoints->get_active_devices_list( [ 'reverse' => true ] );
978
979 foreach ( $devices as $device_name ) {
980 $id_suffix = Breakpoints_Manager::BREAKPOINT_KEY_DESKTOP === $device_name ? '' : '_' . $device_name;
981
982 $this->remove_control( $id . $id_suffix );
983 }
984 }
985
986 /**
987 * Get class name.
988 *
989 * Retrieve the name of the current class.
990 *
991 * @since 1.4.0
992 * @access public
993 *
994 * @return string Class name.
995 */
996 final public function get_class_name() {
997 return get_called_class();
998 }
999
1000 /**
1001 * Get the config.
1002 *
1003 * Retrieve the config or, if non set, use the initial config.
1004 *
1005 * @since 1.4.0
1006 * @access public
1007 *
1008 * @return array|null The config.
1009 */
1010 final public function get_config() {
1011 if ( null === $this->config ) {
1012 // TODO: This is for backwards compatibility starting from 2.9.0
1013 // This if statement should be removed when the method is hard-deprecated
1014 if ( $this->has_own_method( '_get_initial_config', self::class ) ) {
1015 Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( '_get_initial_config', '2.9.0', __CLASS__ . '::get_initial_config()' );
1016
1017 $this->config = $this->_get_initial_config();
1018 } else {
1019 $this->config = $this->get_initial_config();
1020 }
1021
1022 foreach ( $this->additional_config as $key => $value ) {
1023 if ( isset( $this->config[ $key ] ) ) {
1024 $this->config[ $key ] = wp_parse_args( $value, $this->config[ $key ] );
1025 } else {
1026 $this->config[ $key ] = $value;
1027 }
1028 }
1029 }
1030
1031 return $this->config;
1032 }
1033
1034 /**
1035 * Set a config property.
1036 *
1037 * Set a specific property of the config list for this controls-stack.
1038 *
1039 * @since 3.5.0
1040 * @access public
1041 */
1042 public function set_config( $key, $value ) {
1043 if ( isset( $this->additional_config[ $key ] ) ) {
1044 $this->additional_config[ $key ] = wp_parse_args( $value, $this->additional_config[ $key ] );
1045 } else {
1046 $this->additional_config[ $key ] = $value;
1047 }
1048 }
1049
1050 /**
1051 * Get frontend settings keys.
1052 *
1053 * Retrieve settings keys for all frontend controls.
1054 *
1055 * @since 1.6.0
1056 * @access public
1057 *
1058 * @return array Settings keys for each control.
1059 */
1060 final public function get_frontend_settings_keys() {
1061 $controls = [];
1062
1063 foreach ( $this->get_controls() as $control ) {
1064 if ( ! empty( $control['frontend_available'] ) ) {
1065 $controls[] = $control['name'];
1066 }
1067 }
1068
1069 return $controls;
1070 }
1071
1072 /**
1073 * Get controls pointer index.
1074 *
1075 * Retrieve pointer index where the next control should be added.
1076 *
1077 * While using injection point, it will return the injection point index.
1078 * Otherwise index of the last control plus one.
1079 *
1080 * @since 1.9.2
1081 * @access public
1082 *
1083 * @return int Controls pointer index.
1084 */
1085 public function get_pointer_index() {
1086 if ( null !== $this->injection_point ) {
1087 return $this->injection_point['index'];
1088 }
1089
1090 return count( $this->get_controls() );
1091 }
1092
1093 /**
1094 * Get the raw data.
1095 *
1096 * Retrieve all the items or, when requested, a specific item.
1097 *
1098 * @since 1.4.0
1099 * @access public
1100 *
1101 * @param string $item Optional. The requested item. Default is null.
1102 *
1103 * @return mixed The raw data.
1104 */
1105 public function get_data( $item = null ) {
1106 if ( ! $this->settings_sanitized && ( ! $item || 'settings' === $item ) ) {
1107 $this->data['settings'] = $this->sanitize_settings( $this->data['settings'] );
1108
1109 $this->settings_sanitized = true;
1110 }
1111
1112 return self::get_items( $this->data, $item );
1113 }
1114
1115 /**
1116 * @since 2.0.14
1117 * @access public
1118 */
1119 public function get_parsed_dynamic_settings( $setting = null, $settings = null ) {
1120 if ( null === $settings ) {
1121 $settings = $this->get_settings();
1122 }
1123
1124 if ( null === $this->parsed_dynamic_settings ) {
1125 $this->parsed_dynamic_settings = $this->parse_dynamic_settings( $settings );
1126 }
1127
1128 return self::get_items( $this->parsed_dynamic_settings, $setting );
1129 }
1130
1131 /**
1132 * Get active settings.
1133 *
1134 * Retrieve the settings from all the active controls.
1135 *
1136 * @since 1.4.0
1137 * @since 2.1.0 Added the `controls` and the `settings` parameters.
1138 * @access public
1139 *
1140 * @param array $controls Optional. An array of controls. Default is null.
1141 * @param array $settings Optional. Controls settings. Default is null.
1142 *
1143 * @return array Active settings.
1144 */
1145 public function get_active_settings( $settings = null, $controls = null ) {
1146 $is_first_request = ! $settings && ! $this->active_settings;
1147
1148 if ( ! $settings ) {
1149 if ( $this->active_settings ) {
1150 return $this->active_settings;
1151 }
1152
1153 $settings = $this->get_controls_settings();
1154
1155 $controls = $this->get_controls();
1156 }
1157
1158 $active_settings = [];
1159
1160 foreach ( $settings as $setting_key => $setting ) {
1161 if ( ! isset( $controls[ $setting_key ] ) ) {
1162 $active_settings[ $setting_key ] = $setting;
1163
1164 continue;
1165 }
1166
1167 $control = $controls[ $setting_key ];
1168
1169 if ( $this->is_control_visible( $control, $settings ) ) {
1170 $control_obj = Plugin::$instance->controls_manager->get_control( $control['type'] );
1171
1172 if ( $control_obj instanceof Control_Repeater ) {
1173 foreach ( $setting as & $item ) {
1174 $item = $this->get_active_settings( $item, $control['fields'] );
1175 }
1176 }
1177
1178 $active_settings[ $setting_key ] = $setting;
1179 } else {
1180 $active_settings[ $setting_key ] = null;
1181 }
1182 }
1183
1184 if ( $is_first_request ) {
1185 $this->active_settings = $active_settings;
1186 }
1187
1188 return $active_settings;
1189 }
1190
1191 /**
1192 * Get settings for display.
1193 *
1194 * Retrieve all the settings or, when requested, a specific setting for display.
1195 *
1196 * Unlike `get_settings()` method, this method retrieves only active settings
1197 * that passed all the conditions, rendered all the shortcodes and all the dynamic
1198 * tags.
1199 *
1200 * @since 2.0.0
1201 * @access public
1202 *
1203 * @param string $setting_key Optional. The key of the requested setting.
1204 * Default is null.
1205 *
1206 * @return mixed The settings.
1207 */
1208 public function get_settings_for_display( $setting_key = null ) {
1209 if ( ! $this->parsed_active_settings ) {
1210 $this->parsed_active_settings = $this->get_active_settings( $this->get_parsed_dynamic_settings(), $this->get_controls() );
1211 }
1212
1213 return self::get_items( $this->parsed_active_settings, $setting_key );
1214 }
1215
1216 /**
1217 * Parse dynamic settings.
1218 *
1219 * Retrieve the settings with rendered dynamic tags.
1220 *
1221 * @since 2.0.0
1222 * @access public
1223 *
1224 * @param array $settings Optional. The requested setting. Default is null.
1225 * @param array $controls Optional. The controls array. Default is null.
1226 * @param array $all_settings Optional. All the settings. Default is null.
1227 *
1228 * @return array The settings with rendered dynamic tags.
1229 */
1230 public function parse_dynamic_settings( $settings, $controls = null, $all_settings = null ) {
1231 if ( null === $all_settings ) {
1232 $all_settings = $this->get_settings();
1233 }
1234
1235 if ( null === $controls ) {
1236 $controls = $this->get_controls();
1237 }
1238
1239 foreach ( $controls as $control ) {
1240 $control_name = $control['name'];
1241 $control_obj = Plugin::$instance->controls_manager->get_control( $control['type'] );
1242
1243 if ( ! $control_obj instanceof Base_Data_Control ) {
1244 continue;
1245 }
1246
1247 if ( $control_obj instanceof Control_Repeater ) {
1248 if ( ! isset( $settings[ $control_name ] ) ) {
1249 continue;
1250 }
1251
1252 foreach ( $settings[ $control_name ] as & $field ) {
1253 $field = $this->parse_dynamic_settings( $field, $control['fields'], $field );
1254 }
1255
1256 continue;
1257 }
1258
1259 $dynamic_settings = $control_obj->get_settings( 'dynamic' );
1260
1261 if ( ! $dynamic_settings ) {
1262 $dynamic_settings = [];
1263 }
1264
1265 if ( ! empty( $control['dynamic'] ) ) {
1266 $dynamic_settings = array_merge( $dynamic_settings, $control['dynamic'] );
1267 }
1268
1269 if ( empty( $dynamic_settings ) || ! isset( $all_settings[ Manager::DYNAMIC_SETTING_KEY ][ $control_name ] ) ) {
1270 continue;
1271 }
1272
1273 if ( ! empty( $dynamic_settings['active'] ) && ! empty( $all_settings[ Manager::DYNAMIC_SETTING_KEY ][ $control_name ] ) ) {
1274 $parsed_value = $control_obj->parse_tags( $all_settings[ Manager::DYNAMIC_SETTING_KEY ][ $control_name ], $dynamic_settings );
1275
1276 $dynamic_property = ! empty( $dynamic_settings['property'] ) ? $dynamic_settings['property'] : null;
1277
1278 if ( $dynamic_property ) {
1279 $settings[ $control_name ][ $dynamic_property ] = $parsed_value;
1280 } else {
1281 $settings[ $control_name ] = $parsed_value;
1282 }
1283 }
1284 }
1285
1286 return $settings;
1287 }
1288
1289 /**
1290 * Get frontend settings.
1291 *
1292 * Retrieve the settings for all frontend controls.
1293 *
1294 * @since 1.6.0
1295 * @access public
1296 *
1297 * @return array Frontend settings.
1298 */
1299 public function get_frontend_settings() {
1300 $frontend_settings = array_intersect_key( $this->get_settings_for_display(), array_flip( $this->get_frontend_settings_keys() ) );
1301
1302 foreach ( $frontend_settings as $key => $setting ) {
1303 if ( in_array( $setting, [ null, '' ], true ) ) {
1304 unset( $frontend_settings[ $key ] );
1305 }
1306 }
1307
1308 return $frontend_settings;
1309 }
1310
1311 /**
1312 * Filter controls settings.
1313 *
1314 * Receives controls, settings and a callback function to filter the settings by
1315 * and returns filtered settings.
1316 *
1317 * @since 1.5.0
1318 * @access public
1319 *
1320 * @param callable $callback The callback function.
1321 * @param array $settings Optional. Control settings. Default is an empty
1322 * array.
1323 * @param array $controls Optional. Controls list. Default is an empty
1324 * array.
1325 *
1326 * @return array Filtered settings.
1327 */
1328 public function filter_controls_settings( callable $callback, array $settings = [], array $controls = [] ) {
1329 if ( ! $settings ) {
1330 $settings = $this->get_settings();
1331 }
1332
1333 if ( ! $controls ) {
1334 $controls = $this->get_controls();
1335 }
1336
1337 return array_reduce(
1338 array_keys( $settings ), function( $filtered_settings, $setting_key ) use ( $controls, $settings, $callback ) {
1339 if ( isset( $controls[ $setting_key ] ) ) {
1340 $result = $callback( $settings[ $setting_key ], $controls[ $setting_key ] );
1341
1342 if ( null !== $result ) {
1343 $filtered_settings[ $setting_key ] = $result;
1344 }
1345 }
1346
1347 return $filtered_settings;
1348 }, []
1349 );
1350 }
1351
1352 /**
1353 * Get Responsive Control Device Suffix
1354 *
1355 * @param array $control
1356 * @return string $device suffix
1357 */
1358 protected function get_responsive_control_device_suffix( $control ) {
1359 if ( ! empty( $control['responsive']['max'] ) ) {
1360 $query_device = $control['responsive']['max'];
1361 } elseif ( ! empty( $control['responsive']['min'] ) ) {
1362 $query_device = $control['responsive']['min'];
1363 } else {
1364 return '';
1365 }
1366
1367 return 'desktop' === $query_device ? '' : '_' . $query_device;
1368 }
1369
1370 /**
1371 * Whether the control is visible or not.
1372 *
1373 * Used to determine whether the control is visible or not.
1374 *
1375 * @since 1.4.0
1376 * @access public
1377 *
1378 * @param array $control The control.
1379 * @param array $values Optional. Condition values. Default is null.
1380 *
1381 * @return bool Whether the control is visible.
1382 */
1383 public function is_control_visible( $control, $values = null ) {
1384 if ( null === $values ) {
1385 $values = $this->get_settings();
1386 }
1387
1388 if ( ! empty( $control['conditions'] ) && ! Conditions::check( $control['conditions'], $values ) ) {
1389 return false;
1390 }
1391
1392 if ( empty( $control['condition'] ) ) {
1393 return true;
1394 }
1395
1396 $controls = $this->get_controls();
1397
1398 foreach ( $control['condition'] as $condition_key => $condition_value ) {
1399 preg_match( '/([a-z_\-0-9]+)(?:\[([a-z_]+)])?(!?)$/i', $condition_key, $condition_key_parts );
1400
1401 $pure_condition_key = $condition_key_parts[1];
1402 $condition_sub_key = $condition_key_parts[2];
1403 $is_negative_condition = ! ! $condition_key_parts[3];
1404
1405 if ( ! isset( $values[ $pure_condition_key ] ) || null === $values[ $pure_condition_key ] ) {
1406 return false;
1407 }
1408
1409 $are_control_and_condition_responsive = isset( $control['responsive'] ) && ! empty( $controls[ $pure_condition_key ]['responsive'] );
1410 $condition_name_to_check = $pure_condition_key;
1411
1412 if ( $are_control_and_condition_responsive ) {
1413 $device_suffix = $this->get_responsive_control_device_suffix( $control );
1414
1415 $condition_name_to_check = $pure_condition_key . $device_suffix;
1416
1417 // If the control is not desktop, take the value of the conditioning control of the corresponding device.
1418 $instance_value = $values[ $pure_condition_key . $device_suffix ];
1419 } else {
1420 $instance_value = $values[ $pure_condition_key ];
1421 }
1422
1423 if ( ! $instance_value ) {
1424 $parent = isset( $controls[ $condition_name_to_check ]['parent'] ) ? $controls[ $condition_name_to_check ]['parent'] : false;
1425
1426 while ( $parent ) {
1427 $instance_value = $values[ $parent ];
1428
1429 if ( $instance_value ) {
1430 break;
1431 }
1432 $parent = isset( $controls[ $parent ]['parent'] ) ? $controls[ $parent ]['parent'] : false;
1433 }
1434 }
1435
1436 if ( $condition_sub_key && is_array( $instance_value ) ) {
1437 if ( ! isset( $instance_value[ $condition_sub_key ] ) ) {
1438 return false;
1439 }
1440
1441 $instance_value = $instance_value[ $condition_sub_key ];
1442 }
1443
1444 /**
1445 * If the $condition_value is a non empty array - check if the $condition_value contains the $instance_value,
1446 * If the $instance_value is a non empty array - check if the $instance_value contains the $condition_value
1447 * otherwise check if they are equal. ( and give the ability to check if the value is an empty array )
1448 */
1449 if ( is_array( $condition_value ) && ! empty( $condition_value ) ) {
1450 $is_contains = in_array( $instance_value, $condition_value, true );
1451 } elseif ( is_array( $instance_value ) && ! empty( $instance_value ) ) {
1452 $is_contains = in_array( $condition_value, $instance_value, true );
1453 } else {
1454 $is_contains = $instance_value === $condition_value;
1455 }
1456
1457 if ( $is_negative_condition && $is_contains || ! $is_negative_condition && ! $is_contains ) {
1458 return false;
1459 }
1460 }
1461
1462 return true;
1463 }
1464
1465 /**
1466 * Start controls section.
1467 *
1468 * Used to add a new section of controls. When you use this method, all the
1469 * registered controls from this point will be assigned to this section,
1470 * until you close the section using `end_controls_section()` method.
1471 *
1472 * This method should be used inside `register_controls()`.
1473 *
1474 * @since 1.4.0
1475 * @access public
1476 *
1477 * @param string $section_id Section ID.
1478 * @param array $args Section arguments Optional.
1479 */
1480 public function start_controls_section( $section_id, array $args = [] ) {
1481 $stack_name = $this->get_name();
1482
1483 /**
1484 * Before section start.
1485 *
1486 * Fires before Elementor section starts in the editor panel.
1487 *
1488 * @since 1.4.0
1489 *
1490 * @param Controls_Stack $this The control.
1491 * @param string $section_id Section ID.
1492 * @param array $args Section arguments.
1493 */
1494 do_action( 'elementor/element/before_section_start', $this, $section_id, $args );
1495
1496 /**
1497 * Before section start.
1498 *
1499 * Fires before Elementor section starts in the editor panel.
1500 *
1501 * The dynamic portions of the hook name, `$stack_name` and `$section_id`, refers to the stack name and section ID, respectively.
1502 *
1503 * @since 1.4.0
1504 *
1505 * @param Controls_Stack $this The control.
1506 * @param array $args Section arguments.
1507 */
1508 do_action( "elementor/element/{$stack_name}/{$section_id}/before_section_start", $this, $args );
1509
1510 $args['type'] = Controls_Manager::SECTION;
1511
1512 $this->add_control( $section_id, $args );
1513
1514 if ( null !== $this->current_section ) {
1515 wp_die( sprintf( 'Elementor: You can\'t start a section before the end of the previous section "%s".', $this->current_section['section'] ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1516 }
1517
1518 $this->current_section = $this->get_section_args( $section_id );
1519
1520 if ( $this->injection_point ) {
1521 $this->injection_point['section'] = $this->current_section;
1522 }
1523
1524 /**
1525 * After section start.
1526 *
1527 * Fires after Elementor section starts in the editor panel.
1528 *
1529 * @since 1.4.0
1530 *
1531 * @param Controls_Stack $this The control.
1532 * @param string $section_id Section ID.
1533 * @param array $args Section arguments.
1534 */
1535 do_action( 'elementor/element/after_section_start', $this, $section_id, $args );
1536
1537 /**
1538 * After section start.
1539 *
1540 * Fires after Elementor section starts in the editor panel.
1541 *
1542 * The dynamic portions of the hook name, `$stack_name` and `$section_id`, refers to the stack name and section ID, respectively.
1543 *
1544 * @since 1.4.0
1545 *
1546 * @param Controls_Stack $this The control.
1547 * @param array $args Section arguments.
1548 */
1549 do_action( "elementor/element/{$stack_name}/{$section_id}/after_section_start", $this, $args );
1550 }
1551
1552 /**
1553 * End controls section.
1554 *
1555 * Used to close an existing open controls section. When you use this method
1556 * it stops adding new controls to this section.
1557 *
1558 * This method should be used inside `register_controls()`.
1559 *
1560 * @since 1.4.0
1561 * @access public
1562 */
1563 public function end_controls_section() {
1564 $stack_name = $this->get_name();
1565
1566 // Save the current section for the action.
1567 $current_section = $this->current_section;
1568 $section_id = $current_section['section'];
1569 $args = [
1570 'tab' => $current_section['tab'],
1571 ];
1572
1573 /**
1574 * Before section end.
1575 *
1576 * Fires before Elementor section ends in the editor panel.
1577 *
1578 * @since 1.4.0
1579 *
1580 * @param Controls_Stack $this The control.
1581 * @param string $section_id Section ID.
1582 * @param array $args Section arguments.
1583 */
1584 do_action( 'elementor/element/before_section_end', $this, $section_id, $args );
1585
1586 /**
1587 * Before section end.
1588 *
1589 * Fires before Elementor section ends in the editor panel.
1590 *
1591 * The dynamic portions of the hook name, `$stack_name` and `$section_id`, refers to the stack name and section ID, respectively.
1592 *
1593 * @since 1.4.0
1594 *
1595 * @param Controls_Stack $this The control.
1596 * @param array $args Section arguments.
1597 */
1598 do_action( "elementor/element/{$stack_name}/{$section_id}/before_section_end", $this, $args );
1599
1600 $this->current_section = null;
1601
1602 /**
1603 * After section end.
1604 *
1605 * Fires after Elementor section ends in the editor panel.
1606 *
1607 * @since 1.4.0
1608 *
1609 * @param Controls_Stack $this The control.
1610 * @param string $section_id Section ID.
1611 * @param array $args Section arguments.
1612 */
1613 do_action( 'elementor/element/after_section_end', $this, $section_id, $args );
1614
1615 /**
1616 * After section end.
1617 *
1618 * Fires after Elementor section ends in the editor panel.
1619 *
1620 * The dynamic portions of the hook name, `$stack_name` and `$section_id`, refers to the stack name and section ID, respectively.
1621 *
1622 * @since 1.4.0
1623 *
1624 * @param Controls_Stack $this The control.
1625 * @param array $args Section arguments.
1626 */
1627 do_action( "elementor/element/{$stack_name}/{$section_id}/after_section_end", $this, $args );
1628 }
1629
1630 /**
1631 * Start controls tabs.
1632 *
1633 * Used to add a new set of tabs inside a section. You should use this
1634 * method before adding new individual tabs using `start_controls_tab()`.
1635 * Each tab added after this point will be assigned to this group of tabs,
1636 * until you close it using `end_controls_tabs()` method.
1637 *
1638 * This method should be used inside `register_controls()`.
1639 *
1640 * @since 1.4.0
1641 * @access public
1642 *
1643 * @param string $tabs_id Tabs ID.
1644 * @param array $args Tabs arguments.
1645 */
1646 public function start_controls_tabs( $tabs_id, array $args = [] ) {
1647 if ( null !== $this->current_tab ) {
1648 wp_die( sprintf( 'Elementor: You can\'t start tabs before the end of the previous tabs "%s".', $this->current_tab['tabs_wrapper'] ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1649 }
1650
1651 $args['type'] = Controls_Manager::TABS;
1652
1653 $this->add_control( $tabs_id, $args );
1654
1655 $this->current_tab = [
1656 'tabs_wrapper' => $tabs_id,
1657 ];
1658
1659 foreach ( [ 'condition', 'conditions' ] as $key ) {
1660 if ( ! empty( $args[ $key ] ) ) {
1661 $this->current_tab[ $key ] = $args[ $key ];
1662 }
1663 }
1664
1665 if ( $this->injection_point ) {
1666 $this->injection_point['tab'] = $this->current_tab;
1667 }
1668 }
1669
1670 /**
1671 * End controls tabs.
1672 *
1673 * Used to close an existing open controls tabs. When you use this method it
1674 * stops adding new controls to this tabs.
1675 *
1676 * This method should be used inside `register_controls()`.
1677 *
1678 * @since 1.4.0
1679 * @access public
1680 */
1681 public function end_controls_tabs() {
1682 $this->current_tab = null;
1683 }
1684
1685 /**
1686 * Start controls tab.
1687 *
1688 * Used to add a new tab inside a group of tabs. Use this method before
1689 * adding new individual tabs using `start_controls_tab()`.
1690 * Each tab added after this point will be assigned to this group of tabs,
1691 * until you close it using `end_controls_tab()` method.
1692 *
1693 * This method should be used inside `register_controls()`.
1694 *
1695 * @since 1.4.0
1696 * @access public
1697 *
1698 * @param string $tab_id Tab ID.
1699 * @param array $args Tab arguments.
1700 */
1701 public function start_controls_tab( $tab_id, $args ) {
1702 if ( ! empty( $this->current_tab['inner_tab'] ) ) {
1703 wp_die( sprintf( 'Elementor: You can\'t start a tab before the end of the previous tab "%s".', $this->current_tab['inner_tab'] ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1704 }
1705
1706 $args['type'] = Controls_Manager::TAB;
1707 $args['tabs_wrapper'] = $this->current_tab['tabs_wrapper'];
1708
1709 $this->add_control( $tab_id, $args );
1710
1711 $this->current_tab['inner_tab'] = $tab_id;
1712
1713 if ( $this->injection_point ) {
1714 $this->injection_point['tab']['inner_tab'] = $this->current_tab['inner_tab'];
1715 }
1716 }
1717
1718 /**
1719 * End controls tab.
1720 *
1721 * Used to close an existing open controls tab. When you use this method it
1722 * stops adding new controls to this tab.
1723 *
1724 * This method should be used inside `register_controls()`.
1725 *
1726 * @since 1.4.0
1727 * @access public
1728 */
1729 public function end_controls_tab() {
1730 unset( $this->current_tab['inner_tab'] );
1731 }
1732
1733 /**
1734 * Start popover.
1735 *
1736 * Used to add a new set of controls in a popover. When you use this method,
1737 * all the registered controls from this point will be assigned to this
1738 * popover, until you close the popover using `end_popover()` method.
1739 *
1740 * This method should be used inside `register_controls()`.
1741 *
1742 * @since 1.9.0
1743 * @access public
1744 */
1745 final public function start_popover() {
1746 $this->current_popover = [
1747 'initialized' => false,
1748 ];
1749 }
1750
1751 /**
1752 * End popover.
1753 *
1754 * Used to close an existing open popover. When you use this method it stops
1755 * adding new controls to this popover.
1756 *
1757 * This method should be used inside `register_controls()`.
1758 *
1759 * @since 1.9.0
1760 * @access public
1761 */
1762 final public function end_popover() {
1763 $this->current_popover = null;
1764
1765 $last_control_key = $this->get_control_key( $this->get_pointer_index() - 1 );
1766
1767 $args = [
1768 'popover' => [
1769 'end' => true,
1770 ],
1771 ];
1772
1773 $options = [
1774 'recursive' => true,
1775 ];
1776
1777 $this->update_control( $last_control_key, $args, $options );
1778 }
1779
1780 /**
1781 * Add render attribute.
1782 *
1783 * Used to add attributes to a specific HTML element.
1784 *
1785 * The HTML tag is represented by the element parameter, then you need to
1786 * define the attribute key and the attribute key. The final result will be:
1787 * `<element attribute_key="attribute_value">`.
1788 *
1789 * Example usage:
1790 *
1791 * `$this->add_render_attribute( 'wrapper', 'class', 'custom-widget-wrapper-class' );`
1792 * `$this->add_render_attribute( 'widget', 'id', 'custom-widget-id' );`
1793 * `$this->add_render_attribute( 'button', [ 'class' => 'custom-button-class', 'id' => 'custom-button-id' ] );`
1794 *
1795 * @since 1.0.0
1796 * @access public
1797 *
1798 * @param array|string $element The HTML element.
1799 * @param array|string $key Optional. Attribute key. Default is null.
1800 * @param array|string $value Optional. Attribute value. Default is null.
1801 * @param bool $overwrite Optional. Whether to overwrite existing
1802 * attribute. Default is false, not to overwrite.
1803 *
1804 * @return self Current instance of the element.
1805 */
1806 public function add_render_attribute( $element, $key = null, $value = null, $overwrite = false ) {
1807 if ( is_array( $element ) ) {
1808 foreach ( $element as $element_key => $attributes ) {
1809 $this->add_render_attribute( $element_key, $attributes, null, $overwrite );
1810 }
1811
1812 return $this;
1813 }
1814
1815 if ( is_array( $key ) ) {
1816 foreach ( $key as $attribute_key => $attributes ) {
1817 $this->add_render_attribute( $element, $attribute_key, $attributes, $overwrite );
1818 }
1819
1820 return $this;
1821 }
1822
1823 if ( empty( $this->render_attributes[ $element ][ $key ] ) ) {
1824 $this->render_attributes[ $element ][ $key ] = [];
1825 }
1826
1827 settype( $value, 'array' );
1828
1829 if ( $overwrite ) {
1830 $this->render_attributes[ $element ][ $key ] = $value;
1831 } else {
1832 $this->render_attributes[ $element ][ $key ] = array_merge( $this->render_attributes[ $element ][ $key ], $value );
1833 }
1834
1835 return $this;
1836 }
1837
1838 /**
1839 * Get Render Attributes
1840 *
1841 * Used to retrieve render attribute.
1842 *
1843 * The returned array is either all elements and their attributes if no `$element` is specified, an array of all
1844 * attributes of a specific element or a specific attribute properties if `$key` is specified.
1845 *
1846 * Returns null if one of the requested parameters isn't set.
1847 *
1848 * @since 2.2.6
1849 * @access public
1850 * @param string $element
1851 * @param string $key
1852 *
1853 * @return array
1854 */
1855 public function get_render_attributes( $element = '', $key = '' ) {
1856 $attributes = $this->render_attributes;
1857
1858 if ( $element ) {
1859 if ( ! isset( $attributes[ $element ] ) ) {
1860 return null;
1861 }
1862
1863 $attributes = $attributes[ $element ];
1864
1865 if ( $key ) {
1866 if ( ! isset( $attributes[ $key ] ) ) {
1867 return null;
1868 }
1869
1870 $attributes = $attributes[ $key ];
1871 }
1872 }
1873
1874 return $attributes;
1875 }
1876
1877 /**
1878 * Set render attribute.
1879 *
1880 * Used to set the value of the HTML element render attribute or to update
1881 * an existing render attribute.
1882 *
1883 * @since 1.0.0
1884 * @access public
1885 *
1886 * @param array|string $element The HTML element.
1887 * @param array|string $key Optional. Attribute key. Default is null.
1888 * @param array|string $value Optional. Attribute value. Default is null.
1889 *
1890 * @return self Current instance of the element.
1891 */
1892 public function set_render_attribute( $element, $key = null, $value = null ) {
1893 return $this->add_render_attribute( $element, $key, $value, true );
1894 }
1895
1896 /**
1897 * Remove render attribute.
1898 *
1899 * Used to remove an element (with its keys and their values), key (with its values),
1900 * or value/s from an HTML element's render attribute.
1901 *
1902 * @since 2.7.0
1903 * @access public
1904 *
1905 * @param string $element The HTML element.
1906 * @param string $key Optional. Attribute key. Default is null.
1907 * @param array|string $values Optional. Attribute value/s. Default is null.
1908 */
1909 public function remove_render_attribute( $element, $key = null, $values = null ) {
1910 if ( $key && ! isset( $this->render_attributes[ $element ][ $key ] ) ) {
1911 return;
1912 }
1913
1914 if ( $values ) {
1915 $values = (array) $values;
1916
1917 $this->render_attributes[ $element ][ $key ] = array_diff( $this->render_attributes[ $element ][ $key ], $values );
1918
1919 return;
1920 }
1921
1922 if ( $key ) {
1923 unset( $this->render_attributes[ $element ][ $key ] );
1924
1925 return;
1926 }
1927
1928 if ( isset( $this->render_attributes[ $element ] ) ) {
1929 unset( $this->render_attributes[ $element ] );
1930 }
1931 }
1932
1933 /**
1934 * Get render attribute string.
1935 *
1936 * Used to retrieve the value of the render attribute.
1937 *
1938 * @since 1.0.0
1939 * @access public
1940 *
1941 * @param string $element The element.
1942 *
1943 * @return string Render attribute string, or an empty string if the attribute
1944 * is empty or not exist.
1945 */
1946 public function get_render_attribute_string( $element ) {
1947 if ( empty( $this->render_attributes[ $element ] ) ) {
1948 return '';
1949 }
1950
1951 return Utils::render_html_attributes( $this->render_attributes[ $element ] );
1952 }
1953
1954 /**
1955 * Print render attribute string.
1956 *
1957 * Used to output the rendered attribute.
1958 *
1959 * @since 2.0.0
1960 * @access public
1961 *
1962 * @param array|string $element The element.
1963 */
1964 public function print_render_attribute_string( $element ) {
1965 echo $this->get_render_attribute_string( $element ); // XSS ok.
1966 }
1967
1968 /**
1969 * Print element template.
1970 *
1971 * Used to generate the element template on the editor.
1972 *
1973 * @since 2.0.0
1974 * @access public
1975 */
1976 public function print_template() {
1977 ob_start();
1978
1979 // TODO: This is for backwards compatibility starting from 2.9.0
1980 // This `if` statement should be removed when the method is removed
1981 if ( $this->has_own_method( '_content_template', self::class ) ) {
1982 Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( '_content_template', '2.9.0', __CLASS__ . '::content_template()' );
1983
1984 $this->_content_template();
1985 } else {
1986 $this->content_template();
1987 }
1988
1989 $template_content = ob_get_clean();
1990
1991 $element_type = $this->get_type();
1992
1993 /**
1994 * Template content.
1995 *
1996 * Filters the controls stack template content before it's printed in the editor.
1997 *
1998 * The dynamic portion of the hook name, `$element_type`, refers to the element type.
1999 *
2000 * @since 1.0.0
2001 *
2002 * @param string $content_template The controls stack template in the editor.
2003 * @param Controls_Stack $this The controls stack.
2004 */
2005 $template_content = apply_filters( "elementor/{$element_type}/print_template", $template_content, $this );
2006
2007 if ( empty( $template_content ) ) {
2008 return;
2009 }
2010 ?>
2011 <script type="text/html" id="tmpl-elementor-<?php echo esc_attr( $this->get_name() ); ?>-content">
2012 <?php $this->print_template_content( $template_content ); ?>
2013 </script>
2014 <?php
2015 }
2016
2017 /**
2018 * On import update dynamic content (e.g. post and term IDs).
2019 *
2020 * @since 3.8.0
2021 *
2022 * @param array $config The config of the passed element.
2023 * @param array $data The data that requires updating/replacement when imported.
2024 * @param array|null $controls The available controls.
2025 *
2026 * @return array Element data.
2027 */
2028 public static function on_import_update_dynamic_content( array $config, array $data, $controls = null ) : array {
2029 return $config;
2030 }
2031
2032 /**
2033 * Start injection.
2034 *
2035 * Used to inject controls and sections to a specific position in the stack.
2036 *
2037 * When you use this method, all the registered controls and sections will
2038 * be injected to a specific position in the stack, until you stop the
2039 * injection using `end_injection()` method.
2040 *
2041 * @since 1.7.1
2042 * @access public
2043 *
2044 * @param array $position {
2045 * The position where to start the injection.
2046 *
2047 * @type string $type Injection type, either `control` or `section`.
2048 * Default is `control`.
2049 * @type string $at Where to inject. If `$type` is `control` accepts
2050 * `before` and `after`. If `$type` is `section`
2051 * accepts `start` and `end`. Default values based on
2052 * the `type`.
2053 * @type string $of Control/Section ID.
2054 * }
2055 */
2056 final public function start_injection( array $position ) {
2057 if ( $this->injection_point ) {
2058 wp_die( 'A controls injection is already opened. Please close current injection before starting a new one (use `end_injection`).' );
2059 }
2060
2061 $this->injection_point = $this->get_position_info( $position );
2062 }
2063
2064 /**
2065 * End injection.
2066 *
2067 * Used to close an existing opened injection point.
2068 *
2069 * When you use this method it stops adding new controls and sections to
2070 * this point and continue to add controls to the regular position in the
2071 * stack.
2072 *
2073 * @since 1.7.1
2074 * @access public
2075 */
2076 final public function end_injection() {
2077 $this->injection_point = null;
2078 }
2079
2080 /**
2081 * Get injection point.
2082 *
2083 * Retrieve the injection point in the stack where new controls and sections
2084 * will be inserted.
2085 *
2086 * @since 1.9.2
2087 * @access public
2088 *
2089 * @return array|null An array when an injection point is defined, null
2090 * otherwise.
2091 */
2092 final public function get_injection_point() {
2093 return $this->injection_point;
2094 }
2095
2096 /**
2097 * Register controls.
2098 *
2099 * Used to add new controls to any element type. For example, external
2100 * developers use this method to register controls in a widget.
2101 *
2102 * Should be inherited and register new controls using `add_control()`,
2103 * `add_responsive_control()` and `add_group_control()`, inside control
2104 * wrappers like `start_controls_section()`, `start_controls_tabs()` and
2105 * `start_controls_tab()`.
2106 *
2107 * @since 1.4.0
2108 * @access protected
2109 * @deprecated 3.1.0 Use `Controls_Stack::register_controls()` instead
2110 */
2111 protected function _register_controls() {
2112 Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( __METHOD__, '3.1.0', __CLASS__ . '::register_controls()' );
2113
2114 $this->register_controls();
2115 }
2116
2117 /**
2118 * Register controls.
2119 *
2120 * Used to add new controls to any element type. For example, external
2121 * developers use this method to register controls in a widget.
2122 *
2123 * Should be inherited and register new controls using `add_control()`,
2124 * `add_responsive_control()` and `add_group_control()`, inside control
2125 * wrappers like `start_controls_section()`, `start_controls_tabs()` and
2126 * `start_controls_tab()`.
2127 *
2128 * @since 3.1.0
2129 * @access protected
2130 */
2131 protected function register_controls() {}
2132
2133 /**
2134 * Get default data.
2135 *
2136 * Retrieve the default data. Used to reset the data on initialization.
2137 *
2138 * @since 1.4.0
2139 * @access protected
2140 *
2141 * @return array Default data.
2142 */
2143 protected function get_default_data() {
2144 return [
2145 'id' => 0,
2146 'settings' => [],
2147 ];
2148 }
2149
2150 /**
2151 * @since 2.3.0
2152 * @access protected
2153 */
2154 protected function get_init_settings() {
2155 $settings = $this->get_data( 'settings' );
2156
2157 foreach ( $this->get_controls() as $control ) {
2158 $control_obj = Plugin::$instance->controls_manager->get_control( $control['type'] );
2159
2160 if ( ! $control_obj instanceof Base_Data_Control ) {
2161 continue;
2162 }
2163
2164 $control = array_merge_recursive( $control_obj->get_settings(), $control );
2165
2166 $settings[ $control['name'] ] = $control_obj->get_value( $control, $settings );
2167 }
2168
2169 return $settings;
2170 }
2171
2172 /**
2173 * Get initial config.
2174 *
2175 * Retrieve the current element initial configuration - controls list and
2176 * the tabs assigned to the control.
2177 *
2178 * @since 2.9.0
2179 * @access protected
2180 *
2181 * @return array The initial config.
2182 */
2183 protected function get_initial_config() {
2184 return [
2185 'controls' => $this->get_controls(),
2186 ];
2187 }
2188
2189 /**
2190 * Get initial config.
2191 *
2192 * Retrieve the current element initial configuration - controls list and
2193 * the tabs assigned to the control.
2194 *
2195 * @since 1.4.0
2196 * @deprecated 2.9.0 use `get_initial_config()` instead
2197 * @access protected
2198 *
2199 * @return array The initial config.
2200 */
2201 protected function _get_initial_config() {
2202 Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( __METHOD__, '2.9.0', __CLASS__ . '::get_initial_config()' );
2203
2204 return $this->get_initial_config();
2205 }
2206
2207 /**
2208 * Get section arguments.
2209 *
2210 * Retrieve the section arguments based on section ID.
2211 *
2212 * @since 1.4.0
2213 * @access protected
2214 *
2215 * @param string $section_id Section ID.
2216 *
2217 * @return array Section arguments.
2218 */
2219 protected function get_section_args( $section_id ) {
2220 $section_control = $this->get_controls( $section_id );
2221
2222 $section_args_keys = [ 'tab', 'condition' ];
2223
2224 $args = array_intersect_key( $section_control, array_flip( $section_args_keys ) );
2225
2226 $args['section'] = $section_id;
2227
2228 return $args;
2229 }
2230
2231 /**
2232 * Render element.
2233 *
2234 * Generates the final HTML on the frontend.
2235 *
2236 * @since 2.0.0
2237 * @access protected
2238 */
2239 protected function render() {}
2240
2241 /**
2242 * Render element in static mode.
2243 *
2244 * If not inherent will call the base render.
2245 */
2246 protected function render_static() {
2247 $this->render();
2248 }
2249
2250 /**
2251 * Determine the render logic.
2252 */
2253 protected function render_by_mode() {
2254 if ( Plugin::$instance->frontend->is_static_render_mode() ) {
2255 $this->render_static();
2256
2257 return;
2258 }
2259
2260 $this->render();
2261 }
2262
2263 /**
2264 * Print content template.
2265 *
2266 * Used to generate the content template on the editor, using a
2267 * Backbone JavaScript template.
2268 *
2269 * @access protected
2270 * @since 2.0.0
2271 *
2272 * @param string $template_content Template content.
2273 */
2274 protected function print_template_content( $template_content ) {
2275 echo $template_content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
2276 }
2277
2278 /**
2279 * Render element output in the editor.
2280 *
2281 * Used to generate the live preview, using a Backbone JavaScript template.
2282 *
2283 * @since 2.9.0
2284 * @access protected
2285 */
2286 protected function content_template() {}
2287
2288 /**
2289 * Render element output in the editor.
2290 *
2291 * Used to generate the live preview, using a Backbone JavaScript template.
2292 *
2293 * @since 2.0.0
2294 * @deprecated 2.9.0 use `content_template()` instead
2295 * @access protected
2296 */
2297 protected function _content_template() {
2298 Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( __METHOD__, '2.9.0', __CLASS__ . '::content_template()' );
2299
2300 $this->content_template();
2301 }
2302
2303 /**
2304 * Initialize controls.
2305 *
2306 * Register the all controls added by `register_controls()`.
2307 *
2308 * @since 2.0.0
2309 * @access protected
2310 */
2311 protected function init_controls() {
2312 Plugin::$instance->controls_manager->open_stack( $this );
2313
2314 // TODO: This is for backwards compatibility starting from 2.9.0
2315 // This `if` statement should be removed when the method is removed
2316 if ( $this->has_own_method( '_register_controls', self::class ) ) {
2317 Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( '_register_controls', '3.1.0', __CLASS__ . '::register_controls()' );
2318
2319 $this->_register_controls();
2320 } else {
2321 $this->register_controls();
2322 }
2323 }
2324
2325 protected function handle_control_position( array $args, $control_id, $overwrite ) {
2326 if ( isset( $args['type'] ) && in_array( $args['type'], [ Controls_Manager::SECTION, Controls_Manager::WP_WIDGET ], true ) ) {
2327 return $args;
2328 }
2329
2330 $target_section_args = $this->current_section;
2331
2332 $target_tab = $this->current_tab;
2333
2334 if ( $this->injection_point ) {
2335 $target_section_args = $this->injection_point['section'];
2336
2337 if ( ! empty( $this->injection_point['tab'] ) ) {
2338 $target_tab = $this->injection_point['tab'];
2339 }
2340 }
2341
2342 if ( null !== $target_section_args ) {
2343 if ( ! empty( $args['section'] ) || ! empty( $args['tab'] ) ) {
2344 _doing_it_wrong( sprintf( '%s::%s', get_called_class(), __FUNCTION__ ), sprintf( 'Cannot redeclare control with `tab` or `section` args inside section "%s".', $control_id ), '1.0.0' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
2345 }
2346
2347 $args = array_replace_recursive( $target_section_args, $args );
2348
2349 if ( null !== $target_tab ) {
2350 $args = array_replace_recursive( $target_tab, $args );
2351 }
2352 } elseif ( empty( $args['section'] ) && ( ! $overwrite || is_wp_error( Plugin::$instance->controls_manager->get_control_from_stack( $this->get_unique_name(), $control_id ) ) ) ) {
2353 wp_die( sprintf( '%s::%s: Cannot add a control outside of a section (use `start_controls_section`).', get_called_class(), __FUNCTION__ ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
2354 }
2355
2356 return $args;
2357 }
2358
2359 /**
2360 * Initialize the class.
2361 *
2362 * Set the raw data, the ID and the parsed settings.
2363 *
2364 * @since 2.9.0
2365 * @access protected
2366 *
2367 * @param array $data Initial data.
2368 */
2369 protected function init( $data ) {
2370 $this->data = array_merge( $this->get_default_data(), $data );
2371
2372 $this->id = $data['id'];
2373 }
2374
2375 /**
2376 * Initialize the class.
2377 *
2378 * Set the raw data, the ID and the parsed settings.
2379 *
2380 * @since 1.4.0
2381 * @deprecated 2.9.0 use `init()` instead
2382 * @access protected
2383 *
2384 * @param array $data Initial data.
2385 */
2386 protected function _init( $data ) {
2387 Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( __METHOD__, '2.9.0', __CLASS__ . '::init()' );
2388
2389 $this->init( $data );
2390 }
2391
2392 /**
2393 * Sanitize initial data.
2394 *
2395 * Performs settings cleaning and sanitization.
2396 *
2397 * @since 2.1.5
2398 * @access private
2399 *
2400 * @param array $settings Settings to sanitize.
2401 * @param array $controls Optional. An array of controls. Default is an
2402 * empty array.
2403 *
2404 * @return array Sanitized settings.
2405 */
2406 private function sanitize_settings( array $settings, array $controls = [] ) {
2407 if ( ! $controls ) {
2408 $controls = $this->get_controls();
2409 }
2410
2411 foreach ( $controls as $control ) {
2412 $control_obj = Plugin::$instance->controls_manager->get_control( $control['type'] );
2413
2414 if ( $control_obj instanceof Control_Repeater ) {
2415 if ( empty( $settings[ $control['name'] ] ) ) {
2416 continue;
2417 }
2418
2419 foreach ( $settings[ $control['name'] ] as $index => $repeater_row_data ) {
2420 $sanitized_row_data = $this->sanitize_settings( $repeater_row_data, $control['fields'] );
2421
2422 $settings[ $control['name'] ][ $index ] = $sanitized_row_data;
2423 }
2424
2425 continue;
2426 }
2427
2428 $is_dynamic = isset( $settings[ Manager::DYNAMIC_SETTING_KEY ][ $control['name'] ] );
2429
2430 if ( ! $is_dynamic ) {
2431 continue;
2432 }
2433
2434 $value_to_check = $settings[ Manager::DYNAMIC_SETTING_KEY ][ $control['name'] ];
2435
2436 $tag_text_data = Plugin::$instance->dynamic_tags->tag_text_to_tag_data( $value_to_check );
2437
2438 if ( ! Plugin::$instance->dynamic_tags->get_tag_info( $tag_text_data['name'] ) ) {
2439 unset( $settings[ Manager::DYNAMIC_SETTING_KEY ][ $control['name'] ] );
2440 }
2441 }
2442
2443 return $settings;
2444 }
2445
2446 /**
2447 * Controls stack constructor.
2448 *
2449 * Initializing the control stack class using `$data`. The `$data` is required
2450 * for a normal instance. It is optional only for internal `type instance`.
2451 *
2452 * @since 1.4.0
2453 * @access public
2454 *
2455 * @param array $data Optional. Control stack data. Default is an empty array.
2456 */
2457 public function __construct( array $data = [] ) {
2458 if ( $data ) {
2459 // TODO: This is for backwards compatibility starting from 2.9.0
2460 // This if statement should be removed when the method is hard-deprecated
2461 if ( $this->has_own_method( '_init', self::class ) ) {
2462 Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( '_init', '2.9.0', __CLASS__ . '::init()' );
2463
2464 $this->_init( $data );
2465 } else {
2466 $this->init( $data );
2467 }
2468 }
2469 }
2470 }
2471