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

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