| 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 GalleryAdmin { |
| 21 |
|
| 22 |
/** |
| 23 |
* Singleton Trait. |
| 24 |
*/ |
| 25 |
use SingletonTrait; |
| 26 |
|
| 27 |
/** |
| 28 |
* Module Class Constructor. |
| 29 |
*/ |
| 30 |
private function __construct() { |
| 31 |
add_action( 'admin_footer', [ $this, 'admin_template_js' ] ); |
| 32 |
add_action( 'woocommerce_product_after_variable_attributes', [ $this, 'gallery_admin_html' ], 10, 3 ); |
| 33 |
add_action( 'woocommerce_save_product_variation', [ $this, 'save_variation_gallery' ] ); |
| 34 |
add_action( 'add_meta_boxes', [ $this, 'add_metabox' ] ); |
| 35 |
add_action( 'save_post', [ $this, 'save_metabox' ] ); |
| 36 |
|
| 37 |
// Invalidate cached variation galleries when product data or images change. |
| 38 |
add_action( 'woocommerce_update_product', [ $this, 'flush_product_gallery_cache' ] ); |
| 39 |
add_action( 'woocommerce_save_product_variation', [ $this, 'flush_variation_parent_cache' ] ); |
| 40 |
add_action( 'delete_attachment', [ $this, 'flush_attachment_gallery_cache' ] ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Flush all cached gallery transients for a saved product. |
| 45 |
* |
| 46 |
* @param int $product_id Product ID. |
| 47 |
* |
| 48 |
* @return void |
| 49 |
*/ |
| 50 |
public function flush_product_gallery_cache( $product_id ) { |
| 51 |
GalleryFns::flush_product_gallery_transients( $product_id ); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Flush the parent product default-gallery transient when a variation is saved. |
| 56 |
* |
| 57 |
* The variation's own transient is already cleared in save_variation_gallery(); |
| 58 |
* this also clears the parent product default-images cache. |
| 59 |
* |
| 60 |
* @param int $variation_id Variation ID. |
| 61 |
* |
| 62 |
* @return void |
| 63 |
*/ |
| 64 |
public function flush_variation_parent_cache( $variation_id ) { |
| 65 |
$parent_id = wp_get_post_parent_id( $variation_id ); |
| 66 |
if ( $parent_id ) { |
| 67 |
GalleryFns::delete_transients( $parent_id, 'default-images' ); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Flush cached variation galleries referencing a deleted attachment. |
| 73 |
* |
| 74 |
* @param int $attachment_id Attachment ID being deleted. |
| 75 |
* |
| 76 |
* @return void |
| 77 |
*/ |
| 78 |
public function flush_attachment_gallery_cache( $attachment_id ) { |
| 79 |
GalleryFns::flush_attachment_gallery_transients( $attachment_id ); |
| 80 |
} |
| 81 |
/** |
| 82 |
* Adds a custom meta box to the WooCommerce product edit page. |
| 83 |
*/ |
| 84 |
public function add_metabox() { |
| 85 |
add_meta_box( |
| 86 |
'custom_checkbox_metabox', // Metabox ID. |
| 87 |
__( 'Variation gallery', 'shopbuilder' ), // Title. |
| 88 |
[ $this, 'metabox_callback' ], // Callback function. |
| 89 |
'product', // Post type (WooCommerce product). |
| 90 |
'side', // Position. |
| 91 |
'high' // Priority. |
| 92 |
); |
| 93 |
} |
| 94 |
/** |
| 95 |
* Callback function to display the checkbox in the meta box. |
| 96 |
* |
| 97 |
* @param WP_Post $post The post object. |
| 98 |
*/ |
| 99 |
public function metabox_callback( $post ) { |
| 100 |
$value = get_post_meta( $post->ID, '_rtsb_vg_disable_valiation_gallery', true ); |
| 101 |
?> |
| 102 |
<p> |
| 103 |
<label for="custom_checkbox"> |
| 104 |
<input type="checkbox" id="rtsb_vg_disable_valiation_gallery" name="rtsb_vg_disable_valiation_gallery" value="yes" <?php checked( $value, 'yes' ); ?> /> |
| 105 |
<?php esc_html_e( 'Disable Variation Gallery', 'shopbuilder' ); ?> |
| 106 |
</label><hr/> |
| 107 |
<span><?php esc_html_e( 'Disable variation gallery for this product', 'shopbuilder' ); ?> </span> |
| 108 |
</p> |
| 109 |
<?php |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Saves the checkbox value when the product is updated. |
| 114 |
* |
| 115 |
* @param int $post_id The ID of the product being saved. |
| 116 |
*/ |
| 117 |
public function save_metabox( $post_id ) { |
| 118 |
// Verify nonce for security. |
| 119 |
if ( empty( $_POST['woocommerce_meta_nonce'] ) || ! wp_verify_nonce( wp_unslash( $_POST['woocommerce_meta_nonce'] ), 'woocommerce_save_data' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 120 |
return; |
| 121 |
} |
| 122 |
// Prevent autosave from overwriting. |
| 123 |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 124 |
return; |
| 125 |
} |
| 126 |
// Check if the user has permission to edit the product. |
| 127 |
if ( ! current_user_can( 'edit_product', $post_id ) ) { // phpcs:ignore WordPress.WP.Capabilities.Unknown |
| 128 |
return; |
| 129 |
} |
| 130 |
// Save the checkbox value as 'yes' or 'no'. |
| 131 |
$value = isset( $_POST['rtsb_vg_disable_valiation_gallery'] ) ? 'yes' : 'no'; |
| 132 |
update_post_meta( $post_id, '_rtsb_vg_disable_valiation_gallery', $value ); |
| 133 |
GalleryFns::delete_transients( $post_id, 'default-images' ); |
| 134 |
} |
| 135 |
/** |
| 136 |
* Saves variation gallery image IDs from the variation admin panel. |
| 137 |
* |
| 138 |
* @param int $variation_id The ID of the product variation. |
| 139 |
* |
| 140 |
* @return void |
| 141 |
*/ |
| 142 |
public function save_variation_gallery( $variation_id ) { |
| 143 |
check_ajax_referer( 'save-variations', 'security' ); |
| 144 |
// Check permissions again and make sure we have what we need. |
| 145 |
if ( ! current_user_can( 'edit_products' ) || empty( $_POST ) || empty( $_POST['product_id'] ) ) { // phpcs:ignore WordPress.WP.Capabilities.Unknown |
| 146 |
wp_die( -1 ); |
| 147 |
} |
| 148 |
// Sanitize and save or delete meta. |
| 149 |
if ( isset( $_POST['rtsb_vg'][ $variation_id ] ) && is_array( $_POST['rtsb_vg'][ $variation_id ] ) ) { |
| 150 |
$rtsb_vg_ids = array_map( 'absint', $_POST['rtsb_vg'][ $variation_id ] ); |
| 151 |
$rtsb_vg_ids = array_values( array_unique( $rtsb_vg_ids ) ); |
| 152 |
update_post_meta( $variation_id, 'rtsb_vg_images', $rtsb_vg_ids ); |
| 153 |
} else { |
| 154 |
delete_post_meta( $variation_id, 'rtsb_vg_images' ); |
| 155 |
} |
| 156 |
GalleryFns::delete_transients( $variation_id, 'variation-images' ); |
| 157 |
} |
| 158 |
/** |
| 159 |
* @return void |
| 160 |
*/ |
| 161 |
public function admin_template_js() { |
| 162 |
require_once RTSB_PATH . '/app/Modules/VariationGallery/view/template-admin-thumbnail.php'; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Outputs the variation gallery image section in the product variation admin panel. |
| 167 |
* |
| 168 |
* Enqueues the necessary admin script and renders the gallery UI for a specific |
| 169 |
* variation, allowing admin users to add, preview, and remove multiple images |
| 170 |
* associated with a WooCommerce product variation. |
| 171 |
* |
| 172 |
* @param int $loop The index of the current variation loop. |
| 173 |
* @param array $variation_data Array of variation data. |
| 174 |
* @param WP_Post $variation The variation object (as a WP_Post). |
| 175 |
* |
| 176 |
* @return void |
| 177 |
*/ |
| 178 |
public function gallery_admin_html( $loop, $variation_data, $variation ) { |
| 179 |
$variation_id = absint( $variation->ID ); |
| 180 |
$gallery_images = get_post_meta( $variation_id, 'rtsb_vg_images', true ); |
| 181 |
?> |
| 182 |
<div class="form-row form-row-full rtsb-vg-gallery-wrapper"> |
| 183 |
<h4><?php esc_html_e( 'Variation Image Gallery', 'shopbuilder' ); ?></h4> |
| 184 |
<div class="rtsb-vg-image-container"> |
| 185 |
<ul class="rtsb-vg-images"> |
| 186 |
<?php |
| 187 |
if ( is_array( $gallery_images ) && ! empty( $gallery_images ) ) { |
| 188 |
$gallery_images = array_values( array_unique( $gallery_images ) ); |
| 189 |
foreach ( $gallery_images as $image_id ) : |
| 190 |
$image = wp_get_attachment_image_src( $image_id ); |
| 191 |
/** |
| 192 |
* Functions::gallery_has_video( $image_id );. |
| 193 |
*/ |
| 194 |
if ( empty( $image[0] ) ) { |
| 195 |
continue; |
| 196 |
} |
| 197 |
$add_video_class = ''; |
| 198 |
if ( rtsb()->has_pro() ) { |
| 199 |
$video = trim( get_post_meta( $image_id, 'rtsb_vg_video_link', true ) ?? '' ); |
| 200 |
$add_video_class = ! empty( $video ) ? ' video' : ''; |
| 201 |
} |
| 202 |
?> |
| 203 |
<li class="image<?php echo esc_html( $add_video_class ); ?>"> |
| 204 |
<input type="hidden" name="rtsb_vg[<?php echo absint( $variation_id ); ?>][]" value="<?php echo absint( $image_id ); ?>"> |
| 205 |
<img src="<?php echo esc_url( $image[0] ); ?>"> |
| 206 |
<div class="rtsb-vg-action-button"> |
| 207 |
<span data-tip="Add Video" class="rtsb-vg-media-video-popup dashicons dashicons-video-alt3"></span> |
| 208 |
<span data-tip="Edit Image" class="rtsb-vg-gallery-edit dashicons dashicons-edit"></span> |
| 209 |
<a href="#" class="delete rtsb-vg-remove-image"><span class="dashicons dashicons-no"></span></a> |
| 210 |
</div> |
| 211 |
</li> |
| 212 |
<?php |
| 213 |
endforeach; |
| 214 |
} |
| 215 |
?> |
| 216 |
</ul> |
| 217 |
</div> |
| 218 |
<p class="rtsb-vg-add-image-wrapper hide-if-no-js"> |
| 219 |
<a href="#" data-product_variation_loop="<?php echo absint( $loop ); ?>" |
| 220 |
data-product_variation_id="<?php echo esc_attr( $variation_id ); ?>" |
| 221 |
class="button rtsb-vg-add-image"> |
| 222 |
<?php |
| 223 |
esc_html_e( 'Add Gallery Images', 'shopbuilder' ); |
| 224 |
if ( rtsb()->has_pro() ) { |
| 225 |
echo ' & '; |
| 226 |
esc_html_e( 'Videos', 'shopbuilder' ); |
| 227 |
} |
| 228 |
?> |
| 229 |
</a> |
| 230 |
</p> |
| 231 |
</div> |
| 232 |
<?php |
| 233 |
} |
| 234 |
} |