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

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

2,403 lines 64.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor;
3
4 use Elementor\Core\Base\Base_Object;
5 use Elementor\Core\DynamicTags\Manager;
6 use Elementor\Core\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 = [
966 Breakpoints_Manager::BREAKPOINT_KEY_DESKTOP,
967 Breakpoints_Manager::BREAKPOINT_KEY_TABLET,
968 Breakpoints_Manager::BREAKPOINT_KEY_MOBILE,
969 ];
970
971 foreach ( $devices as $device_name ) {
972 $id_suffix = Breakpoints_Manager::BREAKPOINT_KEY_DESKTOP === $device_name ? '' : '_' . $device_name;
973
974 $this->remove_control( $id . $id_suffix );
975 }
976 }
977
978 /**
979 * Get class name.
980 *
981 * Retrieve the name of the current class.
982 *
983 * @since 1.4.0
984 * @access public
985 *
986 * @return string Class name.
987 */
988 final public function get_class_name() {
989 return get_called_class();
990 }
991
992 /**
993 * Get the config.
994 *
995 * Retrieve the config or, if non set, use the initial config.
996 *
997 * @since 1.4.0
998 * @access public
999 *
1000 * @return array|null The config.
1001 */
1002 final public function get_config() {
1003 if ( null === $this->config ) {
1004 // TODO: This is for backwards compatibility starting from 2.9.0
1005 // This if statement should be removed when the method is hard-deprecated
1006 if ( $this->has_own_method( '_get_initial_config', self::class ) ) {
1007 Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( '_get_initial_config', '2.9.0', __CLASS__ . '::get_initial_config()' );
1008
1009 $this->config = $this->_get_initial_config();
1010 } else {
1011 $this->config = $this->get_initial_config();
1012 }
1013
1014 foreach ( $this->additional_config as $key => $value ) {
1015 if ( isset( $this->config[ $key ] ) ) {
1016 $this->config[ $key ] = wp_parse_args( $value, $this->config[ $key ] );
1017 } else {
1018 $this->config[ $key ] = $value;
1019 }
1020 }
1021 }
1022
1023 return $this->config;
1024 }
1025
1026 /**
1027 * Set a config property.
1028 *
1029 * Set a specific property of the config list for this controls-stack.
1030 *
1031 * @since 3.5.0
1032 * @access public
1033 */
1034 public function set_config( $key, $value ) {
1035 if ( isset( $this->additional_config[ $key ] ) ) {
1036 $this->additional_config[ $key ] = wp_parse_args( $value, $this->additional_config[ $key ] );
1037 } else {
1038 $this->additional_config[ $key ] = $value;
1039 }
1040 }
1041
1042 /**
1043 * Get frontend settings keys.
1044 *
1045 * Retrieve settings keys for all frontend controls.
1046 *
1047 * @since 1.6.0
1048 * @access public
1049 *
1050 * @return array Settings keys for each control.
1051 */
1052 final public function get_frontend_settings_keys() {
1053 $controls = [];
1054
1055 foreach ( $this->get_controls() as $control ) {
1056 if ( ! empty( $control['frontend_available'] ) ) {
1057 $controls[] = $control['name'];
1058 }
1059 }
1060
1061 return $controls;
1062 }
1063
1064 /**
1065 * Get controls pointer index.
1066 *
1067 * Retrieve pointer index where the next control should be added.
1068 *
1069 * While using injection point, it will return the injection point index.
1070 * Otherwise index of the last control plus one.
1071 *
1072 * @since 1.9.2
1073 * @access public
1074 *
1075 * @return int Controls pointer index.
1076 */
1077 public function get_pointer_index() {
1078 if ( null !== $this->injection_point ) {
1079 return $this->injection_point['index'];
1080 }
1081
1082 return count( $this->get_controls() );
1083 }
1084
1085 /**
1086 * Get the raw data.
1087 *
1088 * Retrieve all the items or, when requested, a specific item.
1089 *
1090 * @since 1.4.0
1091 * @access public
1092 *
1093 * @param string $item Optional. The requested item. Default is null.
1094 *
1095 * @return mixed The raw data.
1096 */
1097 public function get_data( $item = null ) {
1098 if ( ! $this->settings_sanitized && ( ! $item || 'settings' === $item ) ) {
1099 $this->data['settings'] = $this->sanitize_settings( $this->data['settings'] );
1100
1101 $this->settings_sanitized = true;
1102 }
1103
1104 return self::get_items( $this->data, $item );
1105 }
1106
1107 /**
1108 * @since 2.0.14
1109 * @access public
1110 */
1111 public function get_parsed_dynamic_settings( $setting = null, $settings = null ) {
1112 if ( null === $settings ) {
1113 $settings = $this->get_settings();
1114 }
1115
1116 if ( null === $this->parsed_dynamic_settings ) {
1117 $this->parsed_dynamic_settings = $this->parse_dynamic_settings( $settings );
1118 }
1119
1120 return self::get_items( $this->parsed_dynamic_settings, $setting );
1121 }
1122
1123 /**
1124 * Get active settings.
1125 *
1126 * Retrieve the settings from all the active controls.
1127 *
1128 * @since 1.4.0
1129 * @since 2.1.0 Added the `controls` and the `settings` parameters.
1130 * @access public
1131 *
1132 * @param array $controls Optional. An array of controls. Default is null.
1133 * @param array $settings Optional. Controls settings. Default is null.
1134 *
1135 * @return array Active settings.
1136 */
1137 public function get_active_settings( $settings = null, $controls = null ) {
1138 $is_first_request = ! $settings && ! $this->active_settings;
1139
1140 if ( ! $settings ) {
1141 if ( $this->active_settings ) {
1142 return $this->active_settings;
1143 }
1144
1145 $settings = $this->get_controls_settings();
1146
1147 $controls = $this->get_controls();
1148 }
1149
1150 $active_settings = [];
1151
1152 foreach ( $settings as $setting_key => $setting ) {
1153 if ( ! isset( $controls[ $setting_key ] ) ) {
1154 $active_settings[ $setting_key ] = $setting;
1155
1156 continue;
1157 }
1158
1159 $control = $controls[ $setting_key ];
1160
1161 if ( $this->is_control_visible( $control, $settings ) ) {
1162 $control_obj = Plugin::$instance->controls_manager->get_control( $control['type'] );
1163
1164 if ( $control_obj instanceof Control_Repeater ) {
1165 foreach ( $setting as & $item ) {
1166 $item = $this->get_active_settings( $item, $control['fields'] );
1167 }
1168 }
1169
1170 $active_settings[ $setting_key ] = $setting;
1171 } else {
1172 $active_settings[ $setting_key ] = null;
1173 }
1174 }
1175
1176 if ( $is_first_request ) {
1177 $this->active_settings = $active_settings;
1178 }
1179
1180 return $active_settings;
1181 }
1182
1183 /**
1184 * Get settings for display.
1185 *
1186 * Retrieve all the settings or, when requested, a specific setting for display.
1187 *
1188 * Unlike `get_settings()` method, this method retrieves only active settings
1189 * that passed all the conditions, rendered all the shortcodes and all the dynamic
1190 * tags.
1191 *
1192 * @since 2.0.0
1193 * @access public
1194 *
1195 * @param string $setting_key Optional. The key of the requested setting.
1196 * Default is null.
1197 *
1198 * @return mixed The settings.
1199 */
1200 public function get_settings_for_display( $setting_key = null ) {
1201 if ( ! $this->parsed_active_settings ) {
1202 $this->parsed_active_settings = $this->get_active_settings( $this->get_parsed_dynamic_settings(), $this->get_controls() );
1203 }
1204
1205 return self::get_items( $this->parsed_active_settings, $setting_key );
1206 }
1207
1208 /**
1209 * Parse dynamic settings.
1210 *
1211 * Retrieve the settings with rendered dynamic tags.
1212 *
1213 * @since 2.0.0
1214 * @access public
1215 *
1216 * @param array $settings Optional. The requested setting. Default is null.
1217 * @param array $controls Optional. The controls array. Default is null.
1218 * @param array $all_settings Optional. All the settings. Default is null.
1219 *
1220 * @return array The settings with rendered dynamic tags.
1221 */
1222 public function parse_dynamic_settings( $settings, $controls = null, $all_settings = null ) {
1223 if ( null === $all_settings ) {
1224 $all_settings = $this->get_settings();
1225 }
1226
1227 if ( null === $controls ) {
1228 $controls = $this->get_controls();
1229 }
1230
1231 foreach ( $controls as $control ) {
1232 $control_name = $control['name'];
1233 $control_obj = Plugin::$instance->controls_manager->get_control( $control['type'] );
1234
1235 if ( ! $control_obj instanceof Base_Data_Control ) {
1236 continue;
1237 }
1238
1239 if ( $control_obj instanceof Control_Repeater ) {
1240 if ( ! isset( $settings[ $control_name ] ) ) {
1241 continue;
1242 }
1243
1244 foreach ( $settings[ $control_name ] as & $field ) {
1245 $field = $this->parse_dynamic_settings( $field, $control['fields'], $field );
1246 }
1247
1248 continue;
1249 }
1250
1251 $dynamic_settings = $control_obj->get_settings( 'dynamic' );
1252
1253 if ( ! $dynamic_settings ) {
1254 $dynamic_settings = [];
1255 }
1256
1257 if ( ! empty( $control['dynamic'] ) ) {
1258 $dynamic_settings = array_merge( $dynamic_settings, $control['dynamic'] );
1259 }
1260
1261 if ( empty( $dynamic_settings ) || ! isset( $all_settings[ Manager::DYNAMIC_SETTING_KEY ][ $control_name ] ) ) {
1262 continue;
1263 }
1264
1265 if ( ! empty( $dynamic_settings['active'] ) && ! empty( $all_settings[ Manager::DYNAMIC_SETTING_KEY ][ $control_name ] ) ) {
1266 $parsed_value = $control_obj->parse_tags( $all_settings[ Manager::DYNAMIC_SETTING_KEY ][ $control_name ], $dynamic_settings );
1267
1268 $dynamic_property = ! empty( $dynamic_settings['property'] ) ? $dynamic_settings['property'] : null;
1269
1270 if ( $dynamic_property ) {
1271 $settings[ $control_name ][ $dynamic_property ] = $parsed_value;
1272 } else {
1273 $settings[ $control_name ] = $parsed_value;
1274 }
1275 }
1276 }
1277
1278 return $settings;
1279 }
1280
1281 /**
1282 * Get frontend settings.
1283 *
1284 * Retrieve the settings for all frontend controls.
1285 *
1286 * @since 1.6.0
1287 * @access public
1288 *
1289 * @return array Frontend settings.
1290 */
1291 public function get_frontend_settings() {
1292 $frontend_settings = array_intersect_key( $this->get_settings_for_display(), array_flip( $this->get_frontend_settings_keys() ) );
1293
1294 foreach ( $frontend_settings as $key => $setting ) {
1295 if ( in_array( $setting, [ null, '' ], true ) ) {
1296 unset( $frontend_settings[ $key ] );
1297 }
1298 }
1299
1300 return $frontend_settings;
1301 }
1302
1303 /**
1304 * Filter controls settings.
1305 *
1306 * Receives controls, settings and a callback function to filter the settings by
1307 * and returns filtered settings.
1308 *
1309 * @since 1.5.0
1310 * @access public
1311 *
1312 * @param callable $callback The callback function.
1313 * @param array $settings Optional. Control settings. Default is an empty
1314 * array.
1315 * @param array $controls Optional. Controls list. Default is an empty
1316 * array.
1317 *
1318 * @return array Filtered settings.
1319 */
1320 public function filter_controls_settings( callable $callback, array $settings = [], array $controls = [] ) {
1321 if ( ! $settings ) {
1322 $settings = $this->get_settings();
1323 }
1324
1325 if ( ! $controls ) {
1326 $controls = $this->get_controls();
1327 }
1328
1329 return array_reduce(
1330 array_keys( $settings ), function( $filtered_settings, $setting_key ) use ( $controls, $settings, $callback ) {
1331 if ( isset( $controls[ $setting_key ] ) ) {
1332 $result = $callback( $settings[ $setting_key ], $controls[ $setting_key ] );
1333
1334 if ( null !== $result ) {
1335 $filtered_settings[ $setting_key ] = $result;
1336 }
1337 }
1338
1339 return $filtered_settings;
1340 }, []
1341 );
1342 }
1343
1344 /**
1345 * Whether the control is visible or not.
1346 *
1347 * Used to determine whether the control is visible or not.
1348 *
1349 * @since 1.4.0
1350 * @access public
1351 *
1352 * @param array $control The control.
1353 * @param array $values Optional. Condition values. Default is null.
1354 *
1355 * @return bool Whether the control is visible.
1356 */
1357 public function is_control_visible( $control, $values = null ) {
1358 if ( null === $values ) {
1359 $values = $this->get_settings();
1360 }
1361
1362 if ( ! empty( $control['conditions'] ) && ! Conditions::check( $control['conditions'], $values ) ) {
1363 return false;
1364 }
1365
1366 if ( empty( $control['condition'] ) ) {
1367 return true;
1368 }
1369
1370 foreach ( $control['condition'] as $condition_key => $condition_value ) {
1371 preg_match( '/([a-z_\-0-9]+)(?:\[([a-z_]+)])?(!?)$/i', $condition_key, $condition_key_parts );
1372
1373 $pure_condition_key = $condition_key_parts[1];
1374 $condition_sub_key = $condition_key_parts[2];
1375 $is_negative_condition = ! ! $condition_key_parts[3];
1376
1377 if ( ! isset( $values[ $pure_condition_key ] ) || null === $values[ $pure_condition_key ] ) {
1378 return false;
1379 }
1380
1381 $instance_value = $values[ $pure_condition_key ];
1382
1383 if ( $condition_sub_key && is_array( $instance_value ) ) {
1384 if ( ! isset( $instance_value[ $condition_sub_key ] ) ) {
1385 return false;
1386 }
1387
1388 $instance_value = $instance_value[ $condition_sub_key ];
1389 }
1390
1391 /**
1392 * If the $condition_value is a non empty array - check if the $condition_value contains the $instance_value,
1393 * If the $instance_value is a non empty array - check if the $instance_value contains the $condition_value
1394 * otherwise check if they are equal. ( and give the ability to check if the value is an empty array )
1395 */
1396 if ( is_array( $condition_value ) && ! empty( $condition_value ) ) {
1397 $is_contains = in_array( $instance_value, $condition_value, true );
1398 } elseif ( is_array( $instance_value ) && ! empty( $instance_value ) ) {
1399 $is_contains = in_array( $condition_value, $instance_value, true );
1400 } else {
1401 $is_contains = $instance_value === $condition_value;
1402 }
1403
1404 if ( $is_negative_condition && $is_contains || ! $is_negative_condition && ! $is_contains ) {
1405 return false;
1406 }
1407 }
1408
1409 return true;
1410 }
1411
1412 /**
1413 * Start controls section.
1414 *
1415 * Used to add a new section of controls. When you use this method, all the
1416 * registered controls from this point will be assigned to this section,
1417 * until you close the section using `end_controls_section()` method.
1418 *
1419 * This method should be used inside `register_controls()`.
1420 *
1421 * @since 1.4.0
1422 * @access public
1423 *
1424 * @param string $section_id Section ID.
1425 * @param array $args Section arguments Optional.
1426 */
1427 public function start_controls_section( $section_id, array $args = [] ) {
1428 $stack_name = $this->get_name();
1429
1430 /**
1431 * Before section start.
1432 *
1433 * Fires before Elementor section starts in the editor panel.
1434 *
1435 * @since 1.4.0
1436 *
1437 * @param Controls_Stack $this The control.
1438 * @param string $section_id Section ID.
1439 * @param array $args Section arguments.
1440 */
1441 do_action( 'elementor/element/before_section_start', $this, $section_id, $args );
1442
1443 /**
1444 * Before section start.
1445 *
1446 * Fires before Elementor section starts in the editor panel.
1447 *
1448 * The dynamic portions of the hook name, `$stack_name` and `$section_id`, refers to the stack name and section ID, respectively.
1449 *
1450 * @since 1.4.0
1451 *
1452 * @param Controls_Stack $this The control.
1453 * @param array $args Section arguments.
1454 */
1455 do_action( "elementor/element/{$stack_name}/{$section_id}/before_section_start", $this, $args );
1456
1457 $args['type'] = Controls_Manager::SECTION;
1458
1459 $this->add_control( $section_id, $args );
1460
1461 if ( null !== $this->current_section ) {
1462 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
1463 }
1464
1465 $this->current_section = $this->get_section_args( $section_id );
1466
1467 if ( $this->injection_point ) {
1468 $this->injection_point['section'] = $this->current_section;
1469 }
1470
1471 /**
1472 * After section start.
1473 *
1474 * Fires after Elementor section starts in the editor panel.
1475 *
1476 * @since 1.4.0
1477 *
1478 * @param Controls_Stack $this The control.
1479 * @param string $section_id Section ID.
1480 * @param array $args Section arguments.
1481 */
1482 do_action( 'elementor/element/after_section_start', $this, $section_id, $args );
1483
1484 /**
1485 * After section start.
1486 *
1487 * Fires after Elementor section starts in the editor panel.
1488 *
1489 * The dynamic portions of the hook name, `$stack_name` and `$section_id`, refers to the stack name and section ID, respectively.
1490 *
1491 * @since 1.4.0
1492 *
1493 * @param Controls_Stack $this The control.
1494 * @param array $args Section arguments.
1495 */
1496 do_action( "elementor/element/{$stack_name}/{$section_id}/after_section_start", $this, $args );
1497 }
1498
1499 /**
1500 * End controls section.
1501 *
1502 * Used to close an existing open controls section. When you use this method
1503 * it stops adding new controls to this section.
1504 *
1505 * This method should be used inside `register_controls()`.
1506 *
1507 * @since 1.4.0
1508 * @access public
1509 */
1510 public function end_controls_section() {
1511 $stack_name = $this->get_name();
1512
1513 // Save the current section for the action.
1514 $current_section = $this->current_section;
1515 $section_id = $current_section['section'];
1516 $args = [
1517 'tab' => $current_section['tab'],
1518 ];
1519
1520 /**
1521 * Before section end.
1522 *
1523 * Fires before Elementor section ends in the editor panel.
1524 *
1525 * @since 1.4.0
1526 *
1527 * @param Controls_Stack $this The control.
1528 * @param string $section_id Section ID.
1529 * @param array $args Section arguments.
1530 */
1531 do_action( 'elementor/element/before_section_end', $this, $section_id, $args );
1532
1533 /**
1534 * Before section end.
1535 *
1536 * Fires before Elementor section ends in the editor panel.
1537 *
1538 * The dynamic portions of the hook name, `$stack_name` and `$section_id`, refers to the stack name and section ID, respectively.
1539 *
1540 * @since 1.4.0
1541 *
1542 * @param Controls_Stack $this The control.
1543 * @param array $args Section arguments.
1544 */
1545 do_action( "elementor/element/{$stack_name}/{$section_id}/before_section_end", $this, $args );
1546
1547 $this->current_section = null;
1548
1549 /**
1550 * After section end.
1551 *
1552 * Fires after Elementor section ends in the editor panel.
1553 *
1554 * @since 1.4.0
1555 *
1556 * @param Controls_Stack $this The control.
1557 * @param string $section_id Section ID.
1558 * @param array $args Section arguments.
1559 */
1560 do_action( 'elementor/element/after_section_end', $this, $section_id, $args );
1561
1562 /**
1563 * After section end.
1564 *
1565 * Fires after Elementor section ends in the editor panel.
1566 *
1567 * The dynamic portions of the hook name, `$stack_name` and `$section_id`, refers to the stack name and section ID, respectively.
1568 *
1569 * @since 1.4.0
1570 *
1571 * @param Controls_Stack $this The control.
1572 * @param array $args Section arguments.
1573 */
1574 do_action( "elementor/element/{$stack_name}/{$section_id}/after_section_end", $this, $args );
1575 }
1576
1577 /**
1578 * Start controls tabs.
1579 *
1580 * Used to add a new set of tabs inside a section. You should use this
1581 * method before adding new individual tabs using `start_controls_tab()`.
1582 * Each tab added after this point will be assigned to this group of tabs,
1583 * until you close it using `end_controls_tabs()` method.
1584 *
1585 * This method should be used inside `register_controls()`.
1586 *
1587 * @since 1.4.0
1588 * @access public
1589 *
1590 * @param string $tabs_id Tabs ID.
1591 * @param array $args Tabs arguments.
1592 */
1593 public function start_controls_tabs( $tabs_id, array $args = [] ) {
1594 if ( null !== $this->current_tab ) {
1595 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
1596 }
1597
1598 $args['type'] = Controls_Manager::TABS;
1599
1600 $this->add_control( $tabs_id, $args );
1601
1602 $this->current_tab = [
1603 'tabs_wrapper' => $tabs_id,
1604 ];
1605
1606 foreach ( [ 'condition', 'conditions' ] as $key ) {
1607 if ( ! empty( $args[ $key ] ) ) {
1608 $this->current_tab[ $key ] = $args[ $key ];
1609 }
1610 }
1611
1612 if ( $this->injection_point ) {
1613 $this->injection_point['tab'] = $this->current_tab;
1614 }
1615 }
1616
1617 /**
1618 * End controls tabs.
1619 *
1620 * Used to close an existing open controls tabs. When you use this method it
1621 * stops adding new controls to this tabs.
1622 *
1623 * This method should be used inside `register_controls()`.
1624 *
1625 * @since 1.4.0
1626 * @access public
1627 */
1628 public function end_controls_tabs() {
1629 $this->current_tab = null;
1630 }
1631
1632 /**
1633 * Start controls tab.
1634 *
1635 * Used to add a new tab inside a group of tabs. Use this method before
1636 * adding new individual tabs using `start_controls_tab()`.
1637 * Each tab added after this point will be assigned to this group of tabs,
1638 * until you close it using `end_controls_tab()` method.
1639 *
1640 * This method should be used inside `register_controls()`.
1641 *
1642 * @since 1.4.0
1643 * @access public
1644 *
1645 * @param string $tab_id Tab ID.
1646 * @param array $args Tab arguments.
1647 */
1648 public function start_controls_tab( $tab_id, $args ) {
1649 if ( ! empty( $this->current_tab['inner_tab'] ) ) {
1650 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
1651 }
1652
1653 $args['type'] = Controls_Manager::TAB;
1654 $args['tabs_wrapper'] = $this->current_tab['tabs_wrapper'];
1655
1656 $this->add_control( $tab_id, $args );
1657
1658 $this->current_tab['inner_tab'] = $tab_id;
1659
1660 if ( $this->injection_point ) {
1661 $this->injection_point['tab']['inner_tab'] = $this->current_tab['inner_tab'];
1662 }
1663 }
1664
1665 /**
1666 * End controls tab.
1667 *
1668 * Used to close an existing open controls tab. When you use this method it
1669 * stops adding new controls to this tab.
1670 *
1671 * This method should be used inside `register_controls()`.
1672 *
1673 * @since 1.4.0
1674 * @access public
1675 */
1676 public function end_controls_tab() {
1677 unset( $this->current_tab['inner_tab'] );
1678 }
1679
1680 /**
1681 * Start popover.
1682 *
1683 * Used to add a new set of controls in a popover. When you use this method,
1684 * all the registered controls from this point will be assigned to this
1685 * popover, until you close the popover using `end_popover()` method.
1686 *
1687 * This method should be used inside `register_controls()`.
1688 *
1689 * @since 1.9.0
1690 * @access public
1691 */
1692 final public function start_popover() {
1693 $this->current_popover = [
1694 'initialized' => false,
1695 ];
1696 }
1697
1698 /**
1699 * End popover.
1700 *
1701 * Used to close an existing open popover. When you use this method it stops
1702 * adding new controls to this popover.
1703 *
1704 * This method should be used inside `register_controls()`.
1705 *
1706 * @since 1.9.0
1707 * @access public
1708 */
1709 final public function end_popover() {
1710 $this->current_popover = null;
1711
1712 $last_control_key = $this->get_control_key( $this->get_pointer_index() - 1 );
1713
1714 $args = [
1715 'popover' => [
1716 'end' => true,
1717 ],
1718 ];
1719
1720 $options = [
1721 'recursive' => true,
1722 ];
1723
1724 $this->update_control( $last_control_key, $args, $options );
1725 }
1726
1727 /**
1728 * Add render attribute.
1729 *
1730 * Used to add attributes to a specific HTML element.
1731 *
1732 * The HTML tag is represented by the element parameter, then you need to
1733 * define the attribute key and the attribute key. The final result will be:
1734 * `<element attribute_key="attribute_value">`.
1735 *
1736 * Example usage:
1737 *
1738 * `$this->add_render_attribute( 'wrapper', 'class', 'custom-widget-wrapper-class' );`
1739 * `$this->add_render_attribute( 'widget', 'id', 'custom-widget-id' );`
1740 * `$this->add_render_attribute( 'button', [ 'class' => 'custom-button-class', 'id' => 'custom-button-id' ] );`
1741 *
1742 * @since 1.0.0
1743 * @access public
1744 *
1745 * @param array|string $element The HTML element.
1746 * @param array|string $key Optional. Attribute key. Default is null.
1747 * @param array|string $value Optional. Attribute value. Default is null.
1748 * @param bool $overwrite Optional. Whether to overwrite existing
1749 * attribute. Default is false, not to overwrite.
1750 *
1751 * @return self Current instance of the element.
1752 */
1753 public function add_render_attribute( $element, $key = null, $value = null, $overwrite = false ) {
1754 if ( is_array( $element ) ) {
1755 foreach ( $element as $element_key => $attributes ) {
1756 $this->add_render_attribute( $element_key, $attributes, null, $overwrite );
1757 }
1758
1759 return $this;
1760 }
1761
1762 if ( is_array( $key ) ) {
1763 foreach ( $key as $attribute_key => $attributes ) {
1764 $this->add_render_attribute( $element, $attribute_key, $attributes, $overwrite );
1765 }
1766
1767 return $this;
1768 }
1769
1770 if ( empty( $this->render_attributes[ $element ][ $key ] ) ) {
1771 $this->render_attributes[ $element ][ $key ] = [];
1772 }
1773
1774 settype( $value, 'array' );
1775
1776 if ( $overwrite ) {
1777 $this->render_attributes[ $element ][ $key ] = $value;
1778 } else {
1779 $this->render_attributes[ $element ][ $key ] = array_merge( $this->render_attributes[ $element ][ $key ], $value );
1780 }
1781
1782 return $this;
1783 }
1784
1785 /**
1786 * Get Render Attributes
1787 *
1788 * Used to retrieve render attribute.
1789 *
1790 * The returned array is either all elements and their attributes if no `$element` is specified, an array of all
1791 * attributes of a specific element or a specific attribute properties if `$key` is specified.
1792 *
1793 * Returns null if one of the requested parameters isn't set.
1794 *
1795 * @since 2.2.6
1796 * @access public
1797 * @param string $element
1798 * @param string $key
1799 *
1800 * @return array
1801 */
1802 public function get_render_attributes( $element = '', $key = '' ) {
1803 $attributes = $this->render_attributes;
1804
1805 if ( $element ) {
1806 if ( ! isset( $attributes[ $element ] ) ) {
1807 return null;
1808 }
1809
1810 $attributes = $attributes[ $element ];
1811
1812 if ( $key ) {
1813 if ( ! isset( $attributes[ $key ] ) ) {
1814 return null;
1815 }
1816
1817 $attributes = $attributes[ $key ];
1818 }
1819 }
1820
1821 return $attributes;
1822 }
1823
1824 /**
1825 * Set render attribute.
1826 *
1827 * Used to set the value of the HTML element render attribute or to update
1828 * an existing render attribute.
1829 *
1830 * @since 1.0.0
1831 * @access public
1832 *
1833 * @param array|string $element The HTML element.
1834 * @param array|string $key Optional. Attribute key. Default is null.
1835 * @param array|string $value Optional. Attribute value. Default is null.
1836 *
1837 * @return self Current instance of the element.
1838 */
1839 public function set_render_attribute( $element, $key = null, $value = null ) {
1840 return $this->add_render_attribute( $element, $key, $value, true );
1841 }
1842
1843 /**
1844 * Remove render attribute.
1845 *
1846 * Used to remove an element (with its keys and their values), key (with its values),
1847 * or value/s from an HTML element's render attribute.
1848 *
1849 * @since 2.7.0
1850 * @access public
1851 *
1852 * @param string $element The HTML element.
1853 * @param string $key Optional. Attribute key. Default is null.
1854 * @param array|string $values Optional. Attribute value/s. Default is null.
1855 */
1856 public function remove_render_attribute( $element, $key = null, $values = null ) {
1857 if ( $key && ! isset( $this->render_attributes[ $element ][ $key ] ) ) {
1858 return;
1859 }
1860
1861 if ( $values ) {
1862 $values = (array) $values;
1863
1864 $this->render_attributes[ $element ][ $key ] = array_diff( $this->render_attributes[ $element ][ $key ], $values );
1865
1866 return;
1867 }
1868
1869 if ( $key ) {
1870 unset( $this->render_attributes[ $element ][ $key ] );
1871
1872 return;
1873 }
1874
1875 if ( isset( $this->render_attributes[ $element ] ) ) {
1876 unset( $this->render_attributes[ $element ] );
1877 }
1878 }
1879
1880 /**
1881 * Get render attribute string.
1882 *
1883 * Used to retrieve the value of the render attribute.
1884 *
1885 * @since 1.0.0
1886 * @access public
1887 *
1888 * @param string $element The element.
1889 *
1890 * @return string Render attribute string, or an empty string if the attribute
1891 * is empty or not exist.
1892 */
1893 public function get_render_attribute_string( $element ) {
1894 if ( empty( $this->render_attributes[ $element ] ) ) {
1895 return '';
1896 }
1897
1898 return Utils::render_html_attributes( $this->render_attributes[ $element ] );
1899 }
1900
1901 /**
1902 * Print render attribute string.
1903 *
1904 * Used to output the rendered attribute.
1905 *
1906 * @since 2.0.0
1907 * @access public
1908 *
1909 * @param array|string $element The element.
1910 */
1911 public function print_render_attribute_string( $element ) {
1912 echo $this->get_render_attribute_string( $element ); // XSS ok.
1913 }
1914
1915 /**
1916 * Print element template.
1917 *
1918 * Used to generate the element template on the editor.
1919 *
1920 * @since 2.0.0
1921 * @access public
1922 */
1923 public function print_template() {
1924 ob_start();
1925
1926 // TODO: This is for backwards compatibility starting from 2.9.0
1927 // This `if` statement should be removed when the method is removed
1928 if ( $this->has_own_method( '_content_template', self::class ) ) {
1929 Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( '_content_template', '2.9.0', __CLASS__ . '::content_template()' );
1930
1931 $this->_content_template();
1932 } else {
1933 $this->content_template();
1934 }
1935
1936 $template_content = ob_get_clean();
1937
1938 $element_type = $this->get_type();
1939
1940 /**
1941 * Template content.
1942 *
1943 * Filters the controls stack template content before it's printed in the editor.
1944 *
1945 * The dynamic portion of the hook name, `$element_type`, refers to the element type.
1946 *
1947 * @since 1.0.0
1948 *
1949 * @param string $content_template The controls stack template in the editor.
1950 * @param Controls_Stack $this The controls stack.
1951 */
1952 $template_content = apply_filters( "elementor/{$element_type}/print_template", $template_content, $this );
1953
1954 if ( empty( $template_content ) ) {
1955 return;
1956 }
1957 ?>
1958 <script type="text/html" id="tmpl-elementor-<?php echo esc_attr( $this->get_name() ); ?>-content">
1959 <?php $this->print_template_content( $template_content ); ?>
1960 </script>
1961 <?php
1962 }
1963
1964 /**
1965 * Start injection.
1966 *
1967 * Used to inject controls and sections to a specific position in the stack.
1968 *
1969 * When you use this method, all the registered controls and sections will
1970 * be injected to a specific position in the stack, until you stop the
1971 * injection using `end_injection()` method.
1972 *
1973 * @since 1.7.1
1974 * @access public
1975 *
1976 * @param array $position {
1977 * The position where to start the injection.
1978 *
1979 * @type string $type Injection type, either `control` or `section`.
1980 * Default is `control`.
1981 * @type string $at Where to inject. If `$type` is `control` accepts
1982 * `before` and `after`. If `$type` is `section`
1983 * accepts `start` and `end`. Default values based on
1984 * the `type`.
1985 * @type string $of Control/Section ID.
1986 * }
1987 */
1988 final public function start_injection( array $position ) {
1989 if ( $this->injection_point ) {
1990 wp_die( 'A controls injection is already opened. Please close current injection before starting a new one (use `end_injection`).' );
1991 }
1992
1993 $this->injection_point = $this->get_position_info( $position );
1994 }
1995
1996 /**
1997 * End injection.
1998 *
1999 * Used to close an existing opened injection point.
2000 *
2001 * When you use this method it stops adding new controls and sections to
2002 * this point and continue to add controls to the regular position in the
2003 * stack.
2004 *
2005 * @since 1.7.1
2006 * @access public
2007 */
2008 final public function end_injection() {
2009 $this->injection_point = null;
2010 }
2011
2012 /**
2013 * Get injection point.
2014 *
2015 * Retrieve the injection point in the stack where new controls and sections
2016 * will be inserted.
2017 *
2018 * @since 1.9.2
2019 * @access public
2020 *
2021 * @return array|null An array when an injection point is defined, null
2022 * otherwise.
2023 */
2024 final public function get_injection_point() {
2025 return $this->injection_point;
2026 }
2027
2028 /**
2029 * Register controls.
2030 *
2031 * Used to add new controls to any element type. For example, external
2032 * developers use this method to register controls in a widget.
2033 *
2034 * Should be inherited and register new controls using `add_control()`,
2035 * `add_responsive_control()` and `add_group_control()`, inside control
2036 * wrappers like `start_controls_section()`, `start_controls_tabs()` and
2037 * `start_controls_tab()`.
2038 *
2039 * @since 1.4.0
2040 * @access protected
2041 * @deprecated 3.1.0 Use `Controls_Stack::register_controls()` instead
2042 */
2043 protected function _register_controls() {
2044 Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( __METHOD__, '3.1.0', __CLASS__ . '::register_controls()' );
2045
2046 $this->register_controls();
2047 }
2048
2049 /**
2050 * Register controls.
2051 *
2052 * Used to add new controls to any element type. For example, external
2053 * developers use this method to register controls in a widget.
2054 *
2055 * Should be inherited and register new controls using `add_control()`,
2056 * `add_responsive_control()` and `add_group_control()`, inside control
2057 * wrappers like `start_controls_section()`, `start_controls_tabs()` and
2058 * `start_controls_tab()`.
2059 *
2060 * @since 3.1.0
2061 * @access protected
2062 */
2063 protected function register_controls() {}
2064
2065 /**
2066 * Get default data.
2067 *
2068 * Retrieve the default data. Used to reset the data on initialization.
2069 *
2070 * @since 1.4.0
2071 * @access protected
2072 *
2073 * @return array Default data.
2074 */
2075 protected function get_default_data() {
2076 return [
2077 'id' => 0,
2078 'settings' => [],
2079 ];
2080 }
2081
2082 /**
2083 * @since 2.3.0
2084 * @access protected
2085 */
2086 protected function get_init_settings() {
2087 $settings = $this->get_data( 'settings' );
2088
2089 foreach ( $this->get_controls() as $control ) {
2090 $control_obj = Plugin::$instance->controls_manager->get_control( $control['type'] );
2091
2092 if ( ! $control_obj instanceof Base_Data_Control ) {
2093 continue;
2094 }
2095
2096 $control = array_merge_recursive( $control_obj->get_settings(), $control );
2097
2098 $settings[ $control['name'] ] = $control_obj->get_value( $control, $settings );
2099 }
2100
2101 return $settings;
2102 }
2103
2104 /**
2105 * Get initial config.
2106 *
2107 * Retrieve the current element initial configuration - controls list and
2108 * the tabs assigned to the control.
2109 *
2110 * @since 2.9.0
2111 * @access protected
2112 *
2113 * @return array The initial config.
2114 */
2115 protected function get_initial_config() {
2116 return [
2117 'controls' => $this->get_controls(),
2118 ];
2119 }
2120
2121 /**
2122 * Get initial config.
2123 *
2124 * Retrieve the current element initial configuration - controls list and
2125 * the tabs assigned to the control.
2126 *
2127 * @since 1.4.0
2128 * @deprecated 2.9.0 use `get_initial_config()` instead
2129 * @access protected
2130 *
2131 * @return array The initial config.
2132 */
2133 protected function _get_initial_config() {
2134 Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( __METHOD__, '2.9.0', __CLASS__ . '::get_initial_config()' );
2135
2136 return $this->get_initial_config();
2137 }
2138
2139 /**
2140 * Get section arguments.
2141 *
2142 * Retrieve the section arguments based on section ID.
2143 *
2144 * @since 1.4.0
2145 * @access protected
2146 *
2147 * @param string $section_id Section ID.
2148 *
2149 * @return array Section arguments.
2150 */
2151 protected function get_section_args( $section_id ) {
2152 $section_control = $this->get_controls( $section_id );
2153
2154 $section_args_keys = [ 'tab', 'condition' ];
2155
2156 $args = array_intersect_key( $section_control, array_flip( $section_args_keys ) );
2157
2158 $args['section'] = $section_id;
2159
2160 return $args;
2161 }
2162
2163 /**
2164 * Render element.
2165 *
2166 * Generates the final HTML on the frontend.
2167 *
2168 * @since 2.0.0
2169 * @access protected
2170 */
2171 protected function render() {}
2172
2173 /**
2174 * Render element in static mode.
2175 *
2176 * If not inherent will call the base render.
2177 */
2178 protected function render_static() {
2179 $this->render();
2180 }
2181
2182 /**
2183 * Determine the render logic.
2184 */
2185 protected function render_by_mode() {
2186 if ( Plugin::$instance->frontend->is_static_render_mode() ) {
2187 $this->render_static();
2188
2189 return;
2190 }
2191
2192 $this->render();
2193 }
2194
2195 /**
2196 * Print content template.
2197 *
2198 * Used to generate the content template on the editor, using a
2199 * Backbone JavaScript template.
2200 *
2201 * @access protected
2202 * @since 2.0.0
2203 *
2204 * @param string $template_content Template content.
2205 */
2206 protected function print_template_content( $template_content ) {
2207 echo $template_content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
2208 }
2209
2210 /**
2211 * Render element output in the editor.
2212 *
2213 * Used to generate the live preview, using a Backbone JavaScript template.
2214 *
2215 * @since 2.9.0
2216 * @access protected
2217 */
2218 protected function content_template() {}
2219
2220 /**
2221 * Render element output in the editor.
2222 *
2223 * Used to generate the live preview, using a Backbone JavaScript template.
2224 *
2225 * @since 2.0.0
2226 * @deprecated 2.9.0 use `content_template()` instead
2227 * @access protected
2228 */
2229 protected function _content_template() {
2230 Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( __METHOD__, '2.9.0', __CLASS__ . '::content_template()' );
2231
2232 $this->content_template();
2233 }
2234
2235 /**
2236 * Initialize controls.
2237 *
2238 * Register the all controls added by `register_controls()`.
2239 *
2240 * @since 2.0.0
2241 * @access protected
2242 */
2243 protected function init_controls() {
2244 Plugin::$instance->controls_manager->open_stack( $this );
2245
2246 // TODO: This is for backwards compatibility starting from 2.9.0
2247 // This `if` statement should be removed when the method is removed
2248 if ( $this->has_own_method( '_register_controls', self::class ) ) {
2249 Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( '_register_controls', '3.1.0', __CLASS__ . '::register_controls()' );
2250
2251 $this->_register_controls();
2252 } else {
2253 $this->register_controls();
2254 }
2255 }
2256
2257 protected function handle_control_position( array $args, $control_id, $overwrite ) {
2258 if ( isset( $args['type'] ) && in_array( $args['type'], [ Controls_Manager::SECTION, Controls_Manager::WP_WIDGET ], true ) ) {
2259 return $args;
2260 }
2261
2262 $target_section_args = $this->current_section;
2263
2264 $target_tab = $this->current_tab;
2265
2266 if ( $this->injection_point ) {
2267 $target_section_args = $this->injection_point['section'];
2268
2269 if ( ! empty( $this->injection_point['tab'] ) ) {
2270 $target_tab = $this->injection_point['tab'];
2271 }
2272 }
2273
2274 if ( null !== $target_section_args ) {
2275 if ( ! empty( $args['section'] ) || ! empty( $args['tab'] ) ) {
2276 _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
2277 }
2278
2279 $args = array_replace_recursive( $target_section_args, $args );
2280
2281 if ( null !== $target_tab ) {
2282 $args = array_replace_recursive( $target_tab, $args );
2283 }
2284 } elseif ( empty( $args['section'] ) && ( ! $overwrite || is_wp_error( Plugin::$instance->controls_manager->get_control_from_stack( $this->get_unique_name(), $control_id ) ) ) ) {
2285 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
2286 }
2287
2288 return $args;
2289 }
2290
2291 /**
2292 * Initialize the class.
2293 *
2294 * Set the raw data, the ID and the parsed settings.
2295 *
2296 * @since 2.9.0
2297 * @access protected
2298 *
2299 * @param array $data Initial data.
2300 */
2301 protected function init( $data ) {
2302 $this->data = array_merge( $this->get_default_data(), $data );
2303
2304 $this->id = $data['id'];
2305 }
2306
2307 /**
2308 * Initialize the class.
2309 *
2310 * Set the raw data, the ID and the parsed settings.
2311 *
2312 * @since 1.4.0
2313 * @deprecated 2.9.0 use `init()` instead
2314 * @access protected
2315 *
2316 * @param array $data Initial data.
2317 */
2318 protected function _init( $data ) {
2319 Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( __METHOD__, '2.9.0', __CLASS__ . '::init()' );
2320
2321 $this->init( $data );
2322 }
2323
2324 /**
2325 * Sanitize initial data.
2326 *
2327 * Performs settings cleaning and sanitization.
2328 *
2329 * @since 2.1.5
2330 * @access private
2331 *
2332 * @param array $settings Settings to sanitize.
2333 * @param array $controls Optional. An array of controls. Default is an
2334 * empty array.
2335 *
2336 * @return array Sanitized settings.
2337 */
2338 private function sanitize_settings( array $settings, array $controls = [] ) {
2339 if ( ! $controls ) {
2340 $controls = $this->get_controls();
2341 }
2342
2343 foreach ( $controls as $control ) {
2344 $control_obj = Plugin::$instance->controls_manager->get_control( $control['type'] );
2345
2346 if ( $control_obj instanceof Control_Repeater ) {
2347 if ( empty( $settings[ $control['name'] ] ) ) {
2348 continue;
2349 }
2350
2351 foreach ( $settings[ $control['name'] ] as $index => $repeater_row_data ) {
2352 $sanitized_row_data = $this->sanitize_settings( $repeater_row_data, $control['fields'] );
2353
2354 $settings[ $control['name'] ][ $index ] = $sanitized_row_data;
2355 }
2356
2357 continue;
2358 }
2359
2360 $is_dynamic = isset( $settings[ Manager::DYNAMIC_SETTING_KEY ][ $control['name'] ] );
2361
2362 if ( ! $is_dynamic ) {
2363 continue;
2364 }
2365
2366 $value_to_check = $settings[ Manager::DYNAMIC_SETTING_KEY ][ $control['name'] ];
2367
2368 $tag_text_data = Plugin::$instance->dynamic_tags->tag_text_to_tag_data( $value_to_check );
2369
2370 if ( ! Plugin::$instance->dynamic_tags->get_tag_info( $tag_text_data['name'] ) ) {
2371 unset( $settings[ Manager::DYNAMIC_SETTING_KEY ][ $control['name'] ] );
2372 }
2373 }
2374
2375 return $settings;
2376 }
2377
2378 /**
2379 * Controls stack constructor.
2380 *
2381 * Initializing the control stack class using `$data`. The `$data` is required
2382 * for a normal instance. It is optional only for internal `type instance`.
2383 *
2384 * @since 1.4.0
2385 * @access public
2386 *
2387 * @param array $data Optional. Control stack data. Default is an empty array.
2388 */
2389 public function __construct( array $data = [] ) {
2390 if ( $data ) {
2391 // TODO: This is for backwards compatibility starting from 2.9.0
2392 // This if statement should be removed when the method is hard-deprecated
2393 if ( $this->has_own_method( '_init', self::class ) ) {
2394 Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( '_init', '2.9.0', __CLASS__ . '::init()' );
2395
2396 $this->_init( $data );
2397 } else {
2398 $this->init( $data );
2399 }
2400 }
2401 }
2402 }
2403