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

image.php in Elementor Website Builder – more than just a page builder 3.13.1, at includes/widgets/image.php

848 lines 18.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 if ( ! defined( 'ABSPATH' ) ) {
5 exit; // Exit if accessed directly.
6 }
7
8 use Elementor\Core\Kits\Documents\Tabs\Global_Colors;
9 use Elementor\Core\Kits\Documents\Tabs\Global_Typography;
10
11 /**
12 * Elementor image widget.
13 *
14 * Elementor widget that displays an image into the page.
15 *
16 * @since 1.0.0
17 */
18 class Widget_Image extends Widget_Base {
19
20 /**
21 * Get widget name.
22 *
23 * Retrieve image widget name.
24 *
25 * @since 1.0.0
26 * @access public
27 *
28 * @return string Widget name.
29 */
30 public function get_name() {
31 return 'image';
32 }
33
34 /**
35 * Get widget title.
36 *
37 * Retrieve image widget title.
38 *
39 * @since 1.0.0
40 * @access public
41 *
42 * @return string Widget title.
43 */
44 public function get_title() {
45 return esc_html__( 'Image', 'elementor' );
46 }
47
48 /**
49 * Get widget icon.
50 *
51 * Retrieve image widget icon.
52 *
53 * @since 1.0.0
54 * @access public
55 *
56 * @return string Widget icon.
57 */
58 public function get_icon() {
59 return 'eicon-image';
60 }
61
62 /**
63 * Get widget categories.
64 *
65 * Retrieve the list of categories the image widget belongs to.
66 *
67 * Used to determine where to display the widget in the editor.
68 *
69 * @since 2.0.0
70 * @access public
71 *
72 * @return array Widget categories.
73 */
74 public function get_categories() {
75 return [ 'basic' ];
76 }
77
78 /**
79 * Get widget keywords.
80 *
81 * Retrieve the list of keywords the widget belongs to.
82 *
83 * @since 2.1.0
84 * @access public
85 *
86 * @return array Widget keywords.
87 */
88 public function get_keywords() {
89 return [ 'image', 'photo', 'visual' ];
90 }
91
92 /**
93 * Register image widget controls.
94 *
95 * Adds different input fields to allow the user to change and customize the widget settings.
96 *
97 * @since 3.1.0
98 * @access protected
99 */
100 protected function register_controls() {
101 $this->start_controls_section(
102 'section_image',
103 [
104 'label' => esc_html__( 'Image', 'elementor' ),
105 ]
106 );
107
108 $this->add_control(
109 'image',
110 [
111 'label' => esc_html__( 'Choose Image', 'elementor' ),
112 'type' => Controls_Manager::MEDIA,
113 'dynamic' => [
114 'active' => true,
115 ],
116 'default' => [
117 'url' => Utils::get_placeholder_image_src(),
118 ],
119 ]
120 );
121
122 $this->add_group_control(
123 Group_Control_Image_Size::get_type(),
124 [
125 'name' => 'image', // Usage: `{name}_size` and `{name}_custom_dimension`, in this case `image_size` and `image_custom_dimension`.
126 'default' => 'large',
127 'separator' => 'none',
128 ]
129 );
130
131 $this->add_responsive_control(
132 'align',
133 [
134 'label' => esc_html__( 'Alignment', 'elementor' ),
135 'type' => Controls_Manager::CHOOSE,
136 'options' => [
137 'left' => [
138 'title' => esc_html__( 'Left', 'elementor' ),
139 'icon' => 'eicon-text-align-left',
140 ],
141 'center' => [
142 'title' => esc_html__( 'Center', 'elementor' ),
143 'icon' => 'eicon-text-align-center',
144 ],
145 'right' => [
146 'title' => esc_html__( 'Right', 'elementor' ),
147 'icon' => 'eicon-text-align-right',
148 ],
149 ],
150 'selectors' => [
151 '{{WRAPPER}}' => 'text-align: {{VALUE}};',
152 ],
153 ]
154 );
155
156 $this->add_control(
157 'caption_source',
158 [
159 'label' => esc_html__( 'Caption', 'elementor' ),
160 'type' => Controls_Manager::SELECT,
161 'options' => [
162 'none' => esc_html__( 'None', 'elementor' ),
163 'attachment' => esc_html__( 'Attachment Caption', 'elementor' ),
164 'custom' => esc_html__( 'Custom Caption', 'elementor' ),
165 ],
166 'default' => 'none',
167 ]
168 );
169
170 $this->add_control(
171 'caption',
172 [
173 'label' => esc_html__( 'Custom Caption', 'elementor' ),
174 'type' => Controls_Manager::TEXT,
175 'default' => '',
176 'placeholder' => esc_html__( 'Enter your image caption', 'elementor' ),
177 'condition' => [
178 'caption_source' => 'custom',
179 ],
180 'dynamic' => [
181 'active' => true,
182 ],
183 ]
184 );
185
186 $this->add_control(
187 'link_to',
188 [
189 'label' => esc_html__( 'Link', 'elementor' ),
190 'type' => Controls_Manager::SELECT,
191 'default' => 'none',
192 'options' => [
193 'none' => esc_html__( 'None', 'elementor' ),
194 'file' => esc_html__( 'Media File', 'elementor' ),
195 'custom' => esc_html__( 'Custom URL', 'elementor' ),
196 ],
197 ]
198 );
199
200 $this->add_control(
201 'link',
202 [
203 'label' => esc_html__( 'Link', 'elementor' ),
204 'type' => Controls_Manager::URL,
205 'dynamic' => [
206 'active' => true,
207 ],
208 'placeholder' => esc_html__( 'https://your-link.com', 'elementor' ),
209 'condition' => [
210 'link_to' => 'custom',
211 ],
212 'show_label' => false,
213 ]
214 );
215
216 $this->add_control(
217 'open_lightbox',
218 [
219 'label' => esc_html__( 'Lightbox', 'elementor' ),
220 'type' => Controls_Manager::SELECT,
221 'description' => sprintf(
222 /* translators: 1: Link open tag, 2: Link close tag. */
223 esc_html__( 'Manage your site’s lightbox settings in the %1$sLightbox panel%2$s.', 'elementor' ),
224 '<a href="javascript: $e.run( \'panel/global/open\' ).then( () => $e.route( \'panel/global/settings-lightbox\' ) )">',
225 '</a>'
226 ),
227 'default' => 'default',
228 'options' => [
229 'default' => esc_html__( 'Default', 'elementor' ),
230 'yes' => esc_html__( 'Yes', 'elementor' ),
231 'no' => esc_html__( 'No', 'elementor' ),
232 ],
233 'condition' => [
234 'link_to' => 'file',
235 ],
236 ]
237 );
238
239 $this->add_control(
240 'view',
241 [
242 'label' => esc_html__( 'View', 'elementor' ),
243 'type' => Controls_Manager::HIDDEN,
244 'default' => 'traditional',
245 ]
246 );
247
248 $this->end_controls_section();
249
250 $this->start_controls_section(
251 'section_style_image',
252 [
253 'label' => esc_html__( 'Image', 'elementor' ),
254 'tab' => Controls_Manager::TAB_STYLE,
255 ]
256 );
257
258 $this->add_responsive_control(
259 'width',
260 [
261 'label' => esc_html__( 'Width', 'elementor' ),
262 'type' => Controls_Manager::SLIDER,
263 'default' => [
264 'unit' => '%',
265 ],
266 'tablet_default' => [
267 'unit' => '%',
268 ],
269 'mobile_default' => [
270 'unit' => '%',
271 ],
272 'size_units' => [ 'px', '%', 'em', 'rem', 'vw', 'custom' ],
273 'range' => [
274 '%' => [
275 'min' => 1,
276 'max' => 100,
277 ],
278 'px' => [
279 'min' => 1,
280 'max' => 1000,
281 ],
282 'vw' => [
283 'min' => 1,
284 'max' => 100,
285 ],
286 ],
287 'selectors' => [
288 '{{WRAPPER}} img' => 'width: {{SIZE}}{{UNIT}};',
289 ],
290 ]
291 );
292
293 $this->add_responsive_control(
294 'space',
295 [
296 'label' => esc_html__( 'Max Width', 'elementor' ),
297 'type' => Controls_Manager::SLIDER,
298 'default' => [
299 'unit' => '%',
300 ],
301 'tablet_default' => [
302 'unit' => '%',
303 ],
304 'mobile_default' => [
305 'unit' => '%',
306 ],
307 'size_units' => [ 'px', '%', 'em', 'rem', 'vw', 'custom' ],
308 'range' => [
309 '%' => [
310 'min' => 1,
311 'max' => 100,
312 ],
313 'px' => [
314 'min' => 1,
315 'max' => 1000,
316 ],
317 'vw' => [
318 'min' => 1,
319 'max' => 100,
320 ],
321 ],
322 'selectors' => [
323 '{{WRAPPER}} img' => 'max-width: {{SIZE}}{{UNIT}};',
324 ],
325 ]
326 );
327
328 $this->add_responsive_control(
329 'height',
330 [
331 'label' => esc_html__( 'Height', 'elementor' ),
332 'type' => Controls_Manager::SLIDER,
333 'size_units' => [ 'px', '%', 'em', 'rem', 'vh', 'custom' ],
334 'range' => [
335 'px' => [
336 'min' => 1,
337 'max' => 500,
338 ],
339 'vh' => [
340 'min' => 1,
341 'max' => 100,
342 ],
343 ],
344 'selectors' => [
345 '{{WRAPPER}} img' => 'height: {{SIZE}}{{UNIT}};',
346 ],
347 ]
348 );
349
350 $this->add_responsive_control(
351 'object-fit',
352 [
353 'label' => esc_html__( 'Object Fit', 'elementor' ),
354 'type' => Controls_Manager::SELECT,
355 'condition' => [
356 'height[size]!' => '',
357 ],
358 'options' => [
359 '' => esc_html__( 'Default', 'elementor' ),
360 'fill' => esc_html__( 'Fill', 'elementor' ),
361 'cover' => esc_html__( 'Cover', 'elementor' ),
362 'contain' => esc_html__( 'Contain', 'elementor' ),
363 ],
364 'default' => '',
365 'selectors' => [
366 '{{WRAPPER}} img' => 'object-fit: {{VALUE}};',
367 ],
368 ]
369 );
370
371 $this->add_control(
372 'separator_panel_style',
373 [
374 'type' => Controls_Manager::DIVIDER,
375 'style' => 'thick',
376 ]
377 );
378
379 $this->start_controls_tabs( 'image_effects' );
380
381 $this->start_controls_tab( 'normal',
382 [
383 'label' => esc_html__( 'Normal', 'elementor' ),
384 ]
385 );
386
387 $this->add_control(
388 'opacity',
389 [
390 'label' => esc_html__( 'Opacity', 'elementor' ),
391 'type' => Controls_Manager::SLIDER,
392 'range' => [
393 'px' => [
394 'max' => 1,
395 'min' => 0.10,
396 'step' => 0.01,
397 ],
398 ],
399 'selectors' => [
400 '{{WRAPPER}} img' => 'opacity: {{SIZE}};',
401 ],
402 ]
403 );
404
405 $this->add_group_control(
406 Group_Control_Css_Filter::get_type(),
407 [
408 'name' => 'css_filters',
409 'selector' => '{{WRAPPER}} img',
410 ]
411 );
412
413 $this->end_controls_tab();
414
415 $this->start_controls_tab( 'hover',
416 [
417 'label' => esc_html__( 'Hover', 'elementor' ),
418 ]
419 );
420
421 $this->add_control(
422 'opacity_hover',
423 [
424 'label' => esc_html__( 'Opacity', 'elementor' ),
425 'type' => Controls_Manager::SLIDER,
426 'range' => [
427 'px' => [
428 'max' => 1,
429 'min' => 0.10,
430 'step' => 0.01,
431 ],
432 ],
433 'selectors' => [
434 '{{WRAPPER}}:hover img' => 'opacity: {{SIZE}};',
435 ],
436 ]
437 );
438
439 $this->add_group_control(
440 Group_Control_Css_Filter::get_type(),
441 [
442 'name' => 'css_filters_hover',
443 'selector' => '{{WRAPPER}}:hover img',
444 ]
445 );
446
447 $this->add_control(
448 'background_hover_transition',
449 [
450 'label' => esc_html__( 'Transition Duration', 'elementor' ),
451 'type' => Controls_Manager::SLIDER,
452 'range' => [
453 'px' => [
454 'max' => 3,
455 'step' => 0.1,
456 ],
457 ],
458 'selectors' => [
459 '{{WRAPPER}} img' => 'transition-duration: {{SIZE}}s',
460 ],
461 ]
462 );
463
464 $this->add_control(
465 'hover_animation',
466 [
467 'label' => esc_html__( 'Hover Animation', 'elementor' ),
468 'type' => Controls_Manager::HOVER_ANIMATION,
469 ]
470 );
471
472 $this->end_controls_tab();
473
474 $this->end_controls_tabs();
475
476 $this->add_group_control(
477 Group_Control_Border::get_type(),
478 [
479 'name' => 'image_border',
480 'selector' => '{{WRAPPER}} img',
481 'separator' => 'before',
482 ]
483 );
484
485 $this->add_responsive_control(
486 'image_border_radius',
487 [
488 'label' => esc_html__( 'Border Radius', 'elementor' ),
489 'type' => Controls_Manager::DIMENSIONS,
490 'size_units' => [ 'px', '%', 'em', 'rem', 'custom' ],
491 'selectors' => [
492 '{{WRAPPER}} img' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
493 ],
494 ]
495 );
496
497 $this->add_group_control(
498 Group_Control_Box_Shadow::get_type(),
499 [
500 'name' => 'image_box_shadow',
501 'exclude' => [
502 'box_shadow_position',
503 ],
504 'selector' => '{{WRAPPER}} img',
505 ]
506 );
507
508 $this->end_controls_section();
509
510 $this->start_controls_section(
511 'section_style_caption',
512 [
513 'label' => esc_html__( 'Caption', 'elementor' ),
514 'tab' => Controls_Manager::TAB_STYLE,
515 'condition' => [
516 'caption_source!' => 'none',
517 ],
518 ]
519 );
520
521 $this->add_responsive_control(
522 'caption_align',
523 [
524 'label' => esc_html__( 'Alignment', 'elementor' ),
525 'type' => Controls_Manager::CHOOSE,
526 'options' => [
527 'left' => [
528 'title' => esc_html__( 'Left', 'elementor' ),
529 'icon' => 'eicon-text-align-left',
530 ],
531 'center' => [
532 'title' => esc_html__( 'Center', 'elementor' ),
533 'icon' => 'eicon-text-align-center',
534 ],
535 'right' => [
536 'title' => esc_html__( 'Right', 'elementor' ),
537 'icon' => 'eicon-text-align-right',
538 ],
539 'justify' => [
540 'title' => esc_html__( 'Justified', 'elementor' ),
541 'icon' => 'eicon-text-align-justify',
542 ],
543 ],
544 'default' => '',
545 'selectors' => [
546 '{{WRAPPER}} .widget-image-caption' => 'text-align: {{VALUE}};',
547 ],
548 ]
549 );
550
551 $this->add_control(
552 'text_color',
553 [
554 'label' => esc_html__( 'Text Color', 'elementor' ),
555 'type' => Controls_Manager::COLOR,
556 'default' => '',
557 'selectors' => [
558 '{{WRAPPER}} .widget-image-caption' => 'color: {{VALUE}};',
559 ],
560 'global' => [
561 'default' => Global_Colors::COLOR_TEXT,
562 ],
563 ]
564 );
565
566 $this->add_control(
567 'caption_background_color',
568 [
569 'label' => esc_html__( 'Background Color', 'elementor' ),
570 'type' => Controls_Manager::COLOR,
571 'selectors' => [
572 '{{WRAPPER}} .widget-image-caption' => 'background-color: {{VALUE}};',
573 ],
574 ]
575 );
576
577 $this->add_group_control(
578 Group_Control_Typography::get_type(),
579 [
580 'name' => 'caption_typography',
581 'selector' => '{{WRAPPER}} .widget-image-caption',
582 'global' => [
583 'default' => Global_Typography::TYPOGRAPHY_TEXT,
584 ],
585 ]
586 );
587
588 $this->add_group_control(
589 Group_Control_Text_Shadow::get_type(),
590 [
591 'name' => 'caption_text_shadow',
592 'selector' => '{{WRAPPER}} .widget-image-caption',
593 ]
594 );
595
596 $this->add_responsive_control(
597 'caption_space',
598 [
599 'label' => esc_html__( 'Spacing', 'elementor' ),
600 'type' => Controls_Manager::SLIDER,
601 'range' => [
602 'px' => [
603 'min' => 0,
604 'max' => 100,
605 ],
606 ],
607 'selectors' => [
608 '{{WRAPPER}} .widget-image-caption' => 'margin-top: {{SIZE}}{{UNIT}};',
609 ],
610 ]
611 );
612
613 $this->end_controls_section();
614 }
615
616 /**
617 * Check if the current widget has caption
618 *
619 * @access private
620 * @since 2.3.0
621 *
622 * @param array $settings
623 *
624 * @return boolean
625 */
626 private function has_caption( $settings ) {
627 return ( ! empty( $settings['caption_source'] ) && 'none' !== $settings['caption_source'] );
628 }
629
630 /**
631 * Get the caption for current widget.
632 *
633 * @access private
634 * @since 2.3.0
635 * @param $settings
636 *
637 * @return string
638 */
639 private function get_caption( $settings ) {
640 $caption = '';
641 if ( ! empty( $settings['caption_source'] ) ) {
642 switch ( $settings['caption_source'] ) {
643 case 'attachment':
644 $caption = wp_get_attachment_caption( $settings['image']['id'] );
645 break;
646 case 'custom':
647 $caption = ! Utils::is_empty( $settings['caption'] ) ? $settings['caption'] : '';
648 }
649 }
650 return $caption;
651 }
652
653 /**
654 * Render image widget output on the frontend.
655 *
656 * Written in PHP and used to generate the final HTML.
657 *
658 * @since 1.0.0
659 * @access protected
660 */
661 protected function render() {
662 $settings = $this->get_settings_for_display();
663
664 if ( empty( $settings['image']['url'] ) ) {
665 return;
666 }
667
668 if ( ! Plugin::$instance->experiments->is_feature_active( 'e_dom_optimization' ) ) {
669 $this->add_render_attribute( 'wrapper', 'class', 'elementor-image' );
670 }
671
672 $has_caption = $this->has_caption( $settings );
673
674 $link = $this->get_link_url( $settings );
675
676 if ( $link ) {
677 $this->add_link_attributes( 'link', $link );
678
679 if ( Plugin::$instance->editor->is_edit_mode() ) {
680 $this->add_render_attribute( 'link', [
681 'class' => 'elementor-clickable',
682 ] );
683 }
684
685 if ( 'custom' !== $settings['link_to'] ) {
686 $this->add_lightbox_data_attributes( 'link', $settings['image']['id'], $settings['open_lightbox'] );
687 }
688 } ?>
689 <?php if ( ! Plugin::$instance->experiments->is_feature_active( 'e_dom_optimization' ) ) { ?>
690 <div <?php $this->print_render_attribute_string( 'wrapper' ); ?>>
691 <?php } ?>
692 <?php if ( $has_caption ) : ?>
693 <figure class="wp-caption">
694 <?php endif; ?>
695 <?php if ( $link ) : ?>
696 <a <?php $this->print_render_attribute_string( 'link' ); ?>>
697 <?php endif; ?>
698 <?php Group_Control_Image_Size::print_attachment_image_html( $settings ); ?>
699 <?php if ( $link ) : ?>
700 </a>
701 <?php endif; ?>
702 <?php if ( $has_caption ) : ?>
703 <figcaption class="widget-image-caption wp-caption-text"><?php
704 echo wp_kses_post( $this->get_caption( $settings ) );
705 ?></figcaption>
706 <?php endif; ?>
707 <?php if ( $has_caption ) : ?>
708 </figure>
709 <?php endif; ?>
710 <?php if ( ! Plugin::$instance->experiments->is_feature_active( 'e_dom_optimization' ) ) { ?>
711 </div>
712 <?php } ?>
713 <?php
714 }
715
716 /**
717 * Render image widget output in the editor.
718 *
719 * Written as a Backbone JavaScript template and used to generate the live preview.
720 *
721 * @since 2.9.0
722 * @access protected
723 */
724 protected function content_template() {
725 ?>
726 <# if ( settings.image.url ) {
727 var image = {
728 id: settings.image.id,
729 url: settings.image.url,
730 size: settings.image_size,
731 dimension: settings.image_custom_dimension,
732 model: view.getEditModel()
733 };
734
735 var image_url = elementor.imagesManager.getImageUrl( image );
736
737 if ( ! image_url ) {
738 return;
739 }
740
741 var hasCaption = function() {
742 if( ! settings.caption_source || 'none' === settings.caption_source ) {
743 return false;
744 }
745 return true;
746 }
747
748 var ensureAttachmentData = function( id ) {
749 if ( 'undefined' === typeof wp.media.attachment( id ).get( 'caption' ) ) {
750 wp.media.attachment( id ).fetch().then( function( data ) {
751 view.render();
752 } );
753 }
754 }
755
756 var getAttachmentCaption = function( id ) {
757 if ( ! id ) {
758 return '';
759 }
760 ensureAttachmentData( id );
761 return wp.media.attachment( id ).get( 'caption' );
762 }
763
764 var getCaption = function() {
765 if ( ! hasCaption() ) {
766 return '';
767 }
768 return 'custom' === settings.caption_source ? settings.caption : getAttachmentCaption( settings.image.id );
769 }
770
771 var link_url;
772
773 if ( 'custom' === settings.link_to ) {
774 link_url = settings.link.url;
775 }
776
777 if ( 'file' === settings.link_to ) {
778 link_url = settings.image.url;
779 }
780
781 <?php if ( ! Plugin::$instance->experiments->is_feature_active( 'e_dom_optimization' ) ) { ?>
782 #><div class="elementor-image{{ settings.shape ? ' elementor-image-shape-' + settings.shape : '' }}"><#
783 <?php } ?>
784
785 var imgClass = '';
786
787 if ( '' !== settings.hover_animation ) {
788 imgClass = 'elementor-animation-' + settings.hover_animation;
789 }
790
791 if ( hasCaption() ) {
792 #><figure class="wp-caption"><#
793 }
794
795 if ( link_url ) {
796 #><a class="elementor-clickable" data-elementor-open-lightbox="{{ settings.open_lightbox }}" href="{{ link_url }}"><#
797 }
798 #><img src="{{ image_url }}" class="{{ imgClass }}" /><#
799
800 if ( link_url ) {
801 #></a><#
802 }
803
804 if ( hasCaption() ) {
805 #><figcaption class="widget-image-caption wp-caption-text">{{{ getCaption() }}}</figcaption><#
806 }
807
808 if ( hasCaption() ) {
809 #></figure><#
810 }
811
812 <?php if ( ! Plugin::$instance->experiments->is_feature_active( 'e_dom_optimization' ) ) { ?>
813 #></div><#
814 <?php } ?>
815
816 } #>
817 <?php
818 }
819
820 /**
821 * Retrieve image widget link URL.
822 *
823 * @since 3.11.0
824 * @access protected
825 *
826 * @param array $settings
827 *
828 * @return array|string|false An array/string containing the link URL, or false if no link.
829 */
830 protected function get_link_url( $settings ) {
831 if ( 'none' === $settings['link_to'] ) {
832 return false;
833 }
834
835 if ( 'custom' === $settings['link_to'] ) {
836 if ( empty( $settings['link']['url'] ) ) {
837 return false;
838 }
839
840 return $settings['link'];
841 }
842
843 return [
844 'url' => $settings['image']['url'],
845 ];
846 }
847 }
848