mes * * This hook defines which block names should have block name and attribute data- attributes appended on render. * * @since 5.9.0 * * @param array $allowed_namespaces List of namespaces. */ $allowed_blocks = (array) apply_filters( '__experimental_woocommerce_blocks_add_data_attributes_to_block', array() ); $blocks_with_woo_parents = $this->get_registered_blocks_with_woocommerce_parent(); $block_has_woo_parent = in_array( $block_name, array_keys( $blocks_with_woo_parents ), true ); $in_allowed_namespace_list = in_array( $block_namespace, $allowed_namespaces, true ); $in_allowed_block_list = in_array( $block_name, $allowed_blocks, true ); return $block_has_woo_parent || $in_allowed_block_list || $in_allowed_namespace_list; } /** * Add data- attributes to blocks when rendered if the block is under the woocommerce/ namespace. * * @param string $content Block content. * @param array $block Parsed block data. * @return string */ public function add_data_attributes( $content, $block ) { if ( ! is_string( $content ) || ! $this->block_should_have_data_attributes( $block['blockName'] ) ) { return $content; } $attributes = (array) $block['attrs']; $exclude_attributes = array( 'className', 'align' ); $processor = new \WP_HTML_Tag_Processor( $content ); if ( false === $processor->next_tag() || $processor->is_tag_closer() ) { return $content; } foreach ( $attributes as $key => $value ) { if ( ! is_string( $key ) || in_array( $key, $exclude_attributes, true ) ) { continue; } if ( is_bool( $value ) ) { $value = $value ? 'true' : 'false'; } if ( ! is_scalar( $value ) ) { $value = wp_json_encode( $value ); } // For output consistency, we convert camelCase to kebab-case and output in lowercase. $key = strtolower( preg_replace( '/(?set_attribute( "data-{$key}", $value ); } // Set this last to prevent user-input from overriding it. $processor->set_attribute( 'data-block-name', $block['blockName'] ); return $processor->get_updated_html(); } /** * Adds a redirect field to the login and registration forms so blocks can redirect users after they log in or register. */ public function redirect_to_field() { // phpcs:ignore WordPress.Security.NonceVerification if ( empty( $_GET['redirect_to'] ) ) { return; } echo ''; // phpcs:ignore WordPress.Security.NonceVerification } /** * Hide legacy widgets with a feature complete block equivalent in the inserter * and prevent them from showing as an option in the Legacy Widget block. * * @param array $widget_types An array of widgets hidden in core. * @return array $widget_types An array including the WooCommerce widgets to hide. */ public function hide_legacy_widgets_with_block_equivalent( $widget_types ) { array_push( $widget_types, 'woocommerce_product_search', 'woocommerce_product_categories', 'woocommerce_recent_reviews', 'woocommerce_product_tag_cloud', 'woocommerce_price_filter', 'woocommerce_layered_nav', 'woocommerce_layered_nav_filters', 'woocommerce_rating_filter' ); return $widget_types; } /** * Delete product transients when a product is deleted. * * @deprecated since 10.6.0 * @return void */ public function delete_product_transients() { wc_deprecated_function( __METHOD__, '10.6.0' ); } /** * Get list of block types. * * @return array */ protected function get_block_types() { $block_types = array( 'ActiveFilters', 'AddToCartForm', 'AllProducts', 'AllReviews', 'AttributeFilter', 'Breadcrumbs', 'CartLink', 'CatalogSorting', 'CategoryTitle', 'CategoryDescription', 'ClassicTemplate', 'ClassicShortcode', 'ComingSoon', 'CouponCode', 'CustomerAccount', 'Dropdown', 'EmailContent', 'FeaturedCategory', 'FeaturedProduct', 'FilterWrapper', 'HandpickedProducts', 'MiniCart', 'NextPreviousButtons', 'StoreNotices', 'PaymentMethodIcons', 'PriceFilter', 'ProductBestSellers', 'ProductButton', 'ProductCategories', 'ProductCategory', 'ProductCollection\Controller', 'ProductCollection\NoResults', 'ProductFilters', 'ProductFilterStatus', 'ProductFilterPrice', 'ProductFilterPriceSlider', 'ProductFilterAttribute', 'ProductFilterRating', 'ProductFilterActive', 'ProductFilterRemovableChips', 'ProductFilterClearButton', 'ProductFilterCheckboxList', 'ProductFilterChips', 'ProductFilterTaxonomy', 'ProductGallery', 'ProductGalleryLargeImage', 'ProductGalleryThumbnails', 'ProductImage', 'ProductImageGallery', 'ProductMeta', 'ProductNew', 'ProductOnSale', 'ProductPrice', 'ProductTemplate', 'ProductQuery', 'ProductAverageRating', 'ProductRating', 'ProductRatingCounter', 'ProductRatingStars', 'ProductResultsCount', 'ProductSaleBadge', 'ProductSearch', 'ProductSKU', 'ProductStockIndicator', 'ProductSummary', 'ProductTag', 'ProductTitle', 'ProductTopRated', 'ProductsByAttribute', 'RatingFilter', 'ReviewsByCategory', 'ReviewsByProduct', 'RelatedProducts', 'SingleProduct', 'StockFilter', 'PageContentWrapper', 'OrderConfirmation\Status', 'OrderConfirmation\Summary', 'OrderConfirmation\Totals', 'OrderConfirmation\TotalsWrapper', 'OrderConfirmation\Downloads', 'OrderConfirmation\DownloadsWrapper', 'OrderConfirmation\BillingAddress', 'OrderConfirmation\ShippingAddress', 'OrderConfirmation\BillingWrapper', 'OrderConfirmation\ShippingWrapper', 'OrderConfirmation\AdditionalInformation', 'OrderConfirmation\AdditionalFieldsWrapper', 'OrderConfirmation\AdditionalFields', 'OrderConfirmation\CreateAccount', 'ProductDetails', 'ProductDescription', 'ProductSpecifications', // Generic blocks that will be pushed upstream. 'Accordion\AccordionGroup', 'Accordion\AccordionItem', 'Accordion\AccordionPanel', 'Accordion\AccordionHeader', // End: generic blocks that will be pushed upstream. 'Reviews\ProductReviews', 'Reviews\ProductReviewRating', 'Reviews\ProductReviewsTitle', 'Reviews\ProductReviewForm', 'Reviews\ProductReviewDate', 'Reviews\ProductReviewContent', 'Reviews\ProductReviewAuthorName', 'Reviews\ProductReviewsPagination', 'Reviews\ProductReviewsPaginationNext', 'Reviews\ProductReviewsPaginationPrevious', 'Reviews\ProductReviewsPaginationNumbers', 'Reviews\ProductReviewTemplate', ); $block_types = array_merge( $block_types, Cart::get_cart_block_types(), Checkout::get_checkout_block_types(), MiniCartContents::get_mini_cart_block_types() ); // Registered unconditionally so content saved while the feature was on // doesn't surface an "unsupported block" notice once it's disabled. The // block gates its own auto-injection, rendering, and inserter visibility // on the `saved-for-later` feature (see SavedForLater). $block_types[] = 'SavedForLater'; if ( wc_get_container()->get( ShopperListsController::class )->is_enabled( 'wishlist' ) ) { $block_types[] = 'Wishlist'; $block_types[] = 'AddToWishlistButton'; } if ( wp_is_block_theme() ) { $block_types[] = 'AddToCartWithOptions\AddToCartWithOptions'; $block_types[] = 'AddToCartWithOptions\QuantitySelector'; $block_types[] = 'AddToCartWithOptions\VariationDescription'; $block_types[] = 'AddToCartWithOptions\VariationSelector'; $block_types[] = 'AddToCartWithOptions\VariationSelectorAttribute'; $block_types[] = 'AddToCartWithOptions\VariationSelectorAttributeName'; $block_types[] = 'AddToCartWithOptions\GroupedProductSelector'; $block_types[] = 'AddToCartWithOptions\GroupedProductItem'; $block_types[] = 'AddToCartWithOptions\GroupedProductItemSelector'; $block_types[] = 'AddToCartWithOptions\GroupedProductItemLabel'; } /** * Filters the list of allowed block types. * * @since 9.0.0 * * @param array $block_types List of block types. */ return apply_filters( 'woocommerce_get_block_types', $block_types ); } /** * By default, when the classic theme is used, block style is always * enqueued even if the block is not used on the page. We want WooCommerce * store to always performant so we have to manually enqueue the block style * on-demand for classic themes. * * @internal * * @param array $args Block metadata. * @param string $block_name Block name. * * @return array Block metadata. */ public function enqueue_block_style_for_classic_themes( $args, $block_name ) { // Repeatedly checking the theme is expensive. So statically cache this logic result and remove the filter if not needed. static $should_enqueue_block_style_for_classic_themes = null; if ( null === $should_enqueue_block_style_for_classic_themes ) { $should_enqueue_block_style_for_classic_themes = ! ( is_admin() || wp_is_block_theme() || ( function_exists( 'wp_should_load_block_assets_on_demand' ) && wp_should_load_block_assets_on_demand() ) || wp_should_load_separate_core_block_assets() ); } if ( ! $should_enqueue_block_style_for_classic_themes ) { remove_filter( 'register_block_type_args', array( $this, 'enqueue_block_style_for_classic_themes' ), 10 ); return $args; } if ( false === strpos( $block_name, 'woocommerce/' ) || ( empty( $args['style_handles'] ) && empty( $args['style'] ) ) ) { return $args; } $style_handlers = $args['style_handles'] ?? $args['style']; add_filter( 'render_block_' . $block_name, static function ( $html ) use ( $style_handlers ) { array_map( 'wp_enqueue_style', $style_handlers ); return $html; }, 10 ); $args['style_handles'] = array(); $args['style'] = array(); return $args; } /** * Use the WooCommerce asset version when a bundled block.json omits one. * * WordPress otherwise adds the WordPress version to stylesheets registered from that file. * * @internal * * @param array $metadata Block metadata. * @return array Block metadata. */ public function handle_block_type_metadata( $metadata ) { if ( ! is_array( $metadata ) || ! $this->is_woocommerce_block_metadata( $metadata ) ) { return $metadata; } if ( isset( $metadata['version'] ) && '' !== $metadata['version'] ) { return $metadata; } $metadata['version'] = $this->asset_api->wc_version; return $metadata; } /** * Use one shared editor stylesheet for WooCommerce blocks. * * WordPress loads `style` handles in both the frontend and editor. WooCommerce * keeps those per-block handles for frontend performance, but removes them in * admin so the block editor loads the combined stylesheet only. * * @internal * * @param array $settings Block settings. * @param array $metadata Block metadata. * * @return array Block settings. */ public function use_single_block_editor_style( $settings, $metadata ) { if ( ! BlockEditorUnifiedAssets::is_enabled() || ! is_admin() || ! $this->is_woocommerce_block_metadata( $metadata ) ) { return $settings; } $settings['style_handles'] = array(); $settings['style'] = array(); $settings['editor_style_handles'] = array( 'wc-block-library-style' ); $settings['editor_style'] = array( 'wc-block-library-style' ); return $settings; } /** * Check whether block metadata belongs to a block bundled with WooCommerce. * * @param array $metadata Block metadata. * * @return bool Whether the metadata file is in the WooCommerce blocks directory. */ private function is_woocommerce_block_metadata( $metadata ) { static $blocks_path = null; if ( null === $blocks_path ) { $resolved_path = realpath( WC_ABSPATH . 'assets/client/blocks' ); $blocks_path = false === $resolved_path ? '' : trailingslashit( wp_normalize_path( $resolved_path ) ); } if ( '' === $blocks_path || empty( $metadata['file'] ) ) { return false; } return str_starts_with( wp_normalize_path( $metadata['file'] ), $blocks_path ); } /** * Set the preferred taxonomy and term for the breadcrumbs block on the product post type. * * @internal * * @param array $settings The settings for the breadcrumbs block. * @param string $post_type The post type. * @param int $post_id The current post ID. * @return array The settings for the breadcrumbs block. */ public function set_product_breadcrumbs_preferred_taxonomy( $settings, $post_type, $post_id = 0 ) { return Package::container()->get( CoreBreadcrumbsCompatibility::class )->set_product_breadcrumbs_preferred_taxonomy( $settings, $post_type, $post_id ); } /** * Apply WooCommerce compatibility behavior to Core breadcrumb items. * * @internal * * @param array $items Array of breadcrumb items from Core. * @return array Modified breadcrumb items. */ public function apply_woocommerce_breadcrumb_filters( $items ) { return Package::container()->get( CoreBreadcrumbsCompatibility::class )->apply_woocommerce_breadcrumb_filters( $items ); } }