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

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