PluginProbe
Elementor Website Builder – more than just a page builder / 3.20.4
Elementor Website Builder – more than just a page builder v3.20.4
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 / widget-base.php

widget-base.php in Elementor Website Builder – more than just a page builder 3.20.4, at includes/base/widget-base.php

1,255 lines 33.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\Page_Assets\Data_Managers\Responsive_Widgets as Responsive_Widgets_Data_Manager;
5 use Elementor\Core\Page_Assets\Data_Managers\Widgets_Css as Widgets_Css_Data_Manager;
6 use Elementor\Core\Utils\Promotions\Validate_Promotion;
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit; // Exit if accessed directly.
10 }
11
12 /**
13 * Elementor widget base.
14 *
15 * An abstract class to register new Elementor widgets. It extended the
16 * `Element_Base` class to inherit its properties.
17 *
18 * This abstract class must be extended in order to register new widgets.
19 *
20 * @since 1.0.0
21 * @abstract
22 */
23 abstract class Widget_Base extends Element_Base {
24 /**
25 * Whether the widget has content.
26 *
27 * Used in cases where the widget has no content. When widgets uses only
28 * skins to display dynamic content generated on the server. For example the
29 * posts widget in Elementor Pro. Default is true, the widget has content
30 * template.
31 *
32 * @access protected
33 *
34 * @var bool
35 */
36 protected $_has_template_content = true;
37
38 private $is_first_section = true;
39
40 /**
41 * Registered Runtime Widgets.
42 *
43 * Registering in runtime all widgets that are being used on the page.
44 *
45 * @since 3.3.0
46 * @access public
47 * @static
48 *
49 * @var array
50 */
51 public static $registered_runtime_widgets = [];
52
53 public static $registered_inline_css_widgets = [];
54
55 private static $widgets_css_data_manager;
56
57 private static $responsive_widgets_data_manager;
58
59 /**
60 * Get element type.
61 *
62 * Retrieve the element type, in this case `widget`.
63 *
64 * @since 1.0.0
65 * @access public
66 * @static
67 *
68 * @return string The type.
69 */
70 public static function get_type() {
71 return 'widget';
72 }
73
74 /**
75 * Get widget icon.
76 *
77 * Retrieve the widget icon.
78 *
79 * @since 1.0.0
80 * @access public
81 *
82 * @return string Widget icon.
83 */
84 public function get_icon() {
85 return 'eicon-apps';
86 }
87
88 /**
89 * Get widget keywords.
90 *
91 * Retrieve the widget keywords.
92 *
93 * @since 1.0.10
94 * @access public
95 *
96 * @return array Widget keywords.
97 */
98 public function get_keywords() {
99 return [];
100 }
101
102 /**
103 * Get widget categories.
104 *
105 * Retrieve the widget categories.
106 *
107 * @since 1.0.10
108 * @access public
109 *
110 * @return array Widget categories.
111 */
112 public function get_categories() {
113 return [ 'general' ];
114 }
115
116 /**
117 * Get widget upsale data.
118 *
119 * Retrieve the widget promotion data.
120 *
121 * @since 3.18.0
122 * @access protected
123 *
124 * @return array|null Widget promotion data.
125 */
126 protected function get_upsale_data() {
127 return null;
128 }
129
130 /**
131 * Widget base constructor.
132 *
133 * Initializing the widget base class.
134 *
135 * @since 1.0.0
136 * @access public
137 *
138 * @throws \Exception If arguments are missing when initializing a full widget
139 * instance.
140 *
141 * @param array $data Widget data. Default is an empty array.
142 * @param array|null $args Optional. Widget default arguments. Default is null.
143 */
144 public function __construct( $data = [], $args = null ) {
145 parent::__construct( $data, $args );
146
147 $is_type_instance = $this->is_type_instance();
148
149 if ( ! $is_type_instance && null === $args ) {
150 throw new \Exception( 'An `$args` argument is required when initializing a full widget instance.' );
151 }
152
153 if ( $is_type_instance ) {
154 if ( $this->has_own_method( '_register_skins', self::class ) ) {
155 Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( '_register_skins', '3.1.0', __CLASS__ . '::register_skins()' );
156
157 $this->_register_skins();
158 } else {
159 $this->register_skins();
160 }
161
162 $widget_name = $this->get_name();
163
164 /**
165 * Widget skin init.
166 *
167 * Fires when Elementor widget is being initialized.
168 *
169 * The dynamic portion of the hook name, `$widget_name`, refers to the widget name.
170 *
171 * @since 1.0.0
172 *
173 * @param Widget_Base $this The current widget.
174 */
175 do_action( "elementor/widget/{$widget_name}/skins_init", $this );
176 }
177 }
178
179 /**
180 * Get stack.
181 *
182 * Retrieve the widget stack of controls.
183 *
184 * @since 1.9.2
185 * @access public
186 *
187 * @param bool $with_common_controls Optional. Whether to include the common controls. Default is true.
188 *
189 * @return array Widget stack of controls.
190 */
191 public function get_stack( $with_common_controls = true ) {
192 $stack = parent::get_stack();
193
194 if ( $with_common_controls && 'common' !== $this->get_unique_name() ) {
195 /** @var Widget_Common $common_widget */
196 $common_widget = Plugin::$instance->widgets_manager->get_widget_types( 'common' );
197
198 $stack['controls'] = array_merge( $stack['controls'], $common_widget->get_controls() );
199
200 $stack['tabs'] = array_merge( $stack['tabs'], $common_widget->get_tabs_controls() );
201 }
202
203 return $stack;
204 }
205
206 /**
207 * Get widget controls pointer index.
208 *
209 * Retrieve widget pointer index where the next control should be added.
210 *
211 * While using injection point, it will return the injection point index. Otherwise index of the last control of the
212 * current widget itself without the common controls, plus one.
213 *
214 * @since 1.9.2
215 * @access public
216 *
217 * @return int Widget controls pointer index.
218 */
219 public function get_pointer_index() {
220 $injection_point = $this->get_injection_point();
221
222 if ( null !== $injection_point ) {
223 return $injection_point['index'];
224 }
225
226 return count( $this->get_stack( false )['controls'] );
227 }
228
229 /**
230 * Show in panel.
231 *
232 * Whether to show the widget in the panel or not. By default returns true.
233 *
234 * @since 1.0.0
235 * @access public
236 *
237 * @return bool Whether to show the widget in the panel or not.
238 */
239 public function show_in_panel() {
240 return true;
241 }
242
243 /**
244 * Hide on search.
245 *
246 * Whether to hide the widget on search in the panel or not. By default returns false.
247 *
248 * @access public
249 *
250 * @return bool Whether to hide the widget when searching for widget or not.
251 */
252 public function hide_on_search() {
253 return false;
254 }
255
256 /**
257 * Start widget controls section.
258 *
259 * Used to add a new section of controls to the widget. Regular controls and
260 * skin controls.
261 *
262 * Note that when you add new controls to widgets they must be wrapped by
263 * `start_controls_section()` and `end_controls_section()`.
264 *
265 * @since 1.0.0
266 * @access public
267 *
268 * @param string $section_id Section ID.
269 * @param array $args Section arguments Optional.
270 */
271 public function start_controls_section( $section_id, array $args = [] ) {
272 parent::start_controls_section( $section_id, $args );
273
274 if ( $this->is_first_section ) {
275 $this->register_skin_control();
276
277 $this->is_first_section = false;
278 }
279 }
280
281 /**
282 * Register the Skin Control if the widget has skins.
283 *
284 * An internal method that is used to add a skin control to the widget.
285 * Added at the top of the controls section.
286 *
287 * @since 2.0.0
288 * @access private
289 */
290 private function register_skin_control() {
291 $skins = $this->get_skins();
292 if ( ! empty( $skins ) ) {
293 $skin_options = [];
294
295 if ( $this->_has_template_content ) {
296 $skin_options[''] = esc_html__( 'Default', 'elementor' );
297 }
298
299 foreach ( $skins as $skin_id => $skin ) {
300 $skin_options[ $skin_id ] = $skin->get_title();
301 }
302
303 // Get the first item for default value
304 $default_value = array_keys( $skin_options );
305 $default_value = array_shift( $default_value );
306
307 if ( 1 >= count( $skin_options ) ) {
308 $this->add_control(
309 '_skin',
310 [
311 'label' => esc_html__( 'Skin', 'elementor' ),
312 'type' => Controls_Manager::HIDDEN,
313 'default' => $default_value,
314 ]
315 );
316 } else {
317 $this->add_control(
318 '_skin',
319 [
320 'label' => esc_html__( 'Skin', 'elementor' ),
321 'type' => Controls_Manager::SELECT,
322 'default' => $default_value,
323 'options' => $skin_options,
324 ]
325 );
326 }
327 }
328 }
329
330 /**
331 * Register widget skins - deprecated prefixed method
332 *
333 * @since 1.7.12
334 * @access protected
335 * @deprecated 3.1.0 Use `register_skins()` method instead.
336 */
337 protected function _register_skins() {
338 Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( __METHOD__, '3.1.0', 'register_skins()' );
339
340 $this->register_skins();
341 }
342
343 /**
344 * Register widget skins.
345 *
346 * This method is activated while initializing the widget base class. It is
347 * used to assign skins to widgets with `add_skin()` method.
348 *
349 * Usage:
350 *
351 * protected function register_skins() {
352 * $this->add_skin( new Skin_Classic( $this ) );
353 * }
354 *
355 * @since 3.1.0
356 * @access protected
357 */
358 protected function register_skins() {}
359
360 /**
361 * Get initial config.
362 *
363 * Retrieve the current widget initial configuration.
364 *
365 * Adds more configuration on top of the controls list, the tabs assigned to
366 * the control, element name, type, icon and more. This method also adds
367 * widget type, keywords and categories.
368 *
369 * @since 2.9.0
370 * @access protected
371 *
372 * @return array The initial widget config.
373 */
374 protected function get_initial_config() {
375 $config = [
376 'widget_type' => $this->get_name(),
377 'keywords' => $this->get_keywords(),
378 'categories' => $this->get_categories(),
379 'html_wrapper_class' => $this->get_html_wrapper_class(),
380 'show_in_panel' => $this->show_in_panel(),
381 'hide_on_search' => $this->hide_on_search(),
382 'upsale_data' => $this->get_upsale_data(),
383 ];
384
385 $config['upsale_data'] = apply_filters( 'elementor/widgets/' . $this->get_name() . '/custom_promotion', $config['upsale_data'] ) ?? $this->get_upsale_data();
386
387 if ( isset( $config['upsale_data']['image'] ) ) {
388 $config['upsale_data']['image'] = esc_url( $config['upsale_data']['image'] );
389 }
390
391 $stack = Plugin::$instance->controls_manager->get_element_stack( $this );
392
393 if ( $stack ) {
394 $config['controls'] = $this->get_stack( false )['controls'];
395 $config['tabs_controls'] = $this->get_tabs_controls();
396 }
397 if ( isset( $config['upsale_data']['upgrade_url'] ) && false === Validate_Promotion::domain_is_on_elementor_dot_com( $config['upsale_data']['upgrade_url'] ) ) {
398 $config['upsale_data']['upgrade_url'] = esc_url( $this->get_upsale_data()['upgrade_url'] );
399 }
400
401 return array_replace_recursive( parent::get_initial_config(), $config );
402 }
403
404 /**
405 * @since 2.3.1
406 * @access protected
407 */
408 protected function should_print_empty() {
409 return false;
410 }
411
412 /**
413 * Print widget content template.
414 *
415 * Used to generate the widget content template on the editor, using a
416 * Backbone JavaScript template.
417 *
418 * @since 2.0.0
419 * @access protected
420 *
421 * @param string $template_content Template content.
422 */
423 protected function print_template_content( $template_content ) {
424 ?>
425 <div class="elementor-widget-container">
426 <?php
427 echo $template_content; // XSS ok.
428 ?>
429 </div>
430 <?php
431 }
432
433 /**
434 * Parse text editor.
435 *
436 * Parses the content from rich text editor with shortcodes, oEmbed and
437 * filtered data.
438 *
439 * @since 1.0.0
440 * @access protected
441 *
442 * @param string $content Text editor content.
443 *
444 * @return string Parsed content.
445 */
446 protected function parse_text_editor( $content ) {
447 /** This filter is documented in wp-includes/widgets/class-wp-widget-text.php */
448 $content = apply_filters( 'widget_text', $content, $this->get_settings() );
449
450 $content = shortcode_unautop( $content );
451 $content = do_shortcode( $content );
452 $content = wptexturize( $content );
453
454 if ( $GLOBALS['wp_embed'] instanceof \WP_Embed ) {
455 $content = $GLOBALS['wp_embed']->autoembed( $content );
456 }
457
458 return $content;
459 }
460
461 /**
462 * Safe print parsed text editor.
463 *
464 * @uses static::parse_text_editor.
465 *
466 * @access protected
467 *
468 * @param string $content Text editor content.
469 */
470 final protected function print_text_editor( $content ) {
471 // PHPCS - the method `parse_text_editor` is safe.
472 echo static::parse_text_editor( $content ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
473 }
474
475 /**
476 * Get HTML wrapper class.
477 *
478 * Retrieve the widget container class. Can be used to override the
479 * container class for specific widgets.
480 *
481 * @since 2.0.9
482 * @access protected
483 */
484 protected function get_html_wrapper_class() {
485 return 'elementor-widget-' . $this->get_name();
486 }
487
488 /**
489 * Add widget render attributes.
490 *
491 * Used to add attributes to the current widget wrapper HTML tag.
492 *
493 * @since 1.0.0
494 * @access protected
495 */
496 protected function add_render_attributes() {
497 parent::add_render_attributes();
498
499 $this->add_render_attribute(
500 '_wrapper', 'class', [
501 'elementor-widget',
502 $this->get_html_wrapper_class(),
503 ]
504 );
505
506 $settings = $this->get_settings();
507
508 $this->add_render_attribute( '_wrapper', 'data-widget_type', $this->get_name() . '.' . ( ! empty( $settings['_skin'] ) ? $settings['_skin'] : 'default' ) );
509 }
510
511 /**
512 * Add lightbox data to image link.
513 *
514 * Used to add lightbox data attributes to image link HTML.
515 *
516 * @since 2.9.1
517 * @access public
518 *
519 * @param string $link_html Image link HTML.
520 * @param string $id Attachment id.
521 *
522 * @return string Image link HTML with lightbox data attributes.
523 */
524 public function add_lightbox_data_to_image_link( $link_html, $id ) {
525 $settings = $this->get_settings_for_display();
526 $open_lightbox = isset( $settings['open_lightbox'] ) ? $settings['open_lightbox'] : null;
527
528 if ( Plugin::$instance->editor->is_edit_mode() ) {
529 $this->add_render_attribute( 'link', 'class', 'elementor-clickable', true );
530 }
531
532 $this->add_lightbox_data_attributes( 'link', $id, $open_lightbox, $this->get_id(), true );
533 return preg_replace( '/^<a/', '<a ' . $this->get_render_attribute_string( 'link' ), $link_html );
534 }
535
536 /**
537 * Add Light-Box attributes.
538 *
539 * Used to add Light-Box-related data attributes to links that open media files.
540 *
541 * @param array|string $element The link HTML element.
542 * @param int $id The ID of the image
543 * @param string $lightbox_setting_key The setting key that dictates weather to open the image in a lightbox
544 * @param string $group_id Unique ID for a group of lightbox images
545 * @param bool $overwrite Optional. Whether to overwrite existing
546 * attribute. Default is false, not to overwrite.
547 *
548 * @return Widget_Base Current instance of the widget.
549 * @since 2.9.0
550 * @access public
551 *
552 */
553 public function add_lightbox_data_attributes( $element, $id = null, $lightbox_setting_key = null, $group_id = null, $overwrite = false ) {
554 $kit = Plugin::$instance->kits_manager->get_active_kit();
555
556 $is_global_image_lightbox_enabled = 'yes' === $kit->get_settings( 'global_image_lightbox' );
557
558 if ( 'no' === $lightbox_setting_key ) {
559 if ( $is_global_image_lightbox_enabled ) {
560 $this->add_render_attribute( $element, 'data-elementor-open-lightbox', 'no', $overwrite );
561 }
562
563 return $this;
564 }
565
566 if ( 'yes' !== $lightbox_setting_key && ! $is_global_image_lightbox_enabled ) {
567 return $this;
568 }
569
570 $attributes['data-elementor-open-lightbox'] = 'yes';
571
572 $action_hash_params = [];
573
574 if ( $id ) {
575 $action_hash_params['id'] = $id;
576 $action_hash_params['url'] = wp_get_attachment_url( $id );
577 }
578
579 if ( $group_id ) {
580 $attributes['data-elementor-lightbox-slideshow'] = $group_id;
581
582 $action_hash_params['slideshow'] = $group_id;
583 }
584
585 if ( $id ) {
586 $lightbox_image_attributes = Plugin::$instance->images_manager->get_lightbox_image_attributes( $id );
587
588 if ( isset( $lightbox_image_attributes['title'] ) ) {
589 $attributes['data-elementor-lightbox-title'] = $lightbox_image_attributes['title'];
590 }
591
592 if ( isset( $lightbox_image_attributes['description'] ) ) {
593 $attributes['data-elementor-lightbox-description'] = $lightbox_image_attributes['description'];
594 }
595 }
596
597 $attributes['data-e-action-hash'] = Plugin::instance()->frontend->create_action_hash( 'lightbox', $action_hash_params );
598
599 $this->add_render_attribute( $element, $attributes, null, $overwrite );
600
601 return $this;
602 }
603
604 /**
605 * Render widget output on the frontend.
606 *
607 * Used to generate the final HTML displayed on the frontend.
608 *
609 * Note that if skin is selected, it will be rendered by the skin itself,
610 * not the widget.
611 *
612 * @since 1.0.0
613 * @access public
614 */
615 public function render_content() {
616 /**
617 * Before widget render content.
618 *
619 * Fires before Elementor widget is being rendered.
620 *
621 * @since 1.0.0
622 *
623 * @param Widget_Base $this The current widget.
624 */
625 do_action( 'elementor/widget/before_render_content', $this );
626
627 ob_start();
628
629 $skin = $this->get_current_skin();
630 if ( $skin ) {
631 $skin->set_parent( $this );
632 $skin->render_by_mode();
633 } else {
634 $this->render_by_mode();
635 }
636
637 $widget_content = ob_get_clean();
638
639 if ( empty( $widget_content ) ) {
640 return;
641 }
642 ?>
643 <div class="elementor-widget-container">
644 <?php
645 if ( $this->is_widget_first_render( $this->get_group_name() ) ) {
646 $this->register_runtime_widget( $this->get_group_name() );
647 }
648
649 $this->print_widget_css();
650
651 // get_name
652
653 /**
654 * Render widget content.
655 *
656 * Filters the widget content before it's rendered.
657 *
658 * @since 1.0.0
659 *
660 * @param string $widget_content The content of the widget.
661 * @param Widget_Base $this The widget.
662 */
663 $widget_content = apply_filters( 'elementor/widget/render_content', $widget_content, $this );
664
665 echo $widget_content; // XSS ok.
666 ?>
667 </div>
668 <?php
669 }
670
671 protected function is_widget_first_render( $widget_name ) {
672 return ! in_array( $widget_name, self::$registered_runtime_widgets, true );
673 }
674
675 /**
676 * Render widget plain content.
677 *
678 * Elementor saves the page content in a unique way, but it's not the way
679 * WordPress saves data. This method is used to save generated HTML to the
680 * database as plain content the WordPress way.
681 *
682 * When rendering plain content, it allows other WordPress plugins to
683 * interact with the content - to search, check SEO and other purposes. It
684 * also allows the site to keep working even if Elementor is deactivated.
685 *
686 * Note that if the widget uses shortcodes to display the data, the best
687 * practice is to return the shortcode itself.
688 *
689 * Also note that if the widget don't display any content it should return
690 * an empty string. For example Elementor Pro Form Widget uses this method
691 * to return an empty string because there is no content to return. This way
692 * if Elementor Pro will be deactivated there won't be any form to display.
693 *
694 * @since 1.0.0
695 * @access public
696 */
697 public function render_plain_content() {
698 $this->render_content();
699 }
700
701 /**
702 * Before widget rendering.
703 *
704 * Used to add stuff before the widget `_wrapper` element.
705 *
706 * @since 1.0.0
707 * @access public
708 */
709 public function before_render() {
710 ?>
711 <div <?php $this->print_render_attribute_string( '_wrapper' ); ?>>
712 <?php
713 }
714
715 /**
716 * After widget rendering.
717 *
718 * Used to add stuff after the widget `_wrapper` element.
719 *
720 * @since 1.0.0
721 * @access public
722 */
723 public function after_render() {
724 ?>
725 </div>
726 <?php
727 }
728
729 /**
730 * Get the element raw data.
731 *
732 * Retrieve the raw element data, including the id, type, settings, child
733 * elements and whether it is an inner element.
734 *
735 * The data with the HTML used always to display the data, but the Elementor
736 * editor uses the raw data without the HTML in order not to render the data
737 * again.
738 *
739 * @since 1.0.0
740 * @access public
741 *
742 * @param bool $with_html_content Optional. Whether to return the data with
743 * HTML content or without. Used for caching.
744 * Default is false, without HTML.
745 *
746 * @return array Element raw data.
747 */
748 public function get_raw_data( $with_html_content = false ) {
749 $data = parent::get_raw_data( $with_html_content );
750
751 unset( $data['isInner'] );
752
753 $data['widgetType'] = $this->get_data( 'widgetType' );
754
755 if ( $with_html_content ) {
756 ob_start();
757
758 $this->render_content();
759
760 $data['htmlCache'] = ob_get_clean();
761 }
762
763 return $data;
764 }
765
766 /**
767 * Print widget content.
768 *
769 * Output the widget final HTML on the frontend.
770 *
771 * @since 1.0.0
772 * @access protected
773 */
774 protected function print_content() {
775 $this->render_content();
776 }
777
778 /**
779 * Print a setting content without escaping.
780 *
781 * Script tags are allowed on frontend according to the WP theme securing policy.
782 *
783 * @param string $setting
784 * @param null $repeater_name
785 * @param null $index
786 */
787 final public function print_unescaped_setting( $setting, $repeater_name = null, $index = null ) {
788 if ( $repeater_name ) {
789 $repeater = $this->get_settings_for_display( $repeater_name );
790 $output = $repeater[ $index ][ $setting ];
791 } else {
792 $output = $this->get_settings_for_display( $setting );
793 }
794
795 echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
796 }
797
798 /**
799 * Get default data.
800 *
801 * Retrieve the default widget data. Used to reset the data on initialization.
802 *
803 * @since 1.0.0
804 * @access protected
805 *
806 * @return array Default data.
807 */
808 protected function get_default_data() {
809 $data = parent::get_default_data();
810
811 $data['widgetType'] = '';
812
813 return $data;
814 }
815
816 /**
817 * Get default child type.
818 *
819 * Retrieve the widget child type based on element data.
820 *
821 * @since 1.0.0
822 * @access protected
823 *
824 * @param array $element_data Widget ID.
825 *
826 * @return array|false Child type or false if it's not a valid widget.
827 */
828 protected function _get_default_child_type( array $element_data ) {
829 return Plugin::$instance->elements_manager->get_element_types( 'section' );
830 }
831
832 /**
833 * Get repeater setting key.
834 *
835 * Retrieve the unique setting key for the current repeater item. Used to connect the current element in the
836 * repeater to it's settings model and it's control in the panel.
837 *
838 * PHP usage (inside `Widget_Base::render()` method):
839 *
840 * $tabs = $this->get_settings( 'tabs' );
841 * foreach ( $tabs as $index => $item ) {
842 * $tab_title_setting_key = $this->get_repeater_setting_key( 'tab_title', 'tabs', $index );
843 * $this->add_inline_editing_attributes( $tab_title_setting_key, 'none' );
844 * echo '<div ' . $this->get_render_attribute_string( $tab_title_setting_key ) . '>' . $item['tab_title'] . '</div>';
845 * }
846 *
847 * @since 1.8.0
848 * @access protected
849 *
850 * @param string $setting_key The current setting key inside the repeater item (e.g. `tab_title`).
851 * @param string $repeater_key The repeater key containing the array of all the items in the repeater (e.g. `tabs`).
852 * @param int $repeater_item_index The current item index in the repeater array (e.g. `3`).
853 *
854 * @return string The repeater setting key (e.g. `tabs.3.tab_title`).
855 */
856 protected function get_repeater_setting_key( $setting_key, $repeater_key, $repeater_item_index ) {
857 return implode( '.', [ $repeater_key, $repeater_item_index, $setting_key ] );
858 }
859
860 /**
861 * Add inline editing attributes.
862 *
863 * Define specific area in the element to be editable inline. The element can have several areas, with this method
864 * you can set the area inside the element that can be edited inline. You can also define the type of toolbar the
865 * user will see, whether it will be a basic toolbar or an advanced one.
866 *
867 * Note: When you use wysiwyg control use the advanced toolbar, with textarea control use the basic toolbar. Text
868 * control should not have toolbar.
869 *
870 * PHP usage (inside `Widget_Base::render()` method):
871 *
872 * $this->add_inline_editing_attributes( 'text', 'advanced' );
873 * echo '<div ' . $this->get_render_attribute_string( 'text' ) . '>' . $this->get_settings( 'text' ) . '</div>';
874 *
875 * @since 1.8.0
876 * @access protected
877 *
878 * @param string $key Element key.
879 * @param string $toolbar Optional. Toolbar type. Accepted values are `advanced`, `basic` or `none`. Default is
880 * `basic`.
881 */
882 protected function add_inline_editing_attributes( $key, $toolbar = 'basic' ) {
883 if ( ! Plugin::$instance->editor->is_edit_mode() ) {
884 return;
885 }
886
887 $this->add_render_attribute( $key, [
888 'class' => 'elementor-inline-editing',
889 'data-elementor-setting-key' => $key,
890 ] );
891
892 if ( 'basic' !== $toolbar ) {
893 $this->add_render_attribute( $key, [
894 'data-elementor-inline-editing-toolbar' => $toolbar,
895 ] );
896 }
897 }
898
899 /**
900 * Add new skin.
901 *
902 * Register new widget skin to allow the user to set custom designs. Must be
903 * called inside the `register_skins()` method.
904 *
905 * @since 1.0.0
906 * @access public
907 *
908 * @param Skin_Base $skin Skin instance.
909 */
910 public function add_skin( Skin_Base $skin ) {
911 Plugin::$instance->skins_manager->add_skin( $this, $skin );
912 }
913
914 /**
915 * Get single skin.
916 *
917 * Retrieve a single skin based on skin ID, from all the skin assigned to
918 * the widget. If the skin does not exist or not assigned to the widget,
919 * return false.
920 *
921 * @since 1.0.0
922 * @access public
923 *
924 * @param string $skin_id Skin ID.
925 *
926 * @return string|false Single skin, or false.
927 */
928 public function get_skin( $skin_id ) {
929 $skins = $this->get_skins();
930 if ( isset( $skins[ $skin_id ] ) ) {
931 return $skins[ $skin_id ];
932 }
933
934 return false;
935 }
936
937 /**
938 * Get current skin ID.
939 *
940 * Retrieve the ID of the current skin.
941 *
942 * @since 1.0.0
943 * @access public
944 *
945 * @return string Current skin.
946 */
947 public function get_current_skin_id() {
948 return $this->get_settings( '_skin' );
949 }
950
951 /**
952 * Get current skin.
953 *
954 * Retrieve the current skin, or if non exist return false.
955 *
956 * @since 1.0.0
957 * @access public
958 *
959 * @return Skin_Base|false Current skin or false.
960 */
961 public function get_current_skin() {
962 return $this->get_skin( $this->get_current_skin_id() );
963 }
964
965 /**
966 * Remove widget skin.
967 *
968 * Unregister an existing skin and remove it from the widget.
969 *
970 * @since 1.0.0
971 * @access public
972 *
973 * @param string $skin_id Skin ID.
974 *
975 * @return \WP_Error|true Whether the skin was removed successfully from the widget.
976 */
977 public function remove_skin( $skin_id ) {
978 return Plugin::$instance->skins_manager->remove_skin( $this, $skin_id );
979 }
980
981 /**
982 * Get widget skins.
983 *
984 * Retrieve all the skin assigned to the widget.
985 *
986 * @since 1.0.0
987 * @access public
988 *
989 * @return Skin_Base[]
990 */
991 public function get_skins() {
992 return Plugin::$instance->skins_manager->get_skins( $this );
993 }
994
995 /**
996 * Get group name.
997 *
998 * Some widgets need to use group names, this method allows you to create them.
999 * By default it retrieves the regular name.
1000 *
1001 * @since 3.3.0
1002 * @access public
1003 *
1004 * @return string Unique name.
1005 */
1006 public function get_group_name() {
1007 return $this->get_name();
1008 }
1009
1010 /**
1011 * Get Inline CSS dependencies.
1012 *
1013 * Retrieve a list of inline CSS dependencies that the element requires.
1014 *
1015 * @since 3.3.0
1016 * @access public
1017 *
1018 * @return array.
1019 */
1020 public function get_inline_css_depends() {
1021 return [];
1022 }
1023
1024 /**
1025 * @param string $plugin_title Plugin's title
1026 * @param string $since Plugin version widget was deprecated
1027 * @param string $last Plugin version in which the widget will be removed
1028 * @param string $replacement Widget replacement
1029 */
1030 protected function deprecated_notice( $plugin_title, $since, $last = '', $replacement = '' ) {
1031 $this->start_controls_section(
1032 'Deprecated',
1033 [
1034 'label' => esc_html__( 'Deprecated', 'elementor' ),
1035 ]
1036 );
1037
1038 $this->add_control(
1039 'deprecated_notice',
1040 [
1041 'type' => Controls_Manager::DEPRECATED_NOTICE,
1042 'widget' => $this->get_title(),
1043 'since' => $since,
1044 'last' => $last,
1045 'plugin' => $plugin_title,
1046 'replacement' => $replacement,
1047 ]
1048 );
1049
1050 $this->end_controls_section();
1051 }
1052
1053 /**
1054 * Init controls.
1055 *
1056 * Reset the `is_first_section` flag to true, so when the Stacks are cleared
1057 * all the controls will be registered again with their skins and settings.
1058 *
1059 * @since 3.14.0
1060 * @access protected
1061 */
1062 protected function init_controls() {
1063 $this->is_first_section = true;
1064 parent::init_controls();
1065 }
1066
1067 public function register_runtime_widget( $widget_name ) {
1068 self::$registered_runtime_widgets[] = $widget_name;
1069 }
1070
1071 public function get_widget_css_config( $widget_name ) {
1072 $direction = is_rtl() ? '-rtl' : '';
1073
1074 $has_custom_breakpoints = $this->is_custom_breakpoints_widget();
1075
1076 $file_name = 'widget-' . $widget_name . $direction . '.min.css';
1077
1078 // The URL of the widget's external CSS file that is loaded in case that the CSS content is too large to be printed inline.
1079 $file_url = Plugin::$instance->frontend->get_frontend_file_url( $file_name, $has_custom_breakpoints );
1080
1081 // The local path of the widget's CSS file that is being read and saved in the DB when the CSS content should be printed inline.
1082 $file_path = Plugin::$instance->frontend->get_frontend_file_path( $file_name, $has_custom_breakpoints );
1083
1084 return [
1085 'key' => $widget_name,
1086 'version' => ELEMENTOR_VERSION,
1087 'file_path' => $file_path,
1088 'data' => [
1089 'file_url' => $file_url,
1090 ],
1091 ];
1092 }
1093
1094 public function get_css_config() {
1095 return $this->get_widget_css_config( $this->get_group_name() );
1096 }
1097
1098 public function get_responsive_widgets_config() {
1099 $responsive_widgets_data_manager = $this->get_responsive_widgets_data_manager();
1100
1101 return [
1102 'key' => $responsive_widgets_data_manager::RESPONSIVE_WIDGETS_DATABASE_KEY,
1103 'version' => ELEMENTOR_VERSION,
1104 'file_path' => ELEMENTOR_ASSETS_PATH . $responsive_widgets_data_manager::RESPONSIVE_WIDGETS_FILE_PATH,
1105 ];
1106 }
1107
1108 public function get_responsive_widgets() {
1109 $responsive_widgets_data_manager = $this->get_responsive_widgets_data_manager();
1110
1111 $config = $this->get_responsive_widgets_config();
1112
1113 return $responsive_widgets_data_manager->get_asset_data_from_config( $config );
1114 }
1115
1116 /**
1117 * Mark widget as deprecated.
1118 *
1119 * Use `get_deprecation_message()` method to print the message control at specific location in register_controls().
1120 *
1121 * @param $version string The version of Elementor that deprecated the widget.
1122 * @param $message string A message regarding the deprecation.
1123 * @param $replacement string The widget that should be used instead.
1124 */
1125 protected function add_deprecation_message( $version, $message, $replacement ) {
1126 // Expose the config for handling in JS.
1127 $this->set_config( 'deprecation', [
1128 'version' => $version,
1129 'message' => $message,
1130 'replacement' => $replacement,
1131 ] );
1132
1133 $this->add_control(
1134 'deprecation_message',
1135 [
1136 'type' => Controls_Manager::ALERT,
1137 'alert_type' => 'info',
1138 'content' => $message,
1139 'separator' => 'after',
1140 ]
1141 );
1142 }
1143
1144 /**
1145 * Get Responsive Widgets Data Manager.
1146 *
1147 * Retrieve the data manager that handles widgets that are using media queries for custom-breakpoints values.
1148 *
1149 * @since 3.5.0
1150 * @access protected
1151 *
1152 * @return Responsive_Widgets_Data_Manager
1153 */
1154 protected function get_responsive_widgets_data_manager() {
1155 if ( ! self::$responsive_widgets_data_manager ) {
1156 self::$responsive_widgets_data_manager = new Responsive_Widgets_Data_Manager();
1157 }
1158
1159 return self::$responsive_widgets_data_manager;
1160 }
1161
1162 /**
1163 * Is Custom Breakpoints Widget.
1164 *
1165 * Checking if there are active custom-breakpoints and if the widget use them.
1166 *
1167 * @since 3.5.0
1168 * @access protected
1169 *
1170 * @return boolean
1171 */
1172 protected function is_custom_breakpoints_widget() {
1173 $has_custom_breakpoints = Plugin::$instance->breakpoints->has_custom_breakpoints();
1174
1175 if ( $has_custom_breakpoints ) {
1176 $responsive_widgets = $this->get_responsive_widgets();
1177
1178 // The $widget_name can also represents a widgets group name, therefore we need to use the current widget name to check if it's responsive widget.
1179 $current_widget_name = $this->get_name();
1180
1181 // If the widget is not implementing custom-breakpoints media queries then it has no custom- css file.
1182 if ( ! isset( $responsive_widgets[ $current_widget_name ] ) ) {
1183 $has_custom_breakpoints = false;
1184 }
1185 }
1186
1187 return $has_custom_breakpoints;
1188 }
1189
1190 private function get_widget_css() {
1191 $widgets_css_data_manager = $this->get_widgets_css_data_manager();
1192
1193 $widgets_list = $this->get_inline_css_depends();
1194
1195 $widgets_list[] = $this->get_group_name();
1196
1197 $widget_css = '';
1198
1199 foreach ( $widgets_list as $widget_data ) {
1200 $widget_name = isset( $widget_data['name'] ) ? $widget_data['name'] : $widget_data;
1201
1202 if ( ! in_array( $widget_name, self::$registered_inline_css_widgets, true ) ) {
1203 if ( $this->get_group_name() === $widget_name ) {
1204 $config = $this->get_css_config();
1205 } else {
1206 /**
1207 * The core-dependency allowing to create a dependency specifically with the core widgets.
1208 * Otherwise, the config will be taken from the class that inherits from Widget_Base.
1209 */
1210 $is_core_dependency = isset( $widget_data['is_core_dependency'] ) ? true : false;
1211
1212 $config = $is_core_dependency ? self::get_widget_css_config( $widget_name ) : $this->get_widget_css_config( $widget_name );
1213 }
1214
1215 $widget_css .= $widgets_css_data_manager->get_asset_data_from_config( $config );
1216
1217 self::$registered_inline_css_widgets[] = $widget_name;
1218 }
1219 }
1220
1221 return $widget_css;
1222
1223 }
1224
1225 private function is_inline_css_mode() {
1226 static $is_active;
1227
1228 if ( null === $is_active ) {
1229 $is_edit_mode = Plugin::$instance->editor->is_edit_mode();
1230 $is_preview_mode = Plugin::$instance->preview->is_preview_mode();
1231 $is_optimized_mode = Plugin::$instance->experiments->is_feature_active( 'e_optimized_css_loading' );
1232
1233 $is_active = ( Utils::is_script_debug() || $is_edit_mode || $is_preview_mode || ! $is_optimized_mode ) ? false : true;
1234 }
1235
1236 return $is_active;
1237 }
1238
1239 private function print_widget_css() {
1240 if ( ! $this->is_inline_css_mode() ) {
1241 return;
1242 }
1243
1244 Utils::print_unescaped_internal_string( $this->get_widget_css() );
1245 }
1246
1247 private function get_widgets_css_data_manager() {
1248 if ( ! self::$widgets_css_data_manager ) {
1249 self::$widgets_css_data_manager = new Widgets_Css_Data_Manager();
1250 }
1251
1252 return self::$widgets_css_data_manager;
1253 }
1254 }
1255