PluginProbe
Gutenify – Visual Site Builder Blocks & Starter Templates / 1.0.7
Gutenify – Visual Site Builder Blocks & Starter Templates v1.0.7
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-list / index.php

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

182 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Server-side rendering of the `posts` block.
5 *
6 * @package WordPress
7 */
8
9 function gutenify_wc_get_add_to_cart( $product ) {
10 $attributes = array(
11 'aria-label' => $product->add_to_cart_description(),
12 'data-quantity' => '1',
13 'data-product_id' => $product->get_id(),
14 'data-product_sku' => $product->get_sku(),
15 'rel' => 'nofollow',
16 'class' => 'wp-block-button__link add_to_cart_button',
17 );
18
19 if (
20 $product->supports( 'ajax_add_to_cart' ) &&
21 $product->is_purchasable() &&
22 ( $product->is_in_stock() || $product->backorders_allowed() )
23 ) {
24 $attributes['class'] .= ' ajax_add_to_cart';
25 }
26
27 return sprintf(
28 '<a href="%s" %s>%s</a>',
29 esc_url( $product->add_to_cart_url() ),
30 wc_implode_html_attributes( $attributes ),
31 esc_html( $product->add_to_cart_text() )
32 );
33 }
34
35 function gutenify_render_wc_product_list_block( $attributes ) {
36 if ( ! class_exists( 'woocommerce' ) ) {
37 return '';
38 }
39 $query = $attributes['query'];
40 $args = array(
41 'post_status' => 'publish',
42 'post_type' => 'product',
43 );
44
45 if ( ! empty( $query['numberOfItems'] ) ) {
46 $args['posts_per_page'] = $query['numberOfItems'];
47 }
48 if ( ! empty( $query['orderBy'] ) ) {
49 $args['orderby'] = $query['orderBy'];
50 }
51 if ( ! empty( $query['order'] ) ) {
52 $args['order'] = strtoupper( $query['order'] );
53 }
54
55
56 if ( ! empty( $query['tax']['product_cat'] ) ) {
57 $args['tax_query'][] = array(
58 'taxonomy' => 'product_cat',
59 'field' => 'id',
60 'terms' => $query['tax']['product_cat'],
61 );
62 }
63 if ( ! empty( $query['tax']['product_tag'] ) ) {
64 $args['tax_query'][] = array(
65 'taxonomy' => 'product_tag',
66 'field' => 'id',
67 'terms' => $query['tax']['product_tag'],
68 );
69 }
70 $posts = new WP_Query( $args );
71
72 ob_start();
73 if ( $posts->have_posts() ) {
74 $wrapper_class = array(
75 'gutenify-section-' . $attributes['blockClientId'],
76 'wp-block-gutenify-wc-product-list',
77 'gutenify--wc-product-list-' . $attributes['layout'],
78 'gutenify--wc-product-list-col-' . $attributes['columns'],
79 );
80 if ( isset( $attributes['className'] ) ) {
81 array_push( $wrapper_class, $attributes['className'] );
82 }
83 echo sprintf( '<div class="%s">', implode( ' ', $wrapper_class ) );
84 while( $posts->have_posts() ) {
85 $posts->the_post();
86 $product = get_product( get_the_ID() );
87 $permalink = get_the_permalink( $product->get_id() );
88 echo '<div class="gutenify--wc-product--item has-no-hover-shadow-dark">';
89 echo '<div class="gutenify--wc-product--item-wrapper">';
90
91
92 echo '<div class="gutenify--wc-product--thumb">';
93 echo '<a class="image-zoom-hover" href="' . $permalink . '" tabindex="-1">';
94 echo woocommerce_get_product_thumbnail();
95 echo '</a>'; // Product thumb link
96 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>' : '';
97
98 echo '</div>'; // Product thumb
99 echo '<div class="gutenify--wc-product--item-content">';
100
101 echo '<h3 class="gutenify--wc-product--title">';
102 echo '<a rel="bookmark" href="' . $permalink . '" tabindex="-1">';
103 echo $product->get_name();
104 echo '</a>'; // Product title link
105 echo '</h3>'; // Product title
106
107 echo '<div class="gutenify--wc-product--price">';
108 echo $product->get_price_html();
109 echo '</div>'; // Product price
110
111 echo '<div class="wp-block-button wc-block-grid__product-add-to-cart">';
112 echo gutenify_wc_get_add_to_cart( $product );
113 // var_dump( $product );
114 echo '</div>'; // Product add to cart wrapper
115 echo '</div>'; // Product individual wrapper
116 echo '</div>'; // Product individual wrapper
117 echo '</div>'; // Product individual wrapper
118 }
119 echo '</div>'; // Product lists wrapper
120 wp_reset_postdata();
121 }
122 // $products = wc_get_products( $args );
123 // var_dump( $query );
124 // var_dump( $args );
125 // var_dump( $products );
126
127
128
129 $result = ob_get_clean();
130 return $result;
131 }
132
133 /**
134 * Registers the `posts` block on server.
135 */
136 function gutenify_register_wc_product_list_block() {
137 // Return early if this function does not exist.
138 if ( ! function_exists( 'register_block_type' ) ) {
139 return;
140 }
141
142 // Load attributes from block.json.
143 ob_start();
144 include GUTENIFY_PLUGIN_DIR . 'src/blocks/wc-product-list/block.json';
145 $metadata = json_decode( ob_get_clean(), true );
146
147 register_block_type(
148 'gutenify/wc-product-list',
149 array(
150 'attributes' => $metadata['attributes'],
151 'render_callback' => 'gutenify_render_wc_product_list_block',
152 )
153 );
154 }
155 add_action( 'init', 'gutenify_register_wc_product_list_block' );
156
157
158 // add_filter( 'render_block', 'gutenify_add_data_attributes', 10, 2 );
159
160 function gutenify_add_data_attributes( $content, $block ) {
161 $block_name = $block['blockName'];
162 $block_namespace = strtok( $block_name ?? '', '/' );
163
164 /**
165 * Filters the list of allowed block namespaces.
166 *
167 * This hook defines which block namespaces should have block name and attribute `data-` attributes appended on render.
168 *
169 * @param array $allowed_namespaces List of namespaces.
170 */
171 $allowed_namespaces = array_merge( array( 'gutenify' ) );
172 $allowed_blocks = array_merge( array( 'gutenify/wc-product-list' ) );
173 if ( ! in_array( $block_namespace, $allowed_namespaces, true ) && ! in_array( $block_name, $allowed_blocks, true ) ) {
174 return $content;
175 }
176
177 if ( ! class_exists( 'ProductQuery' ) ) {
178 return __return_false();
179 }
180 return 'test';
181 }
182