/*
Theme Name:        ArtFlow
Text Domain:       artflow
Version:           2.0.6
Description:       Artist, Painter Portfolio WordPress Theme
Requires at least: 6.0
Requires PHP:      7.0
Tags:              portfolio
Author:            Pixelwars
Author URI:        https://themeforest.net/user/pixelwars
Theme URI:         https://themeforest.net/user/pixelwars/portfolio
License:           ThemeForest License
License URI:       https://themeforest.net/licenses
Domain Path:       /languages
*/


/*
    Do not remove any of the commented text above as it is used by the theme for proper function!
*/

function artflow_scrolling_gallery_shortcode($atts) {
    // Parse shortcode attributes
    $atts = shortcode_atts(
        [
            'template' => 'vertical', // Default template
            'page_id' => 2116,        // Default page ID
        ],
        $atts,
        'scrolling_gallery'
    );

    ob_start();

    // Get the specified page
    $page = get_post($atts['page_id']);
    if ($page) {
        // Fetch gallery data for the specified page
        $gallery = get_post_gallery($page->ID, false);

        if ($gallery && isset($gallery['ids'])) {
            // Set CSS classes for horizontal or vertical scrolling
            $gallery_class = ($atts['template'] === 'horizontal') ? 'is-horizontal' : 'is-vertical';
            $data_id = ($atts['template'] === 'horizontal') ? '1' : '0';

            ?>
            <div class="iasg is-masked <?php echo esc_attr($gallery_class); ?>" data-id="<?php echo esc_attr($data_id); ?>">
                <div>
                    <?php
                    // Get image IDs from the gallery
                    $image_ids = explode(',', $gallery['ids']);

                    foreach ($image_ids as $image_id) {
                        // Fetch image URL and alt text
                        $image_url = wp_get_attachment_image_url($image_id, 'full');
                        $image_alt = get_post_meta($image_id, '_wp_attachment_image_alt', true);

                        if ($image_url) {
                            ?>
                            <figure>
                                <img src="<?php echo esc_url($image_url); ?>" alt="<?php echo esc_attr($image_alt); ?>">
                            </figure>
                            <?php
                        }
                    }
                    ?>
                </div>
            </div>
            <?php
        } else {
            echo '<p>No gallery found for page ID ' . esc_html($atts['page_id']) . '.</p>';
        }
    } else {
        echo '<p>Page not found.</p>';
    }

    return ob_get_clean();
}

// Register the shortcode
add_shortcode('scrolling_gallery', 'artflow_scrolling_gallery_shortcode');
