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

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