Accordion
1 year ago
AddToCartWithOptions
2 weeks ago
OrderConfirmation
2 months ago
ProductCollection
3 weeks ago
Reviews
2 months ago
AbstractBlock.php
4 weeks ago
AbstractDynamicBlock.php
2 years ago
AbstractInnerBlock.php
1 year ago
AbstractProductGrid.php
3 months ago
ActiveFilters.php
2 years ago
AddToCartForm.php
4 weeks ago
AddToWishlistButton.php
4 weeks ago
AllProducts.php
2 years ago
AllReviews.php
2 years ago
AtomicBlock.php
2 years ago
AttributeFilter.php
1 year ago
Breadcrumbs.php
1 year ago
Cart.php
2 months ago
CartAcceptedPaymentMethodsBlock.php
2 years ago
CartCrossSellsBlock.php
2 years ago
CartCrossSellsProductsBlock.php
2 years ago
CartExpressPaymentBlock.php
1 year ago
CartItemsBlock.php
2 years ago
CartLineItemsBlock.php
2 years ago
CartLink.php
2 months ago
CartOrderSummaryBlock.php
2 years ago
CartOrderSummaryCouponFormBlock.php
2 years ago
CartOrderSummaryDiscountBlock.php
2 years ago
CartOrderSummaryFeeBlock.php
2 years ago
CartOrderSummaryHeadingBlock.php
2 years ago
CartOrderSummaryShippingBlock.php
2 years ago
CartOrderSummarySubtotalBlock.php
2 years ago
CartOrderSummaryTaxesBlock.php
2 years ago
CartOrderSummaryTotalsBlock.php
2 years ago
CartTotalsBlock.php
2 years ago
CatalogSorting.php
4 weeks ago
CategoryDescription.php
2 months ago
CategoryTitle.php
2 months ago
Checkout.php
2 months ago
CheckoutActionsBlock.php
1 year ago
CheckoutAdditionalInformationBlock.php
2 years ago
CheckoutBillingAddressBlock.php
2 years ago
CheckoutContactInformationBlock.php
2 years ago
CheckoutExpressPaymentBlock.php
9 months ago
CheckoutFieldsBlock.php
2 years ago
CheckoutOrderNoteBlock.php
2 years ago
CheckoutOrderSummaryBlock.php
2 years ago
CheckoutOrderSummaryCartItemsBlock.php
2 years ago
CheckoutOrderSummaryCouponFormBlock.php
2 years ago
CheckoutOrderSummaryDiscountBlock.php
2 years ago
CheckoutOrderSummaryFeeBlock.php
2 years ago
CheckoutOrderSummaryShippingBlock.php
2 years ago
CheckoutOrderSummarySubtotalBlock.php
2 years ago
CheckoutOrderSummaryTaxesBlock.php
2 years ago
CheckoutOrderSummaryTotalsBlock.php
2 years ago
CheckoutPaymentBlock.php
2 years ago
CheckoutPickupOptionsBlock.php
2 years ago
CheckoutShippingAddressBlock.php
2 years ago
CheckoutShippingMethodBlock.php
2 years ago
CheckoutShippingMethodsBlock.php
2 years ago
CheckoutTermsBlock.php
2 years ago
CheckoutTotalsBlock.php
2 years ago
ClassicShortcode.php
1 year ago
ClassicTemplate.php
4 months ago
ComingSoon.php
1 year ago
CouponCode.php
2 months ago
CustomerAccount.php
3 months ago
Dropdown.php
4 weeks ago
EmailContent.php
5 months ago
EmptyCartBlock.php
2 years ago
EmptyMiniCartContentsBlock.php
11 months ago
EnableBlockJsonAssetsTrait.php
1 year ago
FeaturedCategory.php
7 months ago
FeaturedItem.php
7 months ago
FeaturedProduct.php
7 months ago
FilledCartBlock.php
2 years ago
FilledMiniCartContentsBlock.php
4 months ago
FilterWrapper.php
2 years ago
HandpickedProducts.php
1 year ago
MiniCart.php
4 weeks ago
MiniCartCartButtonBlock.php
11 months ago
MiniCartCheckoutButtonBlock.php
11 months ago
MiniCartContents.php
3 months ago
MiniCartFooterBlock.php
4 weeks ago
MiniCartItemsBlock.php
11 months ago
MiniCartProductsTableBlock.php
4 weeks ago
MiniCartShoppingButtonBlock.php
9 months ago
MiniCartTitleBlock.php
11 months ago
MiniCartTitleItemsCounterBlock.php
11 months ago
MiniCartTitleLabelBlock.php
11 months ago
NextPreviousButtons.php
5 months ago
PageContentWrapper.php
2 years ago
PaymentMethodIcons.php
11 months ago
PriceFilter.php
1 year ago
ProceedToCheckoutBlock.php
2 years ago
ProductAverageRating.php
1 year ago
ProductBestSellers.php
2 years ago
ProductButton.php
2 months ago
ProductCategories.php
4 weeks ago
ProductCategory.php
2 years ago
ProductDescription.php
9 months ago
ProductDetails.php
5 months ago
ProductFilterActive.php
4 weeks ago
ProductFilterAttribute.php
4 weeks ago
ProductFilterCheckboxList.php
4 weeks ago
ProductFilterChips.php
4 weeks ago
ProductFilterClearButton.php
4 weeks ago
ProductFilterPrice.php
4 weeks ago
ProductFilterPriceSlider.php
4 weeks ago
ProductFilterRating.php
4 weeks ago
ProductFilterRemovableChips.php
4 weeks ago
ProductFilterStatus.php
4 weeks ago
ProductFilterTaxonomy.php
4 weeks ago
ProductFilters.php
3 weeks ago
ProductGallery.php
4 weeks ago
ProductGalleryLargeImage.php
4 weeks ago
ProductGalleryThumbnails.php
4 weeks ago
ProductImage.php
4 weeks ago
ProductImageGallery.php
7 months ago
ProductMeta.php
1 year ago
ProductNew.php
2 years ago
ProductOnSale.php
2 years ago
ProductPrice.php
2 months ago
ProductQuery.php
4 weeks ago
ProductRating.php
1 year ago
ProductRatingCounter.php
1 year ago
ProductRatingStars.php
1 year ago
ProductResultsCount.php
2 months ago
ProductSKU.php
2 months ago
ProductSaleBadge.php
1 year ago
ProductSearch.php
10 months ago
ProductSpecifications.php
2 months ago
ProductStockIndicator.php
2 months ago
ProductSummary.php
7 months ago
ProductTag.php
2 years ago
ProductTemplate.php
2 months ago
ProductTitle.php
1 year ago
ProductTopRated.php
2 years ago
ProductsByAttribute.php
2 years ago
RatingFilter.php
2 years ago
RelatedProducts.php
3 months ago
ReviewsByCategory.php
2 years ago
ReviewsByProduct.php
2 years ago
SavedForLater.php
4 weeks ago
SingleProduct.php
2 months ago
StockFilter.php
2 years ago
StoreNotices.php
1 year ago
Wishlist.php
4 weeks ago
CheckoutExpressPaymentBlock.php
141 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\Blocks\BlockTypes; |
| 3 | |
| 4 | use Automattic\WooCommerce\Blocks\Utils\CartCheckoutUtils; |
| 5 | use Exception; |
| 6 | |
| 7 | /** |
| 8 | * CheckoutExpressPaymentBlock class. |
| 9 | */ |
| 10 | class CheckoutExpressPaymentBlock extends AbstractInnerBlock { |
| 11 | /** |
| 12 | * Block name. |
| 13 | * |
| 14 | * @var string |
| 15 | */ |
| 16 | protected $block_name = 'checkout-express-payment-block'; |
| 17 | |
| 18 | /** |
| 19 | * Default styles for the express payment buttons |
| 20 | * |
| 21 | * @var boolean |
| 22 | */ |
| 23 | protected $default_styles = null; |
| 24 | |
| 25 | /** |
| 26 | * Current styles for the express payment buttons |
| 27 | * |
| 28 | * @var boolean |
| 29 | */ |
| 30 | protected $current_styles = null; |
| 31 | |
| 32 | /** |
| 33 | * Initialise the block |
| 34 | */ |
| 35 | protected function initialize() { |
| 36 | parent::initialize(); |
| 37 | |
| 38 | $this->default_styles = array( |
| 39 | 'showButtonStyles' => false, |
| 40 | 'buttonHeight' => '48', |
| 41 | 'buttonBorderRadius' => '4', |
| 42 | ); |
| 43 | |
| 44 | add_action( 'save_post', array( $this, 'sync_express_payment_attrs' ), 10, 2 ); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Synchorize the express payment attributes between the Cart and Checkout pages. |
| 49 | * |
| 50 | * @param int $post_id Post ID. |
| 51 | * @param WP_Post $post Post object. |
| 52 | */ |
| 53 | public function sync_express_payment_attrs( $post_id, $post ) { |
| 54 | if ( wc_get_page_id( 'cart' ) === $post_id ) { |
| 55 | $cart_or_checkout = 'cart'; |
| 56 | } elseif ( wc_get_page_id( 'checkout' ) === $post_id ) { |
| 57 | $cart_or_checkout = 'checkout'; |
| 58 | } else { |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | // This is not a proper save action, maybe an autosave, so don't continue. |
| 63 | if ( empty( $post->post_status ) || 'inherit' === $post->post_status ) { |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | $block_name = 'woocommerce/' . $cart_or_checkout; |
| 68 | $page_id = 'woocommerce_' . $cart_or_checkout . '_page_id'; |
| 69 | $template_name = 'page-' . $cart_or_checkout; |
| 70 | |
| 71 | // Check if we are editing the cart/checkout page and that it contains a Cart/Checkout block. |
| 72 | // Cast to string for Cart/Checkout page ID comparison because get_option can return it as a string, so better to compare both values as strings. |
| 73 | if ( ! empty( $post->post_type ) && 'wp_template' !== $post->post_type && ( false === has_block( $block_name, $post ) || (string) get_option( $page_id ) !== (string) $post_id ) ) { |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | // Check if we are editing the Cart/Checkout template and that it contains a Cart/Checkout block. |
| 78 | if ( ( ! empty( $post->post_type ) && ! empty( $post->post_name ) && $template_name !== $post->post_name && 'wp_template' === $post->post_type ) || false === has_block( $block_name, $post ) ) { |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | if ( empty( $post->post_content ) ) { |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | try { |
| 87 | // Parse the post content to get the express payment attributes of the current page. |
| 88 | $attrs = CartCheckoutUtils::find_express_checkout_attributes( $post->post_content, $cart_or_checkout ); |
| 89 | |
| 90 | if ( ! is_array( $attrs ) ) { |
| 91 | return; |
| 92 | } |
| 93 | $updated_attrs = array_merge( $this->default_styles, $attrs ); |
| 94 | |
| 95 | // We need to sync the attributes between the Cart and Checkout pages. |
| 96 | $other_page = 'cart' === $cart_or_checkout ? 'checkout' : 'cart'; |
| 97 | |
| 98 | $this->update_other_page_with_express_payment_attrs( $other_page, $updated_attrs ); |
| 99 | } catch ( Exception $e ) { |
| 100 | wc_get_logger()->log( 'error', 'Error updating express payment attributes: ' . $e->getMessage() ); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Update the express payment attributes in the other page (Cart or Checkout). |
| 106 | * |
| 107 | * @param string $cart_or_checkout The page to update. |
| 108 | * @param array $updated_attrs The updated attributes. |
| 109 | */ |
| 110 | private function update_other_page_with_express_payment_attrs( $cart_or_checkout, $updated_attrs ) { |
| 111 | $page_id = 'cart' === $cart_or_checkout ? wc_get_page_id( 'cart' ) : wc_get_page_id( 'checkout' ); |
| 112 | |
| 113 | if ( -1 === $page_id ) { |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | $post = get_post( $page_id ); |
| 118 | |
| 119 | if ( empty( $post->post_content ) ) { |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | $blocks = parse_blocks( $post->post_content ); |
| 124 | CartCheckoutUtils::update_blocks_with_new_attrs( $blocks, $cart_or_checkout, $updated_attrs ); |
| 125 | |
| 126 | $updated_content = serialize_blocks( $blocks ); |
| 127 | remove_action( 'save_post', array( $this, 'sync_express_payment_attrs' ), 10, 2 ); |
| 128 | |
| 129 | wp_update_post( |
| 130 | array( |
| 131 | 'ID' => $page_id, |
| 132 | 'post_content' => $updated_content, |
| 133 | ), |
| 134 | false, |
| 135 | false |
| 136 | ); |
| 137 | |
| 138 | add_action( 'save_post', array( $this, 'sync_express_payment_attrs' ), 10, 2 ); |
| 139 | } |
| 140 | } |
| 141 |