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

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