1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
<?php namespace Automattic\WooCommerce\Blocks\Utils;
/** * Utility methods used for the Product Gallery block. * {@internal This class and its methods are not intended for public use.} */ class ProductGalleryUtils { /** * Get all image IDs for the product. * * @param \WC_Product $product The product object. * @return array An array of image IDs. */ public static function get_all_image_ids( $product ) { if ( ! $product instanceof \WC_Product ) { wc_doing_it_wrong( __FUNCTION__, __( 'Invalid product object.', 'woocommerce' ), '9.8.0' ); return array(); }
$gallery_image_ids = self::get_product_gallery_image_ids( $product ); $product_variation_image_ids = self::get_product_variation_image_ids( $product ); $all_image_ids = array_values( array_map( 'intval', array_unique( array_merge( $gallery_image_ids, $product_variation_image_ids ) ) ) );
if ( empty( $all_image_ids ) ) { return array(); }
return $all_image_ids; }
/** * Get the product gallery image data. * * @param \WC_Product $product The product object to retrieve the gallery images for. * @param string $size The size of the image to retrieve. * @return array An array of image data for the product gallery. */ public static function get_product_gallery_image_data( $product, $size ) { $all_image_ids = self::get_all_image_ids( $product ); return self::get_image_src_data( $all_image_ids, $size, $product->get_title() ); }
/** * Get the product gallery image count. * * @param \WC_Product $product The product object to retrieve the gallery images for. * @return int The number of images in the product gallery. */ public static function get_product_gallery_image_count( $product ) { $all_image_ids = self::get_all_image_ids( $product ); return count( $all_image_ids ); }
/** * Get the image source data. * * @param array $image_ids The image IDs to retrieve the source data for. * @param string $size The size of the image to retrieve. * @param string $product_title The title of the product used for alt fallback. * @return array An array of image source data. */ public static function get_image_src_data( $image_ids, $size, $product_title = '' ) { $image_src_data = array();
foreach ( $image_ids as $index => $image_id ) { if ( 0 === $image_id ) { // Handle placeholder image. $image_src_data[] = array( 'id' => 0, 'src' => wc_placeholder_img_src(), 'srcset' => '', 'sizes' => '', 'alt' => '', ); continue; }
// Get the image source. $full_src = wp_get_attachment_image_src( $image_id, $size );
// Get srcset and sizes. $srcset = wp_get_attachment_image_srcset( $image_id, $size ); $sizes = wp_get_attachment_image_sizes( $image_id, $size ); $alt = get_post_meta( $image_id, '_wp_attachment_image_alt', true );
$image_src_data[] = array( 'id' => $image_id, 'src' => $full_src ? $full_src[0] : '', 'srcset' => $srcset ? $srcset : '', 'sizes' => $sizes ? $sizes : '', 'alt' => $alt ? $alt : sprintf( /* translators: 1: Product title 2: Image number */ __( '%1$s - Image %2$d', 'woocommerce' ), $product_title, $index + 1 ), ); }
return $image_src_data; }
/** * Get the product variation image data. * * @param \WC_Product $product The product object to retrieve the variation images for. * @return array An array of image data for the product variation images. */ public static function get_product_variation_image_ids( $product ) { $variation_image_ids = array();
if ( ! $product instanceof \WC_Product ) { wc_doing_it_wrong( __FUNCTION__, __( 'Invalid product object.', 'woocommerce' ), '9.8.0' ); return $variation_image_ids; }
try { if ( $product->is_type( 'variable' ) ) { $variations = $product->get_children(); foreach ( $variations as $variation_id ) { $variation = wc_get_product( $variation_id ); if ( $variation ) { $variation_image_id = $variation->get_image_id(); if ( ! empty( $variation_image_id ) && ! in_array( strval( $variation_image_id ), $variation_image_ids, true ) ) { $variation_image_ids[] = strval( $variation_image_id ); } } } } } catch ( \Exception $e ) { // Log the error but continue execution. error_log( 'Error getting product variation image IDs: ' . $e->getMessage() ); }
return $variation_image_ids; }
/** * Get the product gallery image IDs. * * @param \WC_Product $product The product object to retrieve the gallery images for. * @return array An array of unique image IDs for the product gallery. */ public static function get_product_gallery_image_ids( $product ) { $product_image_ids = array();
// Main product featured image. $featured_image_id = $product->get_image_id();
if ( $featured_image_id ) { $product_image_ids[] = $featured_image_id; }
// All other product gallery images. $product_gallery_image_ids = $product->get_gallery_image_ids();
if ( ! empty( $product_gallery_image_ids ) ) { // 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. $product_image_ids = array_unique( array_merge( $product_image_ids, $product_gallery_image_ids ) ); }
// If the Product image is not set and there are no gallery images, we need to set it to a placeholder image. if ( ! $featured_image_id && empty( $product_gallery_image_ids ) ) { $product_image_ids[] = '0'; }
foreach ( $product_image_ids as $key => $image_id ) { $product_image_ids[ $key ] = strval( $image_id ); }
// Reindex array. $product_image_ids = array_values( $product_image_ids );
return $product_image_ids; } }
|