Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Product Gallery Block > Remove global variable overwrite and keep support for the Single Product Block. #9475

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 40 additions & 7 deletions src/BlockTypes/ProductImageGallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,56 @@ protected function get_block_type_uses_context() {
* @return string Rendered block type output.
*/
protected function render( $attributes, $content, $block ) {

$post_id = $block->context['postId'];
global $product;

if ( ! isset( $post_id ) ) {
return '';
}

$product = wc_get_product( $post_id );
if ( ! $product instanceof \WC_Product ) {
return '';
}

if ( class_exists( 'WC_Frontend_Scripts' ) ) {
$frontend_scripts = new \WC_Frontend_Scripts();
$frontend_scripts::load_scripts();
}

$classname = $attributes['className'] ?? '';
$classname = $attributes['className'] ?? '';
$sale_badge_html = $product->is_on_sale() ? '<span class="onsale">' . esc_html__( 'Sale!', 'woo-gutenberg-products-block' ) . '</span>' : '';
$columns = 4;
$post_thumbnail_id = $product->get_image_id();
$wrapper_classes = array(
'woocommerce-product-gallery',
'woocommerce-product-gallery--' . ( $post_thumbnail_id ? 'with-images' : 'without-images' ),
'woocommerce-product-gallery--columns-' . absint( $columns ),
'images',
);
ob_start();
woocommerce_show_product_sale_flash();
$sale_badge_html = ob_get_clean();
?>
<div class="<?php echo esc_attr( implode( ' ', array_map( 'sanitize_html_class', $wrapper_classes ) ) ); ?>" data-columns="<?php echo esc_attr( $columns ); ?>" style="opacity: 0; transition: opacity .25s ease-in-out;">
<div class="woocommerce-product-gallery__wrapper">
<?php
if ( $post_thumbnail_id ) {
$html = wc_get_gallery_image_html( $post_thumbnail_id, true );
} else {
$html = '<div class="woocommerce-product-gallery__image--placeholder">';
$html .= sprintf( '<img src="%s" alt="%s" class="wp-post-image" />', esc_url( wc_placeholder_img_src( 'woo-gutenberg-products-block' ) ), esc_html__( 'Awaiting product image', 'woo-gutenberg-products-block' ) );
$html .= '</div>';
}

ob_start();
woocommerce_show_product_images();
echo wp_kses_post( $html );
$attachment_ids = $product->get_gallery_image_ids();
if ( $attachment_ids && $product->get_image_id() ) {
foreach ( $attachment_ids as $attachment_id ) {
echo wc_get_gallery_image_html( $attachment_id ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
}
?>
</div>
</div>
<?php
$product_image_gallery_html = ob_get_clean();

return sprintf(
Expand Down