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

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