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rc.2 3.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / Blocks / Utils / ProductGalleryUtils.php
woocommerce / src / Blocks / Utils Last commit date
BlockHooksTrait.php 4 weeks ago BlockTemplateUtils.php 2 months ago BlocksSharedState.php 3 months ago BlocksWpQuery.php 2 years ago CartCheckoutUtils.php 4 weeks ago MiniCartUtils.php 1 year ago ProductAvailabilityUtils.php 11 months ago ProductDataUtils.php 1 year ago ProductGalleryUtils.php 4 weeks ago StyleAttributesUtils.php 1 year ago Utils.php 2 years ago
ProductGalleryUtils.php
330 lines
1 <?php
2 namespace Automattic\WooCommerce\Blocks\Utils;
3
4 use Automattic\WooCommerce\Internal\VariationGallery\Package as VariationGalleryPackage;
5
6 /**
7 * Utility methods used for the Product Gallery block.
8 * {@internal This class and its methods are not intended for public use.}
9 */
10 class ProductGalleryUtils {
11 /**
12 * Get all image IDs for the product.
13 *
14 * @param \WC_Product $product The product object.
15 * @return array An array of image IDs.
16 */
17 public static function get_all_image_ids( $product ) {
18 if ( ! $product instanceof \WC_Product ) {
19 wc_doing_it_wrong( __FUNCTION__, __( 'Invalid product object.', 'woocommerce' ), '9.8.0' );
20 return array();
21 }
22
23 $gallery_image_ids = self::get_product_gallery_image_ids( $product );
24 $product_variation_image_ids = self::get_product_variation_image_ids( $product );
25 $all_image_ids = array_values( array_map( 'intval', array_unique( array_merge( $gallery_image_ids, $product_variation_image_ids ) ) ) );
26
27 if ( empty( $all_image_ids ) ) {
28 return array();
29 }
30
31 return $all_image_ids;
32 }
33
34 /**
35 * Get the product gallery image data.
36 *
37 * @param \WC_Product $product The product object to retrieve the gallery images for.
38 * @param string $size The size of the image to retrieve.
39 * @return array An array of image data for the product gallery.
40 */
41 public static function get_product_gallery_image_data( $product, $size ) {
42 $all_image_ids = self::get_all_image_ids( $product );
43 return self::get_image_src_data( $all_image_ids, $size, $product->get_title() );
44 }
45
46 /**
47 * Get the product gallery image count.
48 *
49 * @param \WC_Product $product The product object to retrieve the gallery images for.
50 * @return int The number of images in the product gallery.
51 */
52 public static function get_product_gallery_image_count( $product ) {
53 $all_image_ids = self::get_all_image_ids( $product );
54 return count( $all_image_ids );
55 }
56
57 /**
58 * Get the image source data.
59 *
60 * @param array $image_ids The image IDs to retrieve the source data for.
61 * @param string $size The size of the image to retrieve.
62 * @param string $product_title The title of the product used for alt fallback.
63 * @return array An array of image source data.
64 */
65 public static function get_image_src_data( $image_ids, $size, $product_title = '' ) {
66 $image_src_data = array();
67
68 foreach ( $image_ids as $index => $image_id ) {
69 if ( 0 === $image_id ) {
70 // Handle placeholder image.
71 $image_src_data[] = array(
72 'id' => 0,
73 'src' => wc_placeholder_img_src(),
74 'srcset' => '',
75 'sizes' => '',
76 'alt' => '',
77 );
78 continue;
79 }
80
81 // Get the image source.
82 $full_src = wp_get_attachment_image_src( $image_id, $size );
83
84 // Get srcset and sizes.
85 $srcset = wp_get_attachment_image_srcset( $image_id, $size );
86 $sizes = wp_get_attachment_image_sizes( $image_id, $size );
87 $alt = get_post_meta( $image_id, '_wp_attachment_image_alt', true );
88
89 $image_src_data[] = array(
90 'id' => $image_id,
91 'src' => $full_src ? $full_src[0] : '',
92 'srcset' => $srcset ? $srcset : '',
93 'sizes' => $sizes ? $sizes : '',
94 'alt' => $alt ? $alt : sprintf(
95 /* translators: 1: Product title 2: Image number */
96 __( '%1$s - Image %2$d', 'woocommerce' ),
97 $product_title,
98 $index + 1
99 ),
100 );
101 }
102
103 return $image_src_data;
104 }
105
106 /**
107 * Get the product variation image data.
108 *
109 * @param \WC_Product $product The product object to retrieve the variation images for.
110 * @return array An array of image data for the product variation images.
111 */
112 public static function get_product_variation_image_ids( $product ) {
113 $variation_image_ids = array();
114
115 if ( ! $product instanceof \WC_Product ) {
116 wc_doing_it_wrong( __FUNCTION__, __( 'Invalid product object.', 'woocommerce' ), '9.8.0' );
117 return $variation_image_ids;
118 }
119
120 try {
121 if ( $product->is_type( 'variable' ) ) {
122 $variation_gallery_data = self::get_product_variation_gallery_data( $product );
123
124 foreach ( $variation_gallery_data as $variation_data ) {
125 $variation_image_ids = array_merge( $variation_image_ids, $variation_data['image_ids'] );
126 }
127 }
128 } catch ( \Exception $e ) {
129 // Log the error but continue execution.
130 error_log( 'Error getting product variation image IDs: ' . $e->getMessage() );
131 }
132
133 $unique_int_ids = array_unique( array_map( 'intval', $variation_image_ids ) );
134
135 return array_values( array_map( 'strval', $unique_int_ids ) );
136 }
137
138 /**
139 * Get variation gallery data keyed by variation ID.
140 *
141 * @param \WC_Product $product The product object to retrieve variation gallery data for.
142 * @return array<int, array<string, mixed>> Variation gallery data.
143 */
144 public static function get_product_variation_gallery_data( $product ) {
145 $variation_gallery_data = array();
146
147 if ( ! $product instanceof \WC_Product ) {
148 wc_doing_it_wrong( __FUNCTION__, __( 'Invalid product object.', 'woocommerce' ), '10.8.0' );
149 return $variation_gallery_data;
150 }
151
152 if ( ! $product->is_type( 'variable' ) ) {
153 return $variation_gallery_data;
154 }
155
156 $variations = $product->get_children();
157 if ( ! empty( $variations ) ) {
158 // Bulk-load posts + postmeta into WP's object cache.
159 _prime_post_caches( $variations );
160 }
161
162 // 0 is placeholder image ID.
163 $parent_featured_id = 0;
164 $product_image_id = (int) $product->get_image_id();
165 if ( $product_image_id && wp_attachment_is_image( $product_image_id ) ) {
166 $parent_featured_id = $product_image_id;
167 }
168
169 $parent_gallery_ids = array_map( 'intval', $product->get_gallery_image_ids() );
170 $parent_gallery_ids = array_filter( $parent_gallery_ids, 'wp_attachment_is_image' );
171 $parent_gallery_extras = array_values( array_diff( $parent_gallery_ids, array( $parent_featured_id ) ) );
172
173 foreach ( $variations as $variation_id ) {
174 $variation_id = (int) $variation_id;
175 $entry = self::build_variation_gallery_entry( $variation_id, $parent_featured_id, $parent_gallery_extras );
176
177 if ( null !== $entry ) {
178 $variation_gallery_data[ $variation_id ] = $entry;
179 }
180 }
181
182 return $variation_gallery_data;
183 }
184
185 /**
186 * Build the gallery payload for a single variation, or null when the
187 * post isn't a real variation.
188 *
189 * Decision tree (variation chosen):
190 * - no variation images → parent featured + parent gallery
191 * - own featured only → variation featured + parent gallery extras
192 * - own featured + gallery (flag on) → variation images only
193 * - gallery only, no own featured (potential AVI shape) → parent featured + variation gallery
194 *
195 * @param int $variation_id Variation post ID.
196 * @param int $parent_featured_id Parent product's featured image ID (0 if missing/invalid).
197 * @param int[] $parent_gallery_extras Parent gallery image IDs, with the featured filtered out.
198 * @return array<string, mixed>|null
199 */
200 private static function build_variation_gallery_entry( int $variation_id, int $parent_featured_id, array $parent_gallery_extras ): ?array {
201 $variation = wc_get_product( $variation_id );
202
203 if ( ! $variation instanceof \WC_Product_Variation ) {
204 return null;
205 }
206
207 $featured_id = (int) $variation->get_image_id();
208 $featured_valid = $featured_id && wp_attachment_is_image( $featured_id );
209
210 $variation_gallery_ids = array();
211 if ( VariationGalleryPackage::is_enabled() ) {
212 $variation_gallery_ids = array_map( 'intval', $variation->get_gallery_image_ids() );
213 $variation_gallery_ids = array_filter( $variation_gallery_ids, 'wp_attachment_is_image' );
214 $variation_gallery_ids = array_values( $variation_gallery_ids );
215 }
216
217 // No images from variation - full parent fallback.
218 if ( ! $featured_valid && empty( $variation_gallery_ids ) ) {
219 $parent_image_ids = array_values(
220 array_filter( array_merge( array( $parent_featured_id ), $parent_gallery_extras ) )
221 );
222
223 if ( empty( $parent_image_ids ) ) {
224 return array(
225 'image_id' => 0,
226 'image_ids' => array( 0 ),
227 );
228 }
229
230 return array(
231 'image_id' => $parent_image_ids[0],
232 'image_ids' => $parent_image_ids,
233 );
234 }
235
236 // Variation has featured image and gallery - full variation gallery.
237 if ( ! empty( $variation_gallery_ids ) ) {
238 $featured = $featured_valid ? $featured_id : $variation_gallery_ids[0];
239 $image_ids = array_values(
240 array_unique( array_merge( array( $featured ), $variation_gallery_ids ) )
241 );
242
243 return array(
244 'image_id' => $featured,
245 'image_ids' => $image_ids,
246 );
247 }
248
249 // Variation has only featured image - variation featured and parent gallery.
250 $image_ids = array_values(
251 array_unique( array_merge( array( $featured_id ), $parent_gallery_extras ) )
252 );
253
254 return array(
255 'image_id' => $featured_id,
256 'image_ids' => $image_ids,
257 );
258 }
259
260 /**
261 * Get all image IDs relevant to a variation gallery.
262 *
263 * @param \WC_Product_Variation $variation The variation object.
264 * @return array<int> Variation image IDs.
265 */
266 public static function get_variation_gallery_image_ids( \WC_Product_Variation $variation ) {
267 $image_ids = array();
268 $variation_image_id = (int) $variation->get_image_id();
269 $gallery_image_ids = array_map( 'intval', $variation->get_gallery_image_ids() );
270
271 if ( $variation_image_id ) {
272 $image_ids[] = $variation_image_id;
273 }
274
275 if ( ! empty( $gallery_image_ids ) ) {
276 $image_ids = array_merge( $image_ids, $gallery_image_ids );
277 }
278
279 // Filter out missing/invalid attachments to avoid rendering phantom
280 // empty `<li>` wrappers that the visibility watch can't manage.
281 $image_ids = array_filter(
282 $image_ids,
283 function ( $id ) {
284 return $id > 0 && wp_attachment_is_image( $id );
285 }
286 );
287
288 return array_values( array_unique( $image_ids ) );
289 }
290
291 /**
292 * Get the product gallery image IDs.
293 *
294 * @param \WC_Product $product The product object to retrieve the gallery images for.
295 * @return array An array of unique image IDs for the product gallery.
296 */
297 public static function get_product_gallery_image_ids( $product ) {
298 $product_image_ids = array();
299
300 // Main product featured image.
301 $featured_image_id = $product->get_image_id();
302
303 if ( $featured_image_id ) {
304 $product_image_ids[] = $featured_image_id;
305 }
306
307 // All other product gallery images.
308 $product_gallery_image_ids = $product->get_gallery_image_ids();
309
310 if ( ! empty( $product_gallery_image_ids ) ) {
311 // We don't want to show the same image twice, so we have to remove the featured image from the gallery if it's there.
312 $product_image_ids = array_unique( array_merge( $product_image_ids, $product_gallery_image_ids ) );
313 }
314
315 // If the Product image is not set and there are no gallery images, we need to set it to a placeholder image.
316 if ( ! $featured_image_id && empty( $product_gallery_image_ids ) ) {
317 $product_image_ids[] = '0';
318 }
319
320 foreach ( $product_image_ids as $key => $image_id ) {
321 $product_image_ids[ $key ] = strval( $image_id );
322 }
323
324 // Reindex array.
325 $product_image_ids = array_values( $product_image_ids );
326
327 return $product_image_ids;
328 }
329 }
330