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