PluginProbe
Elementor Website Builder – more than just a page builder / 3.19.2
Elementor Website Builder – more than just a page builder v3.19.2
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.19.2, at includes/widgets/image.php

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