PATH:
home
/
lab2454c
/
caimegroup.com
/
wp-content
/
plugins
/
woocommerce
/
src
/
Blocks
/
BlockTypes
<?php namespace Automattic\WooCommerce\Blocks\BlockTypes; /** * SingleProduct class. */ class SingleProduct extends AbstractBlock { /** * Block name. * * @var string */ protected $block_name = 'single-product'; /** * Product ID of the current product to be displayed in the Single Product block. * This is used to replace the global post for the Single Product inner blocks. * * @var int */ protected $product_id = 0; /** * Single Product inner blocks names. * This is used to map all the inner blocks for a Single Product block. * * @var array */ protected $single_product_inner_blocks_names = []; /** * Initialize the block and Hook into the `render_block_context` filter * to update the context with the correct data. * * @var string */ protected function initialize() { parent::initialize(); add_filter( 'render_block_context', [ $this, 'update_context' ], 10, 3 ); add_filter( 'render_block_core/post-excerpt', [ $this, 'restore_global_post' ], 10, 3 ); add_filter( 'render_block_core/post-title', [ $this, 'restore_global_post' ], 10, 3 ); } /** * Restore the global post variable right before generating the render output for the post title and/or post excerpt blocks. * * This is required due to the changes made via the replace_post_for_single_product_inner_block method. * It is a temporary fix to ensure these blocks work as expected until Gutenberg versions 15.2 and 15.6 are part of the core of WordPress. * * @see https://github.com/WordPress/gutenberg/pull/48001 * @see https://github.com/WordPress/gutenberg/pull/49495 * * @param string $block_content The block content. * @param array $parsed_block The full block, including name and attributes. * @param \WP_Block $block_instance The block instance. * * @return mixed */ public function restore_global_post( $block_content, $parsed_block, $block_instance ) { if ( isset( $block_instance->context['singleProduct'] ) && $block_instance->context['singleProduct'] ) { wp_reset_postdata(); } return $block_content; } /** * Update the context by injecting the correct post data * for each one of the Single Product inner blocks. * * @param array $context Block context. * @param array $block Block attributes. * @param WP_Block $parent_block Block instance. * * @return array Updated block context. */ public function update_context( $context, $block, $parent_block ) { if ( 'woocommerce/single-product' === $block['blockName'] && isset( $block['attrs']['productId'] ) ) { $this->product_id = $block['attrs']['productId']; $this->single_product_inner_blocks_names = array_reverse( $this->extract_single_product_inner_block_names( $block ) ); } $this->replace_post_for_single_product_inner_block( $block, $context ); return $context; } /** * Extract the inner block names for the Single Product block. This way it's possible * to map all the inner blocks for a Single Product block and manipulate the data as needed. * * @param array $block The Single Product block or its inner blocks. * @param array $result Array of inner block names. * * @return array Array containing all the inner block names of a Single Product block. */ protected function extract_single_product_inner_block_names( $block, &$result = [] ) { if ( isset( $block['blockName'] ) ) { $result[] = $block['blockName']; } if ( isset( $block['innerBlocks'] ) ) { foreach ( $block['innerBlocks'] as $inner_block ) { $this->extract_single_product_inner_block_names( $inner_block, $result ); } } return $result; } /** * Replace the global post for the Single Product inner blocks and reset it after. * * This is needed because some of the inner blocks may use the global post * instead of fetching the product through the `productId` attribute, so even if the * `productId` is passed to the inner block, it will still use the global post. * * @param array $block Block attributes. * @param array $context Block context. */ protected function replace_post_for_single_product_inner_block( $block, &$context ) { if ( $this->single_product_inner_blocks_names ) { $block_name = array_pop( $this->single_product_inner_blocks_names ); if ( $block_name === $block['blockName'] ) { /** * This is a temporary fix to ensure the Post Title and Excerpt blocks work as expected * until Gutenberg versions 15.2 and 15.6 are included in the core of WordPress. * * Important: the original post data is restored in the restore_global_post method. * * @see https://github.com/WordPress/gutenberg/pull/48001 * @see https://github.com/WordPress/gutenberg/pull/49495 */ if ( 'core/post-excerpt' === $block_name || 'core/post-title' === $block_name ) { global $post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited $post = get_post( $this->product_id ); if ( $post instanceof \WP_Post ) { setup_postdata( $post ); } } $context['postId'] = $this->product_id; $context['singleProduct'] = true; } } } /** * Get the frontend script handle for this block type. * * @param string $key Data to get, or default to everything. * * @return null This block has no frontend script. */ protected function get_block_type_script( $key = null ) { return null; } }
[+]
..
[-] CheckoutContactInformationBlock.php
[edit]
[-] MiniCartProductsTableBlock.php
[edit]
[-] ProductFilterAttribute.php
[edit]
[-] CartItemsBlock.php
[edit]
[-] MiniCartShoppingButtonBlock.php
[edit]
[-] CartOrderSummaryTaxesBlock.php
[edit]
[-] AbstractInnerBlock.php
[edit]
[-] CheckoutOrderNoteBlock.php
[edit]
[-] AddToCartForm.php
[edit]
[-] ProductCategories.php
[edit]
[-] ProductFilterStockStatus.php
[edit]
[-] CheckoutExpressPaymentBlock.php
[edit]
[-] CartExpressPaymentBlock.php
[edit]
[-] CheckoutAdditionalInformationBlock.php
[edit]
[-] ProductPrice.php
[edit]
[-] CheckoutOrderSummaryCouponFormBlock.php
[edit]
[-] AtomicBlock.php
[edit]
[-] MiniCartItemsBlock.php
[edit]
[-] ProductSaleBadge.php
[edit]
[-] CartAcceptedPaymentMethodsBlock.php
[edit]
[-] CheckoutPickupOptionsBlock.php
[edit]
[-] ProductQuery.php
[edit]
[-] AbstractProductGrid.php
[edit]
[-] ProductSummary.php
[edit]
[-] CheckoutFieldsBlock.php
[edit]
[-] CustomerAccount.php
[edit]
[-] ReviewsByCategory.php
[edit]
[-] ProductFilter.php
[edit]
[-] ClassicTemplate.php
[edit]
[-] ProductFilterActive.php
[edit]
[-] AbstractDynamicBlock.php
[edit]
[-] ProductGalleryLargeImage.php
[edit]
[-] CartOrderSummarySubtotalBlock.php
[edit]
[-] CartLineItemsBlock.php
[edit]
[-] ProductRating.php
[edit]
[-] ProductFilterPrice.php
[edit]
[-] MiniCartCheckoutButtonBlock.php
[edit]
[-] PriceFilter.php
[edit]
[-] ProductTitle.php
[edit]
[-] PageContentWrapper.php
[edit]
[-] CartCrossSellsBlock.php
[edit]
[-] ProductDetails.php
[edit]
[-] ProductGalleryThumbnails.php
[edit]
[-] ProductButton.php
[edit]
[-] RelatedProducts.php
[edit]
[-] FeaturedCategory.php
[edit]
[-] CheckoutOrderSummaryFeeBlock.php
[edit]
[-] FilledCartBlock.php
[edit]
[-] ProductReviews.php
[edit]
[-] CatalogSorting.php
[edit]
[-] MiniCartContents.php
[edit]
[-] EmptyCartBlock.php
[edit]
[-] ProductCollection.php
[edit]
[-] CheckoutBillingAddressBlock.php
[edit]
[-] CartOrderSummaryHeadingBlock.php
[edit]
[-] SingleProduct.php
[edit]
[-] ProceedToCheckoutBlock.php
[edit]
[-] CheckoutPaymentBlock.php
[edit]
[-] CheckoutTermsBlock.php
[edit]
[-] ProductGallery.php
[edit]
[-] EmptyMiniCartContentsBlock.php
[edit]
[-] ProductCollectionNoResults.php
[edit]
[-] ProductTag.php
[edit]
[-] CheckoutActionsBlock.php
[edit]
[-] CheckoutShippingMethodsBlock.php
[edit]
[-] ProductResultsCount.php
[edit]
[-] MiniCartTitleBlock.php
[edit]
[-] FilledMiniCartContentsBlock.php
[edit]
[-] AttributeFilter.php
[edit]
[-] RatingFilter.php
[edit]
[-] CartOrderSummaryDiscountBlock.php
[edit]
[-] CheckoutOrderSummarySubtotalBlock.php
[edit]
[-] HandpickedProducts.php
[edit]
[-] ProductGalleryPager.php
[edit]
[-] CheckoutOrderSummaryDiscountBlock.php
[edit]
[-] ProductImageGallery.php
[edit]
[-] CartOrderSummaryShippingBlock.php
[edit]
[-] ProductAddToCart.php
[edit]
[-] StockFilter.php
[edit]
[-] StoreNotices.php
[edit]
[-] ReviewsByProduct.php
[edit]
[-] CheckoutShippingAddressBlock.php
[edit]
[-] CartOrderSummaryBlock.php
[edit]
[-] ProductFilterRating.php
[edit]
[-] MiniCartTitleLabelBlock.php
[edit]
[+]
OrderConfirmation
[-] ProductSKU.php
[edit]
[-] ProductStockIndicator.php
[edit]
[-] ActiveFilters.php
[edit]
[-] ProductRatingCounter.php
[edit]
[-] ProductsByAttribute.php
[edit]
[-] MiniCartCartButtonBlock.php
[edit]
[-] FeaturedItem.php
[edit]
[-] MiniCartFooterBlock.php
[edit]
[-] FilterWrapper.php
[edit]
[-] ProductNew.php
[edit]
[-] CheckoutOrderSummaryTaxesBlock.php
[edit]
[-] AllReviews.php
[edit]
[-] CartCrossSellsProductsBlock.php
[edit]
[-] ProductTemplate.php
[edit]
[-] CartOrderSummaryFeeBlock.php
[edit]
[-] MiniCart.php
[edit]
[-] ProductTopRated.php
[edit]
[-] ProductGalleryLargeImageNextPrevious.php
[edit]
[-] Checkout.php
[edit]
[-] CheckoutTotalsBlock.php
[edit]
[-] Breadcrumbs.php
[edit]
[-] ProductImage.php
[edit]
[-] Cart.php
[edit]
[-] AbstractBlock.php
[edit]
[-] ProductSearch.php
[edit]
[-] CartTotalsBlock.php
[edit]
[-] AllProducts.php
[edit]
[-] MiniCartTitleItemsCounterBlock.php
[edit]
[-] CheckoutOrderSummaryBlock.php
[edit]
[-] error_log
[edit]
[-] ClassicShortcode.php
[edit]
[-] ProductOnSale.php
[edit]
[-] CheckoutOrderSummaryShippingBlock.php
[edit]
[-] ProductRatingStars.php
[edit]
[-] ProductBestSellers.php
[edit]
[-] CartOrderSummaryCouponFormBlock.php
[edit]
[-] ProductCategory.php
[edit]
[-] CheckoutOrderSummaryCartItemsBlock.php
[edit]
[-] ProductAverageRating.php
[edit]
[-] FeaturedProduct.php
[edit]
[-] CheckoutShippingMethodBlock.php
[edit]