Accordion
1 year ago
AddToCartWithOptions
3 days ago
OrderConfirmation
2 months ago
ProductCollection
3 days ago
Reviews
2 months ago
AbstractBlock.php
3 days ago
AbstractDynamicBlock.php
2 years ago
AbstractInnerBlock.php
1 year ago
AbstractProductGrid.php
3 months ago
ActiveFilters.php
2 years ago
AddToCartForm.php
1 month ago
AddToWishlistButton.php
1 month ago
AllProducts.php
3 days ago
AllReviews.php
2 years ago
AtomicBlock.php
2 years ago
AttributeFilter.php
1 year ago
Breadcrumbs.php
3 days ago
Cart.php
3 days 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
1 month ago
CategoryDescription.php
2 months ago
CategoryTitle.php
2 months ago
Checkout.php
3 days 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
3 days 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
3 days ago
MiniCartCartButtonBlock.php
11 months ago
MiniCartCheckoutButtonBlock.php
11 months ago
MiniCartContents.php
3 months ago
MiniCartFooterBlock.php
3 days ago
MiniCartItemsBlock.php
11 months ago
MiniCartProductsTableBlock.php
3 days 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
3 days ago
ProductCategories.php
1 month ago
ProductCategory.php
2 years ago
ProductDescription.php
9 months ago
ProductDetails.php
5 months ago
ProductFilterActive.php
1 month ago
ProductFilterAttribute.php
3 days ago
ProductFilterCheckboxList.php
1 month ago
ProductFilterChips.php
1 month ago
ProductFilterClearButton.php
1 month ago
ProductFilterPrice.php
1 month ago
ProductFilterPriceSlider.php
1 month ago
ProductFilterRating.php
1 month ago
ProductFilterRemovableChips.php
1 month ago
ProductFilterStatus.php
3 days ago
ProductFilterTaxonomy.php
3 days ago
ProductFilters.php
3 days ago
ProductGallery.php
1 month ago
ProductGalleryLargeImage.php
1 month ago
ProductGalleryThumbnails.php
1 month ago
ProductImage.php
3 days ago
ProductImageGallery.php
8 months ago
ProductMeta.php
1 year ago
ProductNew.php
2 years ago
ProductOnSale.php
2 years ago
ProductPrice.php
2 months ago
ProductQuery.php
3 days 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
3 days 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
1 month ago
SingleProduct.php
2 months ago
StockFilter.php
2 years ago
StoreNotices.php
1 year ago
Wishlist.php
1 month ago
Breadcrumbs.php
158 lines
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | |
| 4 | namespace Automattic\WooCommerce\Blocks\BlockTypes; |
| 5 | |
| 6 | use Automattic\WooCommerce\Blocks\Utils\StyleAttributesUtils; |
| 7 | use WP_Block; |
| 8 | |
| 9 | /** |
| 10 | * Breadcrumbs class. |
| 11 | */ |
| 12 | class Breadcrumbs extends AbstractBlock { |
| 13 | |
| 14 | /** |
| 15 | * Block name. |
| 16 | * |
| 17 | * @var string |
| 18 | */ |
| 19 | protected $block_name = 'breadcrumbs'; |
| 20 | |
| 21 | /** |
| 22 | * Render the block. |
| 23 | * |
| 24 | * @param array $attributes Block attributes. |
| 25 | * @param string $content Block content. |
| 26 | * @param WP_Block $block Block instance. |
| 27 | * |
| 28 | * @return string | void Rendered block output. |
| 29 | */ |
| 30 | protected function render( $attributes, $content, $block ) { |
| 31 | ob_start(); |
| 32 | woocommerce_breadcrumb(); |
| 33 | $breadcrumb = ob_get_clean(); |
| 34 | |
| 35 | if ( ! $breadcrumb ) { |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | $classes_and_styles = StyleAttributesUtils::get_classes_and_styles_by_attributes( $attributes, array(), array( 'font_size' ) ); |
| 40 | |
| 41 | $font_size_classes_and_styles = $this->get_font_size_classes_and_styles( $attributes, $block ); |
| 42 | $classes_and_styles['classes'] = $classes_and_styles['classes'] . ' ' . ( $font_size_classes_and_styles['class'] ?? '' ) . ' '; |
| 43 | $classes_and_styles['styles'] = $classes_and_styles['styles'] . ' ' . ( $font_size_classes_and_styles['style'] ?? '' ) . ' '; |
| 44 | |
| 45 | $wrapper_attributes = get_block_wrapper_attributes( |
| 46 | array( |
| 47 | 'class' => 'woocommerce wc-block-breadcrumbs ' . trim( $classes_and_styles['classes'] ), |
| 48 | 'style' => trim( $classes_and_styles['styles'] ), |
| 49 | ) |
| 50 | ); |
| 51 | |
| 52 | $has_non_small_custom_font_size = strpos( $font_size_classes_and_styles['class'] ?? '', 'has-small-font-size' ) === false; |
| 53 | |
| 54 | // Remove the default 'has-small-font-size' class when the block has a custom font size different from small. |
| 55 | // This is needed because the block.json defines a default font size, which is considered an anti-pattern |
| 56 | // since styles should be defined by themes and plugins instead. |
| 57 | if ( $has_non_small_custom_font_size ) { |
| 58 | $wrapper_attributes = str_replace( 'has-small-font-size', '', $wrapper_attributes ); |
| 59 | } |
| 60 | |
| 61 | return sprintf( |
| 62 | '<div %1$s>%2$s</div>', |
| 63 | $wrapper_attributes, |
| 64 | $breadcrumb |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Get the frontend script handle for this block type. |
| 70 | * |
| 71 | * @param string $key Data to get, or default to everything. |
| 72 | */ |
| 73 | protected function get_block_type_script( $key = null ) { |
| 74 | return null; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Gets font size classes and styles for the breadcrumbs block. |
| 79 | * |
| 80 | * @param array $attributes The block attributes. |
| 81 | * @param WP_Block $block The block instance. |
| 82 | * @return array The font size classes and styles. |
| 83 | */ |
| 84 | private function get_font_size_classes_and_styles( array $attributes, $block ) { |
| 85 | $custom_font_size = $attributes['style']['typography']['fontSize'] ?? ''; |
| 86 | |
| 87 | if ( '' !== $custom_font_size ) { |
| 88 | return array( |
| 89 | 'class' => null, |
| 90 | 'style' => sprintf( 'font-size: %s;', $custom_font_size ), |
| 91 | ); |
| 92 | } |
| 93 | |
| 94 | $explicit_font_size = isset( $block->parsed_block['attrs']['fontSize'] ) ? $block->parsed_block['attrs']['fontSize'] : null; |
| 95 | |
| 96 | if ( is_string( $explicit_font_size ) && '' !== $explicit_font_size ) { |
| 97 | return array( |
| 98 | 'class' => sprintf( 'has-font-size has-%s-font-size', $explicit_font_size ), |
| 99 | 'style' => null, |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | $theme_font_size_classes_and_styles = $this->get_theme_font_size_classes_and_styles(); |
| 104 | |
| 105 | if ( $theme_font_size_classes_and_styles['class'] || $theme_font_size_classes_and_styles['style'] ) { |
| 106 | return $theme_font_size_classes_and_styles; |
| 107 | } |
| 108 | |
| 109 | $font_size = $attributes['fontSize'] ?? ''; |
| 110 | |
| 111 | if ( $font_size ) { |
| 112 | return array( |
| 113 | 'class' => sprintf( 'has-font-size has-%s-font-size', $font_size ), |
| 114 | 'style' => null, |
| 115 | ); |
| 116 | } |
| 117 | |
| 118 | return array( |
| 119 | 'class' => null, |
| 120 | 'style' => null, |
| 121 | ); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Gets font size classes and styles from theme.json block styles. |
| 126 | * |
| 127 | * @return array The font size classes and styles. |
| 128 | */ |
| 129 | private function get_theme_font_size_classes_and_styles() { |
| 130 | $theme_font_size = wp_get_global_styles( |
| 131 | array( 'blocks', 'woocommerce/breadcrumbs', 'typography', 'fontSize' ) |
| 132 | ); |
| 133 | |
| 134 | if ( ! is_string( $theme_font_size ) || '' === $theme_font_size ) { |
| 135 | return array( |
| 136 | 'class' => null, |
| 137 | 'style' => null, |
| 138 | ); |
| 139 | } |
| 140 | |
| 141 | $preset_prefix = 'var(--wp--preset--font-size--'; |
| 142 | |
| 143 | if ( str_starts_with( $theme_font_size, $preset_prefix ) ) { |
| 144 | $slug = rtrim( substr( $theme_font_size, strlen( $preset_prefix ) ), ')' ); |
| 145 | |
| 146 | return array( |
| 147 | 'class' => sprintf( 'has-font-size has-%s-font-size', $slug ), |
| 148 | 'style' => null, |
| 149 | ); |
| 150 | } |
| 151 | |
| 152 | return array( |
| 153 | 'class' => null, |
| 154 | 'style' => sprintf( 'font-size: %s;', $theme_font_size ), |
| 155 | ); |
| 156 | } |
| 157 | } |
| 158 |