From e6aa4c21655f96bfbe08612819ec90a32d92a7ab Mon Sep 17 00:00:00 2001 From: Patricia Hillebrandt Date: Fri, 12 May 2023 20:34:32 +0200 Subject: [PATCH 1/5] Remove global variable overwrite from Add to Cart Form block. --- src/BlockTypes/AddToCartForm.php | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/BlockTypes/AddToCartForm.php b/src/BlockTypes/AddToCartForm.php index 775876783ce..6152c4448aa 100644 --- a/src/BlockTypes/AddToCartForm.php +++ b/src/BlockTypes/AddToCartForm.php @@ -25,33 +25,28 @@ class AddToCartForm extends AbstractBlock { * @return string | void Rendered block output. */ protected function render( $attributes, $content, $block ) { - global $product; - $post_id = $block->context['postId']; if ( ! isset( $post_id ) ) { return ''; } - if ( ! $product instanceof \WC_Product ) { - $product = wc_get_product( $post_id ); - if ( ! $product instanceof \WC_Product ) { - return ''; - } + $single_product = wc_get_product( $post_id ); + if ( ! $single_product instanceof \WC_Product ) { + return ''; } ob_start(); - /** * Trigger the single product add to cart action for each product type. * * @since 9.7.0 */ - do_action( 'woocommerce_' . $product->get_type() . '_add_to_cart' ); + do_action( 'woocommerce_' . $single_product->get_type() . '_add_to_cart' ); - $product = ob_get_clean(); + $single_product = ob_get_clean(); - if ( ! $product ) { + if ( ! $single_product ) { return ''; } @@ -63,7 +58,7 @@ protected function render( $attributes, $content, $block ) { esc_attr( $classes_and_styles['classes'] ), esc_attr( $classname ), esc_attr( $classes_and_styles['styles'] ), - $product + $single_product ); } From d6ea1dd17fc5822727f5bbd59fedc131d051b160 Mon Sep 17 00:00:00 2001 From: Patricia Hillebrandt Date: Fri, 12 May 2023 22:27:11 +0200 Subject: [PATCH 2/5] Remove call to the global variable and add support for the gallery block to continue to work within the Single Product template without any problems. --- src/BlockTypes/ProductImageGallery.php | 55 +++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 5 deletions(-) diff --git a/src/BlockTypes/ProductImageGallery.php b/src/BlockTypes/ProductImageGallery.php index d726bc642b2..5d433360851 100644 --- a/src/BlockTypes/ProductImageGallery.php +++ b/src/BlockTypes/ProductImageGallery.php @@ -37,10 +37,17 @@ 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; - $product = wc_get_product( $post_id ); + + if ( ! isset( $post_id ) ) { + return ''; + } + + $single_post = get_post( $post_id ); + $single_product = wc_get_product( $post_id ); + if ( ! $single_product instanceof \WC_Product ) { + return ''; + } if ( class_exists( 'WC_Frontend_Scripts' ) ) { $frontend_scripts = new \WC_Frontend_Scripts(); @@ -49,11 +56,49 @@ protected function render( $attributes, $content, $block ) { $classname = $attributes['className'] ?? ''; ob_start(); - woocommerce_show_product_sale_flash(); + if ( $single_product->is_on_sale() ) { + echo apply_filters( 'woocommerce_sale_flash', '' . esc_html__( 'Sale!', 'woocommerce' ) . '', $single_post, $single_product ); + } + $sale_badge_html = ob_get_clean(); ob_start(); - woocommerce_show_product_images(); + $columns = apply_filters( 'woocommerce_product_thumbnails_columns', 4 ); + $post_thumbnail_id = $single_product->get_image_id(); + $wrapper_classes = apply_filters( + 'woocommerce_single_product_image_gallery_classes', + array( + 'woocommerce-product-gallery', + 'woocommerce-product-gallery--' . ( $post_thumbnail_id ? 'with-images' : 'without-images' ), + 'woocommerce-product-gallery--columns-' . absint( $columns ), + 'images', + ) + ); + ?> +
+ '; + } + + echo apply_filters( 'woocommerce_single_product_image_thumbnail_html', $html, $post_thumbnail_id ); + + $attachment_ids = $single_product->get_gallery_image_ids(); + + if ( $attachment_ids && $single_product->get_image_id() ) { + foreach ( $attachment_ids as $attachment_id ) { + echo apply_filters( 'woocommerce_single_product_image_thumbnail_html', wc_get_gallery_image_html( $attachment_id ), $attachment_id ); + } + } + ?> +
+ + Date: Fri, 12 May 2023 22:30:32 +0200 Subject: [PATCH 3/5] Add docblocks for referencing the original templates from the core of Woo. --- src/BlockTypes/ProductImageGallery.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/BlockTypes/ProductImageGallery.php b/src/BlockTypes/ProductImageGallery.php index 5d433360851..78613be4e3c 100644 --- a/src/BlockTypes/ProductImageGallery.php +++ b/src/BlockTypes/ProductImageGallery.php @@ -43,7 +43,7 @@ protected function render( $attributes, $content, $block ) { return ''; } - $single_post = get_post( $post_id ); + $single_post = get_post( $post_id ); $single_product = wc_get_product( $post_id ); if ( ! $single_product instanceof \WC_Product ) { return ''; @@ -56,6 +56,9 @@ protected function render( $attributes, $content, $block ) { $classname = $attributes['className'] ?? ''; ob_start(); + /** + * Based on the single-product/sale-flash.php template from the core of WooCommerce. + */ if ( $single_product->is_on_sale() ) { echo apply_filters( 'woocommerce_sale_flash', '' . esc_html__( 'Sale!', 'woocommerce' ) . '', $single_post, $single_product ); } @@ -63,6 +66,9 @@ protected function render( $attributes, $content, $block ) { $sale_badge_html = ob_get_clean(); ob_start(); + /** + * Based on the single-product/product-image.php template from the core of WooCommerce. + */ $columns = apply_filters( 'woocommerce_product_thumbnails_columns', 4 ); $post_thumbnail_id = $single_product->get_image_id(); $wrapper_classes = apply_filters( From f13174346439bec2f97080385a9bbe0a5455be5f Mon Sep 17 00:00:00 2001 From: Patricia Hillebrandt Date: Sat, 13 May 2023 11:54:43 +0200 Subject: [PATCH 4/5] Have an adapted version of the native image gallery templates available within the ProductImageGallery class for rendering the Product Image block. --- src/BlockTypes/ProductImageGallery.php | 166 ++++++++++++++++--------- 1 file changed, 109 insertions(+), 57 deletions(-) diff --git a/src/BlockTypes/ProductImageGallery.php b/src/BlockTypes/ProductImageGallery.php index 78613be4e3c..77be8805859 100644 --- a/src/BlockTypes/ProductImageGallery.php +++ b/src/BlockTypes/ProductImageGallery.php @@ -28,12 +28,111 @@ protected function get_block_type_uses_context() { return [ 'query', 'queryId', 'postId' ]; } + /** + * Based on the single-product/sale-flash.php template from the core of WooCommerce. + * + * @param \WC_Product $product Product instance. + * @param \WP_Post $post Post instance. + */ + private function sale_flash( $product, $post ) { + if ( ! $product->is_on_sale() ) { + return ''; + } + + ob_start(); + echo sprintf( '%1$s', esc_html__( 'Sale!', 'woo-gutenberg-products-block' ) ); + return ob_get_clean(); + } + + /** + * Based on the single-product/product-image.php template from the core of WooCommerce. + * + * @param \WC_Product $product Product instance. + * @param \WP_Post $post Post instance. + * + * @return string + */ + private function product_image( $product, $post ) { + /** + * The woocommerce_product_thumbnails_columns filter. + * + * Allow users to modify the number of columns displayed in the product gallery. + * + * @since TBD + */ + $columns = apply_filters( 'woocommerce_product_thumbnails_columns', 4 ); + $post_thumbnail_id = $product->get_image_id(); + /** + * The woocommerce_single_product_image_gallery_classes filter. + * + * Allow users to modify the class names for the product image gallery. + * + * @since TBD + */ + $wrapper_classes = apply_filters( + 'woocommerce_single_product_image_gallery_classes', + array( + 'woocommerce-product-gallery', + 'woocommerce-product-gallery--' . ( $post_thumbnail_id ? 'with-images' : 'without-images' ), + 'woocommerce-product-gallery--columns-' . absint( $columns ), + 'images', + ) + ); + + if ( $post_thumbnail_id ) { + $product_image_gallery_placeholder = wc_get_gallery_image_html( $post_thumbnail_id, true ); + } else { + $product_image_gallery_placeholder = sprintf( + '', + esc_url( wc_placeholder_img_src( 'woocommerce_single' ) ), + esc_html__( 'Awaiting product image', 'woo-gutenberg-products-block' ) + ); + } + /** + * The woocommerce_single_product_image_thumbnail_html filter. + * + * Allow users to modify the product gallery thumbnail html. + * + * @since TBD + */ + $product_image_gallery_placeholder = apply_filters( 'woocommerce_single_product_image_thumbnail_html', $product_image_gallery_placeholder, $post_thumbnail_id ); + + return sprintf( + '
', + esc_attr( implode( ' ', array_map( 'sanitize_html_class', $wrapper_classes ) ) ), + esc_attr( $columns ), + $product_image_gallery_placeholder, + $this->product_thumbnails( $product ) + ); + } + + /** + * Based on the single-product/product-thumbnails.php template from the core of WooCommerce. + * + * @param \WC_Product $product Product instance. + * + * @return false|string + */ + private function product_thumbnails( $product ) { + $attachment_ids = $product->get_gallery_image_ids(); + if ( ! $attachment_ids || ! $product->get_image_id() ) { + return ''; + } + + ob_start(); + foreach ( $attachment_ids as $attachment_id ) { + echo wp_kses_post( wc_get_gallery_image_html( $attachment_id ) ); + } + return ob_get_clean(); + } + /** * Include and render the block. * - * @param array $attributes Block attributes. Default empty array. - * @param string $content Block content. Default empty string. - * @param WP_Block $block Block instance. + * @param array $attributes Block attributes. Default empty array. + * @param string $content Block content. Default empty string. + * @param \WP_Block $block Block instance. + * * @return string Rendered block type output. */ protected function render( $attributes, $content, $block ) { @@ -43,7 +142,11 @@ protected function render( $attributes, $content, $block ) { return ''; } - $single_post = get_post( $post_id ); + $single_post = get_post( $post_id ); + if ( ! $single_post instanceof \WP_Post ) { + return ''; + } + $single_product = wc_get_product( $post_id ); if ( ! $single_product instanceof \WC_Product ) { return ''; @@ -55,63 +158,12 @@ protected function render( $attributes, $content, $block ) { } $classname = $attributes['className'] ?? ''; - ob_start(); - /** - * Based on the single-product/sale-flash.php template from the core of WooCommerce. - */ - if ( $single_product->is_on_sale() ) { - echo apply_filters( 'woocommerce_sale_flash', '' . esc_html__( 'Sale!', 'woocommerce' ) . '', $single_post, $single_product ); - } - - $sale_badge_html = ob_get_clean(); - - ob_start(); - /** - * Based on the single-product/product-image.php template from the core of WooCommerce. - */ - $columns = apply_filters( 'woocommerce_product_thumbnails_columns', 4 ); - $post_thumbnail_id = $single_product->get_image_id(); - $wrapper_classes = apply_filters( - 'woocommerce_single_product_image_gallery_classes', - array( - 'woocommerce-product-gallery', - 'woocommerce-product-gallery--' . ( $post_thumbnail_id ? 'with-images' : 'without-images' ), - 'woocommerce-product-gallery--columns-' . absint( $columns ), - 'images', - ) - ); - ?> -
- '; - } - - echo apply_filters( 'woocommerce_single_product_image_thumbnail_html', $html, $post_thumbnail_id ); - - $attachment_ids = $single_product->get_gallery_image_ids(); - - if ( $attachment_ids && $single_product->get_image_id() ) { - foreach ( $attachment_ids as $attachment_id ) { - echo apply_filters( 'woocommerce_single_product_image_thumbnail_html', wc_get_gallery_image_html( $attachment_id ), $attachment_id ); - } - } - ?> -
- - %2$s %3$s', esc_attr( $classname ), - $sale_badge_html, - $product_image_gallery_html + $this->sale_flash( $single_product, $single_post ), + $this->product_image( $single_product, $single_post ) ); } From 4dd7b2a1df833d442c5837136a5f776f1c06129e Mon Sep 17 00:00:00 2001 From: Patricia Hillebrandt Date: Mon, 15 May 2023 22:16:02 +0200 Subject: [PATCH 5/5] Rename single_product to product. --- src/BlockTypes/AddToCartForm.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/BlockTypes/AddToCartForm.php b/src/BlockTypes/AddToCartForm.php index 6152c4448aa..0a194d4a5b5 100644 --- a/src/BlockTypes/AddToCartForm.php +++ b/src/BlockTypes/AddToCartForm.php @@ -31,8 +31,8 @@ protected function render( $attributes, $content, $block ) { return ''; } - $single_product = wc_get_product( $post_id ); - if ( ! $single_product instanceof \WC_Product ) { + $product = wc_get_product( $post_id ); + if ( ! $product instanceof \WC_Product ) { return ''; } @@ -42,11 +42,11 @@ protected function render( $attributes, $content, $block ) { * * @since 9.7.0 */ - do_action( 'woocommerce_' . $single_product->get_type() . '_add_to_cart' ); + do_action( 'woocommerce_' . $product->get_type() . '_add_to_cart' ); - $single_product = ob_get_clean(); + $product = ob_get_clean(); - if ( ! $single_product ) { + if ( ! $product ) { return ''; } @@ -58,7 +58,7 @@ protected function render( $attributes, $content, $block ) { esc_attr( $classes_and_styles['classes'] ), esc_attr( $classname ), esc_attr( $classes_and_styles['styles'] ), - $single_product + $product ); }