Accordion
1 year ago
AddToCartWithOptions
1 week 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
Dropdown.php
150 lines
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | |
| 4 | namespace Automattic\WooCommerce\Blocks\BlockTypes; |
| 5 | |
| 6 | /** |
| 7 | * Dropdown block (native select). |
| 8 | */ |
| 9 | final class Dropdown extends AbstractBlock { |
| 10 | |
| 11 | use EnableBlockJsonAssetsTrait; |
| 12 | |
| 13 | /** |
| 14 | * Block name. |
| 15 | * |
| 16 | * @var string |
| 17 | */ |
| 18 | protected $block_name = 'dropdown'; |
| 19 | |
| 20 | /** |
| 21 | * Plain-text label for a select option (no HTML in `<option>`). |
| 22 | * |
| 23 | * @param array $item Selectable item from context. |
| 24 | * @return string |
| 25 | */ |
| 26 | private function get_option_text( array $item ): string { |
| 27 | if ( isset( $item['label'] ) && is_string( $item['label'] ) ) { |
| 28 | return wp_strip_all_tags( $item['label'] ); |
| 29 | } |
| 30 | if ( ! empty( $item['ariaLabel'] ) && is_string( $item['ariaLabel'] ) ) { |
| 31 | return $item['ariaLabel']; |
| 32 | } |
| 33 | return ''; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Render the block. |
| 38 | * |
| 39 | * @param array $attributes Block attributes. |
| 40 | * @param string $content Block content. |
| 41 | * @param \WP_Block $block Block instance. |
| 42 | * @return string Rendered block type output. |
| 43 | */ |
| 44 | protected function render( $attributes, $content, $block ) { |
| 45 | if ( empty( $block->context['woocommerce/selectableItems'] ) ) { |
| 46 | return ''; |
| 47 | } |
| 48 | |
| 49 | $selectable_items = $block->context['woocommerce/selectableItems']; |
| 50 | $items = is_array( $selectable_items['items'] ?? null ) ? $selectable_items['items'] : array(); |
| 51 | $store_namespace = is_string( $selectable_items['storeNamespace'] ?? null ) ? $selectable_items['storeNamespace'] : 'woocommerce/add-to-cart-with-options'; |
| 52 | |
| 53 | if ( empty( $items ) ) { |
| 54 | return ''; |
| 55 | } |
| 56 | |
| 57 | $attribute_id = $block->context['woocommerce/attributeId'] ?? ''; |
| 58 | $has_external_label = is_string( $attribute_id ) && '' !== $attribute_id; |
| 59 | $select_id = $has_external_label |
| 60 | ? $attribute_id |
| 61 | : wp_unique_id( 'wc-block-dropdown-' ); |
| 62 | |
| 63 | $wrapper_attributes = array( |
| 64 | 'class' => 'wc-block-dropdown', |
| 65 | 'data-wp-interactive' => 'woocommerce/dropdown', |
| 66 | 'data-wp-context' => (string) wp_json_encode( |
| 67 | array( |
| 68 | 'storeNamespace' => $store_namespace, |
| 69 | ), |
| 70 | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP |
| 71 | ), |
| 72 | ); |
| 73 | |
| 74 | $aria_label = isset( $selectable_items['groupLabel'] ) && is_string( $selectable_items['groupLabel'] ) ? $selectable_items['groupLabel'] : __( 'Choose an option', 'woocommerce' ); |
| 75 | |
| 76 | ob_start(); |
| 77 | ?> |
| 78 | <div <?php echo get_block_wrapper_attributes( $wrapper_attributes ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>> |
| 79 | <fieldset class="wc-block-dropdown__fieldset"> |
| 80 | <?php if ( ! $has_external_label ) : ?> |
| 81 | <legend class="screen-reader-text"><?php echo esc_html( __( 'Choose an option', 'woocommerce' ) ); ?></legend> |
| 82 | <?php endif; ?> |
| 83 | <select |
| 84 | class="wc-block-dropdown__select" |
| 85 | id="<?php echo esc_attr( $select_id ); ?>" |
| 86 | <?php if ( $has_external_label ) : ?> |
| 87 | aria-labelledby="<?php echo esc_attr( $attribute_id . '_label' ); ?>" |
| 88 | <?php else : ?> |
| 89 | aria-label="<?php echo esc_attr( $aria_label ); ?>" |
| 90 | <?php endif; ?> |
| 91 | data-wp-bind--value="state.selectValue" |
| 92 | data-wp-on--change="actions.onDropdownChange" |
| 93 | > |
| 94 | <option value=""> |
| 95 | <?php echo esc_html( __( 'Choose an option', 'woocommerce' ) ); ?> |
| 96 | </option> |
| 97 | <?php foreach ( $items as $item ) : ?> |
| 98 | <?php |
| 99 | if ( ! is_array( $item ) ) { |
| 100 | continue; |
| 101 | } |
| 102 | $option_label = $this->get_option_text( $item ); |
| 103 | if ( empty( $option_label ) ) { |
| 104 | continue; |
| 105 | } |
| 106 | $item_id = isset( $item['id'] ) && is_string( $item['id'] ) ? $item['id'] : ''; |
| 107 | $item_value = isset( $item['value'] ) && is_string( $item['value'] ) ? $item['value'] : ''; |
| 108 | ?> |
| 109 | <option |
| 110 | id="<?php echo esc_attr( $item_id ); ?>" |
| 111 | value="<?php echo esc_attr( $item_value ); ?>" |
| 112 | <?php disabled( ! empty( $item['disabled'] ) ); ?> |
| 113 | <?php |
| 114 | if ( ! empty( $item['hidden'] ) ) : |
| 115 | ?> |
| 116 | hidden |
| 117 | <?php |
| 118 | endif; |
| 119 | if ( ! empty( $item['selected'] ) ) : |
| 120 | ?> |
| 121 | selected |
| 122 | <?php |
| 123 | endif; |
| 124 | ?> |
| 125 | data-wp-each-child |
| 126 | > |
| 127 | <?php echo esc_html( $option_label ); ?> |
| 128 | </option> |
| 129 | <?php endforeach; ?> |
| 130 | <template |
| 131 | data-wp-interactive="<?php echo esc_attr( $store_namespace ); ?>" |
| 132 | data-wp-each--item="state.selectableItems" |
| 133 | data-wp-each-key="context.item.id" |
| 134 | > |
| 135 | <option |
| 136 | data-wp-bind--value="context.item.value" |
| 137 | data-wp-bind--disabled="context.item.disabled" |
| 138 | data-wp-bind--hidden="context.item.hidden" |
| 139 | data-wp-text="context.item.label" |
| 140 | ></option> |
| 141 | </template> |
| 142 | </select> |
| 143 | </fieldset> |
| 144 | </div> |
| 145 | <?php |
| 146 | $output = ob_get_clean(); |
| 147 | return is_string( $output ) ? $output : ''; |
| 148 | } |
| 149 | } |
| 150 |