PluginProbe
Elementor Website Builder – more than just a page builder / 3.17.3
Elementor Website Builder – more than just a page builder v3.17.3
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.17.3, at includes/base/controls-stack.php

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