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

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

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