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

Commit

Permalink
Product Image Gallery > Reintroduce filters and override-restore the …
Browse files Browse the repository at this point in the history
…global product variable. (#9630)

* Reintroduce filters to the product gallery block and override/restore the global product variable.

* Restore the product global variable earlier and update docblock for the get_block_type_uses_context method.
  • Loading branch information
nefeline authored Jun 2, 2023
1 parent 515c674 commit 0857cbf
Showing 1 changed file with 13 additions and 35 deletions.
48 changes: 13 additions & 35 deletions src/BlockTypes/ProductImageGallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protected function register_block_type_assets() {
/**
* Register the context
*
* @var string
* @return string[]
*/
protected function get_block_type_uses_context() {
return [ 'query', 'queryId', 'postId' ];
Expand All @@ -43,8 +43,13 @@ protected function render( $attributes, $content, $block ) {
return '';
}

$product = wc_get_product( $post_id );
global $product;

$previous_product = $product;
$product = wc_get_product( $post_id );
if ( ! $product instanceof \WC_Product ) {
$product = $previous_product;

return '';
}

Expand All @@ -53,48 +58,21 @@ protected function render( $attributes, $content, $block ) {
$frontend_scripts::load_scripts();
}

$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();
?>
<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>';
}
woocommerce_show_product_sale_flash();
$sale_badge_html = ob_get_clean();

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
ob_start();
woocommerce_show_product_images();
$product_image_gallery_html = ob_get_clean();

$product = $previous_product;
$classname = $attributes['className'] ?? '';
return sprintf(
'<div class="wp-block-woocommerce-product-image-gallery %1$s">%2$s %3$s</div>',
esc_attr( $classname ),
$sale_badge_html,
$product_image_gallery_html
);

}
}

0 comments on commit 0857cbf

Please sign in to comment.