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

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