PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 3.2.3
ShopBuilder – WooCommerce Builder For Elementor v3.2.3
3.4.1 3.4.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.13 2.1.14 2.1.15 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.3.0 All 62 releases
shopbuilder / app / Modules / VariationGallery / GalleryAdmin.php

GalleryAdmin.php in ShopBuilder – WooCommerce Builder For Elementor 3.2.3, at app/Modules/VariationGallery/GalleryAdmin.php

190 lines 6.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 /**
38 * Adds a custom meta box to the WooCommerce product edit page.
39 */
40 public function add_metabox() {
41 add_meta_box(
42 'custom_checkbox_metabox', // Metabox ID.
43 __( 'Variation gallery', 'shopbuilder' ), // Title.
44 [ $this, 'metabox_callback' ], // Callback function.
45 'product', // Post type (WooCommerce product).
46 'side', // Position.
47 'high' // Priority.
48 );
49 }
50 /**
51 * Callback function to display the checkbox in the meta box.
52 *
53 * @param WP_Post $post The post object.
54 */
55 public function metabox_callback( $post ) {
56 $value = get_post_meta( $post->ID, '_rtsb_vg_disable_valiation_gallery', true );
57 ?>
58 <p>
59 <label for="custom_checkbox">
60 <input type="checkbox" id="rtsb_vg_disable_valiation_gallery" name="rtsb_vg_disable_valiation_gallery" value="yes" <?php checked( $value, 'yes' ); ?> />
61 <?php esc_html_e( 'Disable Variation Gallery', 'shopbuilder' ); ?>
62 </label><br/>
63 <span><?php esc_html_e( 'Disable variation gallery for this product', 'shopbuilder' ); ?> </span>
64 </p>
65 <?php
66 }
67
68 /**
69 * Saves the checkbox value when the product is updated.
70 *
71 * @param int $post_id The ID of the product being saved.
72 */
73 public function save_metabox( $post_id ) {
74 // Verify nonce for security.
75 if ( empty( $_POST['woocommerce_meta_nonce'] ) || ! wp_verify_nonce( wp_unslash( $_POST['woocommerce_meta_nonce'] ), 'woocommerce_save_data' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
76 return;
77 }
78 // Prevent autosave from overwriting.
79 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
80 return;
81 }
82 // Check if the user has permission to edit the product.
83 if ( ! current_user_can( 'edit_product', $post_id ) ) { // phpcs:ignore WordPress.WP.Capabilities.Unknown
84 return;
85 }
86 // Save the checkbox value as 'yes' or 'no'.
87 $value = isset( $_POST['rtsb_vg_disable_valiation_gallery'] ) ? 'yes' : 'no';
88 update_post_meta( $post_id, '_rtsb_vg_disable_valiation_gallery', $value );
89 GalleryFns::delete_transients( $post_id, 'default-images' );
90 }
91 /**
92 * Saves variation gallery image IDs from the variation admin panel.
93 *
94 * @param int $variation_id The ID of the product variation.
95 *
96 * @return void
97 */
98 public function save_variation_gallery( $variation_id ) {
99 check_ajax_referer( 'save-variations', 'security' );
100 // Check permissions again and make sure we have what we need.
101 if ( ! current_user_can( 'edit_products' ) || empty( $_POST ) || empty( $_POST['product_id'] ) ) { // phpcs:ignore WordPress.WP.Capabilities.Unknown
102 wp_die( -1 );
103 }
104 // Sanitize and save or delete meta.
105 if ( isset( $_POST['rtsb_vg'][ $variation_id ] ) && is_array( $_POST['rtsb_vg'][ $variation_id ] ) ) {
106 $rtsb_vg_ids = array_map( 'absint', $_POST['rtsb_vg'][ $variation_id ] );
107 $rtsb_vg_ids = array_values( array_unique( $rtsb_vg_ids ) );
108 update_post_meta( $variation_id, 'rtsb_vg_images', $rtsb_vg_ids );
109 } else {
110 delete_post_meta( $variation_id, 'rtsb_vg_images' );
111 }
112 GalleryFns::delete_transients( $variation_id, 'variation-images' );
113 }
114 /**
115 * @return void
116 */
117 public function admin_template_js() {
118 require_once RTSB_PATH . '/app/Modules/VariationGallery/view/template-admin-thumbnail.php';
119 }
120
121 /**
122 * Outputs the variation gallery image section in the product variation admin panel.
123 *
124 * Enqueues the necessary admin script and renders the gallery UI for a specific
125 * variation, allowing admin users to add, preview, and remove multiple images
126 * associated with a WooCommerce product variation.
127 *
128 * @param int $loop The index of the current variation loop.
129 * @param array $variation_data Array of variation data.
130 * @param WP_Post $variation The variation object (as a WP_Post).
131 *
132 * @return void
133 */
134 public function gallery_admin_html( $loop, $variation_data, $variation ) {
135 $variation_id = absint( $variation->ID );
136 $gallery_images = get_post_meta( $variation_id, 'rtsb_vg_images', true );
137 ?>
138 <div class="form-row form-row-full rtsb-vg-gallery-wrapper">
139 <h4><?php esc_html_e( 'Variation Image Gallery', 'shopbuilder' ); ?></h4>
140 <div class="rtsb-vg-image-container">
141 <ul class="rtsb-vg-images">
142 <?php
143 if ( is_array( $gallery_images ) && ! empty( $gallery_images ) ) {
144 $gallery_images = array_values( array_unique( $gallery_images ) );
145 foreach ( $gallery_images as $image_id ) :
146 $image = wp_get_attachment_image_src( $image_id );
147 /**
148 * Functions::gallery_has_video( $image_id );.
149 */
150 if ( empty( $image[0] ) ) {
151 continue;
152 }
153 $add_video_class = '';
154 if ( rtsb()->has_pro() ) {
155 $video = trim( get_post_meta( $image_id, 'rtsb_vg_video_link', true ) ?? '' );
156 $add_video_class = ! empty( $video ) ? ' video' : '';
157 }
158 ?>
159 <li class="image<?php echo esc_html( $add_video_class ); ?>">
160 <input type="hidden" name="rtsb_vg[<?php echo absint( $variation_id ); ?>][]" value="<?php echo absint( $image_id ); ?>">
161 <img src="<?php echo esc_url( $image[0] ); ?>">
162 <div class="rtsb-vg-action-button">
163 <span data-tip="Add Video" class="rtsb-vg-media-video-popup dashicons dashicons-video-alt3"></span>
164 <span data-tip="Edit Image" class="rtsb-vg-gallery-edit dashicons dashicons-edit"></span>
165 <a href="#" class="delete rtsb-vg-remove-image"><span class="dashicons dashicons-no"></span></a>
166 </div>
167 </li>
168 <?php
169 endforeach;
170 }
171 ?>
172 </ul>
173 </div>
174 <p class="rtsb-vg-add-image-wrapper hide-if-no-js">
175 <a href="#" data-product_variation_loop="<?php echo absint( $loop ); ?>"
176 data-product_variation_id="<?php echo esc_attr( $variation_id ); ?>"
177 class="button rtsb-vg-add-image">
178 <?php
179 esc_html_e( 'Add Gallery Images', 'shopbuilder' );
180 if ( rtsb()->has_pro() ) {
181 echo ' & ';
182 esc_html_e( 'Videos', 'shopbuilder' );
183 }
184 ?>
185 </a>
186 </p>
187 </div>
188 <?php
189 }
190 }