PluginProbe
Elementor Website Builder – more than just a page builder / 3.5.4
Elementor Website Builder – more than just a page builder v3.5.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.5.4, at includes/base/widget-base.php

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