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 |