block.json
11 months ago
controller.php
1 year ago
index.asset.php
11 months ago
index.js
11 months ago
style-index-rtl.css
1 year ago
style-index.css
1 year ago
view.php
11 months ago
view.php
80 lines
| 1 | <?php |
| 2 | // we have no posts. |
| 3 | |
| 4 | use SureCart\Models\Blocks\ProductPageBlock; |
| 5 | |
| 6 | if ( ! $query->have_posts() ) { |
| 7 | return ''; |
| 8 | } |
| 9 | ?> |
| 10 | |
| 11 | <ul <?php echo wp_kses_data( get_block_wrapper_attributes( $wrapper_attributes ) ); ?>> |
| 12 | <?php |
| 13 | while ( $query->have_posts() ) : |
| 14 | $query->the_post(); |
| 15 | |
| 16 | // Get an instance of the current Post Template block. |
| 17 | $block_instance = $block->parsed_block; |
| 18 | |
| 19 | // Set the block name to one that does not correspond to an existing registered block. |
| 20 | // This ensures that for the inner instances of the Post Template block, we do not render any block supports. |
| 21 | $block_instance['blockName'] = 'core/null'; |
| 22 | |
| 23 | $product_post_id = get_the_ID(); |
| 24 | $product_post_type = get_post_type(); |
| 25 | $filter_block_context = static function ( $context ) use ( $product_post_id, $product_post_type ) { |
| 26 | $context['postType'] = $product_post_type; |
| 27 | $context['postId'] = $product_post_id; |
| 28 | return $context; |
| 29 | }; |
| 30 | |
| 31 | $change_thumbnail_size = static function ( $size, $post_id ) use ( $product_post_id ) { |
| 32 | if ( $post_id === $product_post_id ) { |
| 33 | return apply_filters( 'surecart/product-list/thumbnail-cover-size', 'large', $post_id ); |
| 34 | } |
| 35 | return $size; |
| 36 | }; |
| 37 | |
| 38 | // Use an early priority to so that other 'render_block_context' filters have access to the values. |
| 39 | add_filter( 'render_block_context', $filter_block_context, 1 ); |
| 40 | add_filter( 'post_thumbnail_size', $change_thumbnail_size, 1, 2 ); |
| 41 | |
| 42 | // Render the inner blocks of the Post Template block with `dynamic` set to `false` to prevent calling |
| 43 | // `render_callback` and ensure that no wrapper markup is included. |
| 44 | $block_content = ( new WP_Block( $block_instance ) )->render( array( 'dynamic' => false ) ); |
| 45 | $has_button = ( new \WP_HTML_Tag_Processor( $block_content ?? '' ) )->next_tag( 'button' ); |
| 46 | $has_link = ( new \WP_HTML_Tag_Processor( $block_content ?? '' ) )->next_tag( 'a' ); |
| 47 | $html_tag = $has_link || $has_button ? 'form' : 'a'; |
| 48 | |
| 49 | remove_filter( 'render_block_context', $filter_block_context, 1 ); |
| 50 | remove_filter( 'post_thumbnail_size', $change_thumbnail_size, 1 ); |
| 51 | |
| 52 | // Wrap the render inner blocks in a `li` element with the appropriate post classes. |
| 53 | $post_classes = implode( ' ', get_post_class( 'wp-block-post' ) ); |
| 54 | |
| 55 | $controller = new ProductPageBlock(); |
| 56 | $state = $controller->state(); |
| 57 | $context = $controller->context(); |
| 58 | |
| 59 | wp_interactivity_state( 'surecart/product-page', $state ); |
| 60 | ?> |
| 61 | |
| 62 | <li class="sc-product-item sc-has-animation-fade-up" data-wp-key="post-template-item-<?php echo (int) $product_post_id; ?>"> |
| 63 | <form |
| 64 | data-wp-interactive='{ "namespace": "surecart/product-page" }' |
| 65 | data-wp-on--submit="callbacks.handleSubmit" |
| 66 | data-wp-init="callbacks.init" |
| 67 | <?php |
| 68 | echo wp_kses_data( wp_interactivity_data_wp_context( $context ) ); |
| 69 | ?> |
| 70 | > |
| 71 | <a class="sc-product-item-link" href="<?php echo esc_url( get_the_permalink() ); ?>"> |
| 72 | <?php echo $block_content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 73 | </a> |
| 74 | </form> |
| 75 | </li> |
| 76 | <?php endwhile; ?> |
| 77 | </ul> |
| 78 | |
| 79 | <?php wp_reset_postdata(); ?> |
| 80 |