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

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