| 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 |
if ( ! wp_verify_nonce( Fns::get_nonce(), rtsb()->nonceText ) ) { |
| 80 |
wp_send_json_error( esc_html__( 'Something Went Wrong', 'shopbuilder' ) ); |
| 81 |
} |
| 82 |
$product_id = isset( $_POST['product_id'] ) ? absint( $_POST['product_id'] ) : 0; |
| 83 |
|
| 84 |
if ( ! $product_id ) { |
| 85 |
wp_send_json_error( esc_html__( 'Invalid product ID.', 'shopbuilder' ) ); |
| 86 |
} |
| 87 |
|
| 88 |
$product = wc_get_product( $product_id ); |
| 89 |
|
| 90 |
if ( ! $product ) { |
| 91 |
wp_send_json_error( esc_html__( 'Product not found.', 'shopbuilder' ) ); |
| 92 |
} |
| 93 |
|
| 94 |
if ( ! $this->can_user_view_product( $product ) ) { |
| 95 |
wp_send_json_error( esc_html__( 'You do not have permission to view this product.', 'shopbuilder' ) ); |
| 96 |
} |
| 97 |
|
| 98 |
$product_id = isset( $_POST['product_id'] ) ? absint( $_POST['product_id'] ) : 0; |
| 99 |
$images = GalleryFns::get_gallery_images_and_props( $product_id ); |
| 100 |
wp_send_json_success( $images ); |
| 101 |
} |
| 102 |
/** |
| 103 |
* Determines if the current user has permission to view a product. |
| 104 |
* |
| 105 |
* Checks product status and visibility settings against user capabilities. |
| 106 |
* |
| 107 |
* @param \WC_Product $product The product to check access for. |
| 108 |
* @return bool True if user can view the product, false otherwise. |
| 109 |
*/ |
| 110 |
private function can_user_view_product( $product ) { |
| 111 |
$product_id = $product->get_id(); |
| 112 |
$status = get_post_status( $product->get_id() ); |
| 113 |
$visibility = $product->get_catalog_visibility(); |
| 114 |
|
| 115 |
if ( 'publish' === $status && 'visible' === $visibility ) { |
| 116 |
return true; |
| 117 |
} |
| 118 |
|
| 119 |
return current_user_can( 'edit_post', $product_id ); |
| 120 |
} |
| 121 |
/** |
| 122 |
* @param string $template template. |
| 123 |
* @param string $template_name template name. |
| 124 |
* @return mixed|null |
| 125 |
*/ |
| 126 |
public function override_templates( $template, $template_name ) { |
| 127 |
// List of templates you want to override. |
| 128 |
if ( 'single-product/product-image.php' === $template_name ) { |
| 129 |
$galleryStyle = GalleryFns::get_options( 'gallery_style' ); |
| 130 |
$galleryStyle = apply_filters( 'rtsb/vg/thumbnails/position', $galleryStyle ); |
| 131 |
$temp = rtsb()->has_pro() && 'grid' === $galleryStyle ? 'variation-gallery/vg-grid-view' : 'variation-gallery/product-image'; |
| 132 |
$custom_template = Fns::locate_template( $temp ); |
| 133 |
if ( file_exists( $custom_template ) ) { |
| 134 |
return $custom_template; |
| 135 |
} |
| 136 |
} |
| 137 |
return $template; |
| 138 |
} |
| 139 |
/** |
| 140 |
* @return void |
| 141 |
*/ |
| 142 |
public function slider_and_thumbnail_template_js() { |
| 143 |
wp_enqueue_script( 'underscore' ); |
| 144 |
Fns::load_template( 'variation-gallery/vg-slider-template', [] ); |
| 145 |
Fns::load_template( 'variation-gallery/vg-thumbnail-template', [] ); |
| 146 |
} |
| 147 |
} |
| 148 |
|