| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677 |
- <?php
- namespace Elementor;
- if ( ! defined( 'ABSPATH' ) ) {
- exit; // Exit if accessed directly.
- }
- /**
- * Elementor image widget.
- *
- * Elementor widget that displays an image into the page.
- *
- * @since 1.0.0
- */
- class Widget_Image extends Widget_Base {
- /**
- * Get widget name.
- *
- * Retrieve image widget name.
- *
- * @since 1.0.0
- * @access public
- *
- * @return string Widget name.
- */
- public function get_name() {
- return 'image';
- }
- /**
- * Get widget title.
- *
- * Retrieve image widget title.
- *
- * @since 1.0.0
- * @access public
- *
- * @return string Widget title.
- */
- public function get_title() {
- return __( 'Image', 'elementor' );
- }
- /**
- * Get widget icon.
- *
- * Retrieve image widget icon.
- *
- * @since 1.0.0
- * @access public
- *
- * @return string Widget icon.
- */
- public function get_icon() {
- return 'eicon-insert-image';
- }
- /**
- * Get widget categories.
- *
- * Retrieve the list of categories the image widget belongs to.
- *
- * Used to determine where to display the widget in the editor.
- *
- * @since 2.0.0
- * @access public
- *
- * @return array Widget categories.
- */
- public function get_categories() {
- return [ 'basic' ];
- }
- /**
- * Get widget keywords.
- *
- * Retrieve the list of keywords the widget belongs to.
- *
- * @since 2.1.0
- * @access public
- *
- * @return array Widget keywords.
- */
- public function get_keywords() {
- return [ 'image', 'photo', 'visual' ];
- }
- /**
- * Register image widget controls.
- *
- * Adds different input fields to allow the user to change and customize the widget settings.
- *
- * @since 1.0.0
- * @access protected
- */
- protected function _register_controls() {
- $this->start_controls_section(
- 'section_image',
- [
- 'label' => __( 'Image', 'elementor' ),
- ]
- );
- $this->add_control(
- 'image',
- [
- 'label' => __( 'Choose Image', 'elementor' ),
- 'type' => Controls_Manager::MEDIA,
- 'dynamic' => [
- 'active' => true,
- ],
- 'default' => [
- 'url' => Utils::get_placeholder_image_src(),
- ],
- ]
- );
- $this->add_group_control(
- Group_Control_Image_Size::get_type(),
- [
- 'name' => 'image', // Usage: `{name}_size` and `{name}_custom_dimension`, in this case `image_size` and `image_custom_dimension`.
- 'default' => 'large',
- 'separator' => 'none',
- ]
- );
- $this->add_responsive_control(
- 'align',
- [
- 'label' => __( 'Alignment', 'elementor' ),
- 'type' => Controls_Manager::CHOOSE,
- 'options' => [
- 'left' => [
- 'title' => __( 'Left', 'elementor' ),
- 'icon' => 'fa fa-align-left',
- ],
- 'center' => [
- 'title' => __( 'Center', 'elementor' ),
- 'icon' => 'fa fa-align-center',
- ],
- 'right' => [
- 'title' => __( 'Right', 'elementor' ),
- 'icon' => 'fa fa-align-right',
- ],
- ],
- 'selectors' => [
- '{{WRAPPER}}' => 'text-align: {{VALUE}};',
- ],
- ]
- );
- $this->add_control(
- 'caption',
- [
- 'label' => __( 'Caption', 'elementor' ),
- 'type' => Controls_Manager::TEXT,
- 'default' => '',
- 'placeholder' => __( 'Enter your image caption', 'elementor' ),
- ]
- );
- $this->add_control(
- 'link_to',
- [
- 'label' => __( 'Link to', 'elementor' ),
- 'type' => Controls_Manager::SELECT,
- 'default' => 'none',
- 'options' => [
- 'none' => __( 'None', 'elementor' ),
- 'file' => __( 'Media File', 'elementor' ),
- 'custom' => __( 'Custom URL', 'elementor' ),
- ],
- ]
- );
- $this->add_control(
- 'link',
- [
- 'label' => __( 'Link to', 'elementor' ),
- 'type' => Controls_Manager::URL,
- 'dynamic' => [
- 'active' => true,
- ],
- 'placeholder' => __( 'https://your-link.com', 'elementor' ),
- 'condition' => [
- 'link_to' => 'custom',
- ],
- 'show_label' => false,
- ]
- );
- $this->add_control(
- 'open_lightbox',
- [
- 'label' => __( 'Lightbox', 'elementor' ),
- 'type' => Controls_Manager::SELECT,
- 'default' => 'default',
- 'options' => [
- 'default' => __( 'Default', 'elementor' ),
- 'yes' => __( 'Yes', 'elementor' ),
- 'no' => __( 'No', 'elementor' ),
- ],
- 'condition' => [
- 'link_to' => 'file',
- ],
- ]
- );
- $this->add_control(
- 'view',
- [
- 'label' => __( 'View', 'elementor' ),
- 'type' => Controls_Manager::HIDDEN,
- 'default' => 'traditional',
- ]
- );
- $this->end_controls_section();
- $this->start_controls_section(
- 'section_style_image',
- [
- 'label' => __( 'Image', 'elementor' ),
- 'tab' => Controls_Manager::TAB_STYLE,
- ]
- );
- $this->add_responsive_control(
- 'width',
- [
- 'label' => __( 'Width', 'elementor' ),
- 'type' => Controls_Manager::SLIDER,
- 'default' => [
- 'unit' => '%',
- ],
- 'tablet_default' => [
- 'unit' => '%',
- ],
- 'mobile_default' => [
- 'unit' => '%',
- ],
- 'size_units' => [ '%', 'px', 'vw' ],
- 'range' => [
- '%' => [
- 'min' => 1,
- 'max' => 100,
- ],
- 'px' => [
- 'min' => 1,
- 'max' => 1000,
- ],
- 'vw' => [
- 'min' => 1,
- 'max' => 100,
- ],
- ],
- 'selectors' => [
- '{{WRAPPER}} .elementor-image img' => 'width: {{SIZE}}{{UNIT}};',
- ],
- ]
- );
- $this->add_responsive_control(
- 'space',
- [
- 'label' => __( 'Max Width', 'elementor' ) . ' (%)',
- 'type' => Controls_Manager::SLIDER,
- 'default' => [
- 'unit' => '%',
- ],
- 'tablet_default' => [
- 'unit' => '%',
- ],
- 'mobile_default' => [
- 'unit' => '%',
- ],
- 'size_units' => [ '%' ],
- 'range' => [
- '%' => [
- 'min' => 1,
- 'max' => 100,
- ],
- ],
- 'selectors' => [
- '{{WRAPPER}} .elementor-image img' => 'max-width: {{SIZE}}{{UNIT}};',
- ],
- ]
- );
- $this->add_control(
- 'separator_panel_style',
- [
- 'type' => Controls_Manager::DIVIDER,
- 'style' => 'thick',
- ]
- );
- $this->start_controls_tabs( 'image_effects' );
- $this->start_controls_tab( 'normal',
- [
- 'label' => __( 'Normal', 'elementor' ),
- ]
- );
- $this->add_control(
- 'opacity',
- [
- 'label' => __( 'Opacity', 'elementor' ),
- 'type' => Controls_Manager::SLIDER,
- 'range' => [
- 'px' => [
- 'max' => 1,
- 'min' => 0.10,
- 'step' => 0.01,
- ],
- ],
- 'selectors' => [
- '{{WRAPPER}} .elementor-image img' => 'opacity: {{SIZE}};',
- ],
- ]
- );
- $this->add_group_control(
- Group_Control_Css_Filter::get_type(),
- [
- 'name' => 'css_filters',
- 'selector' => '{{WRAPPER}} .elementor-image img',
- ]
- );
- $this->end_controls_tab();
- $this->start_controls_tab( 'hover',
- [
- 'label' => __( 'Hover', 'elementor' ),
- ]
- );
- $this->add_control(
- 'opacity_hover',
- [
- 'label' => __( 'Opacity', 'elementor' ),
- 'type' => Controls_Manager::SLIDER,
- 'range' => [
- 'px' => [
- 'max' => 1,
- 'min' => 0.10,
- 'step' => 0.01,
- ],
- ],
- 'selectors' => [
- '{{WRAPPER}} .elementor-image:hover img' => 'opacity: {{SIZE}};',
- ],
- ]
- );
- $this->add_group_control(
- Group_Control_Css_Filter::get_type(),
- [
- 'name' => 'css_filters_hover',
- 'selector' => '{{WRAPPER}} .elementor-image:hover img',
- ]
- );
- $this->add_control(
- 'background_hover_transition',
- [
- 'label' => __( 'Transition Duration', 'elementor' ),
- 'type' => Controls_Manager::SLIDER,
- 'range' => [
- 'px' => [
- 'max' => 3,
- 'step' => 0.1,
- ],
- ],
- 'selectors' => [
- '{{WRAPPER}} .elementor-image img' => 'transition-duration: {{SIZE}}s',
- ],
- ]
- );
- $this->add_control(
- 'hover_animation',
- [
- 'label' => __( 'Hover Animation', 'elementor' ),
- 'type' => Controls_Manager::HOVER_ANIMATION,
- ]
- );
- $this->end_controls_tab();
- $this->end_controls_tabs();
- $this->add_group_control(
- Group_Control_Border::get_type(),
- [
- 'name' => 'image_border',
- 'selector' => '{{WRAPPER}} .elementor-image img',
- 'separator' => 'before',
- ]
- );
- $this->add_responsive_control(
- 'image_border_radius',
- [
- 'label' => __( 'Border Radius', 'elementor' ),
- 'type' => Controls_Manager::DIMENSIONS,
- 'size_units' => [ 'px', '%' ],
- 'selectors' => [
- '{{WRAPPER}} .elementor-image img' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
- ],
- ]
- );
- $this->add_group_control(
- Group_Control_Box_Shadow::get_type(),
- [
- 'name' => 'image_box_shadow',
- 'exclude' => [
- 'box_shadow_position',
- ],
- 'selector' => '{{WRAPPER}} .elementor-image img',
- ]
- );
- $this->end_controls_section();
- $this->start_controls_section(
- 'section_style_caption',
- [
- 'label' => __( 'Caption', 'elementor' ),
- 'tab' => Controls_Manager::TAB_STYLE,
- 'condition' => [
- 'caption!' => '',
- ],
- ]
- );
- $this->add_control(
- 'caption_align',
- [
- 'label' => __( 'Alignment', 'elementor' ),
- 'type' => Controls_Manager::CHOOSE,
- 'options' => [
- 'left' => [
- 'title' => __( 'Left', 'elementor' ),
- 'icon' => 'fa fa-align-left',
- ],
- 'center' => [
- 'title' => __( 'Center', 'elementor' ),
- 'icon' => 'fa fa-align-center',
- ],
- 'right' => [
- 'title' => __( 'Right', 'elementor' ),
- 'icon' => 'fa fa-align-right',
- ],
- 'justify' => [
- 'title' => __( 'Justified', 'elementor' ),
- 'icon' => 'fa fa-align-justify',
- ],
- ],
- 'default' => '',
- 'selectors' => [
- '{{WRAPPER}} .widget-image-caption' => 'text-align: {{VALUE}};',
- ],
- ]
- );
- $this->add_control(
- 'text_color',
- [
- 'label' => __( 'Text Color', 'elementor' ),
- 'type' => Controls_Manager::COLOR,
- 'default' => '',
- 'selectors' => [
- '{{WRAPPER}} .widget-image-caption' => 'color: {{VALUE}};',
- ],
- 'scheme' => [
- 'type' => Scheme_Color::get_type(),
- 'value' => Scheme_Color::COLOR_3,
- ],
- ]
- );
- $this->add_group_control(
- Group_Control_Typography::get_type(),
- [
- 'name' => 'caption_typography',
- 'selector' => '{{WRAPPER}} .widget-image-caption',
- 'scheme' => Scheme_Typography::TYPOGRAPHY_3,
- ]
- );
- $this->add_responsive_control(
- 'caption_space',
- [
- 'label' => __( 'Spacing', 'elementor' ),
- 'type' => Controls_Manager::SLIDER,
- 'range' => [
- 'px' => [
- 'min' => 0,
- 'max' => 100,
- ],
- ],
- 'selectors' => [
- '{{WRAPPER}} .widget-image-caption' => 'margin-top: {{SIZE}}{{UNIT}};',
- ],
- ]
- );
- $this->end_controls_section();
- }
- /**
- * Render image widget output on the frontend.
- *
- * Written in PHP and used to generate the final HTML.
- *
- * @since 1.0.0
- * @access protected
- */
- protected function render() {
- $settings = $this->get_settings_for_display();
- if ( empty( $settings['image']['url'] ) ) {
- return;
- }
- $has_caption = ! empty( $settings['caption'] );
- $this->add_render_attribute( 'wrapper', 'class', 'elementor-image' );
- if ( ! empty( $settings['shape'] ) ) {
- $this->add_render_attribute( 'wrapper', 'class', 'elementor-image-shape-' . $settings['shape'] );
- }
- $link = $this->get_link_url( $settings );
- if ( $link ) {
- $this->add_render_attribute( 'link', [
- 'href' => $link['url'],
- 'data-elementor-open-lightbox' => $settings['open_lightbox'],
- ] );
- if ( Plugin::$instance->editor->is_edit_mode() ) {
- $this->add_render_attribute( 'link', [
- 'class' => 'elementor-clickable',
- ] );
- }
- if ( ! empty( $link['is_external'] ) ) {
- $this->add_render_attribute( 'link', 'target', '_blank' );
- }
- if ( ! empty( $link['nofollow'] ) ) {
- $this->add_render_attribute( 'link', 'rel', 'nofollow' );
- }
- } ?>
- <div <?php echo $this->get_render_attribute_string( 'wrapper' ); ?>>
- <?php if ( $has_caption ) : ?>
- <figure class="wp-caption">
- <?php endif; ?>
- <?php if ( $link ) : ?>
- <a <?php echo $this->get_render_attribute_string( 'link' ); ?>>
- <?php endif; ?>
- <?php echo Group_Control_Image_Size::get_attachment_image_html( $settings ); ?>
- <?php if ( $link ) : ?>
- </a>
- <?php endif; ?>
- <?php if ( $has_caption ) : ?>
- <figcaption class="widget-image-caption wp-caption-text"><?php echo $settings['caption']; ?></figcaption>
- <?php endif; ?>
- <?php if ( $has_caption ) : ?>
- </figure>
- <?php endif; ?>
- </div>
- <?php
- }
- /**
- * Render image widget output in the editor.
- *
- * Written as a Backbone JavaScript template and used to generate the live preview.
- *
- * @since 1.0.0
- * @access protected
- */
- protected function _content_template() {
- ?>
- <# if ( settings.image.url ) {
- var image = {
- id: settings.image.id,
- url: settings.image.url,
- size: settings.image_size,
- dimension: settings.image_custom_dimension,
- model: view.getEditModel()
- };
- var image_url = elementor.imagesManager.getImageUrl( image );
- if ( ! image_url ) {
- return;
- }
- var link_url;
- if ( 'custom' === settings.link_to ) {
- link_url = settings.link.url;
- }
- if ( 'file' === settings.link_to ) {
- link_url = settings.image.url;
- }
- #><div class="elementor-image{{ settings.shape ? ' elementor-image-shape-' + settings.shape : '' }}"><#
- var imgClass = '',
- hasCaption = '' !== settings.caption;
- if ( '' !== settings.hover_animation ) {
- imgClass = 'elementor-animation-' + settings.hover_animation;
- }
- if ( hasCaption ) {
- #><figure class="wp-caption"><#
- }
- if ( link_url ) {
- #><a class="elementor-clickable" data-elementor-open-lightbox="{{ settings.open_lightbox }}" href="{{ link_url }}"><#
- }
- #><img src="{{ image_url }}" class="{{ imgClass }}" /><#
- if ( link_url ) {
- #></a><#
- }
- if ( hasCaption ) {
- #><figcaption class="widget-image-caption wp-caption-text">{{{ settings.caption }}}</figcaption><#
- }
- if ( hasCaption ) {
- #></figure><#
- }
- #></div><#
- } #>
- <?php
- }
- /**
- * Retrieve image widget link URL.
- *
- * @since 1.0.0
- * @access private
- *
- * @param array $settings
- *
- * @return array|string|false An array/string containing the link URL, or false if no link.
- */
- private function get_link_url( $settings ) {
- if ( 'none' === $settings['link_to'] ) {
- return false;
- }
- if ( 'custom' === $settings['link_to'] ) {
- if ( empty( $settings['link']['url'] ) ) {
- return false;
- }
- return $settings['link'];
- }
- return [
- 'url' => $settings['image']['url'],
- ];
- }
- }
|