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

2,479 lines 67.2 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 if ( ! empty( $control_args['parent'] ) ) {
934 $this->update_control( $control_args['parent'], [ 'inheritors' => [ $control_name ] ] );
935 }
936
937 if ( ! empty( $options['overwrite'] ) ) {
938 $this->update_control( $control_name, $control_args, [
939 'recursive' => ! empty( $options['recursive'] ),
940 ] );
941 } else {
942 $this->add_control( $control_name, $control_args, $options );
943 }
944 }
945 }
946
947 /**
948 * Update responsive control in stack.
949 *
950 * Change the value of an existing responsive control in the stack. When you
951 * add new control you set the `$args` parameter, this method allows you to
952 * update the arguments by passing new data.
953 *
954 * @since 1.4.0
955 * @access public
956 *
957 * @param string $id Responsive control ID.
958 * @param array $args Responsive control arguments.
959 * @param array $options Optional. Additional options.
960 */
961 final public function update_responsive_control( $id, array $args, array $options = [] ) {
962 $this->add_responsive_control( $id, $args, [
963 'overwrite' => true,
964 'recursive' => ! empty( $options['recursive'] ),
965 ] );
966 }
967
968 /**
969 * Remove responsive control from stack.
970 *
971 * Unregister an existing responsive control and remove it from the stack.
972 *
973 * @since 1.4.0
974 * @access public
975 *
976 * @param string $id Responsive control ID.
977 */
978 final public function remove_responsive_control( $id ) {
979 $devices = Plugin::$instance->breakpoints->get_active_devices_list( [ 'reverse' => true ] );
980
981 foreach ( $devices as $device_name ) {
982 $id_suffix = Breakpoints_Manager::BREAKPOINT_KEY_DESKTOP === $device_name ? '' : '_' . $device_name;
983
984 $this->remove_control( $id . $id_suffix );
985 }
986 }
987
988 /**
989 * Get class name.
990 *
991 * Retrieve the name of the current class.
992 *
993 * @since 1.4.0
994 * @access public
995 *
996 * @return string Class name.
997 */
998 final public function get_class_name() {
999 return get_called_class();
1000 }
1001
1002 /**
1003 * Get the config.
1004 *
1005 * Retrieve the config or, if non set, use the initial config.
1006 *
1007 * @since 1.4.0
1008 * @access public
1009 *
1010 * @return array|null The config.
1011 */
1012 final public function get_config() {
1013 if ( null === $this->config ) {
1014 // TODO: This is for backwards compatibility starting from 2.9.0
1015 // This if statement should be removed when the method is hard-deprecated
1016 if ( $this->has_own_method( '_get_initial_config', self::class ) ) {
1017 Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( '_get_initial_config', '2.9.0', __CLASS__ . '::get_initial_config()' );
1018
1019 $this->config = $this->_get_initial_config();
1020 } else {
1021 $this->config = $this->get_initial_config();
1022 }
1023
1024 foreach ( $this->additional_config as $key => $value ) {
1025 if ( isset( $this->config[ $key ] ) ) {
1026 $this->config[ $key ] = wp_parse_args( $value, $this->config[ $key ] );
1027 } else {
1028 $this->config[ $key ] = $value;
1029 }
1030 }
1031 }
1032
1033 return $this->config;
1034 }
1035
1036 /**
1037 * Set a config property.
1038 *
1039 * Set a specific property of the config list for this controls-stack.
1040 *
1041 * @since 3.5.0
1042 * @access public
1043 */
1044 public function set_config( $key, $value ) {
1045 if ( isset( $this->additional_config[ $key ] ) ) {
1046 $this->additional_config[ $key ] = wp_parse_args( $value, $this->additional_config[ $key ] );
1047 } else {
1048 $this->additional_config[ $key ] = $value;
1049 }
1050 }
1051
1052 /**
1053 * Get frontend settings keys.
1054 *
1055 * Retrieve settings keys for all frontend controls.
1056 *
1057 * @since 1.6.0
1058 * @access public
1059 *
1060 * @return array Settings keys for each control.
1061 */
1062 final public function get_frontend_settings_keys() {
1063 $controls = [];
1064
1065 foreach ( $this->get_controls() as $control ) {
1066 if ( ! empty( $control['frontend_available'] ) ) {
1067 $controls[] = $control['name'];
1068 }
1069 }
1070
1071 return $controls;
1072 }
1073
1074 /**
1075 * Get controls pointer index.
1076 *
1077 * Retrieve pointer index where the next control should be added.
1078 *
1079 * While using injection point, it will return the injection point index.
1080 * Otherwise index of the last control plus one.
1081 *
1082 * @since 1.9.2
1083 * @access public
1084 *
1085 * @return int Controls pointer index.
1086 */
1087 public function get_pointer_index() {
1088 if ( null !== $this->injection_point ) {
1089 return $this->injection_point['index'];
1090 }
1091
1092 return count( $this->get_controls() );
1093 }
1094
1095 /**
1096 * Get the raw data.
1097 *
1098 * Retrieve all the items or, when requested, a specific item.
1099 *
1100 * @since 1.4.0
1101 * @access public
1102 *
1103 * @param string $item Optional. The requested item. Default is null.
1104 *
1105 * @return mixed The raw data.
1106 */
1107 public function get_data( $item = null ) {
1108 if ( ! $this->settings_sanitized && ( ! $item || 'settings' === $item ) ) {
1109 $this->data['settings'] = $this->sanitize_settings( $this->data['settings'] );
1110
1111 $this->settings_sanitized = true;
1112 }
1113
1114 return self::get_items( $this->data, $item );
1115 }
1116
1117 /**
1118 * @since 2.0.14
1119 * @access public
1120 */
1121 public function get_parsed_dynamic_settings( $setting = null, $settings = null ) {
1122 if ( null === $settings ) {
1123 $settings = $this->get_settings();
1124 }
1125
1126 if ( null === $this->parsed_dynamic_settings ) {
1127 $this->parsed_dynamic_settings = $this->parse_dynamic_settings( $settings );
1128 }
1129
1130 return self::get_items( $this->parsed_dynamic_settings, $setting );
1131 }
1132
1133 /**
1134 * Get active settings.
1135 *
1136 * Retrieve the settings from all the active controls.
1137 *
1138 * @since 1.4.0
1139 * @since 2.1.0 Added the `controls` and the `settings` parameters.
1140 * @access public
1141 *
1142 * @param array $controls Optional. An array of controls. Default is null.
1143 * @param array $settings Optional. Controls settings. Default is null.
1144 *
1145 * @return array Active settings.
1146 */
1147 public function get_active_settings( $settings = null, $controls = null ) {
1148 $is_first_request = ! $settings && ! $this->active_settings;
1149
1150 if ( ! $settings ) {
1151 if ( $this->active_settings ) {
1152 return $this->active_settings;
1153 }
1154
1155 $settings = $this->get_controls_settings();
1156
1157 $controls = $this->get_controls();
1158 }
1159
1160 $active_settings = [];
1161
1162 foreach ( $settings as $setting_key => $setting ) {
1163 if ( ! isset( $controls[ $setting_key ] ) ) {
1164 $active_settings[ $setting_key ] = $setting;
1165
1166 continue;
1167 }
1168
1169 $control = $controls[ $setting_key ];
1170
1171 if ( $this->is_control_visible( $control, $settings ) ) {
1172 $control_obj = Plugin::$instance->controls_manager->get_control( $control['type'] );
1173
1174 if ( $control_obj instanceof Control_Repeater ) {
1175 foreach ( $setting as & $item ) {
1176 $item = $this->get_active_settings( $item, $control['fields'] );
1177 }
1178 }
1179
1180 $active_settings[ $setting_key ] = $setting;
1181 } else {
1182 $active_settings[ $setting_key ] = null;
1183 }
1184 }
1185
1186 if ( $is_first_request ) {
1187 $this->active_settings = $active_settings;
1188 }
1189
1190 return $active_settings;
1191 }
1192
1193 /**
1194 * Get settings for display.
1195 *
1196 * Retrieve all the settings or, when requested, a specific setting for display.
1197 *
1198 * Unlike `get_settings()` method, this method retrieves only active settings
1199 * that passed all the conditions, rendered all the shortcodes and all the dynamic
1200 * tags.
1201 *
1202 * @since 2.0.0
1203 * @access public
1204 *
1205 * @param string $setting_key Optional. The key of the requested setting.
1206 * Default is null.
1207 *
1208 * @return mixed The settings.
1209 */
1210 public function get_settings_for_display( $setting_key = null ) {
1211 if ( ! $this->parsed_active_settings ) {
1212 $this->parsed_active_settings = $this->get_active_settings( $this->get_parsed_dynamic_settings(), $this->get_controls() );
1213 }
1214
1215 return self::get_items( $this->parsed_active_settings, $setting_key );
1216 }
1217
1218 /**
1219 * Parse dynamic settings.
1220 *
1221 * Retrieve the settings with rendered dynamic tags.
1222 *
1223 * @since 2.0.0
1224 * @access public
1225 *
1226 * @param array $settings Optional. The requested setting. Default is null.
1227 * @param array $controls Optional. The controls array. Default is null.
1228 * @param array $all_settings Optional. All the settings. Default is null.
1229 *
1230 * @return array The settings with rendered dynamic tags.
1231 */
1232 public function parse_dynamic_settings( $settings, $controls = null, $all_settings = null ) {
1233 if ( null === $all_settings ) {
1234 $all_settings = $this->get_settings();
1235 }
1236
1237 if ( null === $controls ) {
1238 $controls = $this->get_controls();
1239 }
1240
1241 foreach ( $controls as $control ) {
1242 $control_name = $control['name'];
1243 $control_obj = Plugin::$instance->controls_manager->get_control( $control['type'] );
1244
1245 if ( ! $control_obj instanceof Base_Data_Control ) {
1246 continue;
1247 }
1248
1249 if ( $control_obj instanceof Control_Repeater ) {
1250 if ( ! isset( $settings[ $control_name ] ) ) {
1251 continue;
1252 }
1253
1254 foreach ( $settings[ $control_name ] as & $field ) {
1255 $field = $this->parse_dynamic_settings( $field, $control['fields'], $field );
1256 }
1257
1258 continue;
1259 }
1260
1261 $dynamic_settings = $control_obj->get_settings( 'dynamic' );
1262
1263 if ( ! $dynamic_settings ) {
1264 $dynamic_settings = [];
1265 }
1266
1267 if ( ! empty( $control['dynamic'] ) ) {
1268 $dynamic_settings = array_merge( $dynamic_settings, $control['dynamic'] );
1269 }
1270
1271 if ( empty( $dynamic_settings ) || ! isset( $all_settings[ Manager::DYNAMIC_SETTING_KEY ][ $control_name ] ) ) {
1272 continue;
1273 }
1274
1275 if ( ! empty( $dynamic_settings['active'] ) && ! empty( $all_settings[ Manager::DYNAMIC_SETTING_KEY ][ $control_name ] ) ) {
1276 $parsed_value = $control_obj->parse_tags( $all_settings[ Manager::DYNAMIC_SETTING_KEY ][ $control_name ], $dynamic_settings );
1277
1278 $dynamic_property = ! empty( $dynamic_settings['property'] ) ? $dynamic_settings['property'] : null;
1279
1280 if ( $dynamic_property ) {
1281 $settings[ $control_name ][ $dynamic_property ] = $parsed_value;
1282 } else {
1283 $settings[ $control_name ] = $parsed_value;
1284 }
1285 }
1286 }
1287
1288 return $settings;
1289 }
1290
1291 /**
1292 * Get frontend settings.
1293 *
1294 * Retrieve the settings for all frontend controls.
1295 *
1296 * @since 1.6.0
1297 * @access public
1298 *
1299 * @return array Frontend settings.
1300 */
1301 public function get_frontend_settings() {
1302 $frontend_settings = array_intersect_key( $this->get_settings_for_display(), array_flip( $this->get_frontend_settings_keys() ) );
1303
1304 foreach ( $frontend_settings as $key => $setting ) {
1305 if ( in_array( $setting, [ null, '' ], true ) ) {
1306 unset( $frontend_settings[ $key ] );
1307 }
1308 }
1309
1310 return $frontend_settings;
1311 }
1312
1313 /**
1314 * Filter controls settings.
1315 *
1316 * Receives controls, settings and a callback function to filter the settings by
1317 * and returns filtered settings.
1318 *
1319 * @since 1.5.0
1320 * @access public
1321 *
1322 * @param callable $callback The callback function.
1323 * @param array $settings Optional. Control settings. Default is an empty
1324 * array.
1325 * @param array $controls Optional. Controls list. Default is an empty
1326 * array.
1327 *
1328 * @return array Filtered settings.
1329 */
1330 public function filter_controls_settings( callable $callback, array $settings = [], array $controls = [] ) {
1331 if ( ! $settings ) {
1332 $settings = $this->get_settings();
1333 }
1334
1335 if ( ! $controls ) {
1336 $controls = $this->get_controls();
1337 }
1338
1339 return array_reduce(
1340 array_keys( $settings ), function( $filtered_settings, $setting_key ) use ( $controls, $settings, $callback ) {
1341 if ( isset( $controls[ $setting_key ] ) ) {
1342 $result = $callback( $settings[ $setting_key ], $controls[ $setting_key ] );
1343
1344 if ( null !== $result ) {
1345 $filtered_settings[ $setting_key ] = $result;
1346 }
1347 }
1348
1349 return $filtered_settings;
1350 }, []
1351 );
1352 }
1353
1354 /**
1355 * Get Responsive Control Device Suffix
1356 *
1357 * @deprecated 3.7.6
1358 * @param array $control
1359 * @return string $device suffix
1360 */
1361 protected function get_responsive_control_device_suffix( $control ) {
1362 Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( __METHOD__, '3.7.6', 'Elementor\Controls_Manager::get_responsive_control_device_suffix()' );
1363
1364 return Controls_Manager::get_responsive_control_device_suffix( $control );
1365 }
1366
1367 /**
1368 * Whether the control is visible or not.
1369 *
1370 * Used to determine whether the control is visible or not.
1371 *
1372 * @since 1.4.0
1373 * @access public
1374 *
1375 * @param array $control The control.
1376 * @param array $values Optional. Condition values. Default is null.
1377 *
1378 * @return bool Whether the control is visible.
1379 */
1380 public function is_control_visible( $control, $values = null ) {
1381 if ( null === $values ) {
1382 $values = $this->get_settings();
1383 }
1384
1385 if ( ! empty( $control['conditions'] ) && ! Conditions::check( $control['conditions'], $values ) ) {
1386 return false;
1387 }
1388
1389 if ( empty( $control['condition'] ) ) {
1390 return true;
1391 }
1392
1393 $controls = $this->get_controls();
1394
1395 foreach ( $control['condition'] as $condition_key => $condition_value ) {
1396 preg_match( '/([a-z_\-0-9]+)(?:\[([a-z_]+)])?(!?)$/i', $condition_key, $condition_key_parts );
1397
1398 $pure_condition_key = $condition_key_parts[1];
1399 $condition_sub_key = $condition_key_parts[2];
1400 $is_negative_condition = ! ! $condition_key_parts[3];
1401
1402 if ( ! isset( $values[ $pure_condition_key ] ) || null === $values[ $pure_condition_key ] ) {
1403 return false;
1404 }
1405
1406 $are_control_and_condition_responsive = isset( $control['responsive'] ) && ! empty( $controls[ $pure_condition_key ]['responsive'] );
1407 $condition_name_to_check = $pure_condition_key;
1408
1409 if ( $are_control_and_condition_responsive ) {
1410 $device_suffix = Controls_Manager::get_responsive_control_device_suffix( $control );
1411
1412 $condition_name_to_check = $pure_condition_key . $device_suffix;
1413
1414 // If the control is not desktop, and a conditioning control for the corresponding device exists, use it.
1415 $instance_value = $values[ $pure_condition_key . $device_suffix ] ?? $values[ $pure_condition_key ];
1416 } else {
1417 $instance_value = $values[ $pure_condition_key ];
1418 }
1419
1420 if ( $condition_sub_key && is_array( $instance_value ) ) {
1421 if ( ! isset( $instance_value[ $condition_sub_key ] ) ) {
1422 return false;
1423 }
1424
1425 $instance_value = $instance_value[ $condition_sub_key ];
1426 }
1427
1428 if ( ! $instance_value ) {
1429 $parent = isset( $controls[ $condition_name_to_check ]['parent'] ) ? $controls[ $condition_name_to_check ]['parent'] : false;
1430
1431 while ( $parent ) {
1432 $instance_value = $values[ $parent ];
1433
1434 if ( $instance_value ) {
1435 if ( ! is_array( $instance_value ) ) {
1436 break;
1437 }
1438
1439 if ( $condition_sub_key && isset( $instance_value[ $condition_sub_key ] ) ) {
1440 $instance_value = $instance_value[ $condition_sub_key ];
1441
1442 if ( '' !== $instance_value ) {
1443 break;
1444 }
1445 }
1446 }
1447
1448 $parent = isset( $controls[ $parent ]['parent'] ) ? $controls[ $parent ]['parent'] : false;
1449 }
1450 }
1451
1452 /**
1453 * If the $condition_value is a non empty array - check if the $condition_value contains the $instance_value,
1454 * If the $instance_value is a non empty array - check if the $instance_value contains the $condition_value
1455 * otherwise check if they are equal. ( and give the ability to check if the value is an empty array )
1456 */
1457 if ( is_array( $condition_value ) && ! empty( $condition_value ) ) {
1458 $is_contains = in_array( $instance_value, $condition_value, true );
1459 } elseif ( is_array( $instance_value ) && ! empty( $instance_value ) ) {
1460 $is_contains = in_array( $condition_value, $instance_value, true );
1461 } else {
1462 $is_contains = $instance_value === $condition_value;
1463 }
1464
1465 if ( $is_negative_condition && $is_contains || ! $is_negative_condition && ! $is_contains ) {
1466 return false;
1467 }
1468 }
1469
1470 return true;
1471 }
1472
1473 /**
1474 * Start controls section.
1475 *
1476 * Used to add a new section of controls. When you use this method, all the
1477 * registered controls from this point will be assigned to this section,
1478 * until you close the section using `end_controls_section()` method.
1479 *
1480 * This method should be used inside `register_controls()`.
1481 *
1482 * @since 1.4.0
1483 * @access public
1484 *
1485 * @param string $section_id Section ID.
1486 * @param array $args Section arguments Optional.
1487 */
1488 public function start_controls_section( $section_id, array $args = [] ) {
1489 $stack_name = $this->get_name();
1490
1491 /**
1492 * Before section start.
1493 *
1494 * Fires before Elementor section starts in the editor panel.
1495 *
1496 * @since 1.4.0
1497 *
1498 * @param Controls_Stack $this The control.
1499 * @param string $section_id Section ID.
1500 * @param array $args Section arguments.
1501 */
1502 do_action( 'elementor/element/before_section_start', $this, $section_id, $args );
1503
1504 /**
1505 * Before section start.
1506 *
1507 * Fires before Elementor section starts in the editor panel.
1508 *
1509 * The dynamic portions of the hook name, `$stack_name` and `$section_id`, refers to the stack name and section ID, respectively.
1510 *
1511 * @since 1.4.0
1512 *
1513 * @param Controls_Stack $this The control.
1514 * @param array $args Section arguments.
1515 */
1516 do_action( "elementor/element/{$stack_name}/{$section_id}/before_section_start", $this, $args );
1517
1518 $args['type'] = Controls_Manager::SECTION;
1519
1520 $this->add_control( $section_id, $args );
1521
1522 if ( null !== $this->current_section ) {
1523 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
1524 }
1525
1526 $this->current_section = $this->get_section_args( $section_id );
1527
1528 if ( $this->injection_point ) {
1529 $this->injection_point['section'] = $this->current_section;
1530 }
1531
1532 /**
1533 * After section start.
1534 *
1535 * Fires after Elementor section starts in the editor panel.
1536 *
1537 * @since 1.4.0
1538 *
1539 * @param Controls_Stack $this The control.
1540 * @param string $section_id Section ID.
1541 * @param array $args Section arguments.
1542 */
1543 do_action( 'elementor/element/after_section_start', $this, $section_id, $args );
1544
1545 /**
1546 * After section start.
1547 *
1548 * Fires after Elementor section starts in the editor panel.
1549 *
1550 * The dynamic portions of the hook name, `$stack_name` and `$section_id`, refers to the stack name and section ID, respectively.
1551 *
1552 * @since 1.4.0
1553 *
1554 * @param Controls_Stack $this The control.
1555 * @param array $args Section arguments.
1556 */
1557 do_action( "elementor/element/{$stack_name}/{$section_id}/after_section_start", $this, $args );
1558 }
1559
1560 /**
1561 * End controls section.
1562 *
1563 * Used to close an existing open controls section. When you use this method
1564 * it stops adding new controls to this section.
1565 *
1566 * This method should be used inside `register_controls()`.
1567 *
1568 * @since 1.4.0
1569 * @access public
1570 */
1571 public function end_controls_section() {
1572 $stack_name = $this->get_name();
1573
1574 // Save the current section for the action.
1575 $current_section = $this->current_section;
1576 $section_id = $current_section['section'];
1577 $args = [
1578 'tab' => $current_section['tab'],
1579 ];
1580
1581 /**
1582 * Before section end.
1583 *
1584 * Fires before Elementor section ends in the editor panel.
1585 *
1586 * @since 1.4.0
1587 *
1588 * @param Controls_Stack $this The control.
1589 * @param string $section_id Section ID.
1590 * @param array $args Section arguments.
1591 */
1592 do_action( 'elementor/element/before_section_end', $this, $section_id, $args );
1593
1594 /**
1595 * Before section end.
1596 *
1597 * Fires before Elementor section ends in the editor panel.
1598 *
1599 * The dynamic portions of the hook name, `$stack_name` and `$section_id`, refers to the stack name and section ID, respectively.
1600 *
1601 * @since 1.4.0
1602 *
1603 * @param Controls_Stack $this The control.
1604 * @param array $args Section arguments.
1605 */
1606 do_action( "elementor/element/{$stack_name}/{$section_id}/before_section_end", $this, $args );
1607
1608 $this->current_section = null;
1609
1610 /**
1611 * After section end.
1612 *
1613 * Fires after Elementor section ends in the editor panel.
1614 *
1615 * @since 1.4.0
1616 *
1617 * @param Controls_Stack $this The control.
1618 * @param string $section_id Section ID.
1619 * @param array $args Section arguments.
1620 */
1621 do_action( 'elementor/element/after_section_end', $this, $section_id, $args );
1622
1623 /**
1624 * After section end.
1625 *
1626 * Fires after Elementor section ends in the editor panel.
1627 *
1628 * The dynamic portions of the hook name, `$stack_name` and `$section_id`, refers to the stack name and section ID, respectively.
1629 *
1630 * @since 1.4.0
1631 *
1632 * @param Controls_Stack $this The control.
1633 * @param array $args Section arguments.
1634 */
1635 do_action( "elementor/element/{$stack_name}/{$section_id}/after_section_end", $this, $args );
1636 }
1637
1638 /**
1639 * Start controls tabs.
1640 *
1641 * Used to add a new set of tabs inside a section. You should use this
1642 * method before adding new individual tabs using `start_controls_tab()`.
1643 * Each tab added after this point will be assigned to this group of tabs,
1644 * until you close it using `end_controls_tabs()` method.
1645 *
1646 * This method should be used inside `register_controls()`.
1647 *
1648 * @since 1.4.0
1649 * @access public
1650 *
1651 * @param string $tabs_id Tabs ID.
1652 * @param array $args Tabs arguments.
1653 */
1654 public function start_controls_tabs( $tabs_id, array $args = [] ) {
1655 if ( null !== $this->current_tab ) {
1656 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
1657 }
1658
1659 $args['type'] = Controls_Manager::TABS;
1660
1661 $this->add_control( $tabs_id, $args );
1662
1663 $this->current_tab = [
1664 'tabs_wrapper' => $tabs_id,
1665 ];
1666
1667 foreach ( [ 'condition', 'conditions' ] as $key ) {
1668 if ( ! empty( $args[ $key ] ) ) {
1669 $this->current_tab[ $key ] = $args[ $key ];
1670 }
1671 }
1672
1673 if ( $this->injection_point ) {
1674 $this->injection_point['tab'] = $this->current_tab;
1675 }
1676 }
1677
1678 /**
1679 * End controls tabs.
1680 *
1681 * Used to close an existing open controls tabs. When you use this method it
1682 * stops adding new controls to this tabs.
1683 *
1684 * This method should be used inside `register_controls()`.
1685 *
1686 * @since 1.4.0
1687 * @access public
1688 */
1689 public function end_controls_tabs() {
1690 $this->current_tab = null;
1691 }
1692
1693 /**
1694 * Start controls tab.
1695 *
1696 * Used to add a new tab inside a group of tabs. Use this method before
1697 * adding new individual tabs using `start_controls_tab()`.
1698 * Each tab added after this point will be assigned to this group of tabs,
1699 * until you close it using `end_controls_tab()` method.
1700 *
1701 * This method should be used inside `register_controls()`.
1702 *
1703 * @since 1.4.0
1704 * @access public
1705 *
1706 * @param string $tab_id Tab ID.
1707 * @param array $args Tab arguments.
1708 */
1709 public function start_controls_tab( $tab_id, $args ) {
1710 if ( ! empty( $this->current_tab['inner_tab'] ) ) {
1711 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
1712 }
1713
1714 $args['type'] = Controls_Manager::TAB;
1715 $args['tabs_wrapper'] = $this->current_tab['tabs_wrapper'];
1716
1717 $this->add_control( $tab_id, $args );
1718
1719 $this->current_tab['inner_tab'] = $tab_id;
1720
1721 if ( $this->injection_point ) {
1722 $this->injection_point['tab']['inner_tab'] = $this->current_tab['inner_tab'];
1723 }
1724 }
1725
1726 /**
1727 * End controls tab.
1728 *
1729 * Used to close an existing open controls tab. When you use this method it
1730 * stops adding new controls to this tab.
1731 *
1732 * This method should be used inside `register_controls()`.
1733 *
1734 * @since 1.4.0
1735 * @access public
1736 */
1737 public function end_controls_tab() {
1738 unset( $this->current_tab['inner_tab'] );
1739 }
1740
1741 /**
1742 * Start popover.
1743 *
1744 * Used to add a new set of controls in a popover. When you use this method,
1745 * all the registered controls from this point will be assigned to this
1746 * popover, until you close the popover using `end_popover()` method.
1747 *
1748 * This method should be used inside `register_controls()`.
1749 *
1750 * @since 1.9.0
1751 * @access public
1752 */
1753 final public function start_popover() {
1754 $this->current_popover = [
1755 'initialized' => false,
1756 ];
1757 }
1758
1759 /**
1760 * End popover.
1761 *
1762 * Used to close an existing open popover. When you use this method it stops
1763 * adding new controls to this popover.
1764 *
1765 * This method should be used inside `register_controls()`.
1766 *
1767 * @since 1.9.0
1768 * @access public
1769 */
1770 final public function end_popover() {
1771 $this->current_popover = null;
1772
1773 $last_control_key = $this->get_control_key( $this->get_pointer_index() - 1 );
1774
1775 $args = [
1776 'popover' => [
1777 'end' => true,
1778 ],
1779 ];
1780
1781 $options = [
1782 'recursive' => true,
1783 ];
1784
1785 $this->update_control( $last_control_key, $args, $options );
1786 }
1787
1788 /**
1789 * Add render attribute.
1790 *
1791 * Used to add attributes to a specific HTML element.
1792 *
1793 * The HTML tag is represented by the element parameter, then you need to
1794 * define the attribute key and the attribute key. The final result will be:
1795 * `<element attribute_key="attribute_value">`.
1796 *
1797 * Example usage:
1798 *
1799 * `$this->add_render_attribute( 'wrapper', 'class', 'custom-widget-wrapper-class' );`
1800 * `$this->add_render_attribute( 'widget', 'id', 'custom-widget-id' );`
1801 * `$this->add_render_attribute( 'button', [ 'class' => 'custom-button-class', 'id' => 'custom-button-id' ] );`
1802 *
1803 * @since 1.0.0
1804 * @access public
1805 *
1806 * @param array|string $element The HTML element.
1807 * @param array|string $key Optional. Attribute key. Default is null.
1808 * @param array|string $value Optional. Attribute value. Default is null.
1809 * @param bool $overwrite Optional. Whether to overwrite existing
1810 * attribute. Default is false, not to overwrite.
1811 *
1812 * @return self Current instance of the element.
1813 */
1814 public function add_render_attribute( $element, $key = null, $value = null, $overwrite = false ) {
1815 if ( is_array( $element ) ) {
1816 foreach ( $element as $element_key => $attributes ) {
1817 $this->add_render_attribute( $element_key, $attributes, null, $overwrite );
1818 }
1819
1820 return $this;
1821 }
1822
1823 if ( is_array( $key ) ) {
1824 foreach ( $key as $attribute_key => $attributes ) {
1825 $this->add_render_attribute( $element, $attribute_key, $attributes, $overwrite );
1826 }
1827
1828 return $this;
1829 }
1830
1831 if ( empty( $this->render_attributes[ $element ][ $key ] ) ) {
1832 $this->render_attributes[ $element ][ $key ] = [];
1833 }
1834
1835 settype( $value, 'array' );
1836
1837 if ( $overwrite ) {
1838 $this->render_attributes[ $element ][ $key ] = $value;
1839 } else {
1840 $this->render_attributes[ $element ][ $key ] = array_merge( $this->render_attributes[ $element ][ $key ], $value );
1841 }
1842
1843 return $this;
1844 }
1845
1846 /**
1847 * Get Render Attributes
1848 *
1849 * Used to retrieve render attribute.
1850 *
1851 * The returned array is either all elements and their attributes if no `$element` is specified, an array of all
1852 * attributes of a specific element or a specific attribute properties if `$key` is specified.
1853 *
1854 * Returns null if one of the requested parameters isn't set.
1855 *
1856 * @since 2.2.6
1857 * @access public
1858 * @param string $element
1859 * @param string $key
1860 *
1861 * @return array
1862 */
1863 public function get_render_attributes( $element = '', $key = '' ) {
1864 $attributes = $this->render_attributes;
1865
1866 if ( $element ) {
1867 if ( ! isset( $attributes[ $element ] ) ) {
1868 return null;
1869 }
1870
1871 $attributes = $attributes[ $element ];
1872
1873 if ( $key ) {
1874 if ( ! isset( $attributes[ $key ] ) ) {
1875 return null;
1876 }
1877
1878 $attributes = $attributes[ $key ];
1879 }
1880 }
1881
1882 return $attributes;
1883 }
1884
1885 /**
1886 * Set render attribute.
1887 *
1888 * Used to set the value of the HTML element render attribute or to update
1889 * an existing render attribute.
1890 *
1891 * @since 1.0.0
1892 * @access public
1893 *
1894 * @param array|string $element The HTML element.
1895 * @param array|string $key Optional. Attribute key. Default is null.
1896 * @param array|string $value Optional. Attribute value. Default is null.
1897 *
1898 * @return self Current instance of the element.
1899 */
1900 public function set_render_attribute( $element, $key = null, $value = null ) {
1901 return $this->add_render_attribute( $element, $key, $value, true );
1902 }
1903
1904 /**
1905 * Remove render attribute.
1906 *
1907 * Used to remove an element (with its keys and their values), key (with its values),
1908 * or value/s from an HTML element's render attribute.
1909 *
1910 * @since 2.7.0
1911 * @access public
1912 *
1913 * @param string $element The HTML element.
1914 * @param string $key Optional. Attribute key. Default is null.
1915 * @param array|string $values Optional. Attribute value/s. Default is null.
1916 */
1917 public function remove_render_attribute( $element, $key = null, $values = null ) {
1918 if ( $key && ! isset( $this->render_attributes[ $element ][ $key ] ) ) {
1919 return;
1920 }
1921
1922 if ( $values ) {
1923 $values = (array) $values;
1924
1925 $this->render_attributes[ $element ][ $key ] = array_diff( $this->render_attributes[ $element ][ $key ], $values );
1926
1927 return;
1928 }
1929
1930 if ( $key ) {
1931 unset( $this->render_attributes[ $element ][ $key ] );
1932
1933 return;
1934 }
1935
1936 if ( isset( $this->render_attributes[ $element ] ) ) {
1937 unset( $this->render_attributes[ $element ] );
1938 }
1939 }
1940
1941 /**
1942 * Get render attribute string.
1943 *
1944 * Used to retrieve the value of the render attribute.
1945 *
1946 * @since 1.0.0
1947 * @access public
1948 *
1949 * @param string $element The element.
1950 *
1951 * @return string Render attribute string, or an empty string if the attribute
1952 * is empty or not exist.
1953 */
1954 public function get_render_attribute_string( $element ) {
1955 if ( empty( $this->render_attributes[ $element ] ) ) {
1956 return '';
1957 }
1958
1959 return Utils::render_html_attributes( $this->render_attributes[ $element ] );
1960 }
1961
1962 /**
1963 * Print render attribute string.
1964 *
1965 * Used to output the rendered attribute.
1966 *
1967 * @since 2.0.0
1968 * @access public
1969 *
1970 * @param array|string $element The element.
1971 */
1972 public function print_render_attribute_string( $element ) {
1973 echo $this->get_render_attribute_string( $element ); // XSS ok.
1974 }
1975
1976 /**
1977 * Print element template.
1978 *
1979 * Used to generate the element template on the editor.
1980 *
1981 * @since 2.0.0
1982 * @access public
1983 */
1984 public function print_template() {
1985 ob_start();
1986
1987 // TODO: This is for backwards compatibility starting from 2.9.0
1988 // This `if` statement should be removed when the method is removed
1989 if ( $this->has_own_method( '_content_template', self::class ) ) {
1990 Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( '_content_template', '2.9.0', __CLASS__ . '::content_template()' );
1991
1992 $this->_content_template();
1993 } else {
1994 $this->content_template();
1995 }
1996
1997 $template_content = ob_get_clean();
1998
1999 $element_type = $this->get_type();
2000
2001 /**
2002 * Template content.
2003 *
2004 * Filters the controls stack template content before it's printed in the editor.
2005 *
2006 * The dynamic portion of the hook name, `$element_type`, refers to the element type.
2007 *
2008 * @since 1.0.0
2009 *
2010 * @param string $content_template The controls stack template in the editor.
2011 * @param Controls_Stack $this The controls stack.
2012 */
2013 $template_content = apply_filters( "elementor/{$element_type}/print_template", $template_content, $this );
2014
2015 if ( empty( $template_content ) ) {
2016 return;
2017 }
2018 ?>
2019 <script type="text/html" id="tmpl-elementor-<?php echo esc_attr( $this->get_name() ); ?>-content">
2020 <?php $this->print_template_content( $template_content ); ?>
2021 </script>
2022 <?php
2023 }
2024
2025 /**
2026 * On import update dynamic content (e.g. post and term IDs).
2027 *
2028 * @since 3.8.0
2029 *
2030 * @param array $config The config of the passed element.
2031 * @param array $data The data that requires updating/replacement when imported.
2032 * @param array|null $controls The available controls.
2033 *
2034 * @return array Element data.
2035 */
2036 public static function on_import_update_dynamic_content( array $config, array $data, $controls = null ) : array {
2037 return $config;
2038 }
2039
2040 /**
2041 * Start injection.
2042 *
2043 * Used to inject controls and sections to a specific position in the stack.
2044 *
2045 * When you use this method, all the registered controls and sections will
2046 * be injected to a specific position in the stack, until you stop the
2047 * injection using `end_injection()` method.
2048 *
2049 * @since 1.7.1
2050 * @access public
2051 *
2052 * @param array $position {
2053 * The position where to start the injection.
2054 *
2055 * @type string $type Injection type, either `control` or `section`.
2056 * Default is `control`.
2057 * @type string $at Where to inject. If `$type` is `control` accepts
2058 * `before` and `after`. If `$type` is `section`
2059 * accepts `start` and `end`. Default values based on
2060 * the `type`.
2061 * @type string $of Control/Section ID.
2062 * }
2063 */
2064 final public function start_injection( array $position ) {
2065 if ( $this->injection_point ) {
2066 wp_die( 'A controls injection is already opened. Please close current injection before starting a new one (use `end_injection`).' );
2067 }
2068
2069 $this->injection_point = $this->get_position_info( $position );
2070 }
2071
2072 /**
2073 * End injection.
2074 *
2075 * Used to close an existing opened injection point.
2076 *
2077 * When you use this method it stops adding new controls and sections to
2078 * this point and continue to add controls to the regular position in the
2079 * stack.
2080 *
2081 * @since 1.7.1
2082 * @access public
2083 */
2084 final public function end_injection() {
2085 $this->injection_point = null;
2086 }
2087
2088 /**
2089 * Get injection point.
2090 *
2091 * Retrieve the injection point in the stack where new controls and sections
2092 * will be inserted.
2093 *
2094 * @since 1.9.2
2095 * @access public
2096 *
2097 * @return array|null An array when an injection point is defined, null
2098 * otherwise.
2099 */
2100 final public function get_injection_point() {
2101 return $this->injection_point;
2102 }
2103
2104 /**
2105 * Register controls.
2106 *
2107 * Used to add new controls to any element type. For example, external
2108 * developers use this method to register controls in a widget.
2109 *
2110 * Should be inherited and register new controls using `add_control()`,
2111 * `add_responsive_control()` and `add_group_control()`, inside control
2112 * wrappers like `start_controls_section()`, `start_controls_tabs()` and
2113 * `start_controls_tab()`.
2114 *
2115 * @since 1.4.0
2116 * @access protected
2117 * @deprecated 3.1.0 Use `Controls_Stack::register_controls()` instead
2118 */
2119 protected function _register_controls() {
2120 Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( __METHOD__, '3.1.0', __CLASS__ . '::register_controls()' );
2121
2122 $this->register_controls();
2123 }
2124
2125 /**
2126 * Register controls.
2127 *
2128 * Used to add new controls to any element type. For example, external
2129 * developers use this method to register controls in a widget.
2130 *
2131 * Should be inherited and register new controls using `add_control()`,
2132 * `add_responsive_control()` and `add_group_control()`, inside control
2133 * wrappers like `start_controls_section()`, `start_controls_tabs()` and
2134 * `start_controls_tab()`.
2135 *
2136 * @since 3.1.0
2137 * @access protected
2138 */
2139 protected function register_controls() {}
2140
2141 /**
2142 * Get default data.
2143 *
2144 * Retrieve the default data. Used to reset the data on initialization.
2145 *
2146 * @since 1.4.0
2147 * @access protected
2148 *
2149 * @return array Default data.
2150 */
2151 protected function get_default_data() {
2152 return [
2153 'id' => 0,
2154 'settings' => [],
2155 ];
2156 }
2157
2158 /**
2159 * @since 2.3.0
2160 * @access protected
2161 */
2162 protected function get_init_settings() {
2163 $settings = $this->get_data( 'settings' );
2164
2165 foreach ( $this->get_controls() as $control ) {
2166 $control_obj = Plugin::$instance->controls_manager->get_control( $control['type'] );
2167
2168 if ( ! $control_obj instanceof Base_Data_Control ) {
2169 continue;
2170 }
2171
2172 $control = array_merge_recursive( $control_obj->get_settings(), $control );
2173
2174 $settings[ $control['name'] ] = $control_obj->get_value( $control, $settings );
2175 }
2176
2177 return $settings;
2178 }
2179
2180 /**
2181 * Get initial config.
2182 *
2183 * Retrieve the current element initial configuration - controls list and
2184 * the tabs assigned to the control.
2185 *
2186 * @since 2.9.0
2187 * @access protected
2188 *
2189 * @return array The initial config.
2190 */
2191 protected function get_initial_config() {
2192 return [
2193 'controls' => $this->get_controls(),
2194 ];
2195 }
2196
2197 /**
2198 * Get initial config.
2199 *
2200 * Retrieve the current element initial configuration - controls list and
2201 * the tabs assigned to the control.
2202 *
2203 * @since 1.4.0
2204 * @deprecated 2.9.0 use `get_initial_config()` instead
2205 * @access protected
2206 *
2207 * @return array The initial config.
2208 */
2209 protected function _get_initial_config() {
2210 Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( __METHOD__, '2.9.0', __CLASS__ . '::get_initial_config()' );
2211
2212 return $this->get_initial_config();
2213 }
2214
2215 /**
2216 * Get section arguments.
2217 *
2218 * Retrieve the section arguments based on section ID.
2219 *
2220 * @since 1.4.0
2221 * @access protected
2222 *
2223 * @param string $section_id Section ID.
2224 *
2225 * @return array Section arguments.
2226 */
2227 protected function get_section_args( $section_id ) {
2228 $section_control = $this->get_controls( $section_id );
2229
2230 $section_args_keys = [ 'tab', 'condition' ];
2231
2232 $args = array_intersect_key( $section_control, array_flip( $section_args_keys ) );
2233
2234 $args['section'] = $section_id;
2235
2236 return $args;
2237 }
2238
2239 /**
2240 * Render element.
2241 *
2242 * Generates the final HTML on the frontend.
2243 *
2244 * @since 2.0.0
2245 * @access protected
2246 */
2247 protected function render() {}
2248
2249 /**
2250 * Render element in static mode.
2251 *
2252 * If not inherent will call the base render.
2253 */
2254 protected function render_static() {
2255 $this->render();
2256 }
2257
2258 /**
2259 * Determine the render logic.
2260 */
2261 protected function render_by_mode() {
2262 if ( Plugin::$instance->frontend->is_static_render_mode() ) {
2263 $this->render_static();
2264
2265 return;
2266 }
2267
2268 $this->render();
2269 }
2270
2271 /**
2272 * Print content template.
2273 *
2274 * Used to generate the content template on the editor, using a
2275 * Backbone JavaScript template.
2276 *
2277 * @access protected
2278 * @since 2.0.0
2279 *
2280 * @param string $template_content Template content.
2281 */
2282 protected function print_template_content( $template_content ) {
2283 echo $template_content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
2284 }
2285
2286 /**
2287 * Render element output in the editor.
2288 *
2289 * Used to generate the live preview, using a Backbone JavaScript template.
2290 *
2291 * @since 2.9.0
2292 * @access protected
2293 */
2294 protected function content_template() {}
2295
2296 /**
2297 * Render element output in the editor.
2298 *
2299 * Used to generate the live preview, using a Backbone JavaScript template.
2300 *
2301 * @since 2.0.0
2302 * @deprecated 2.9.0 use `content_template()` instead
2303 * @access protected
2304 */
2305 protected function _content_template() {
2306 Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( __METHOD__, '2.9.0', __CLASS__ . '::content_template()' );
2307
2308 $this->content_template();
2309 }
2310
2311 /**
2312 * Initialize controls.
2313 *
2314 * Register the all controls added by `register_controls()`.
2315 *
2316 * @since 2.0.0
2317 * @access protected
2318 */
2319 protected function init_controls() {
2320 Plugin::$instance->controls_manager->open_stack( $this );
2321
2322 // TODO: This is for backwards compatibility starting from 2.9.0
2323 // This `if` statement should be removed when the method is removed
2324 if ( $this->has_own_method( '_register_controls', self::class ) ) {
2325 Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( '_register_controls', '3.1.0', __CLASS__ . '::register_controls()' );
2326
2327 $this->_register_controls();
2328 } else {
2329 $this->register_controls();
2330 }
2331 }
2332
2333 protected function handle_control_position( array $args, $control_id, $overwrite ) {
2334 if ( isset( $args['type'] ) && in_array( $args['type'], [ Controls_Manager::SECTION, Controls_Manager::WP_WIDGET ], true ) ) {
2335 return $args;
2336 }
2337
2338 $target_section_args = $this->current_section;
2339
2340 $target_tab = $this->current_tab;
2341
2342 if ( $this->injection_point ) {
2343 $target_section_args = $this->injection_point['section'];
2344
2345 if ( ! empty( $this->injection_point['tab'] ) ) {
2346 $target_tab = $this->injection_point['tab'];
2347 }
2348 }
2349
2350 if ( null !== $target_section_args ) {
2351 if ( ! empty( $args['section'] ) || ! empty( $args['tab'] ) ) {
2352 _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
2353 }
2354
2355 $args = array_replace_recursive( $target_section_args, $args );
2356
2357 if ( null !== $target_tab ) {
2358 $args = array_replace_recursive( $target_tab, $args );
2359 }
2360 } elseif ( empty( $args['section'] ) && ( ! $overwrite || is_wp_error( Plugin::$instance->controls_manager->get_control_from_stack( $this->get_unique_name(), $control_id ) ) ) ) {
2361 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
2362 }
2363
2364 return $args;
2365 }
2366
2367 /**
2368 * Initialize the class.
2369 *
2370 * Set the raw data, the ID and the parsed settings.
2371 *
2372 * @since 2.9.0
2373 * @access protected
2374 *
2375 * @param array $data Initial data.
2376 */
2377 protected function init( $data ) {
2378 $this->data = array_merge( $this->get_default_data(), $data );
2379
2380 $this->id = $data['id'];
2381 }
2382
2383 /**
2384 * Initialize the class.
2385 *
2386 * Set the raw data, the ID and the parsed settings.
2387 *
2388 * @since 1.4.0
2389 * @deprecated 2.9.0 use `init()` instead
2390 * @access protected
2391 *
2392 * @param array $data Initial data.
2393 */
2394 protected function _init( $data ) {
2395 Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( __METHOD__, '2.9.0', __CLASS__ . '::init()' );
2396
2397 $this->init( $data );
2398 }
2399
2400 /**
2401 * Sanitize initial data.
2402 *
2403 * Performs settings cleaning and sanitization.
2404 *
2405 * @since 2.1.5
2406 * @access private
2407 *
2408 * @param array $settings Settings to sanitize.
2409 * @param array $controls Optional. An array of controls. Default is an
2410 * empty array.
2411 *
2412 * @return array Sanitized settings.
2413 */
2414 private function sanitize_settings( array $settings, array $controls = [] ) {
2415 if ( ! $controls ) {
2416 $controls = $this->get_controls();
2417 }
2418
2419 foreach ( $controls as $control ) {
2420 $control_obj = Plugin::$instance->controls_manager->get_control( $control['type'] );
2421
2422 if ( $control_obj instanceof Control_Repeater ) {
2423 if ( empty( $settings[ $control['name'] ] ) ) {
2424 continue;
2425 }
2426
2427 foreach ( $settings[ $control['name'] ] as $index => $repeater_row_data ) {
2428 $sanitized_row_data = $this->sanitize_settings( $repeater_row_data, $control['fields'] );
2429
2430 $settings[ $control['name'] ][ $index ] = $sanitized_row_data;
2431 }
2432
2433 continue;
2434 }
2435
2436 $is_dynamic = isset( $settings[ Manager::DYNAMIC_SETTING_KEY ][ $control['name'] ] );
2437
2438 if ( ! $is_dynamic ) {
2439 continue;
2440 }
2441
2442 $value_to_check = $settings[ Manager::DYNAMIC_SETTING_KEY ][ $control['name'] ];
2443
2444 $tag_text_data = Plugin::$instance->dynamic_tags->tag_text_to_tag_data( $value_to_check );
2445
2446 if ( ! Plugin::$instance->dynamic_tags->get_tag_info( $tag_text_data['name'] ) ) {
2447 unset( $settings[ Manager::DYNAMIC_SETTING_KEY ][ $control['name'] ] );
2448 }
2449 }
2450
2451 return $settings;
2452 }
2453
2454 /**
2455 * Controls stack constructor.
2456 *
2457 * Initializing the control stack class using `$data`. The `$data` is required
2458 * for a normal instance. It is optional only for internal `type instance`.
2459 *
2460 * @since 1.4.0
2461 * @access public
2462 *
2463 * @param array $data Optional. Control stack data. Default is an empty array.
2464 */
2465 public function __construct( array $data = [] ) {
2466 if ( $data ) {
2467 // TODO: This is for backwards compatibility starting from 2.9.0
2468 // This if statement should be removed when the method is hard-deprecated
2469 if ( $this->has_own_method( '_init', self::class ) ) {
2470 Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( '_init', '2.9.0', __CLASS__ . '::init()' );
2471
2472 $this->_init( $data );
2473 } else {
2474 $this->init( $data );
2475 }
2476 }
2477 }
2478 }
2479