| 1 |
<?php |
| 2 |
/** |
| 3 |
* Sticky add-to-cart Functions Class. |
| 4 |
* |
| 5 |
* @package Rse\SB |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace RadiusTheme\SB\Modules\VariationGallery; |
| 9 |
|
| 10 |
use RadiusTheme\SB\Helpers\Cache; |
| 11 |
use RadiusTheme\SB\Helpers\Fns; |
| 12 |
use WC_Product; |
| 13 |
|
| 14 |
defined( 'ABSPATH' ) || exit(); |
| 15 |
|
| 16 |
/** |
| 17 |
* Sticky add-to-cart Functions Class. |
| 18 |
*/ |
| 19 |
class GalleryFns { |
| 20 |
/** |
| 21 |
* @param string $key Default Attribute. |
| 22 |
* @param array|string $default Default Attribute. |
| 23 |
* @return array|string |
| 24 |
*/ |
| 25 |
public static function get_options( $key = null, $default = '' ) { |
| 26 |
$options = Fns::get_options( 'modules', 'variation_gallery' ); |
| 27 |
if ( $key ) { |
| 28 |
if ( isset( $options[ $key ] ) ) { |
| 29 |
return $options[ $key ]; |
| 30 |
} else { |
| 31 |
return $default; |
| 32 |
} |
| 33 |
} |
| 34 |
return $options; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Output the HTML image tag for a product attachment. |
| 39 |
* |
| 40 |
* Retrieves the image properties using `product_attachment_props()` and outputs |
| 41 |
* the image using `wp_get_attachment_image()`. Intended for variation galleries |
| 42 |
* or product thumbnails. |
| 43 |
* |
| 44 |
* @param int $image_id The attachment/image ID. |
| 45 |
* @param \WC_Product $product The WooCommerce product object. |
| 46 |
* @param string $size The image size to output. Default 'woocommerce_thumbnail'. |
| 47 |
* |
| 48 |
* @return string The HTML. |
| 49 |
*/ |
| 50 |
public static function get_main_attachment_html( $image_id, $product, $size = 'woocommerce_thumbnail' ) { |
| 51 |
$props = self::product_attachment_props( $image_id, $product ); |
| 52 |
$html = self::wc_get_gallery_image_html( $image_id, true ); |
| 53 |
return apply_filters( 'rtsb/vg/get/main/attachment/html', $html, $props, $image_id, $product, $size ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Get HTML for a gallery image. |
| 58 |
* |
| 59 |
* Hooks: woocommerce_gallery_thumbnail_size, woocommerce_gallery_image_size and woocommerce_gallery_full_size accept name based image sizes, or an array of width/height values. |
| 60 |
* |
| 61 |
* @since 3.3.2 |
| 62 |
* @param int $attachment_id Attachment ID. |
| 63 |
* @param bool $main_image Is this the main image or a thumbnail?. |
| 64 |
* @param int $image_index The image index in the gallery. |
| 65 |
* @return string |
| 66 |
*/ |
| 67 |
public static function wc_get_gallery_image_html( $attachment_id, $main_image = false, $image_index = -1 ) { |
| 68 |
global $product; |
| 69 |
|
| 70 |
$flexslider = (bool) apply_filters( 'woocommerce_single_product_flexslider_enabled', get_theme_support( 'wc-product-gallery-slider' ) ); |
| 71 |
$gallery_thumbnail = wc_get_image_size( 'gallery_thumbnail' ); |
| 72 |
$thumbnail_size = apply_filters( 'woocommerce_gallery_thumbnail_size', [ $gallery_thumbnail['width'], $gallery_thumbnail['height'] ] ); |
| 73 |
$image_size = apply_filters( 'woocommerce_gallery_image_size', $flexslider || $main_image ? 'woocommerce_single' : $thumbnail_size ); |
| 74 |
$full_size = apply_filters( 'woocommerce_gallery_full_size', apply_filters( 'woocommerce_product_thumbnails_large_size', 'full' ) ); |
| 75 |
$thumbnail_src = wp_get_attachment_image_src( $attachment_id, $thumbnail_size ); |
| 76 |
$thumbnail_srcset = wp_get_attachment_image_srcset( $attachment_id, $thumbnail_size ); |
| 77 |
$thumbnail_sizes = wp_get_attachment_image_sizes( $attachment_id, $thumbnail_size ); |
| 78 |
$full_src = wp_get_attachment_image_src( $attachment_id, $full_size ); |
| 79 |
$alt_text = trim( wp_strip_all_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) ); |
| 80 |
$alt_text = ( empty( $alt_text ) && ( $product instanceof WC_Product ) ) ? woocommerce_get_alt_from_product_title_and_position( $product->get_title(), $main_image, $image_index ) : $alt_text; |
| 81 |
|
| 82 |
/** |
| 83 |
* Filters the attributes for the image markup. |
| 84 |
* |
| 85 |
* @since 3.3.2 |
| 86 |
* |
| 87 |
* @param array $image_attributes Attributes for the image markup. |
| 88 |
*/ |
| 89 |
$image_params = apply_filters( |
| 90 |
'woocommerce_gallery_image_html_attachment_image_params', |
| 91 |
[ |
| 92 |
'title' => _wp_specialchars( get_post_field( 'post_title', $attachment_id ), ENT_QUOTES, 'UTF-8', true ), |
| 93 |
'data-caption' => _wp_specialchars( get_post_field( 'post_excerpt', $attachment_id ), ENT_QUOTES, 'UTF-8', true ), |
| 94 |
'data-src' => esc_url( $full_src[0] ), |
| 95 |
'data-large_image' => esc_url( $full_src[0] ), |
| 96 |
'data-large_image_width' => esc_attr( $full_src[1] ), |
| 97 |
'data-large_image_height' => esc_attr( $full_src[2] ), |
| 98 |
'class' => esc_attr( $main_image ? 'wp-post-image' : '' ), |
| 99 |
'alt' => esc_attr( $alt_text ), |
| 100 |
], |
| 101 |
$attachment_id, |
| 102 |
$image_size, |
| 103 |
$main_image |
| 104 |
); |
| 105 |
|
| 106 |
if ( isset( $image_params['title'] ) ) { |
| 107 |
unset( $image_params['title'] ); |
| 108 |
} |
| 109 |
|
| 110 |
$image = wp_get_attachment_image( |
| 111 |
$attachment_id, |
| 112 |
$image_size, |
| 113 |
false, |
| 114 |
$image_params |
| 115 |
); |
| 116 |
|
| 117 |
return $image; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Get the HTML for a variation gallery thumbnail image. |
| 122 |
* |
| 123 |
* Wraps the image in a container div and optionally adds a video-specific class |
| 124 |
* if a video link is associated with the image. The final HTML is passed through |
| 125 |
* a filter for customization. |
| 126 |
* |
| 127 |
* @param int $image_id The attachment/image ID. |
| 128 |
* @param \WC_Product $product The WooCommerce product object. |
| 129 |
* @param string $size The image size to output. Default 'thumbnail'. |
| 130 |
* |
| 131 |
* @return string Filtered HTML for the thumbnail image. |
| 132 |
*/ |
| 133 |
public static function get_thumbnail_attachment_html( $image_id, $product, $size = 'thumbnail' ) { |
| 134 |
$props = self::product_attachment_props( $image_id, $product ); |
| 135 |
$add_video_class = ! empty( $props['rtsb_vg_video_link'] ) ? ' rtsb-vs-video' : ''; |
| 136 |
ob_start(); |
| 137 |
?> |
| 138 |
<div class="rtsb-vs-thumb-item <?php echo esc_attr( $add_video_class ); ?>"> |
| 139 |
<?php Fns::print_html( wp_get_attachment_image( $image_id, $size ), true ); ?> |
| 140 |
</div> |
| 141 |
<?php |
| 142 |
$html = ob_get_clean(); |
| 143 |
return apply_filters( 'rtsb/vg/get/thumbnail/attachment/html', $html, $props, $image_id, $product, $size ); |
| 144 |
} |
| 145 |
/** |
| 146 |
* @param int $image_id image id. |
| 147 |
* @param object $product WC_Product Object. |
| 148 |
* @return array |
| 149 |
*/ |
| 150 |
public static function product_attachment_props( $image_id, $product ) { |
| 151 |
$props = wc_get_product_attachment_props( $image_id, $product ); |
| 152 |
$props['image_id'] = $image_id; |
| 153 |
return apply_filters( 'rtsb/product/attachment/props', $props, $image_id, $product ); |
| 154 |
} |
| 155 |
/** |
| 156 |
* @param int $product_id product id. |
| 157 |
* |
| 158 |
* @return mixed|void |
| 159 |
*/ |
| 160 |
public static function get_gallery_images_and_props( $product_id ) { |
| 161 |
$transient_name = self::get_transient_name( $product_id, 'default-images' ); |
| 162 |
$images = get_transient( $transient_name ); |
| 163 |
if ( false !== $images ) { |
| 164 |
return apply_filters( 'rtsb/vg/get/gallery/images', $images, $product_id ); |
| 165 |
} |
| 166 |
$product = wc_get_product( $product_id ); |
| 167 |
$product_id = $product->get_id(); |
| 168 |
$attachment_ids = $product->get_gallery_image_ids(); |
| 169 |
$post_thumbnail_id = $product->get_image_id(); |
| 170 |
$images = []; |
| 171 |
$post_thumbnail_id = (int) apply_filters( 'rtsb/vg/post/thumbnail/id', $post_thumbnail_id, $attachment_ids, $product ); |
| 172 |
$attachment_ids = (array) apply_filters( 'rtsb/vg/attachment/ids', $attachment_ids, $post_thumbnail_id, $product ); |
| 173 |
if ( ! empty( $post_thumbnail_id ) ) { |
| 174 |
array_unshift( $attachment_ids, $post_thumbnail_id ); |
| 175 |
} |
| 176 |
if ( is_array( $attachment_ids ) && ! empty( $attachment_ids ) ) { |
| 177 |
foreach ( $attachment_ids as $i => $image_id ) { |
| 178 |
if ( $image_id ) { |
| 179 |
$images[ $i ] = self::product_attachment_props( $image_id, $product ); |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
set_transient( $transient_name, $images, 12 * HOUR_IN_SECONDS ); |
| 184 |
Cache::set_transient_cache_key( $transient_name ); |
| 185 |
return apply_filters( 'rtsb/vg/get/gallery/images', $images, $product_id ); |
| 186 |
} |
| 187 |
/** |
| 188 |
* Helper: Get all images transient name for specific variation/product |
| 189 |
* |
| 190 |
* @param int $id unique id. |
| 191 |
* @param string $type transient type. |
| 192 |
* |
| 193 |
* @return string |
| 194 |
*/ |
| 195 |
public static function get_transient_name( $id, $type ) { |
| 196 |
if ( 'default-images' === $type ) { |
| 197 |
$id = self::wpml_get_original_variation_id( $id ); |
| 198 |
$transient_name = sprintf( 'rtsb_vg_default_images_%d', $id ); |
| 199 |
} elseif ( 'variation-images' === $type ) { |
| 200 |
$id = self::wpml_get_original_variation_id( $id ); |
| 201 |
$transient_name = sprintf( 'rtsb_vg_variation_images_%d', $id ); |
| 202 |
} elseif ( 'sizes' === $type ) { |
| 203 |
$transient_name = sprintf( 'rtsb_vg_variation_image_sizes_%d', $id ); |
| 204 |
} elseif ( 'variation' === $type ) { |
| 205 |
$transient_name = sprintf( 'rtsb_vg_variation_%d', $id ); |
| 206 |
} else { |
| 207 |
$transient_name = false; |
| 208 |
} |
| 209 |
return apply_filters( 'rtsb/vg/transient/name', $transient_name, $type, $id ); |
| 210 |
} |
| 211 |
/** |
| 212 |
* Helper: Delete all transient |
| 213 |
* |
| 214 |
* @param int $product_id Product id. |
| 215 |
* @param string $type Transient type. |
| 216 |
*/ |
| 217 |
public static function delete_transients( $product_id = false, $type = '' ) { |
| 218 |
if ( $product_id ) { |
| 219 |
if ( $type ) { |
| 220 |
$default_transient_name = self::get_transient_name( $product_id, $type ); |
| 221 |
delete_transient( $default_transient_name ); |
| 222 |
} else { |
| 223 |
$default_transient_name = self::get_transient_name( $product_id, 'default-images' ); |
| 224 |
delete_transient( $default_transient_name ); |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Helper: WPML - Get original variation ID |
| 231 |
* |
| 232 |
* If WPML is active and this is a translated variaition, get the original ID. |
| 233 |
* |
| 234 |
* @param int $id object id. |
| 235 |
* |
| 236 |
* @return int |
| 237 |
*/ |
| 238 |
public static function wpml_get_original_variation_id( $id ) { |
| 239 |
$wpml_original_variation_id = get_post_meta( $id, '_wcml_duplicate_of_variation', true ); |
| 240 |
if ( $wpml_original_variation_id ) { |
| 241 |
$id = $wpml_original_variation_id; |
| 242 |
} |
| 243 |
return $id; |
| 244 |
} |
| 245 |
} |
| 246 |
|