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

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