<?php
namespace Calio\Widgets;

use Elementor\Controls_Manager;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Typography;

if (!defined('ABSPATH')) {
    exit;
}

/**
 * Renders gallery layouts with optional lightbox helpers, badges and layered captions.
 */
class ImageGalleryWidget extends BaseWidget {
    public function get_name(): string { return 'calio-image-gallery'; }
    public function get_title(): string { return esc_html__('Image Gallery', 'calio-addon'); }
    public function get_icon(): string { return 'eicon-gallery-grid'; }
    public function get_style_depends(): array { return $this->resolve_widget_style_depends(); }

    /**
     * Registers gallery content and style controls for layout, captions and hover states.
     */
    protected function register_controls(): void {
        $this->start_controls_section('gallery', ['label' => esc_html__('Gallery', 'calio-addon')]);
        $this->add_control('images', ['label' => esc_html__('Add Images', 'calio-addon'), 'type' => Controls_Manager::GALLERY, 'default' => []]);
        $this->add_control('columns', ['label' => esc_html__('Columns', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => '3', 'options' => ['2' => '2', '3' => '3', '4' => '4']]);
        $this->add_control('layout_skin', ['label' => esc_html__('Layout Skin', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'grid', 'options' => ['grid' => esc_html__('Grid', 'calio-addon'), 'cards' => esc_html__('Cards', 'calio-addon'), 'overlay' => esc_html__('Overlay', 'calio-addon')]]);
        $this->add_control('chrome', ['label' => esc_html__('Card Chrome', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'flat', 'options' => ['flat' => esc_html__('Flat', 'calio-addon'), 'framed' => esc_html__('Framed', 'calio-addon'), 'shadowed' => esc_html__('Shadowed', 'calio-addon')]]);
        $this->add_control('image_ratio', ['label' => esc_html__('Image Ratio', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'landscape', 'options' => ['auto' => esc_html__('Auto', 'calio-addon'), 'square' => esc_html__('Square', 'calio-addon'), 'landscape' => esc_html__('Landscape', 'calio-addon'), 'portrait' => esc_html__('Portrait', 'calio-addon')]]);
        $this->add_control('lightbox', ['label' => esc_html__('Open in Lightbox', 'calio-addon'), 'type' => Controls_Manager::SWITCHER, 'return_value' => 'yes', 'default' => 'yes']);
        $this->add_control('show_zoom_hint', ['label' => esc_html__('Show Zoom Hint', 'calio-addon'), 'type' => Controls_Manager::SWITCHER, 'return_value' => 'yes', 'default' => 'yes', 'condition' => ['lightbox' => 'yes']]);
        $this->add_control('show_index', ['label' => esc_html__('Show Index Badge', 'calio-addon'), 'type' => Controls_Manager::SWITCHER, 'return_value' => 'yes', 'default' => '']);
        $this->add_control('show_caption', ['label' => esc_html__('Show Caption', 'calio-addon'), 'type' => Controls_Manager::SWITCHER, 'return_value' => 'yes', 'default' => 'yes']);
        $this->add_control('meta_badge_text', ['label' => esc_html__('Meta Badge Text', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => esc_html__('Featured', 'calio-addon'), 'condition' => ['show_caption' => 'yes']]);
        $this->add_control('caption_eyebrow', ['label' => esc_html__('Caption Eyebrow', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => '', 'condition' => ['show_caption' => 'yes']]);
        $this->add_control('caption_mode', ['label' => esc_html__('Caption Mode', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'below', 'options' => ['below' => esc_html__('Below', 'calio-addon'), 'overlay' => esc_html__('Overlay', 'calio-addon')], 'condition' => ['show_caption' => 'yes']]);
        $this->add_control('meta_mode', ['label' => esc_html__('Meta Mode', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'badge', 'options' => ['badge' => esc_html__('Badge', 'calio-addon'), 'inline' => esc_html__('Inline Meta', 'calio-addon'), 'hidden' => esc_html__('Hidden', 'calio-addon')], 'condition' => ['show_caption' => 'yes']]);
        $this->add_control('caption_skin', ['label' => esc_html__('Caption Skin', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'soft', 'options' => ['soft' => esc_html__('Soft', 'calio-addon'), 'solid' => esc_html__('Solid', 'calio-addon'), 'glass' => esc_html__('Glass', 'calio-addon')], 'condition' => ['show_caption' => 'yes']]);
        $this->add_control('caption_reveal', ['label' => esc_html__('Caption Reveal', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'always', 'options' => ['always' => esc_html__('Always', 'calio-addon'), 'hover' => esc_html__('On Hover', 'calio-addon')], 'condition' => ['show_caption' => 'yes']]);
        $this->add_control('info_layout', ['label' => esc_html__('Info Layout', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'stacked', 'options' => ['compact' => esc_html__('Compact', 'calio-addon'), 'stacked' => esc_html__('Stacked', 'calio-addon'), 'split' => esc_html__('Split', 'calio-addon')], 'condition' => ['show_caption' => 'yes']]);
        $this->add_control('show_title_divider', ['label' => esc_html__('Show Title Divider', 'calio-addon'), 'type' => Controls_Manager::SWITCHER, 'return_value' => 'yes', 'condition' => ['show_caption' => 'yes']]);
        $this->add_control('hover_skin', ['label' => esc_html__('Hover Skin', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'zoom', 'options' => ['none' => esc_html__('None', 'calio-addon'), 'zoom' => esc_html__('Zoom', 'calio-addon'), 'fade' => esc_html__('Fade', 'calio-addon'), 'lift' => esc_html__('Lift', 'calio-addon')]]);
        $this->add_control('image_filter', ['label' => esc_html__('Image Mood', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'none', 'options' => ['none' => esc_html__('None', 'calio-addon'), 'grayscale' => esc_html__('Grayscale', 'calio-addon'), 'warm' => esc_html__('Warm', 'calio-addon'), 'cool' => esc_html__('Cool', 'calio-addon')]]);
        $this->add_control('overlay_depth', ['label' => esc_html__('Overlay Depth', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'medium', 'options' => ['soft' => esc_html__('Soft', 'calio-addon'), 'medium' => esc_html__('Medium', 'calio-addon'), 'strong' => esc_html__('Strong', 'calio-addon')], 'condition' => ['layout_skin' => 'overlay']]);
        $this->add_control('overlay_preset', ['label' => esc_html__('Overlay Preset', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'standard', 'options' => ['standard' => esc_html__('Standard', 'calio-addon'), 'spotlight' => esc_html__('Spotlight', 'calio-addon'), 'editorial' => esc_html__('Editorial', 'calio-addon')], 'condition' => ['layout_skin' => 'overlay', 'show_caption' => 'yes']]);
        $this->add_control('gap', ['label' => esc_html__('Gap', 'calio-addon'), 'type' => Controls_Manager::SLIDER, 'range' => ['px' => ['min' => 0, 'max' => 40]], 'selectors' => ['{{WRAPPER}} .calio-image-gallery' => 'gap: {{SIZE}}{{UNIT}};']]);
        $this->add_dimensions_control('card_padding', esc_html__('Card Padding', 'calio-addon'), '{{WRAPPER}} .calio-image-gallery.skin-cards .calio-image-gallery-item', 'padding');
        $this->add_dimensions_control('card_margin', esc_html__('Card Margin', 'calio-addon'), '{{WRAPPER}} .calio-image-gallery-item', 'margin');
        $this->add_control('image_position', ['label' => esc_html__('Image Position', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'center center', 'options' => ['center center' => esc_html__('Center', 'calio-addon'), 'center top' => esc_html__('Top', 'calio-addon'), 'center bottom' => esc_html__('Bottom', 'calio-addon')], 'selectors' => ['{{WRAPPER}} .calio-image-gallery-media img' => 'object-position: {{VALUE}};']]);
        $this->add_dimensions_control('radius', esc_html__('Radius', 'calio-addon'), ['{{WRAPPER}} .calio-image-gallery-media', '{{WRAPPER}} .calio-image-gallery-media img', '{{WRAPPER}} .calio-image-gallery figcaption'], 'border-radius', ['size_units' => ['px', '%']]);
        $this->add_control('caption_align', ['label' => esc_html__('Caption Alignment', 'calio-addon'), 'type' => Controls_Manager::CHOOSE, 'default' => 'left', 'condition' => ['show_caption' => 'yes'], 'options' => ['left' => ['title' => esc_html__('Left', 'calio-addon'), 'icon' => 'eicon-text-align-left'], 'center' => ['title' => esc_html__('Center', 'calio-addon'), 'icon' => 'eicon-text-align-center'], 'right' => ['title' => esc_html__('Right', 'calio-addon'), 'icon' => 'eicon-text-align-right']], 'selectors' => ['{{WRAPPER}} .calio-image-gallery figcaption' => 'text-align: {{VALUE}};']]);
        $this->end_controls_section();
        $this->start_controls_section('style_image', ['label' => esc_html__('Image', 'calio-addon'), 'tab' => Controls_Manager::TAB_STYLE]);
        $this->add_style_heading('gallery_image_surface_heading', esc_html__('Surface States', 'calio-addon'));
        $this->start_controls_tabs('gallery_image_state_tabs');
        $this->start_controls_tab('gallery_image_state_normal', ['label' => esc_html__('Normal', 'calio-addon')]);
        $this->add_border_paint_controls('gallery_media_border_normal', esc_html__('Border Color', 'calio-addon'), '{{WRAPPER}} .calio-image-gallery-media');
        $this->add_background_type_control('gallery_img_bg_normal_group', '{{WRAPPER}} .calio-image-gallery-item');
        $this->end_controls_tab();
        $this->start_controls_tab('gallery_image_state_hover', ['label' => esc_html__('Hover', 'calio-addon')]);
        $this->add_border_paint_controls('gallery_media_border_hover', esc_html__('Border Color', 'calio-addon'), '{{WRAPPER}} .calio-image-gallery-item:hover .calio-image-gallery-media');
        $this->add_background_type_control('gallery_img_bg_hover_group', '{{WRAPPER}} .calio-image-gallery-item:hover');
        $this->add_control('img_hover_opacity', ['label' => esc_html__('Hover Opacity', 'calio-addon'), 'type' => Controls_Manager::SLIDER, 'range' => ['px' => ['min' => 0.3, 'max' => 1, 'step' => 0.05]], 'selectors' => ['{{WRAPPER}} .calio-image-gallery-item:hover img' => 'opacity: {{SIZE}};']]);
        $this->add_control('img_hover_scale', ['label' => esc_html__('Hover Scale', 'calio-addon'), 'type' => Controls_Manager::SLIDER, 'range' => ['px' => ['min' => 1, 'max' => 1.2, 'step' => 0.01]], 'selectors' => ['{{WRAPPER}} .calio-image-gallery-item:hover img' => 'transform: scale({{SIZE}});']]);
        $this->end_controls_tab();
        $this->end_controls_tabs();
        $this->add_style_heading('gallery_image_outline_heading', esc_html__('Border Type & Shadow', 'calio-addon'));
        $this->add_group_control(Group_Control_Border::get_type(), ['name' => 'gallery_image_border_group', 'selector' => '{{WRAPPER}} .calio-image-gallery-media']);
        $this->add_group_control(Group_Control_Box_Shadow::get_type(), ['name' => 'gallery_image_shadow_group', 'selector' => '{{WRAPPER}} .calio-image-gallery-item']);
        $this->add_style_heading('gallery_image_spacing_heading', esc_html__('Shape & Spacing', 'calio-addon'));
        $this->add_dimensions_control('caption_padding', esc_html__('Caption Padding', 'calio-addon'), '{{WRAPPER}} .calio-image-gallery figcaption', 'padding');
        $this->add_dimensions_control('caption_margin', esc_html__('Caption Margin', 'calio-addon'), '{{WRAPPER}} .calio-image-gallery figcaption', 'margin');
        $this->end_controls_section();

        $this->start_controls_section('style_caption', ['label' => esc_html__('Caption', 'calio-addon'), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => ['show_caption' => 'yes']]);
        $this->add_style_heading('gallery_caption_surface_heading', esc_html__('Surface States', 'calio-addon'));
        $this->start_controls_tabs('gallery_caption_state_tabs');
        $this->start_controls_tab('gallery_caption_state_normal', ['label' => esc_html__('Normal', 'calio-addon')]);
        $this->add_text_paint_controls('gallery_caption_text_normal', esc_html__('Text Color', 'calio-addon'), '{{WRAPPER}} .calio-image-gallery figcaption');
        $this->add_background_type_control('gallery_caption_bg_normal_group', '{{WRAPPER}} .calio-image-gallery figcaption');
        $this->add_group_control(Group_Control_Typography::get_type(), ['name' => 'caption_typography', 'selector' => '{{WRAPPER}} .calio-image-gallery figcaption']);
        $this->add_text_paint_controls('gallery_title_normal', esc_html__('Title Color', 'calio-addon'), '{{WRAPPER}} .calio-gallery-caption-title');
        $this->add_text_paint_controls('gallery_eyebrow_normal', esc_html__('Eyebrow Color', 'calio-addon'), '{{WRAPPER}} .calio-gallery-caption-eyebrow');
        $this->add_background_paint_controls('gallery_divider_normal', esc_html__('Divider Color', 'calio-addon'), '{{WRAPPER}} .calio-gallery-caption-divider');
        $this->add_dimensions_control('caption_radius', esc_html__('Caption Radius', 'calio-addon'), '{{WRAPPER}} .calio-image-gallery figcaption', 'border-radius', ['size_units' => ['px', '%']]);
        $this->add_text_paint_controls('gallery_badge_text_normal', esc_html__('Badge Text Color', 'calio-addon'), '{{WRAPPER}} .calio-gallery-meta-badge');
        $this->add_background_type_control('gallery_badge_bg_normal_group', '{{WRAPPER}} .calio-gallery-meta-badge');
        $this->add_text_paint_controls('gallery_index_text', esc_html__('Index Text Color', 'calio-addon'), '{{WRAPPER}} .calio-gallery-index');
        $this->add_background_type_control('gallery_index_bg_group', '{{WRAPPER}} .calio-gallery-index');
        $this->add_text_paint_controls('gallery_zoom_text_normal', esc_html__('Zoom Hint Color', 'calio-addon'), '{{WRAPPER}} .calio-gallery-zoom-hint');
        $this->add_background_type_control('gallery_zoom_bg_normal_group', '{{WRAPPER}} .calio-gallery-zoom-hint');
        $this->add_background_paint_controls('gallery_overlay_normal', esc_html__('Overlay Tint', 'calio-addon'), '{{WRAPPER}} .calio-image-gallery.skin-overlay .calio-image-gallery-media::after');
        $this->end_controls_tab();
        $this->start_controls_tab('gallery_caption_state_hover', ['label' => esc_html__('Hover', 'calio-addon')]);
        $this->add_text_paint_controls('gallery_caption_text_hover', esc_html__('Text Color', 'calio-addon'), '{{WRAPPER}} .calio-image-gallery-item:hover figcaption');
        $this->add_background_type_control('gallery_caption_bg_hover_group', '{{WRAPPER}} .calio-image-gallery-item:hover figcaption');
        $this->add_text_paint_controls('gallery_title_hover', esc_html__('Title Color', 'calio-addon'), '{{WRAPPER}} .calio-image-gallery-item:hover .calio-gallery-caption-title');
        $this->add_text_paint_controls('gallery_badge_text_hover', esc_html__('Badge Text Color', 'calio-addon'), '{{WRAPPER}} .calio-image-gallery-item:hover .calio-gallery-meta-badge');
        $this->add_background_type_control('gallery_badge_bg_hover_group', '{{WRAPPER}} .calio-image-gallery-item:hover .calio-gallery-meta-badge');
        $this->add_text_paint_controls('gallery_zoom_text_hover', esc_html__('Zoom Hint Color', 'calio-addon'), '{{WRAPPER}} .calio-image-gallery-item:hover .calio-gallery-zoom-hint');
        $this->add_background_type_control('gallery_zoom_bg_hover_group', '{{WRAPPER}} .calio-image-gallery-item:hover .calio-gallery-zoom-hint');
        $this->add_background_paint_controls('gallery_overlay_hover', esc_html__('Overlay Tint', 'calio-addon'), '{{WRAPPER}} .calio-image-gallery.skin-overlay .calio-image-gallery-item:hover .calio-image-gallery-media::after');
        $this->end_controls_tab();
        $this->start_controls_tab('gallery_caption_state_active', ['label' => esc_html__('Active', 'calio-addon')]);
        $this->add_text_paint_controls('gallery_caption_text_active', esc_html__('Text Color', 'calio-addon'), '{{WRAPPER}} .calio-image-gallery-item:focus-within figcaption');
        $this->add_background_type_control('gallery_caption_bg_active_group', '{{WRAPPER}} .calio-image-gallery-item:focus-within figcaption');
        $this->add_text_paint_controls('gallery_title_active', esc_html__('Title Color', 'calio-addon'), '{{WRAPPER}} .calio-image-gallery-item:focus-within .calio-gallery-caption-title');
        $this->add_text_paint_controls('gallery_badge_text_active', esc_html__('Badge Text Color', 'calio-addon'), '{{WRAPPER}} .calio-image-gallery-item:focus-within .calio-gallery-meta-badge');
        $this->add_background_type_control('gallery_badge_bg_active_group', '{{WRAPPER}} .calio-image-gallery-item:focus-within .calio-gallery-meta-badge');
        $this->add_text_paint_controls('gallery_zoom_text_active', esc_html__('Zoom Hint Color', 'calio-addon'), '{{WRAPPER}} .calio-image-gallery-item:focus-within .calio-gallery-zoom-hint');
        $this->add_background_type_control('gallery_zoom_bg_active_group', '{{WRAPPER}} .calio-image-gallery-item:focus-within .calio-gallery-zoom-hint');
        $this->add_background_paint_controls('gallery_overlay_active', esc_html__('Overlay Tint', 'calio-addon'), '{{WRAPPER}} .calio-image-gallery.skin-overlay .calio-image-gallery-item:focus-within .calio-image-gallery-media::after');
        $this->end_controls_tab();
        $this->end_controls_tabs();
        $this->add_style_heading('gallery_caption_outline_heading', esc_html__('Border Type & Shadow', 'calio-addon'));
        $this->add_group_control(Group_Control_Border::get_type(), ['name' => 'gallery_caption_border_group', 'selector' => '{{WRAPPER}} .calio-image-gallery figcaption']);
        $this->add_group_control(Group_Control_Box_Shadow::get_type(), ['name' => 'gallery_caption_shadow_group', 'selector' => '{{WRAPPER}} .calio-image-gallery figcaption']);
        $this->add_style_heading('gallery_caption_meta_heading', esc_html__('Meta & Helpers', 'calio-addon'));
        $this->end_controls_section();
    }

    /**
     * Outputs sanitized gallery markup with optional overlay helpers and caption metadata.
     */
    protected function render(): void {
        $s = $this->get_settings_for_display();
        $images = $s['images'] ?? [];
        if (!is_array($images) || empty($images)) { return; }

        $showCaption = ($s['show_caption'] ?? 'yes') === 'yes';
        $showIndex = ($s['show_index'] ?? '') === 'yes';
        $showZoomHint = ($s['lightbox'] ?? 'yes') === 'yes' && ($s['show_zoom_hint'] ?? 'yes') === 'yes';
        $metaBadgeText = trim((string) ($s['meta_badge_text'] ?? ''));
        $metaMode = (string) ($s['meta_mode'] ?? 'badge');
        if (!in_array($metaMode, ['badge', 'inline', 'hidden'], true)) { $metaMode = 'badge'; }

        $classes = ['calio-image-gallery', 'calio-cols-' . sanitize_html_class((string) ($s['columns'] ?? '3')), 'skin-' . sanitize_html_class((string) ($s['layout_skin'] ?? 'grid')), 'chrome-' . sanitize_html_class((string) ($s['chrome'] ?? 'flat')), 'ratio-' . sanitize_html_class((string) ($s['image_ratio'] ?? 'landscape')), 'hover-' . sanitize_html_class((string) ($s['hover_skin'] ?? 'zoom')), 'filter-' . sanitize_html_class((string) ($s['image_filter'] ?? 'none')), 'meta-' . sanitize_html_class($metaMode), 'info-layout-' . sanitize_html_class((string) ($s['info_layout'] ?? 'stacked')), 'overlay-depth-' . sanitize_html_class((string) ($s['overlay_depth'] ?? 'medium')), 'overlay-preset-' . sanitize_html_class((string) ($s['overlay_preset'] ?? 'standard'))];
        if ($showCaption) {
            $classes[] = 'caption-' . sanitize_html_class((string) ($s['caption_mode'] ?? 'below'));
            $classes[] = 'caption-skin-' . sanitize_html_class((string) ($s['caption_skin'] ?? 'soft'));
            $classes[] = 'caption-reveal-' . sanitize_html_class((string) ($s['caption_reveal'] ?? 'always'));
        } else {
            $classes[] = 'caption-hidden';
        }
        if ($showIndex) {
            $classes[] = 'has-index';
        }
        if ($showZoomHint) {
            $classes[] = 'has-zoom-hint';
        }
        if (($s['show_title_divider'] ?? '') === 'yes') {
            $classes[] = 'has-title-divider';
        }

        echo '<div class="' . esc_attr(implode(' ', $classes)) . '">';
        foreach ($images as $index => $image) {
            $url = (string) ($image['url'] ?? '');
            if ($url === '') { continue; }

            $caption = trim((string) ($image['caption'] ?? ''));
            $alt = trim((string) ($image['alt'] ?? ''));
            $attachmentId = isset($image['id']) ? absint($image['id']) : 0;
            $title = $attachmentId > 0 ? get_the_title($attachmentId) : '';
            if (!is_string($title)) {
                $title = '';
            }
            $title = trim($title);
            if ($title === '') {
                $title = $alt !== '' ? $alt : sprintf(
                    /* translators: %d: gallery item number. */
                    esc_html__('Gallery Item %d', 'calio-addon'),
                    $index + 1
                );
            }

            echo '<figure class="calio-image-gallery-item">';
            echo '<div class="calio-image-gallery-media">';
            if ($showIndex) {
                echo '<span class="calio-gallery-index">' . esc_html(sprintf('%02d', $index + 1)) . '</span>';
            }
            if ($showZoomHint) {
                echo '<span class="calio-gallery-zoom-hint" aria-hidden="true">+</span>';
            }
            if (($s['lightbox'] ?? 'yes') === 'yes') {
                echo '<a href="' . esc_url($url) . '" data-elementor-open-lightbox="yes">';
            }
            echo '<img src="' . esc_url($url) . '" alt="' . esc_attr($alt) . '" loading="lazy" />';
            if (($s['lightbox'] ?? 'yes') === 'yes') {
                echo '</a>';
            }
            if ($showCaption) {
                echo '<figcaption>';
                if (!empty($s['caption_eyebrow'])) { echo '<span class="calio-gallery-caption-eyebrow">' . esc_html((string) $s['caption_eyebrow']) . '</span>'; }
                echo '<div class="calio-gallery-caption-top">';
                echo '<span class="calio-gallery-caption-title">' . esc_html($title) . '</span>';
                if ($metaMode === 'badge' && $metaBadgeText !== '') {
                    echo '<span class="calio-gallery-meta-badge">' . esc_html($metaBadgeText) . '</span>';
                }
                if ($metaMode === 'inline') {
                    $metaInline = ($metaBadgeText !== '' ? $metaBadgeText : esc_html__('Portfolio', 'calio-addon')) . ' ' . sprintf('%02d/%02d', $index + 1, count($images));
                    echo '<span class="calio-gallery-meta-inline">' . esc_html($metaInline) . '</span>';
                }
                echo '</div>';
                if (($s['show_title_divider'] ?? '') === 'yes') { echo '<span class="calio-gallery-caption-divider" aria-hidden="true"></span>'; }
                if ($caption !== '') {
                    echo '<div class="calio-gallery-caption-text">' . esc_html($caption) . '</div>';
                }
                echo '</figcaption>';
            }
            echo '</div>';
            echo '</figure>';
        }
        echo '</div>';
    }
}


















