PluginProbe
Gutenify – Visual Site Builder Blocks & Starter Templates / 1.0.5
Gutenify – Visual Site Builder Blocks & Starter Templates v1.0.5
1.9.0 1.8.0 1.7.0 1.6.6 1.6.5 1.6.4 trunk 0.0.1 0.0.2 0.0.3 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 All 74 releases
gutenify / src / blocks / wc-product-carousel / index.php

index.php in Gutenify – Visual Site Builder Blocks & Starter Templates 1.0.5, at src/blocks/wc-product-carousel/index.php

199 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Server-side rendering of the `posts` block.
4 *
5 * @package WordPress
6 */
7
8 function gutenify_render_wc_product_carousel_block( $attributes ) {
9 if ( ! class_exists( 'woocommerce' ) ) {
10 return '';
11 }
12 $query = $attributes['query'];
13 $args = array(
14 'post_status' => 'publish',
15 'post_type' => 'product',
16 );
17
18 if ( ! empty( $query['numberOfItems'] ) ) {
19 $args['posts_per_page'] = $query['numberOfItems'];
20 }
21 if ( ! empty( $query['orderBy'] ) ) {
22 $args['orderby'] = $query['orderBy'];
23 }
24 if ( ! empty( $query['order'] ) ) {
25 $args['order'] = strtoupper( $query['order'] );
26 }
27
28 if ( ! empty( $query['tax']['product_cat'] ) ) {
29 $args['tax_query'][] = array(
30 'taxonomy' => 'product_cat',
31 'field' => 'id',
32 'terms' => $query['tax']['product_cat'],
33 );
34 }
35 if ( ! empty( $query['tax']['product_tag'] ) ) {
36 $args['tax_query'][] = array(
37 'taxonomy' => 'product_tag',
38 'field' => 'id',
39 'terms' => $query['tax']['product_tag'],
40 );
41 }
42 $posts = new WP_Query( $args );
43
44 ob_start();
45 if ( $posts->have_posts() ) {
46 $wrapper_class = array(
47 'gutenify-section-' . $attributes['blockClientId'],
48 'wp-block-gutenify-wc-product-carousel',
49 'gutenify--wc-product-carousel-' . $attributes['layout'],
50 'gutenify--wc-product-carousel-col-' . $attributes['columns'],
51 );
52 echo sprintf( '<div class="%s">', implode( ' ', $wrapper_class ) );
53 echo '<div class="swiper-container">';
54 echo '<div class="swiper-wrapper">';
55 while ( $posts->have_posts() ) {
56 $posts->the_post();
57 $product = get_product( get_the_ID() );
58 $permalink = get_the_permalink( $product->get_id() );
59 echo '<div class="gutenify--wc-product--item has-no-hover-shadow-dark swiper-slide">';
60 echo '<div class="gutenify--wc-product--item-wrapper">';
61
62 echo '<div class="gutenify--wc-product--thumb">';
63 echo '<a class="image-hover-zoom" href="' . $permalink . '" tabindex="-1">';
64 echo woocommerce_get_product_thumbnail();
65 echo '</a>'; // Product thumb link
66 echo $product->is_on_sale() ? '<div class="gutenify--wc-product--onsale"><span aria-hidden="true">Sale</span><span class="screen-reader-text">Product on sale</span></div>' : '';
67 echo '</div>'; // Product thumb
68
69
70 echo '<div class="gutenify--wc-product--item-content">';
71 echo '<h3 class="gutenify--wc-product--title">';
72 echo '<a rel="bookmark" href="' . $permalink . '" tabindex="-1">';
73 echo $product->get_name();
74 echo '</a>'; // Product title link
75 echo '</h3>'; // Product title
76
77 echo '<div class="gutenify--wc-product--price">';
78 echo $product->get_price_html();
79 echo '</div>'; // Product price
80
81 echo '<div class="wp-block-button wc-block-grid__product-add-to-cart">';
82 echo gutenify_wc_get_add_to_cart( $product );
83 // var_dump( $product );
84 echo '</div>'; // Product add to cart wrapper
85 echo '</div>'; // .gutenify--wc-product--item-content
86
87 echo '</div>'; // Product individual wrapper
88 echo '</div>'; // Product individual wrapper
89 }
90
91 echo '</div>'; // .swiper-wrapper
92 $has_navigation = ! empty( $attributes['hasNavigation'] ) && true === $attributes['hasNavigation'];
93 $has_pagination = ! empty( $attributes['hasPagination'] ) && true === $attributes['hasPagination'];
94 if ( $has_navigation ) {
95 echo '<div class="swiper-button-next"></div>';
96 echo '<div class="swiper-button-prev"></div>';
97 }
98 if ( $has_pagination ) {
99 echo '<div class="swiper-pagination"></div>';
100 }
101 echo '</div>'; // .swiper-container
102 echo '</div>'; // Product carousels wrapper
103 wp_reset_postdata();
104 }
105 wp_enqueue_script( 'gutenify-swiper' );
106
107 $columns = ! empty( $attributes['columns'] ) ? $attributes['columns'] : 1;
108 $space_between = ! empty( $attributes['spaceBetween'] ) ? $attributes['spaceBetween'] : 0;
109 $swiper_config = array(
110 'loop' => 'true',
111 'slidesPerView' => $columns,
112 'breakpoints' => array(
113 '320' => array(
114 'slidesPerView' => 1,
115 'spaceBetween' => 20,
116 ),
117 // when window width is >= 640px
118 '640' => array(
119 'slidesPerView' => 2,
120 'spaceBetween' => 15,
121 ),
122 '1024' => array(
123 'slidesPerView' => $columns,
124 'spaceBetween' => $space_between,
125 ),
126 ),
127 'spaceBetween' => $space_between,
128 'scrollbar' => array( 'draggable' => true ),
129 );
130
131 if ( $has_navigation ) {
132 $swiper_config['navigation'] = array(
133 'nextEl' => '.swiper-button-next',
134 'prevEl' => '.swiper-button-prev',
135 );
136 }
137
138 if ( $has_pagination ) {
139 $swiper_config['pagination'] = array(
140 'el' => '.swiper-pagination',
141 'clickable' => true,
142 );
143 }
144
145 $block_client_id = ! empty( $attributes['blockClientId'] ) ? $attributes['blockClientId'] : 1;
146 $slider_var = 'slider_' . str_replace( '-', '_', $block_client_id );
147 $slider_function_name = 'reinitSlider_' . $slider_var;
148 ?>
149 <script>
150 jQuery(function($){
151 var sliderConfig = <?php echo json_encode( $swiper_config ); ?>;
152 console.log( sliderConfig );
153 function <?php echo $slider_function_name; ?>() {
154 var swiperWrapper = $(".gutenify-section-<?php echo $block_client_id; ?> .swiper-wrapper");
155 var newSlides = swiperWrapper.children(".swiper-slide").clone(true);
156 ' . $slider_var . '.destroy();
157 swiperWrapper.empty().append(newSlides);
158 swiperWrapper.attr("style", "");
159 mySwiper = new Swiper(".gutenify-section-<?php echo $block_client_id; ?> .swiper-container", sliderConfig);
160 }
161
162 var <?php echo $slider_var; ?> = new Swiper(".gutenify-section-<?php echo $block_client_id; ?> .swiper-container", sliderConfig);
163 $(window).on("resize", function(){
164 ' . $slider_function_name . '();
165 });
166 $(document).on("containerStretched", function(){
167 ' . $slider_function_name . '();
168 });
169 });
170 </script>
171 <?php
172 $result = ob_get_clean();
173 return $result;
174 }
175
176 /**
177 * Registers the `posts` block on server.
178 */
179 function gutenify_register_wc_product_carousel_block() {
180 // Return early if this function does not exist.
181 if ( ! function_exists( 'register_block_type' ) ) {
182 return;
183 }
184
185 // Load attributes from block.json.
186 ob_start();
187 include GUTENIFY_PLUGIN_DIR . 'src/blocks/wc-product-carousel/block.json';
188 $metadata = json_decode( ob_get_clean(), true );
189
190 register_block_type(
191 'gutenify/wc-product-carousel',
192 array(
193 'attributes' => $metadata['attributes'],
194 'render_callback' => 'gutenify_render_wc_product_carousel_block',
195 )
196 );
197 }
198 add_action( 'init', 'gutenify_register_wc_product_carousel_block' );
199