| 1 |
<?php |
| 2 |
use BBlocks\Inc\Blocks\WooAddToCart; |
| 3 |
use BBlocks\Inc\Sanitize; |
| 4 |
|
| 5 |
if ( ! class_exists( WooAddToCart::class ) || ! function_exists( 'wc_get_product' ) ) { |
| 6 |
return; |
| 7 |
} |
| 8 |
|
| 9 |
$id = wp_unique_id( 'bBlocksWooAddToCart-' ); |
| 10 |
$attributes = WooAddToCart::frontendAttributes( $attributes, isset( $block ) ? $block : null ); |
| 11 |
|
| 12 |
if ( ! $attributes ) { |
| 13 |
return; |
| 14 |
} |
| 15 |
|
| 16 |
$attributes = Sanitize::attributes( $attributes ); |
| 17 |
|
| 18 |
$json = wp_json_encode( $attributes, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT ); |
| 19 |
?> |
| 20 |
<div |
| 21 |
<?php // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- get_block_wrapper_attributes() is properly escaped. ?> |
| 22 |
<?php echo get_block_wrapper_attributes(); ?> |
| 23 |
id='<?php echo esc_attr( $id ); ?>' |
| 24 |
data-attributes='<?php echo esc_attr( $json ); ?>' |
| 25 |
> |
| 26 |
<?php |
| 27 |
// Without JavaScript the block cannot render; the product page still sells |
| 28 |
// it. view.js replaces this with the block. |
| 29 |
$productUrl = $attributes['product']['url'] ?? ''; |
| 30 |
if ( $productUrl ) : |
| 31 |
?> |
| 32 |
<noscript><a href='<?php echo esc_url( $productUrl ); ?>'><?php echo esc_html__( 'View product', 'b-blocks' ); ?></a></noscript> |
| 33 |
<?php endif; ?> |
| 34 |
</div> |
| 35 |
|