PluginProbe
Elementor Website Builder – more than just a page builder / 3.6.0-dev1
Elementor Website Builder – more than just a page builder v3.6.0-dev1
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 / trunk / includes / base / widget-base.php

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

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