<?php

/**
 * Provide a public-facing view for the plugin
 *
 * This file is used to markup the public-facing aspects of the plugin.
 *
 * @link       https://wondercode.pro/
 * @since      1.0.0
 *
 * @package    Awesome_Designs_For_Woocommerce
 * @subpackage Awesome_Designs_For_Woocommerce/public/partials
 */

// If this file is called directly, abort.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}


add_filter( 'woocommerce_product_get_image', 'awesome_designs_for_woocommerce_product_get_image', 10, 6 );

function awesome_designs_for_woocommerce_product_get_image( $image, $product, $size, $attr, $placeholder, $image_size ) {
    // Ensure the custom size is used
    $size = 'woocommerce_thumbnail';

    if ( ! is_array( $attr ) ) {
        $attr = array();
    }

    if ( ! is_bool( $placeholder ) ) {
        $placeholder = true;
    }

    // Get the main product image
    remove_filter( 'woocommerce_product_get_image', 'awesome_designs_for_woocommerce_product_get_image', 10, 6 );
    $main_image = $product->get_image( $size, $attr, $placeholder );
    add_filter( 'woocommerce_product_get_image', 'awesome_designs_for_woocommerce_product_get_image', 10, 6 );

    // Get the gallery images
    $gallery_image_ids = $product->get_gallery_image_ids();

    // Get the first gallery image if it exists
    $first_gallery_image = '';
    if ( ! empty( $gallery_image_ids ) ) {
        $first_gallery_image_id = $gallery_image_ids[0];
        $first_gallery_image = wp_get_attachment_image( $first_gallery_image_id, $size, false, $attr );
    }

    // Return both the main image and the first gallery image wrapped in spans
    if ( $main_image || $first_gallery_image ) {
        $image = '<div class="adfw-product-thumbnails">' .
                     ( $main_image ? '<span class="adfw-product-thumb">' . $main_image . '</span>' : '' ) .
                     ( $first_gallery_image ? '<span class="adfw-product-gallery-thumb">' . $first_gallery_image . '</span>' : '' ) .
                 '</div>';
    }

    return $image;
}



add_filter('wc_get_template_part', 'awesome_designs_for_woocommerce_get_template_part', 10, 3);

function awesome_designs_for_woocommerce_get_template_part($template, $slug, $name) {
    if ($slug === 'content' && $name === 'product') {
        $template = dirname(plugin_dir_path(__FILE__)) . '/templates/3/content-product.php';
    }
    return $template;
}