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

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