true, ) ); } /** * Enqueue assets used for rendering the block in editor context. * * This is needed if a block is not yet within the post content--`render` and `enqueue_assets` may not have ran. */ public function enqueue_block_assets() { // Ensures frontend styles for blocks exist in the site editor iframe. if ( class_exists( 'WC_Frontend_Scripts' ) && is_admin() ) { $frontend_scripts = new WC_Frontend_Scripts(); $styles = $frontend_scripts::get_styles(); foreach ( $styles as $handle => $style ) { wp_enqueue_style( $handle, set_url_scheme( $style['src'] ), $style['deps'], $style['version'], $style['media'] ); } } } /** * Enqueue assets specific to this block. * * @param array $attributes Block attributes. * @param string $content Block content. * @param WP_Block $block Block instance. */ protected function enqueue_assets( $attributes, $content, $block ) { parent::enqueue_assets( $attributes, $content, $block ); if ( is_product() ) { add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_legacy_assets' ], 20 ); } } /** * Enqueue legacy assets when this block is used as we don't enqueue them for block themes anymore. * * Note: This enqueue logic is intentionally duplicated in ProductImageGallery.php * to keep legacy blocks independent and allow for separate deprecation paths. * * @see https://github.com/woocommerce/woocommerce/pull/60223 */ public function enqueue_legacy_assets() { // Legacy script dependencies for backward compatibility. if ( current_theme_supports( 'wc-product-gallery-zoom' ) ) { wp_enqueue_script( 'wc-zoom' ); } if ( current_theme_supports( 'wc-product-gallery-slider' ) ) { wp_enqueue_script( 'wc-flexslider' ); } if ( current_theme_supports( 'wc-product-gallery-lightbox' ) ) { wp_enqueue_script( 'wc-photoswipe-ui-default' ); wp_enqueue_style( 'photoswipe-default-skin' ); add_action( 'wp_footer', function () { wc_get_template( 'single-product/photoswipe.php' ); } ); } wp_enqueue_script( 'wc-single-product' ); } /** * Render method for the Classic Template block. This method will determine which template to render. * * @param array $attributes Block attributes. * @param string $content Block content. * @param WP_Block $block Block instance. * @return string | void Rendered block type output. */ protected function render( $attributes, $content, $block ) { if ( ! isset( $attributes['template'] ) ) { return; } /** * We need to load the scripts here because when using block templates wp_head() gets run after the block * template. As a result we are trying to enqueue required scripts before we have even registered them. * * @see https://github.com/woocommerce/woocommerce-gutenberg-products-block/issues/5328#issuecomment-989013447 */ if ( class_exists( 'WC_Frontend_Scripts' ) ) { $frontend_scripts = new WC_Frontend_Scripts(); $frontend_scripts::load_scripts(); } if ( OrderConfirmationTemplate::SLUG === $attributes['template'] ) { return $this->render_order_received(); } if ( is_product() ) { add_filter( 'woocommerce_single_product_zoom_enabled', '__return_true' ); add_filter( 'woocommerce_single_product_photoswipe_enabled', '__return_true' ); add_filter( 'woocommerce_single_product_flexslider_enabled', '__return_true' ); return $this->render_single_product(); } $valid = false; $archive_templates = array( ProductCatalogTemplate::SLUG, ProductCategoryTemplate::SLUG, ProductTagTemplate::SLUG, ProductAttributeTemplate::SLUG, ProductSearchResultsTemplate::SLUG, ); // Set selected template when we directly find template base slug. if ( in_array( $attributes['template'], $archive_templates, true ) ) { $valid = true; } // Set selected template when we find template base slug as prefix for a specific term. foreach ( $archive_templates as $template ) { if ( 0 === strpos( $attributes['template'], $template ) ) { $valid = true; } } if ( $valid ) { // Set this so that our product filters can detect if it's a PHP template. $this->asset_data_registry->add( 'isRenderingPhpTemplate', true ); // Set this so filter blocks being used as widgets know when to render. $this->asset_data_registry->add( 'hasFilterableProducts', true ); $this->asset_data_registry->add( 'pageUrl', html_entity_decode( get_pagenum_link() ) ); return $this->render_archive_product(); } ob_start(); echo "You're using the ClassicTemplate block"; wp_reset_postdata(); return ob_get_clean(); } /** * Render method for rendering the order confirmation template. * * @return string Rendered block type output. */ protected function render_order_received() { ob_start(); echo '