| 1 |
<?php |
| 2 |
/** |
| 3 |
* Main FilterHooks class. |
| 4 |
* |
| 5 |
* @package RadiusTheme\SB |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace RadiusTheme\SB\Modules\VariationGallery; |
| 9 |
|
| 10 |
use RadiusTheme\SB\Helpers\BuilderFns; |
| 11 |
use RadiusTheme\SB\Helpers\Cache; |
| 12 |
use RadiusTheme\SB\Helpers\Fns; |
| 13 |
use RadiusTheme\SB\Traits\SingletonTrait; |
| 14 |
|
| 15 |
defined( 'ABSPATH' ) || exit(); |
| 16 |
|
| 17 |
/** |
| 18 |
* Main FilterHooks class. |
| 19 |
*/ |
| 20 |
class GalleryFrontEnd { |
| 21 |
|
| 22 |
/** |
| 23 |
* Singleton Trait. |
| 24 |
*/ |
| 25 |
use SingletonTrait; |
| 26 |
|
| 27 |
/** |
| 28 |
* Module Class Constructor. |
| 29 |
*/ |
| 30 |
private function __construct() { |
| 31 |
add_filter( 'woocommerce_locate_template', [ $this, 'override_templates' ], 10, 2 ); |
| 32 |
add_action( 'wp_footer', [ $this, 'slider_and_thumbnail_template_js' ] ); |
| 33 |
add_filter( 'woocommerce_available_variation', [ $this, 'available_variation_gallery' ], 90, 3 ); |
| 34 |
|
| 35 |
add_action( 'wp_ajax_rtsb_vg_get_default_gallery_images', [ $this, 'get_default_gallery_images' ] ); |
| 36 |
add_action( 'wp_ajax_nopriv_rtsb_vg_get_default_gallery_images', [ $this, 'get_default_gallery_images' ] ); |
| 37 |
} |
| 38 |
/** |
| 39 |
* @param array $available_variation Available variation. |
| 40 |
* @param object $variationProductObject object. |
| 41 |
* @param object $variation variation. |
| 42 |
* |
| 43 |
* @return array |
| 44 |
*/ |
| 45 |
public function available_variation_gallery( $available_variation, $variationProductObject, $variation ) { |
| 46 |
$variation_id = absint( $variation->get_id() ); |
| 47 |
$transient_name = GalleryFns::get_transient_name( $variation_id, 'variation-images' ); |
| 48 |
$images = get_transient( $transient_name ); |
| 49 |
if ( ! empty( $images ) && is_array( $images ) ) { |
| 50 |
$available_variation['variation_gallery_images'] = $images; |
| 51 |
return apply_filters( 'rtsb/vg/available/variation/gallery', $available_variation, $variation, $variation_id ); |
| 52 |
} |
| 53 |
$has_variation_gallery_images = (bool) get_post_meta( $variation_id, 'rtsb_vg_images', true ); |
| 54 |
if ( $has_variation_gallery_images ) { |
| 55 |
$gallery_images = (array) get_post_meta( $variation_id, 'rtsb_vg_images', true ); |
| 56 |
} else { |
| 57 |
$gallery_images = $variationProductObject->get_gallery_image_ids(); |
| 58 |
} |
| 59 |
$variation_image_id = absint( $variation->get_image_id() ); |
| 60 |
if ( ! empty( $variation_image_id ) ) { |
| 61 |
array_unshift( $gallery_images, $variation_image_id ); |
| 62 |
} |
| 63 |
$gallery_images = array_values( array_unique( $gallery_images ) ); |
| 64 |
$images = []; |
| 65 |
foreach ( $gallery_images as $i => $image_id ) { |
| 66 |
if ( $image_id ) { |
| 67 |
$images[ $i ] = GalleryFns::product_attachment_props( $image_id, $variation ); |
| 68 |
} |
| 69 |
} |
| 70 |
set_transient( $transient_name, $images, HOUR_IN_SECONDS * 12 ); |
| 71 |
Cache::set_transient_cache_key( $transient_name ); |
| 72 |
$available_variation['variation_gallery_images'] = $images; |
| 73 |
return apply_filters( 'rtsb/vg/available/variation/gallery', $available_variation, $variation, $variation_id ); |
| 74 |
} |
| 75 |
/** |
| 76 |
* @return void |
| 77 |
*/ |
| 78 |
public function get_default_gallery_images() { |
| 79 |
// phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 80 |
$product_id = isset( $_POST['product_id'] ) ? absint( $_POST['product_id'] ) : 0; |
| 81 |
$images = GalleryFns::get_gallery_images_and_props( $product_id ); |
| 82 |
wp_send_json_success( $images ); |
| 83 |
} |
| 84 |
/** |
| 85 |
* @param string $template template. |
| 86 |
* @param string $template_name template name. |
| 87 |
* @return mixed|null |
| 88 |
*/ |
| 89 |
public function override_templates( $template, $template_name ) { |
| 90 |
// List of templates you want to override. |
| 91 |
if ( 'single-product/product-image.php' === $template_name ) { |
| 92 |
$galleryStyle = GalleryFns::get_options( 'gallery_style' ); |
| 93 |
$galleryStyle = apply_filters( 'rtsb/vg/thumbnails/position', $galleryStyle ); |
| 94 |
$temp = rtsb()->has_pro() && 'grid' === $galleryStyle ? 'variation-gallery/vg-grid-view' : 'variation-gallery/product-image'; |
| 95 |
$custom_template = Fns::locate_template( $temp ); |
| 96 |
if ( file_exists( $custom_template ) ) { |
| 97 |
return $custom_template; |
| 98 |
} |
| 99 |
} |
| 100 |
return $template; |
| 101 |
} |
| 102 |
/** |
| 103 |
* @return void |
| 104 |
*/ |
| 105 |
public function slider_and_thumbnail_template_js() { |
| 106 |
wp_enqueue_script( 'underscore' ); |
| 107 |
Fns::load_template( 'variation-gallery/vg-slider-template', [] ); |
| 108 |
Fns::load_template( 'variation-gallery/vg-thumbnail-template', [] ); |
| 109 |
} |
| 110 |
} |
| 111 |
|