PluginProbe
Elementor Website Builder – more than just a page builder / 3.13.3
Elementor Website Builder – more than just a page builder v3.13.3
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / includes / base / widget-base.php

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

1,216 lines 32.1 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 private $is_first_section = true;
38
39 /**
40 * Registered Runtime Widgets.
41 *
42 * Registering in runtime all widgets that are being used on the page.
43 *
44 * @since 3.3.0
45 * @access public
46 * @static
47 *
48 * @var array
49 */
50 public static $registered_runtime_widgets = [];
51
52 public static $registered_inline_css_widgets = [];
53
54 private static $widgets_css_data_manager;
55
56 private static $responsive_widgets_data_manager;
57
58 /**
59 * Get element type.
60 *
61 * Retrieve the element type, in this case `widget`.
62 *
63 * @since 1.0.0
64 * @access public
65 * @static
66 *
67 * @return string The type.
68 */
69 public static function get_type() {
70 return 'widget';
71 }
72
73 /**
74 * Get widget icon.
75 *
76 * Retrieve the widget icon.
77 *
78 * @since 1.0.0
79 * @access public
80 *
81 * @return string Widget icon.
82 */
83 public function get_icon() {
84 return 'eicon-apps';
85 }
86
87 /**
88 * Get widget keywords.
89 *
90 * Retrieve the widget keywords.
91 *
92 * @since 1.0.10
93 * @access public
94 *
95 * @return array Widget keywords.
96 */
97 public function get_keywords() {
98 return [];
99 }
100
101 /**
102 * Get widget categories.
103 *
104 * Retrieve the widget categories.
105 *
106 * @since 1.0.10
107 * @access public
108 *
109 * @return array Widget categories.
110 */
111 public function get_categories() {
112 return [ 'general' ];
113 }
114
115 /**
116 * Widget base constructor.
117 *
118 * Initializing the widget base class.
119 *
120 * @since 1.0.0
121 * @access public
122 *
123 * @throws \Exception If arguments are missing when initializing a full widget
124 * instance.
125 *
126 * @param array $data Widget data. Default is an empty array.
127 * @param array|null $args Optional. Widget default arguments. Default is null.
128 */
129 public function __construct( $data = [], $args = null ) {
130 parent::__construct( $data, $args );
131
132 $is_type_instance = $this->is_type_instance();
133
134 if ( ! $is_type_instance && null === $args ) {
135 throw new \Exception( 'An `$args` argument is required when initializing a full widget instance.' );
136 }
137
138 if ( $is_type_instance ) {
139 if ( $this->has_own_method( '_register_skins', self::class ) ) {
140 Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( '_register_skins', '3.1.0', __CLASS__ . '::register_skins()' );
141
142 $this->_register_skins();
143 } else {
144 $this->register_skins();
145 }
146
147 $widget_name = $this->get_name();
148
149 /**
150 * Widget skin init.
151 *
152 * Fires when Elementor widget is being initialized.
153 *
154 * The dynamic portion of the hook name, `$widget_name`, refers to the widget name.
155 *
156 * @since 1.0.0
157 *
158 * @param Widget_Base $this The current widget.
159 */
160 do_action( "elementor/widget/{$widget_name}/skins_init", $this );
161 }
162 }
163
164 /**
165 * Get stack.
166 *
167 * Retrieve the widget stack of controls.
168 *
169 * @since 1.9.2
170 * @access public
171 *
172 * @param bool $with_common_controls Optional. Whether to include the common controls. Default is true.
173 *
174 * @return array Widget stack of controls.
175 */
176 public function get_stack( $with_common_controls = true ) {
177 $stack = parent::get_stack();
178
179 if ( $with_common_controls && 'common' !== $this->get_unique_name() ) {
180 /** @var Widget_Common $common_widget */
181 $common_widget = Plugin::$instance->widgets_manager->get_widget_types( 'common' );
182
183 $stack['controls'] = array_merge( $stack['controls'], $common_widget->get_controls() );
184
185 $stack['tabs'] = array_merge( $stack['tabs'], $common_widget->get_tabs_controls() );
186 }
187
188 return $stack;
189 }
190
191 /**
192 * Get widget controls pointer index.
193 *
194 * Retrieve widget pointer index where the next control should be added.
195 *
196 * While using injection point, it will return the injection point index. Otherwise index of the last control of the
197 * current widget itself without the common controls, plus one.
198 *
199 * @since 1.9.2
200 * @access public
201 *
202 * @return int Widget controls pointer index.
203 */
204 public function get_pointer_index() {
205 $injection_point = $this->get_injection_point();
206
207 if ( null !== $injection_point ) {
208 return $injection_point['index'];
209 }
210
211 return count( $this->get_stack( false )['controls'] );
212 }
213
214 /**
215 * Show in panel.
216 *
217 * Whether to show the widget in the panel or not. By default returns true.
218 *
219 * @since 1.0.0
220 * @access public
221 *
222 * @return bool Whether to show the widget in the panel or not.
223 */
224 public function show_in_panel() {
225 return true;
226 }
227
228 /**
229 * Hide on search.
230 *
231 * Whether to hide the widget on search in the panel or not. By default returns false.
232 *
233 * @access public
234 *
235 * @return bool Whether to hide the widget when searching for widget or not.
236 */
237 public function hide_on_search() {
238 return false;
239 }
240
241 /**
242 * Start widget controls section.
243 *
244 * Used to add a new section of controls to the widget. Regular controls and
245 * skin controls.
246 *
247 * Note that when you add new controls to widgets they must be wrapped by
248 * `start_controls_section()` and `end_controls_section()`.
249 *
250 * @since 1.0.0
251 * @access public
252 *
253 * @param string $section_id Section ID.
254 * @param array $args Section arguments Optional.
255 */
256 public function start_controls_section( $section_id, array $args = [] ) {
257 parent::start_controls_section( $section_id, $args );
258
259 if ( $this->is_first_section ) {
260 $this->register_skin_control();
261
262 $this->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 $action_hash_params = [];
548
549 if ( $id ) {
550 $action_hash_params['id'] = $id;
551 $action_hash_params['url'] = wp_get_attachment_url( $id );
552 }
553
554 if ( $group_id ) {
555 $attributes['data-elementor-lightbox-slideshow'] = $group_id;
556
557 $action_hash_params['slideshow'] = $group_id;
558 }
559
560 if ( $id ) {
561 $lightbox_image_attributes = Plugin::$instance->images_manager->get_lightbox_image_attributes( $id );
562
563 if ( isset( $lightbox_image_attributes['title'] ) ) {
564 $attributes['data-elementor-lightbox-title'] = $lightbox_image_attributes['title'];
565 }
566
567 if ( isset( $lightbox_image_attributes['description'] ) ) {
568 $attributes['data-elementor-lightbox-description'] = $lightbox_image_attributes['description'];
569 }
570 }
571
572 $attributes['data-e-action-hash'] = Plugin::instance()->frontend->create_action_hash( 'lightbox', $action_hash_params );
573
574 $this->add_render_attribute( $element, $attributes, null, $overwrite );
575
576 return $this;
577 }
578
579 /**
580 * Render widget output on the frontend.
581 *
582 * Used to generate the final HTML displayed on the frontend.
583 *
584 * Note that if skin is selected, it will be rendered by the skin itself,
585 * not the widget.
586 *
587 * @since 1.0.0
588 * @access public
589 */
590 public function render_content() {
591 /**
592 * Before widget render content.
593 *
594 * Fires before Elementor widget is being rendered.
595 *
596 * @since 1.0.0
597 *
598 * @param Widget_Base $this The current widget.
599 */
600 do_action( 'elementor/widget/before_render_content', $this );
601
602 ob_start();
603
604 $skin = $this->get_current_skin();
605 if ( $skin ) {
606 $skin->set_parent( $this );
607 $skin->render_by_mode();
608 } else {
609 $this->render_by_mode();
610 }
611
612 $widget_content = ob_get_clean();
613
614 if ( empty( $widget_content ) ) {
615 return;
616 }
617 ?>
618 <div class="elementor-widget-container">
619 <?php
620 if ( $this->is_widget_first_render( $this->get_group_name() ) ) {
621 $this->register_runtime_widget( $this->get_group_name() );
622 }
623
624 $this->print_widget_css();
625
626 // get_name
627
628 /**
629 * Render widget content.
630 *
631 * Filters the widget content before it's rendered.
632 *
633 * @since 1.0.0
634 *
635 * @param string $widget_content The content of the widget.
636 * @param Widget_Base $this The widget.
637 */
638 $widget_content = apply_filters( 'elementor/widget/render_content', $widget_content, $this );
639
640 echo $widget_content; // XSS ok.
641 ?>
642 </div>
643 <?php
644 }
645
646 protected function is_widget_first_render( $widget_name ) {
647 return ! in_array( $widget_name, self::$registered_runtime_widgets, true );
648 }
649
650 /**
651 * Render widget plain content.
652 *
653 * Elementor saves the page content in a unique way, but it's not the way
654 * WordPress saves data. This method is used to save generated HTML to the
655 * database as plain content the WordPress way.
656 *
657 * When rendering plain content, it allows other WordPress plugins to
658 * interact with the content - to search, check SEO and other purposes. It
659 * also allows the site to keep working even if Elementor is deactivated.
660 *
661 * Note that if the widget uses shortcodes to display the data, the best
662 * practice is to return the shortcode itself.
663 *
664 * Also note that if the widget don't display any content it should return
665 * an empty string. For example Elementor Pro Form Widget uses this method
666 * to return an empty string because there is no content to return. This way
667 * if Elementor Pro will be deactivated there won't be any form to display.
668 *
669 * @since 1.0.0
670 * @access public
671 */
672 public function render_plain_content() {
673 $this->render_content();
674 }
675
676 /**
677 * Before widget rendering.
678 *
679 * Used to add stuff before the widget `_wrapper` element.
680 *
681 * @since 1.0.0
682 * @access public
683 */
684 public function before_render() {
685 ?>
686 <div <?php $this->print_render_attribute_string( '_wrapper' ); ?>>
687 <?php
688 }
689
690 /**
691 * After widget rendering.
692 *
693 * Used to add stuff after the widget `_wrapper` element.
694 *
695 * @since 1.0.0
696 * @access public
697 */
698 public function after_render() {
699 ?>
700 </div>
701 <?php
702 }
703
704 /**
705 * Get the element raw data.
706 *
707 * Retrieve the raw element data, including the id, type, settings, child
708 * elements and whether it is an inner element.
709 *
710 * The data with the HTML used always to display the data, but the Elementor
711 * editor uses the raw data without the HTML in order not to render the data
712 * again.
713 *
714 * @since 1.0.0
715 * @access public
716 *
717 * @param bool $with_html_content Optional. Whether to return the data with
718 * HTML content or without. Used for caching.
719 * Default is false, without HTML.
720 *
721 * @return array Element raw data.
722 */
723 public function get_raw_data( $with_html_content = false ) {
724 $data = parent::get_raw_data( $with_html_content );
725
726 unset( $data['isInner'] );
727
728 $data['widgetType'] = $this->get_data( 'widgetType' );
729
730 if ( $with_html_content ) {
731 ob_start();
732
733 $this->render_content();
734
735 $data['htmlCache'] = ob_get_clean();
736 }
737
738 return $data;
739 }
740
741 /**
742 * Print widget content.
743 *
744 * Output the widget final HTML on the frontend.
745 *
746 * @since 1.0.0
747 * @access protected
748 */
749 protected function print_content() {
750 $this->render_content();
751 }
752
753 /**
754 * Print a setting content without escaping.
755 *
756 * Script tags are allowed on frontend according to the WP theme securing policy.
757 *
758 * @param string $setting
759 * @param null $repeater_name
760 * @param null $index
761 */
762 final public function print_unescaped_setting( $setting, $repeater_name = null, $index = null ) {
763 if ( $repeater_name ) {
764 $repeater = $this->get_settings_for_display( $repeater_name );
765 $output = $repeater[ $index ][ $setting ];
766 } else {
767 $output = $this->get_settings_for_display( $setting );
768 }
769
770 echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
771 }
772
773 /**
774 * Get default data.
775 *
776 * Retrieve the default widget data. Used to reset the data on initialization.
777 *
778 * @since 1.0.0
779 * @access protected
780 *
781 * @return array Default data.
782 */
783 protected function get_default_data() {
784 $data = parent::get_default_data();
785
786 $data['widgetType'] = '';
787
788 return $data;
789 }
790
791 /**
792 * Get default child type.
793 *
794 * Retrieve the widget child type based on element data.
795 *
796 * @since 1.0.0
797 * @access protected
798 *
799 * @param array $element_data Widget ID.
800 *
801 * @return array|false Child type or false if it's not a valid widget.
802 */
803 protected function _get_default_child_type( array $element_data ) {
804 return Plugin::$instance->elements_manager->get_element_types( 'section' );
805 }
806
807 /**
808 * Get repeater setting key.
809 *
810 * Retrieve the unique setting key for the current repeater item. Used to connect the current element in the
811 * repeater to it's settings model and it's control in the panel.
812 *
813 * PHP usage (inside `Widget_Base::render()` method):
814 *
815 * $tabs = $this->get_settings( 'tabs' );
816 * foreach ( $tabs as $index => $item ) {
817 * $tab_title_setting_key = $this->get_repeater_setting_key( 'tab_title', 'tabs', $index );
818 * $this->add_inline_editing_attributes( $tab_title_setting_key, 'none' );
819 * echo '<div ' . $this->get_render_attribute_string( $tab_title_setting_key ) . '>' . $item['tab_title'] . '</div>';
820 * }
821 *
822 * @since 1.8.0
823 * @access protected
824 *
825 * @param string $setting_key The current setting key inside the repeater item (e.g. `tab_title`).
826 * @param string $repeater_key The repeater key containing the array of all the items in the repeater (e.g. `tabs`).
827 * @param int $repeater_item_index The current item index in the repeater array (e.g. `3`).
828 *
829 * @return string The repeater setting key (e.g. `tabs.3.tab_title`).
830 */
831 protected function get_repeater_setting_key( $setting_key, $repeater_key, $repeater_item_index ) {
832 return implode( '.', [ $repeater_key, $repeater_item_index, $setting_key ] );
833 }
834
835 /**
836 * Add inline editing attributes.
837 *
838 * Define specific area in the element to be editable inline. The element can have several areas, with this method
839 * you can set the area inside the element that can be edited inline. You can also define the type of toolbar the
840 * user will see, whether it will be a basic toolbar or an advanced one.
841 *
842 * Note: When you use wysiwyg control use the advanced toolbar, with textarea control use the basic toolbar. Text
843 * control should not have toolbar.
844 *
845 * PHP usage (inside `Widget_Base::render()` method):
846 *
847 * $this->add_inline_editing_attributes( 'text', 'advanced' );
848 * echo '<div ' . $this->get_render_attribute_string( 'text' ) . '>' . $this->get_settings( 'text' ) . '</div>';
849 *
850 * @since 1.8.0
851 * @access protected
852 *
853 * @param string $key Element key.
854 * @param string $toolbar Optional. Toolbar type. Accepted values are `advanced`, `basic` or `none`. Default is
855 * `basic`.
856 */
857 protected function add_inline_editing_attributes( $key, $toolbar = 'basic' ) {
858 if ( ! Plugin::$instance->editor->is_edit_mode() ) {
859 return;
860 }
861
862 $this->add_render_attribute( $key, [
863 'class' => 'elementor-inline-editing',
864 'data-elementor-setting-key' => $key,
865 ] );
866
867 if ( 'basic' !== $toolbar ) {
868 $this->add_render_attribute( $key, [
869 'data-elementor-inline-editing-toolbar' => $toolbar,
870 ] );
871 }
872 }
873
874 /**
875 * Add new skin.
876 *
877 * Register new widget skin to allow the user to set custom designs. Must be
878 * called inside the `register_skins()` method.
879 *
880 * @since 1.0.0
881 * @access public
882 *
883 * @param Skin_Base $skin Skin instance.
884 */
885 public function add_skin( Skin_Base $skin ) {
886 Plugin::$instance->skins_manager->add_skin( $this, $skin );
887 }
888
889 /**
890 * Get single skin.
891 *
892 * Retrieve a single skin based on skin ID, from all the skin assigned to
893 * the widget. If the skin does not exist or not assigned to the widget,
894 * return false.
895 *
896 * @since 1.0.0
897 * @access public
898 *
899 * @param string $skin_id Skin ID.
900 *
901 * @return string|false Single skin, or false.
902 */
903 public function get_skin( $skin_id ) {
904 $skins = $this->get_skins();
905 if ( isset( $skins[ $skin_id ] ) ) {
906 return $skins[ $skin_id ];
907 }
908
909 return false;
910 }
911
912 /**
913 * Get current skin ID.
914 *
915 * Retrieve the ID of the current skin.
916 *
917 * @since 1.0.0
918 * @access public
919 *
920 * @return string Current skin.
921 */
922 public function get_current_skin_id() {
923 return $this->get_settings( '_skin' );
924 }
925
926 /**
927 * Get current skin.
928 *
929 * Retrieve the current skin, or if non exist return false.
930 *
931 * @since 1.0.0
932 * @access public
933 *
934 * @return Skin_Base|false Current skin or false.
935 */
936 public function get_current_skin() {
937 return $this->get_skin( $this->get_current_skin_id() );
938 }
939
940 /**
941 * Remove widget skin.
942 *
943 * Unregister an existing skin and remove it from the widget.
944 *
945 * @since 1.0.0
946 * @access public
947 *
948 * @param string $skin_id Skin ID.
949 *
950 * @return \WP_Error|true Whether the skin was removed successfully from the widget.
951 */
952 public function remove_skin( $skin_id ) {
953 return Plugin::$instance->skins_manager->remove_skin( $this, $skin_id );
954 }
955
956 /**
957 * Get widget skins.
958 *
959 * Retrieve all the skin assigned to the widget.
960 *
961 * @since 1.0.0
962 * @access public
963 *
964 * @return Skin_Base[]
965 */
966 public function get_skins() {
967 return Plugin::$instance->skins_manager->get_skins( $this );
968 }
969
970 /**
971 * Get group name.
972 *
973 * Some widgets need to use group names, this method allows you to create them.
974 * By default it retrieves the regular name.
975 *
976 * @since 3.3.0
977 * @access public
978 *
979 * @return string Unique name.
980 */
981 public function get_group_name() {
982 return $this->get_name();
983 }
984
985 /**
986 * Get Inline CSS dependencies.
987 *
988 * Retrieve a list of inline CSS dependencies that the element requires.
989 *
990 * @since 3.3.0
991 * @access public
992 *
993 * @return array.
994 */
995 public function get_inline_css_depends() {
996 return [];
997 }
998
999 /**
1000 * @param string $plugin_title Plugin's title
1001 * @param string $since Plugin version widget was deprecated
1002 * @param string $last Plugin version in which the widget will be removed
1003 * @param string $replacement Widget replacement
1004 */
1005 protected function deprecated_notice( $plugin_title, $since, $last = '', $replacement = '' ) {
1006 $this->start_controls_section(
1007 'Deprecated',
1008 [
1009 'label' => esc_html__( 'Deprecated', 'elementor' ),
1010 ]
1011 );
1012
1013 $this->add_control(
1014 'deprecated_notice',
1015 [
1016 'type' => Controls_Manager::DEPRECATED_NOTICE,
1017 'widget' => $this->get_title(),
1018 'since' => $since,
1019 'last' => $last,
1020 'plugin' => $plugin_title,
1021 'replacement' => $replacement,
1022 ]
1023 );
1024
1025 $this->end_controls_section();
1026 }
1027
1028 public function register_runtime_widget( $widget_name ) {
1029 self::$registered_runtime_widgets[] = $widget_name;
1030 }
1031
1032 public function get_widget_css_config( $widget_name ) {
1033 $direction = is_rtl() ? '-rtl' : '';
1034
1035 $has_custom_breakpoints = $this->is_custom_breakpoints_widget();
1036
1037 $file_name = 'widget-' . $widget_name . $direction . '.min.css';
1038
1039 // 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.
1040 $file_url = Plugin::$instance->frontend->get_frontend_file_url( $file_name, $has_custom_breakpoints );
1041
1042 // 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.
1043 $file_path = Plugin::$instance->frontend->get_frontend_file_path( $file_name, $has_custom_breakpoints );
1044
1045 return [
1046 'key' => $widget_name,
1047 'version' => ELEMENTOR_VERSION,
1048 'file_path' => $file_path,
1049 'data' => [
1050 'file_url' => $file_url,
1051 ],
1052 ];
1053 }
1054
1055 public function get_css_config() {
1056 return $this->get_widget_css_config( $this->get_group_name() );
1057 }
1058
1059 public function get_responsive_widgets_config() {
1060 $responsive_widgets_data_manager = $this->get_responsive_widgets_data_manager();
1061
1062 return [
1063 'key' => $responsive_widgets_data_manager::RESPONSIVE_WIDGETS_DATABASE_KEY,
1064 'version' => ELEMENTOR_VERSION,
1065 'file_path' => ELEMENTOR_ASSETS_PATH . $responsive_widgets_data_manager::RESPONSIVE_WIDGETS_FILE_PATH,
1066 ];
1067 }
1068
1069 public function get_responsive_widgets() {
1070 $responsive_widgets_data_manager = $this->get_responsive_widgets_data_manager();
1071
1072 $config = $this->get_responsive_widgets_config();
1073
1074 return $responsive_widgets_data_manager->get_asset_data_from_config( $config );
1075 }
1076
1077 /**
1078 * Mark widget as deprecated.
1079 *
1080 * Use `get_deprecation_message()` method to print the message control at specific location in register_controls().
1081 *
1082 * @param $version string The version of Elementor that deprecated the widget.
1083 * @param $message string A message regarding the deprecation.
1084 * @param $replacement string The widget that should be used instead.
1085 */
1086 protected function add_deprecation_message( $version, $message, $replacement ) {
1087 // Expose the config for handling in JS.
1088 $this->set_config( 'deprecation', [
1089 'version' => $version,
1090 'message' => $message,
1091 'replacement' => $replacement,
1092 ] );
1093
1094 $this->add_control(
1095 'deprecation_message',
1096 [
1097 'type' => Controls_Manager::RAW_HTML,
1098 'raw' => $message,
1099 'content_classes' => 'elementor-panel-alert elementor-panel-alert-info',
1100 'separator' => 'after',
1101 ]
1102 );
1103 }
1104
1105 /**
1106 * Get Responsive Widgets Data Manager.
1107 *
1108 * Retrieve the data manager that handles widgets that are using media queries for custom-breakpoints values.
1109 *
1110 * @since 3.5.0
1111 * @access protected
1112 *
1113 * @return Responsive_Widgets_Data_Manager
1114 */
1115 protected function get_responsive_widgets_data_manager() {
1116 if ( ! self::$responsive_widgets_data_manager ) {
1117 self::$responsive_widgets_data_manager = new Responsive_Widgets_Data_Manager();
1118 }
1119
1120 return self::$responsive_widgets_data_manager;
1121 }
1122
1123 /**
1124 * Is Custom Breakpoints Widget.
1125 *
1126 * Checking if there are active custom-breakpoints and if the widget use them.
1127 *
1128 * @since 3.5.0
1129 * @access protected
1130 *
1131 * @return boolean
1132 */
1133 protected function is_custom_breakpoints_widget() {
1134 $has_custom_breakpoints = Plugin::$instance->breakpoints->has_custom_breakpoints();
1135
1136 if ( $has_custom_breakpoints ) {
1137 $responsive_widgets = $this->get_responsive_widgets();
1138
1139 // 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.
1140 $current_widget_name = $this->get_name();
1141
1142 // If the widget is not implementing custom-breakpoints media queries then it has no custom- css file.
1143 if ( ! isset( $responsive_widgets[ $current_widget_name ] ) ) {
1144 $has_custom_breakpoints = false;
1145 }
1146 }
1147
1148 return $has_custom_breakpoints;
1149 }
1150
1151 private function get_widget_css() {
1152 $widgets_css_data_manager = $this->get_widgets_css_data_manager();
1153
1154 $widgets_list = $this->get_inline_css_depends();
1155
1156 $widgets_list[] = $this->get_group_name();
1157
1158 $widget_css = '';
1159
1160 foreach ( $widgets_list as $widget_data ) {
1161 $widget_name = isset( $widget_data['name'] ) ? $widget_data['name'] : $widget_data;
1162
1163 if ( ! in_array( $widget_name, self::$registered_inline_css_widgets, true ) ) {
1164 if ( $this->get_group_name() === $widget_name ) {
1165 $config = $this->get_css_config();
1166 } else {
1167 /**
1168 * The core-dependency allowing to create a dependency specifically with the core widgets.
1169 * Otherwise, the config will be taken from the class that inherits from Widget_Base.
1170 */
1171 $is_core_dependency = isset( $widget_data['is_core_dependency'] ) ? true : false;
1172
1173 $config = $is_core_dependency ? self::get_widget_css_config( $widget_name ) : $this->get_widget_css_config( $widget_name );
1174 }
1175
1176 $widget_css .= $widgets_css_data_manager->get_asset_data_from_config( $config );
1177
1178 self::$registered_inline_css_widgets[] = $widget_name;
1179 }
1180 }
1181
1182 return $widget_css;
1183
1184 }
1185
1186 private function is_inline_css_mode() {
1187 static $is_active;
1188
1189 if ( null === $is_active ) {
1190 $is_edit_mode = Plugin::$instance->editor->is_edit_mode();
1191 $is_preview_mode = Plugin::$instance->preview->is_preview_mode();
1192 $is_optimized_mode = Plugin::$instance->experiments->is_feature_active( 'e_optimized_css_loading' );
1193
1194 $is_active = ( Utils::is_script_debug() || $is_edit_mode || $is_preview_mode || ! $is_optimized_mode ) ? false : true;
1195 }
1196
1197 return $is_active;
1198 }
1199
1200 private function print_widget_css() {
1201 if ( ! $this->is_inline_css_mode() ) {
1202 return;
1203 }
1204
1205 Utils::print_unescaped_internal_string( $this->get_widget_css() );
1206 }
1207
1208 private function get_widgets_css_data_manager() {
1209 if ( ! self::$widgets_css_data_manager ) {
1210 self::$widgets_css_data_manager = new Widgets_Css_Data_Manager();
1211 }
1212
1213 return self::$widgets_css_data_manager;
1214 }
1215 }
1216