AI
2 years ago
AIContent
2 years ago
Assets
2 years ago
BlockTypes
2 years ago
Domain
2 years ago
Images
2 years ago
Integrations
2 years ago
Interactivity
2 years ago
InteractivityComponents
2 years ago
Payments
2 years ago
Registry
2 years ago
Shipping
2 years ago
Templates
2 years ago
Utils
2 years ago
Assets.php
2 years ago
AssetsController.php
2 years ago
BlockPatterns.php
2 years ago
BlockTemplatesController.php
2 years ago
BlockTemplatesRegistry.php
2 years ago
BlockTypesController.php
2 years ago
InboxNotifications.php
2 years ago
Installer.php
2 years ago
Library.php
2 years ago
Migration.php
2 years ago
Options.php
2 years ago
Package.php
2 years ago
QueryFilters.php
2 years ago
BlockTypesController.php
357 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\Blocks; |
| 3 | |
| 4 | use Automattic\WooCommerce\Blocks\Package; |
| 5 | use Automattic\WooCommerce\Blocks\Assets\AssetDataRegistry; |
| 6 | use Automattic\WooCommerce\Blocks\Assets\Api as AssetApi; |
| 7 | use Automattic\WooCommerce\Blocks\Integrations\IntegrationRegistry; |
| 8 | use Automattic\WooCommerce\Blocks\BlockTypes\Cart; |
| 9 | use Automattic\WooCommerce\Blocks\BlockTypes\Checkout; |
| 10 | use Automattic\WooCommerce\Blocks\BlockTypes\MiniCartContents; |
| 11 | |
| 12 | /** |
| 13 | * BlockTypesController class. |
| 14 | * |
| 15 | * @since 5.0.0 |
| 16 | * @internal |
| 17 | */ |
| 18 | final class BlockTypesController { |
| 19 | |
| 20 | /** |
| 21 | * Instance of the asset API. |
| 22 | * |
| 23 | * @var AssetApi |
| 24 | */ |
| 25 | protected $asset_api; |
| 26 | |
| 27 | /** |
| 28 | * Instance of the asset data registry. |
| 29 | * |
| 30 | * @var AssetDataRegistry |
| 31 | */ |
| 32 | protected $asset_data_registry; |
| 33 | |
| 34 | /** |
| 35 | * Constructor. |
| 36 | * |
| 37 | * @param AssetApi $asset_api Instance of the asset API. |
| 38 | * @param AssetDataRegistry $asset_data_registry Instance of the asset data registry. |
| 39 | */ |
| 40 | public function __construct( AssetApi $asset_api, AssetDataRegistry $asset_data_registry ) { |
| 41 | $this->asset_api = $asset_api; |
| 42 | $this->asset_data_registry = $asset_data_registry; |
| 43 | $this->init(); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Initialize class features. |
| 48 | */ |
| 49 | protected function init() { |
| 50 | add_action( 'init', array( $this, 'register_blocks' ) ); |
| 51 | add_filter( 'render_block', array( $this, 'add_data_attributes' ), 10, 2 ); |
| 52 | add_action( 'woocommerce_login_form_end', array( $this, 'redirect_to_field' ) ); |
| 53 | add_filter( 'widget_types_to_hide_from_legacy_widget_block', array( $this, 'hide_legacy_widgets_with_block_equivalent' ) ); |
| 54 | add_action( 'woocommerce_delete_product_transients', array( $this, 'delete_product_transients' ) ); |
| 55 | add_filter( |
| 56 | 'woocommerce_is_checkout', |
| 57 | function( $return ) { |
| 58 | return $return || $this->has_block_variation( 'woocommerce/classic-shortcode', 'shortcode', 'checkout' ); |
| 59 | } |
| 60 | ); |
| 61 | add_filter( |
| 62 | 'woocommerce_is_cart', |
| 63 | function( $return ) { |
| 64 | return $return || $this->has_block_variation( 'woocommerce/classic-shortcode', 'shortcode', 'cart' ); |
| 65 | } |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Check if the current post has a block with a specific attribute value. |
| 71 | * |
| 72 | * @param string $block_id The block ID to check for. |
| 73 | * @param string $attribute The attribute to check. |
| 74 | * @param string $value The value to check for. |
| 75 | * @return boolean |
| 76 | */ |
| 77 | private function has_block_variation( $block_id, $attribute, $value ) { |
| 78 | $post = get_post(); |
| 79 | |
| 80 | if ( ! $post ) { |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | if ( has_block( $block_id, $post->ID ) ) { |
| 85 | $blocks = (array) parse_blocks( $post->post_content ); |
| 86 | |
| 87 | foreach ( $blocks as $block ) { |
| 88 | if ( isset( $block['attrs'][ $attribute ] ) && $value === $block['attrs'][ $attribute ] ) { |
| 89 | return true; |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Register blocks, hooking up assets and render functions as needed. |
| 99 | */ |
| 100 | public function register_blocks() { |
| 101 | $block_types = $this->get_block_types(); |
| 102 | |
| 103 | foreach ( $block_types as $block_type ) { |
| 104 | $block_type_class = __NAMESPACE__ . '\\BlockTypes\\' . $block_type; |
| 105 | |
| 106 | new $block_type_class( $this->asset_api, $this->asset_data_registry, new IntegrationRegistry() ); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Add data- attributes to blocks when rendered if the block is under the woocommerce/ namespace. |
| 112 | * |
| 113 | * @param string $content Block content. |
| 114 | * @param array $block Parsed block data. |
| 115 | * @return string |
| 116 | */ |
| 117 | public function add_data_attributes( $content, $block ) { |
| 118 | $block_name = $block['blockName']; |
| 119 | $block_namespace = strtok( $block_name ?? '', '/' ); |
| 120 | |
| 121 | /** |
| 122 | * Filters the list of allowed block namespaces. |
| 123 | * |
| 124 | * This hook defines which block namespaces should have block name and attribute `data-` attributes appended on render. |
| 125 | * |
| 126 | * @since 5.9.0 |
| 127 | * |
| 128 | * @param array $allowed_namespaces List of namespaces. |
| 129 | */ |
| 130 | $allowed_namespaces = array_merge( array( 'woocommerce', 'woocommerce-checkout' ), (array) apply_filters( '__experimental_woocommerce_blocks_add_data_attributes_to_namespace', array() ) ); |
| 131 | |
| 132 | /** |
| 133 | * Filters the list of allowed Block Names |
| 134 | * |
| 135 | * This hook defines which block names should have block name and attribute data- attributes appended on render. |
| 136 | * |
| 137 | * @since 5.9.0 |
| 138 | * |
| 139 | * @param array $allowed_namespaces List of namespaces. |
| 140 | */ |
| 141 | $allowed_blocks = (array) apply_filters( '__experimental_woocommerce_blocks_add_data_attributes_to_block', array() ); |
| 142 | |
| 143 | if ( ! in_array( $block_namespace, $allowed_namespaces, true ) && ! in_array( $block_name, $allowed_blocks, true ) ) { |
| 144 | return $content; |
| 145 | } |
| 146 | |
| 147 | $attributes = (array) $block['attrs']; |
| 148 | $exclude_attributes = array( 'className', 'align' ); |
| 149 | $escaped_data_attributes = array( |
| 150 | 'data-block-name="' . esc_attr( $block['blockName'] ) . '"', |
| 151 | ); |
| 152 | |
| 153 | foreach ( $attributes as $key => $value ) { |
| 154 | if ( in_array( $key, $exclude_attributes, true ) ) { |
| 155 | continue; |
| 156 | } |
| 157 | if ( is_bool( $value ) ) { |
| 158 | $value = $value ? 'true' : 'false'; |
| 159 | } |
| 160 | if ( ! is_scalar( $value ) ) { |
| 161 | $value = wp_json_encode( $value ); |
| 162 | } |
| 163 | $escaped_data_attributes[] = 'data-' . esc_attr( strtolower( preg_replace( '/(?<!\ )[A-Z]/', '-$0', $key ) ) ) . '="' . esc_attr( $value ) . '"'; |
| 164 | } |
| 165 | |
| 166 | return preg_replace( '/^<div /', '<div ' . implode( ' ', $escaped_data_attributes ) . ' ', trim( $content ) ); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Adds a redirect field to the login form so blocks can redirect users after login. |
| 171 | */ |
| 172 | public function redirect_to_field() { |
| 173 | // phpcs:ignore WordPress.Security.NonceVerification |
| 174 | if ( empty( $_GET['redirect_to'] ) ) { |
| 175 | return; |
| 176 | } |
| 177 | echo '<input type="hidden" name="redirect" value="' . esc_attr( esc_url_raw( wp_unslash( $_GET['redirect_to'] ) ) ) . '" />'; // phpcs:ignore WordPress.Security.NonceVerification |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Hide legacy widgets with a feature complete block equivalent in the inserter |
| 182 | * and prevent them from showing as an option in the Legacy Widget block. |
| 183 | * |
| 184 | * @param array $widget_types An array of widgets hidden in core. |
| 185 | * @return array $widget_types An array inluding the WooCommerce widgets to hide. |
| 186 | */ |
| 187 | public function hide_legacy_widgets_with_block_equivalent( $widget_types ) { |
| 188 | array_push( |
| 189 | $widget_types, |
| 190 | 'woocommerce_product_search', |
| 191 | 'woocommerce_product_categories', |
| 192 | 'woocommerce_recent_reviews', |
| 193 | 'woocommerce_product_tag_cloud', |
| 194 | 'woocommerce_price_filter', |
| 195 | 'woocommerce_layered_nav', |
| 196 | 'woocommerce_layered_nav_filters', |
| 197 | 'woocommerce_rating_filter' |
| 198 | ); |
| 199 | |
| 200 | return $widget_types; |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Delete product transients when a product is deleted. |
| 205 | */ |
| 206 | public function delete_product_transients() { |
| 207 | delete_transient( 'wc_blocks_has_downloadable_product' ); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Get list of block types. |
| 212 | * |
| 213 | * @return array |
| 214 | */ |
| 215 | protected function get_block_types() { |
| 216 | global $pagenow; |
| 217 | |
| 218 | $block_types = array( |
| 219 | 'ActiveFilters', |
| 220 | 'AddToCartForm', |
| 221 | 'AllProducts', |
| 222 | 'AllReviews', |
| 223 | 'AttributeFilter', |
| 224 | 'Breadcrumbs', |
| 225 | 'CatalogSorting', |
| 226 | 'ClassicTemplate', |
| 227 | 'ClassicShortcode', |
| 228 | 'CustomerAccount', |
| 229 | 'FeaturedCategory', |
| 230 | 'FeaturedProduct', |
| 231 | 'FilterWrapper', |
| 232 | 'HandpickedProducts', |
| 233 | 'MiniCart', |
| 234 | 'StoreNotices', |
| 235 | 'PriceFilter', |
| 236 | 'ProductAddToCart', |
| 237 | 'ProductBestSellers', |
| 238 | 'ProductButton', |
| 239 | 'ProductCategories', |
| 240 | 'ProductCategory', |
| 241 | 'ProductCollection', |
| 242 | 'ProductCollectionNoResults', |
| 243 | 'ProductGallery', |
| 244 | 'ProductGalleryLargeImage', |
| 245 | 'ProductGalleryLargeImageNextPrevious', |
| 246 | 'ProductGalleryPager', |
| 247 | 'ProductGalleryThumbnails', |
| 248 | 'ProductImage', |
| 249 | 'ProductImageGallery', |
| 250 | 'ProductNew', |
| 251 | 'ProductOnSale', |
| 252 | 'ProductPrice', |
| 253 | 'ProductTemplate', |
| 254 | 'ProductQuery', |
| 255 | 'ProductAverageRating', |
| 256 | 'ProductRating', |
| 257 | 'ProductRatingCounter', |
| 258 | 'ProductRatingStars', |
| 259 | 'ProductResultsCount', |
| 260 | 'ProductReviews', |
| 261 | 'ProductSaleBadge', |
| 262 | 'ProductSearch', |
| 263 | 'ProductSKU', |
| 264 | 'ProductStockIndicator', |
| 265 | 'ProductSummary', |
| 266 | 'ProductTag', |
| 267 | 'ProductTitle', |
| 268 | 'ProductTopRated', |
| 269 | 'ProductsByAttribute', |
| 270 | 'RatingFilter', |
| 271 | 'ReviewsByCategory', |
| 272 | 'ReviewsByProduct', |
| 273 | 'RelatedProducts', |
| 274 | 'ProductDetails', |
| 275 | 'SingleProduct', |
| 276 | 'StockFilter', |
| 277 | 'PageContentWrapper', |
| 278 | 'OrderConfirmation\Status', |
| 279 | 'OrderConfirmation\Summary', |
| 280 | 'OrderConfirmation\Totals', |
| 281 | 'OrderConfirmation\TotalsWrapper', |
| 282 | 'OrderConfirmation\Downloads', |
| 283 | 'OrderConfirmation\DownloadsWrapper', |
| 284 | 'OrderConfirmation\BillingAddress', |
| 285 | 'OrderConfirmation\ShippingAddress', |
| 286 | 'OrderConfirmation\BillingWrapper', |
| 287 | 'OrderConfirmation\ShippingWrapper', |
| 288 | 'OrderConfirmation\AdditionalInformation', |
| 289 | 'OrderConfirmation\AdditionalFieldsWrapper', |
| 290 | 'OrderConfirmation\AdditionalFields', |
| 291 | ); |
| 292 | |
| 293 | $block_types = array_merge( |
| 294 | $block_types, |
| 295 | Cart::get_cart_block_types(), |
| 296 | Checkout::get_checkout_block_types(), |
| 297 | MiniCartContents::get_mini_cart_block_types() |
| 298 | ); |
| 299 | |
| 300 | if ( Package::feature()->is_experimental_build() ) { |
| 301 | $block_types[] = 'ProductFilter'; |
| 302 | $block_types[] = 'ProductFilterStockStatus'; |
| 303 | $block_types[] = 'ProductFilterPrice'; |
| 304 | $block_types[] = 'ProductFilterAttribute'; |
| 305 | $block_types[] = 'ProductFilterRating'; |
| 306 | $block_types[] = 'ProductFilterActive'; |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * This disables specific blocks in Widget Areas by not registering them. |
| 311 | */ |
| 312 | if ( in_array( $pagenow, array( 'widgets.php', 'themes.php', 'customize.php' ), true ) && ( empty( $_GET['page'] ) || 'gutenberg-edit-site' !== $_GET['page'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 313 | $block_types = array_diff( |
| 314 | $block_types, |
| 315 | array( |
| 316 | 'AllProducts', |
| 317 | 'Cart', |
| 318 | 'Checkout', |
| 319 | ) |
| 320 | ); |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * This disables specific blocks in Post and Page editor by not registering them. |
| 325 | */ |
| 326 | if ( in_array( $pagenow, array( 'post.php', 'post-new.php' ), true ) ) { |
| 327 | $block_types = array_diff( |
| 328 | $block_types, |
| 329 | array( |
| 330 | 'AddToCartForm', |
| 331 | 'Breadcrumbs', |
| 332 | 'CatalogSorting', |
| 333 | 'ClassicTemplate', |
| 334 | 'ProductResultsCount', |
| 335 | 'ProductDetails', |
| 336 | 'OrderConfirmation\Status', |
| 337 | 'OrderConfirmation\Summary', |
| 338 | 'OrderConfirmation\Totals', |
| 339 | 'OrderConfirmation\TotalsWrapper', |
| 340 | 'OrderConfirmation\Downloads', |
| 341 | 'OrderConfirmation\DownloadsWrapper', |
| 342 | 'OrderConfirmation\BillingAddress', |
| 343 | 'OrderConfirmation\ShippingAddress', |
| 344 | 'OrderConfirmation\BillingWrapper', |
| 345 | 'OrderConfirmation\ShippingWrapper', |
| 346 | 'OrderConfirmation\AdditionalInformation', |
| 347 | 'OrderConfirmation\AdditionalFieldsWrapper', |
| 348 | 'OrderConfirmation\AdditionalFields', |
| 349 | ) |
| 350 | ); |
| 351 | } |
| 352 | |
| 353 | return $block_types; |
| 354 | } |
| 355 | |
| 356 | } |
| 357 |