| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783 |
- <?php
- namespace Elementor;
- if ( ! defined( 'ABSPATH' ) ) {
- exit; // Exit if accessed directly.
- }
- /**
- * Elementor image carousel widget.
- *
- * Elementor widget that displays a set of images in a rotating carousel or
- * slider.
- *
- * @since 1.0.0
- */
- class Widget_Image_Carousel extends Widget_Base {
- /**
- * Get widget name.
- *
- * Retrieve image carousel widget name.
- *
- * @since 1.0.0
- * @access public
- *
- * @return string Widget name.
- */
- public function get_name() {
- return 'image-carousel';
- }
- /**
- * Get widget title.
- *
- * Retrieve image carousel widget title.
- *
- * @since 1.0.0
- * @access public
- *
- * @return string Widget title.
- */
- public function get_title() {
- return __( 'Image Carousel', 'elementor' );
- }
- /**
- * Get widget icon.
- *
- * Retrieve image carousel widget icon.
- *
- * @since 1.0.0
- * @access public
- *
- * @return string Widget icon.
- */
- public function get_icon() {
- return 'eicon-slider-push';
- }
- /**
- * 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', 'carousel', 'slider' ];
- }
- /**
- * Retrieve the list of scripts the image carousel widget depended on.
- *
- * Used to set scripts dependencies required to run the widget.
- *
- * @since 1.3.0
- * @access public
- *
- * @return array Widget scripts dependencies.
- */
- public function get_script_depends() {
- return [ 'jquery-slick' ];
- }
- /**
- * Register image carousel 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_carousel',
- [
- 'label' => __( 'Image Carousel', 'elementor' ),
- ]
- );
- $this->add_control(
- 'carousel',
- [
- 'label' => __( 'Add Images', 'elementor' ),
- 'type' => Controls_Manager::GALLERY,
- 'default' => [],
- 'show_label' => false,
- 'dynamic' => [
- 'active' => true,
- ],
- ]
- );
- $this->add_group_control(
- Group_Control_Image_Size::get_type(),
- [
- 'name' => 'thumbnail', // Usage: `{name}_size` and `{name}_custom_dimension`, in this case `thumbnail_size` and `thumbnail_custom_dimension`.
- 'separator' => 'none',
- ]
- );
- $slides_to_show = range( 1, 10 );
- $slides_to_show = array_combine( $slides_to_show, $slides_to_show );
- $this->add_responsive_control(
- 'slides_to_show',
- [
- 'label' => __( 'Slides to Show', 'elementor' ),
- 'type' => Controls_Manager::SELECT,
- 'options' => [
- '' => __( 'Default', 'elementor' ),
- ] + $slides_to_show,
- 'frontend_available' => true,
- ]
- );
- $this->add_control(
- 'slides_to_scroll',
- [
- 'label' => __( 'Slides to Scroll', 'elementor' ),
- 'type' => Controls_Manager::SELECT,
- 'description' => __( 'Set how many slides are scrolled per swipe.', 'elementor' ),
- 'default' => '2',
- 'options' => $slides_to_show,
- 'condition' => [
- 'slides_to_show!' => '1',
- ],
- 'frontend_available' => true,
- ]
- );
- $this->add_control(
- 'image_stretch',
- [
- 'label' => __( 'Image Stretch', 'elementor' ),
- 'type' => Controls_Manager::SELECT,
- 'default' => 'no',
- 'options' => [
- 'no' => __( 'No', 'elementor' ),
- 'yes' => __( 'Yes', 'elementor' ),
- ],
- ]
- );
- $this->add_control(
- 'navigation',
- [
- 'label' => __( 'Navigation', 'elementor' ),
- 'type' => Controls_Manager::SELECT,
- 'default' => 'both',
- 'options' => [
- 'both' => __( 'Arrows and Dots', 'elementor' ),
- 'arrows' => __( 'Arrows', 'elementor' ),
- 'dots' => __( 'Dots', 'elementor' ),
- 'none' => __( 'None', 'elementor' ),
- ],
- 'frontend_available' => true,
- ]
- );
- $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,
- '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(
- 'caption_type',
- [
- 'label' => __( 'Caption', 'elementor' ),
- 'type' => Controls_Manager::SELECT,
- 'default' => '',
- 'options' => [
- '' => __( 'None', 'elementor' ),
- 'title' => __( 'Title', 'elementor' ),
- 'caption' => __( 'Caption', 'elementor' ),
- 'description' => __( 'Description', 'elementor' ),
- ],
- ]
- );
- $this->add_control(
- 'view',
- [
- 'label' => __( 'View', 'elementor' ),
- 'type' => Controls_Manager::HIDDEN,
- 'default' => 'traditional',
- ]
- );
- $this->end_controls_section();
- $this->start_controls_section(
- 'section_additional_options',
- [
- 'label' => __( 'Additional Options', 'elementor' ),
- ]
- );
- $this->add_control(
- 'pause_on_hover',
- [
- 'label' => __( 'Pause on Hover', 'elementor' ),
- 'type' => Controls_Manager::SELECT,
- 'default' => 'yes',
- 'options' => [
- 'yes' => __( 'Yes', 'elementor' ),
- 'no' => __( 'No', 'elementor' ),
- ],
- 'frontend_available' => true,
- ]
- );
- $this->add_control(
- 'autoplay',
- [
- 'label' => __( 'Autoplay', 'elementor' ),
- 'type' => Controls_Manager::SELECT,
- 'default' => 'yes',
- 'options' => [
- 'yes' => __( 'Yes', 'elementor' ),
- 'no' => __( 'No', 'elementor' ),
- ],
- 'frontend_available' => true,
- ]
- );
- $this->add_control(
- 'autoplay_speed',
- [
- 'label' => __( 'Autoplay Speed', 'elementor' ),
- 'type' => Controls_Manager::NUMBER,
- 'default' => 5000,
- 'frontend_available' => true,
- ]
- );
- $this->add_control(
- 'infinite',
- [
- 'label' => __( 'Infinite Loop', 'elementor' ),
- 'type' => Controls_Manager::SELECT,
- 'default' => 'yes',
- 'options' => [
- 'yes' => __( 'Yes', 'elementor' ),
- 'no' => __( 'No', 'elementor' ),
- ],
- 'frontend_available' => true,
- ]
- );
- $this->add_control(
- 'effect',
- [
- 'label' => __( 'Effect', 'elementor' ),
- 'type' => Controls_Manager::SELECT,
- 'default' => 'slide',
- 'options' => [
- 'slide' => __( 'Slide', 'elementor' ),
- 'fade' => __( 'Fade', 'elementor' ),
- ],
- 'condition' => [
- 'slides_to_show' => '1',
- ],
- 'frontend_available' => true,
- ]
- );
- $this->add_control(
- 'speed',
- [
- 'label' => __( 'Animation Speed', 'elementor' ),
- 'type' => Controls_Manager::NUMBER,
- 'default' => 500,
- 'frontend_available' => true,
- ]
- );
- $this->add_control(
- 'direction',
- [
- 'label' => __( 'Direction', 'elementor' ),
- 'type' => Controls_Manager::SELECT,
- 'default' => 'ltr',
- 'options' => [
- 'ltr' => __( 'Left', 'elementor' ),
- 'rtl' => __( 'Right', 'elementor' ),
- ],
- 'frontend_available' => true,
- ]
- );
- $this->end_controls_section();
- $this->start_controls_section(
- 'section_style_navigation',
- [
- 'label' => __( 'Navigation', 'elementor' ),
- 'tab' => Controls_Manager::TAB_STYLE,
- 'condition' => [
- 'navigation' => [ 'arrows', 'dots', 'both' ],
- ],
- ]
- );
- $this->add_control(
- 'heading_style_arrows',
- [
- 'label' => __( 'Arrows', 'elementor' ),
- 'type' => Controls_Manager::HEADING,
- 'separator' => 'before',
- 'condition' => [
- 'navigation' => [ 'arrows', 'both' ],
- ],
- ]
- );
- $this->add_control(
- 'arrows_position',
- [
- 'label' => __( 'Position', 'elementor' ),
- 'type' => Controls_Manager::SELECT,
- 'default' => 'inside',
- 'options' => [
- 'inside' => __( 'Inside', 'elementor' ),
- 'outside' => __( 'Outside', 'elementor' ),
- ],
- 'condition' => [
- 'navigation' => [ 'arrows', 'both' ],
- ],
- ]
- );
- $this->add_control(
- 'arrows_size',
- [
- 'label' => __( 'Size', 'elementor' ),
- 'type' => Controls_Manager::SLIDER,
- 'range' => [
- 'px' => [
- 'min' => 20,
- 'max' => 60,
- ],
- ],
- 'selectors' => [
- '{{WRAPPER}} .elementor-image-carousel-wrapper .slick-slider .slick-prev:before, {{WRAPPER}} .elementor-image-carousel-wrapper .slick-slider .slick-next:before' => 'font-size: {{SIZE}}{{UNIT}};',
- ],
- 'condition' => [
- 'navigation' => [ 'arrows', 'both' ],
- ],
- ]
- );
- $this->add_control(
- 'arrows_color',
- [
- 'label' => __( 'Color', 'elementor' ),
- 'type' => Controls_Manager::COLOR,
- 'selectors' => [
- '{{WRAPPER}} .elementor-image-carousel-wrapper .slick-slider .slick-prev:before, {{WRAPPER}} .elementor-image-carousel-wrapper .slick-slider .slick-next:before' => 'color: {{VALUE}};',
- ],
- 'condition' => [
- 'navigation' => [ 'arrows', 'both' ],
- ],
- ]
- );
- $this->add_control(
- 'heading_style_dots',
- [
- 'label' => __( 'Dots', 'elementor' ),
- 'type' => Controls_Manager::HEADING,
- 'separator' => 'before',
- 'condition' => [
- 'navigation' => [ 'dots', 'both' ],
- ],
- ]
- );
- $this->add_control(
- 'dots_position',
- [
- 'label' => __( 'Position', 'elementor' ),
- 'type' => Controls_Manager::SELECT,
- 'default' => 'outside',
- 'options' => [
- 'outside' => __( 'Outside', 'elementor' ),
- 'inside' => __( 'Inside', 'elementor' ),
- ],
- 'condition' => [
- 'navigation' => [ 'dots', 'both' ],
- ],
- ]
- );
- $this->add_control(
- 'dots_size',
- [
- 'label' => __( 'Size', 'elementor' ),
- 'type' => Controls_Manager::SLIDER,
- 'range' => [
- 'px' => [
- 'min' => 5,
- 'max' => 10,
- ],
- ],
- 'selectors' => [
- '{{WRAPPER}} .elementor-image-carousel-wrapper .elementor-image-carousel .slick-dots li button:before' => 'font-size: {{SIZE}}{{UNIT}};',
- ],
- 'condition' => [
- 'navigation' => [ 'dots', 'both' ],
- ],
- ]
- );
- $this->add_control(
- 'dots_color',
- [
- 'label' => __( 'Color', 'elementor' ),
- 'type' => Controls_Manager::COLOR,
- 'selectors' => [
- '{{WRAPPER}} .elementor-image-carousel-wrapper .elementor-image-carousel .slick-dots li button:before' => 'color: {{VALUE}};',
- ],
- 'condition' => [
- 'navigation' => [ 'dots', 'both' ],
- ],
- ]
- );
- $this->end_controls_section();
- $this->start_controls_section(
- 'section_style_image',
- [
- 'label' => __( 'Image', 'elementor' ),
- 'tab' => Controls_Manager::TAB_STYLE,
- ]
- );
- $this->add_control(
- 'image_spacing',
- [
- 'label' => __( 'Spacing', 'elementor' ),
- 'type' => Controls_Manager::SELECT,
- 'options' => [
- '' => __( 'Default', 'elementor' ),
- 'custom' => __( 'Custom', 'elementor' ),
- ],
- 'default' => '',
- 'condition' => [
- 'slides_to_show!' => '1',
- ],
- ]
- );
- $this->add_control(
- 'image_spacing_custom',
- [
- 'label' => __( 'Image Spacing', 'elementor' ),
- 'type' => Controls_Manager::SLIDER,
- 'range' => [
- 'px' => [
- 'max' => 100,
- ],
- ],
- 'default' => [
- 'size' => 20,
- ],
- 'show_label' => false,
- 'selectors' => [
- '{{WRAPPER}} .slick-list' => 'margin-left: -{{SIZE}}{{UNIT}};',
- '{{WRAPPER}} .slick-slide .slick-slide-inner' => 'padding-left: {{SIZE}}{{UNIT}};',
- ],
- 'condition' => [
- 'image_spacing' => 'custom',
- 'slides_to_show!' => '1',
- ],
- ]
- );
- $this->add_group_control(
- Group_Control_Border::get_type(),
- [
- 'name' => 'image_border',
- 'selector' => '{{WRAPPER}} .elementor-image-carousel-wrapper .elementor-image-carousel .slick-slide-image',
- 'separator' => 'before',
- ]
- );
- $this->add_control(
- 'image_border_radius',
- [
- 'label' => __( 'Border Radius', 'elementor' ),
- 'type' => Controls_Manager::DIMENSIONS,
- 'size_units' => [ 'px', '%' ],
- 'selectors' => [
- '{{WRAPPER}} .elementor-image-carousel-wrapper .elementor-image-carousel .slick-slide-image' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
- ],
- ]
- );
- $this->end_controls_section();
- $this->start_controls_section(
- 'section_caption',
- [
- 'label' => __( 'Caption', 'elementor' ),
- 'tab' => Controls_Manager::TAB_STYLE,
- 'condition' => [
- 'caption_type!' => '',
- ],
- ]
- );
- $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' => 'center',
- 'selectors' => [
- '{{WRAPPER}} .elementor-image-carousel-caption' => 'text-align: {{VALUE}};',
- ],
- ]
- );
- $this->add_control(
- 'caption_text_color',
- [
- 'label' => __( 'Text Color', 'elementor' ),
- 'type' => Controls_Manager::COLOR,
- 'default' => '',
- 'selectors' => [
- '{{WRAPPER}} .elementor-image-carousel-caption' => 'color: {{VALUE}};',
- ],
- ]
- );
- $this->add_group_control(
- Group_Control_Typography::get_type(),
- [
- 'name' => 'caption_typography',
- 'scheme' => Scheme_Typography::TYPOGRAPHY_4,
- 'selector' => '{{WRAPPER}} .elementor-image-carousel-caption',
- ]
- );
- $this->end_controls_section();
- }
- /**
- * Render image carousel 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['carousel'] ) ) {
- return;
- }
- $slides = [];
- foreach ( $settings['carousel'] as $index => $attachment ) {
- $image_url = Group_Control_Image_Size::get_attachment_image_src( $attachment['id'], 'thumbnail', $settings );
- $image_html = '<img class="slick-slide-image" src="' . esc_attr( $image_url ) . '" alt="' . esc_attr( Control_Media::get_image_alt( $attachment ) ) . '" />';
- $link = $this->get_link_url( $attachment, $settings );
- if ( $link ) {
- $link_key = 'link_' . $index;
- $this->add_render_attribute( $link_key, [
- 'href' => $link['url'],
- 'data-elementor-open-lightbox' => $settings['open_lightbox'],
- 'data-elementor-lightbox-slideshow' => $this->get_id(),
- 'data-elementor-lightbox-index' => $index,
- ] );
- if ( Plugin::$instance->editor->is_edit_mode() ) {
- $this->add_render_attribute( $link_key, [
- 'class' => 'elementor-clickable',
- ] );
- }
- if ( ! empty( $link['is_external'] ) ) {
- $this->add_render_attribute( $link_key, 'target', '_blank' );
- }
- if ( ! empty( $link['nofollow'] ) ) {
- $this->add_render_attribute( $link_key, 'rel', 'nofollow' );
- }
- $image_html = '<a ' . $this->get_render_attribute_string( $link_key ) . '>' . $image_html . '</a>';
- }
- $image_caption = $this->get_image_caption( $attachment );
- $slide_html = '<div class="slick-slide"><figure class="slick-slide-inner">' . $image_html;
- if ( ! empty( $image_caption ) ) {
- $slide_html .= '<figcaption class="elementor-image-carousel-caption">' . $image_caption . '</figcaption>';
- }
- $slide_html .= '</figure></div>';
- $slides[] = $slide_html;
- }
- if ( empty( $slides ) ) {
- return;
- }
- $this->add_render_attribute( 'carousel', 'class', 'elementor-image-carousel' );
- if ( 'none' !== $settings['navigation'] ) {
- if ( 'dots' !== $settings['navigation'] ) {
- $this->add_render_attribute( 'carousel', 'class', 'slick-arrows-' . $settings['arrows_position'] );
- }
- if ( 'arrows' !== $settings['navigation'] ) {
- $this->add_render_attribute( 'carousel', 'class', 'slick-dots-' . $settings['dots_position'] );
- }
- }
- if ( 'yes' === $settings['image_stretch'] ) {
- $this->add_render_attribute( 'carousel', 'class', 'slick-image-stretch' );
- }
- ?>
- <div class="elementor-image-carousel-wrapper elementor-slick-slider" dir="<?php echo $settings['direction']; ?>">
- <div <?php echo $this->get_render_attribute_string( 'carousel' ); ?>>
- <?php echo implode( '', $slides ); ?>
- </div>
- </div>
- <?php
- }
- /**
- * Retrieve image carousel link URL.
- *
- * @since 1.0.0
- * @access private
- *
- * @param array $attachment
- * @param object $instance
- *
- * @return array|string|false An array/string containing the attachment URL, or false if no link.
- */
- private function get_link_url( $attachment, $instance ) {
- if ( 'none' === $instance['link_to'] ) {
- return false;
- }
- if ( 'custom' === $instance['link_to'] ) {
- if ( empty( $instance['link']['url'] ) ) {
- return false;
- }
- return $instance['link'];
- }
- return [
- 'url' => wp_get_attachment_url( $attachment['id'] ),
- ];
- }
- /**
- * Retrieve image carousel caption.
- *
- * @since 1.2.0
- * @access private
- *
- * @param array $attachment
- *
- * @return string The caption of the image.
- */
- private function get_image_caption( $attachment ) {
- $caption_type = $this->get_settings_for_display( 'caption_type' );
- if ( empty( $caption_type ) ) {
- return '';
- }
- $attachment_post = get_post( $attachment['id'] );
- if ( 'caption' === $caption_type ) {
- return $attachment_post->post_excerpt;
- }
- if ( 'title' === $caption_type ) {
- return $attachment_post->post_title;
- }
- return $attachment_post->post_content;
- }
- }
|