PluginProbe ʕ •ᴥ•ʔ
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 4.6.2
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v4.6.2
4.6.3 4.6.2 4.6.1 4.6.0 4.5.1 4.5.0 4.4.2 4.4.1 4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.3 4.2.2 4.2.1 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.11.0 1.11.1 1.11.2 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.7.2 1.8.0 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 1.9.0 1.9.1 1.9.2 1.9.3 1.9.4 1.9.5 2.0.0 2.0.1 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.10.0 2.10.1 2.11.0 2.11.1 2.11.2 2.11.3 2.11.4 2.12.0 2.13.0 2.14.0 2.14.1 2.15.0 2.15.1 2.16.0 2.16.1 2.16.2 2.16.3 2.17.0 2.17.1 2.17.2 2.18.0 2.19.0 2.19.2 2.19.3 2.19.4 2.2.0 2.2.1 2.20.0 2.20.1 2.20.2 2.20.3 2.20.4 2.20.5 2.20.6 2.21.0 2.22.0 2.22.1 2.23.0 2.24.0 2.25.0 2.25.1 2.25.2 2.26.0 2.27.0 2.27.1 2.28.0 2.29.0 2.29.1 2.29.2 2.29.3 2.29.4 2.3.0 2.3.1 2.30.0 2.31.0 2.31.1 2.31.2 2.31.3 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.40.0 2.40.1 2.5.0 2.5.1 2.5.2 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.9.0 3.0.0 3.0.0-RC1 3.0.0-RC2 3.0.0-beta1 3.0.0-beta2 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.10.0 3.10.1 3.11.0 3.12.0 3.13.0 3.13.1 3.13.2 3.13.3 3.13.4 3.14.0 3.15.0 3.15.1 3.15.2 3.15.3 3.15.4 3.15.5 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.16.6 3.16.7 3.16.8 3.17.0 3.17.1 3.17.2 3.17.3 3.17.4 3.17.5 3.17.6 3.18.0 3.19.0 3.19.1 3.19.2 3.2.0 3.2.1 3.2.2 3.20.0 3.20.1 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.4.3 3.5.0 3.5.1 3.5.2 3.5.3 3.6.0 3.6.1 3.6.2 3.7.0 3.7.1 3.7.2 3.7.3 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.9.0 4.0.0 4.0.1 4.0.2 4.0.3 trunk 4.1.0 0.2.19.1 4.1.1 1.0.0 4.2.0 1.0.1 1.0.2
surecart / packages / blocks / Blocks / ProductItemList / Block.php
surecart / packages / blocks / Blocks / ProductItemList Last commit date
Block.php 1 year ago block.json 4 weeks ago
Block.php
334 lines
1 <?php
2
3 namespace SureCartBlocks\Blocks\ProductItemList;
4
5 use SureCart\Models\Collection;
6 use SureCart\Models\Product;
7 use SureCartBlocks\Blocks\BaseBlock;
8 /**
9 * ProductItemList block
10 */
11 class Block extends BaseBlock {
12 /**
13 * Keeps track of instances of this block.
14 *
15 * @var integer
16 */
17 public static $instance;
18
19 /**
20 * Default block template.
21 *
22 * @var array
23 */
24 protected $default_block_template = [
25 [
26 'blockName' => 'surecart/product-item-image',
27 'attrs' => [
28 'sizing' => 'cover',
29 'ratio' => '1/1.33',
30 'style' => [
31 'border' => [
32 'radius' => '6px',
33 ],
34 'spacing' => [
35 'margin' => [
36 'bottom' => '16px',
37 ],
38 ],
39 ],
40 ],
41 ],
42 [
43 'blockName' => 'surecart/product-item-title',
44 'attrs' => [
45 'level' => 3,
46 'style' => [
47 'typography' => [
48 'fontSize' => '18px',
49 ],
50 'color' => [
51 'text' => '#000000',
52 ],
53 'spacing' => [
54 'margin' => [
55 'bottom' => '8px',
56 ],
57 ],
58 ],
59 ],
60 ],
61 [
62 'blockName' => 'surecart/product-item-price',
63 'attrs' => [
64 'style' => [
65 'typography' => [
66 'fontSize' => '16px',
67 ],
68 'color' => [
69 'text' => '#000000',
70 ],
71 'spacing' => [
72 'margin' => [
73 'bottom' => '8px',
74 ],
75 ],
76 ],
77 ],
78 ],
79 ];
80
81 /**
82 * Get the style for the block.
83 *
84 * @param array $attr Product List attributes.
85 * @return string
86 */
87 public function getProductListStyle( $attr ) {
88 $style = '--sc-product-item-list-column: ' . ( $attr['columns'] ?? 3 ) . ';';
89 $style .= '--sc-product-item-list-gap: ' . $this->getSpacingPresetCssVar( $attr['style']['spacing']['blockGap'] ?? '40px' ) . ';';
90 $style .= '--sc-pagination-font-size: ' . ( $attr['paginationSize'] ?? '14px' ) . ';';
91 return $style;
92 }
93
94 /**
95 * Get the style for the block
96 *
97 * @param array $attr Style variables.
98 * @param string $prefix Prefix for the css variable.
99 * @return string
100 */
101 public function getVars( $attr, $prefix ) {
102 $style = '';
103 // padding.
104 if ( ! empty( $attr['style']['spacing']['padding'] ) ) {
105 $padding = $attr['style']['spacing']['padding'];
106 $style .= '--sc-product-' . $prefix . '-padding-top: ' . $this->getSpacingPresetCssVar( array_key_exists( 'top', $padding ) ? $padding['top'] : '0' ) . ';';
107 $style .= '--sc-product-' . $prefix . '-padding-bottom: ' . $this->getSpacingPresetCssVar( array_key_exists( 'bottom', $padding ) ? $padding['bottom'] : '0' ) . ';';
108 $style .= '--sc-product-' . $prefix . '-padding-left: ' . $this->getSpacingPresetCssVar( array_key_exists( 'left', $padding ) ? $padding['left'] : '0' ) . ';';
109 $style .= '--sc-product-' . $prefix . '-padding-right: ' . $this->getSpacingPresetCssVar( array_key_exists( 'right', $padding ) ? $padding['right'] : '0' ) . ';';
110 }
111 // margin.
112 if ( ! empty( $attr['style']['spacing']['margin'] ) ) {
113 $margin = $attr['style']['spacing']['margin'];
114 $style .= '--sc-product-' . $prefix . '-margin-top: ' . $this->getSpacingPresetCssVar( array_key_exists( 'top', $margin ) ? $margin['top'] : '0' ) . ';';
115 $style .= '--sc-product-' . $prefix . '-margin-bottom: ' . $this->getSpacingPresetCssVar( array_key_exists( 'bottom', $margin ) ? $margin['bottom'] : '0' ) . ';';
116 $style .= '--sc-product-' . $prefix . '-margin-left: ' . $this->getSpacingPresetCssVar( array_key_exists( 'left', $margin ) ? $margin['left'] : '0' ) . ';';
117 $style .= '--sc-product-' . $prefix . '-margin-right: ' . $this->getSpacingPresetCssVar( array_key_exists( 'right', $margin ) ? $margin['right'] : '0' ) . ';';
118 }
119 // aspect ratio.
120 if ( ! empty( $attr['ratio'] ) ) {
121 $style .= '--sc-product-' . $prefix . '-aspect-ratio: ' . $attr['ratio'] . ';';
122 }
123 // border width.
124 if ( ! empty( $attr['style']['border']['width'] ) ) {
125 $style .= '--sc-product-' . $prefix . '-border-width: ' . $attr['style']['border']['width'] . ';';
126 }
127 // border radius.
128 if ( ! empty( $attr['style']['border']['radius'] ) ) {
129 $style .= '--sc-product-' . $prefix . '-border-radius: ' . $attr['style']['border']['radius'] . ';';
130 }
131 // font weight.
132 if ( ! empty( $attr['style']['typography']['fontWeight'] ) ) {
133 $style .= '--sc-product-' . $prefix . '-font-weight: ' . $attr['style']['typography']['fontWeight'] . ';';
134 }
135 // font size.
136 if ( ! empty( $attr['fontSize'] ) || ! empty( $attr['style']['typography']['fontSize'] ) ) {
137 $font_size = ! empty( $attr['fontSize'] ) ? $this->getFontSizePresetCssVar( $attr['fontSize'] ) : $attr['style']['typography']['fontSize'];
138 $style .= '--sc-product-' . $prefix . '-font-size: ' . $font_size . ';';
139 }
140 // border color.
141 if ( ! empty( $attr['borderColor'] ) || ! empty( $attr['style']['border']['color'] ) ) {
142 $border_color = ! empty( $attr['borderColor'] ) ? $this->getColorPresetCssVar( $attr['borderColor'] ) : $attr['style']['border']['color'];
143 $style .= '--sc-product-' . $prefix . '-border-color: ' . $border_color . ';';
144 }
145 // text color.
146 if ( ! empty( $attr['textColor'] ) || ! empty( $attr['style']['color']['text'] ) ) {
147 $text_color = ! empty( $attr['textColor'] ) ? $this->getColorPresetCssVar( $attr['textColor'] ) : $attr['style']['color']['text'];
148 $style .= '--sc-product-' . $prefix . '-text-color: ' . $text_color . ';';
149 }
150 if ( ! empty( $attr['backgroundColor'] ) || ! empty( $attr['style']['color']['background'] ) ) {
151 $background_color = ! empty( $attr['backgroundColor'] ) ? $this->getColorPresetCssVar( $attr['backgroundColor'] ) : $attr['style']['color']['background'];
152 $style .= '--sc-product-' . $prefix . '-background-color: ' . $background_color . ';';
153 }
154 // text align.
155 if ( ! empty( $attr['align'] ) ) {
156 $style .= '--sc-product-' . $prefix . '-align: ' . $attr['align'] . ';';
157 }
158
159 return $style;
160 }
161
162 /**
163 * Get the style for the block
164 *
165 * @param array $attr Product List attributes.
166 * @param array $item_attributes Product item attributes.
167 * @return string
168 */
169 public function getStyle( $attr, $item_attributes ) {
170 $style = 'border-style: none !important;';
171 $style .= $this->getProductListStyle( $attr );
172 $style .= $this->getVars( $item_attributes, 'item' );
173 return $style;
174 }
175
176 /**
177 * Get the dummy products array.
178 *
179 * @param int $limit Limit per page.
180 * @return array Dummy Products.
181 */
182 public function getDummyProducts( $limit = 15 ) {
183 $dummy_products = array();
184
185 for ( $i = 1; $i <= $limit; $i++ ) {
186 $product = array(
187 'permalink' => '#',
188 'name' => __( 'Example Product Title', 'surecart' ),
189 'created_at' => wp_rand( 1, 40 ),
190 'prices' => array(
191 'data' => array(
192 array(
193 'amount' => 1900,
194 'currency' => 'USD',
195 ),
196 ),
197 ),
198 );
199
200 $dummy_products[] = $product;
201 }
202
203 return $dummy_products;
204 }
205 /**
206 * Render the block
207 *
208 * @param array $attributes Block attributes.
209 * @param string $content Post content.
210 *
211 * @return string
212 */
213 public function render( $attributes, $content ) {
214 if ( isset( $attributes['sort_enabled'] ) ) { // This way we know it's the old block.
215 return \SureCart::block()->productListMigration( $attributes, $this->block )->render();
216 }
217
218 self::$instance = wp_unique_id( 'sc-product-item-list-' );
219
220 // check for inner blocks.
221 $product_inner_blocks = $this->block->parsed_block['innerBlocks'] ?? [];
222
223 $product_item_inner_blocks = $product_inner_blocks[0]['innerBlocks'] ?? $this->default_block_template;
224 $product_item_attributes = $product_inner_blocks[0]['attrs'] ?? $attributes;
225
226 $layout_config = array_map(
227 function ( $inner_block ) {
228 return (object) [
229 'blockName' => $inner_block['blockName'],
230 'attributes' => $inner_block['attrs'],
231 ];
232 },
233 $product_item_inner_blocks
234 );
235
236 $style = '';
237 $style .= $this->getStyle( $attributes, $product_item_attributes );
238
239 foreach ( $product_item_inner_blocks as $inner_blocks ) {
240 switch ( $inner_blocks['blockName'] ) {
241 case 'surecart/product-item-image':
242 $style .= $this->getVars( $inner_blocks['attrs'], 'image' );
243 break;
244 case 'surecart/product-item-title':
245 $style .= $this->getVars( $inner_blocks['attrs'], 'title' );
246 break;
247 case 'surecart/product-item-price':
248 $style .= $this->getVars( $inner_blocks['attrs'], 'price' );
249 break;
250 default:
251 break;
252 }
253 }
254
255 // backwards compat.
256 if ( empty( $attributes['type'] ) && ! empty( $attributes['ids'] ) ) {
257 $attributes['type'] = 'custom';
258 }
259 if ( empty( $attributes['type'] ) ) {
260 $attributes['type'] = '';
261 }
262
263 // query posts.
264 $product_query = new \WP_Query(
265 array(
266 'post_type' => 'sc_product',
267 'posts_per_page' => 10,
268 )
269 );
270
271 // get the product for each post.
272 $products = array_map(
273 function ( $post ) {
274 return sc_get_product( $post );
275 },
276 $product_query->posts ?? []
277 );
278
279 \SureCart::assets()->addComponentData(
280 'sc-product-item-list',
281 '#' . self::$instance,
282 [
283 'layoutConfig' => $layout_config,
284 'paginationAlignment' => $attributes['pagination_alignment'],
285 'limit' => $attributes['limit'],
286 'style' => $style,
287 'pagination' => [
288 'total' => $product_query->found_posts,
289 'total_pages' => $product_query->max_num_pages,
290 ],
291 'page' => (int) ( $_GET['product-page'] ?? 1 ),
292 'ids' => 'custom' === $attributes['type'] ? array_values( array_filter( $attributes['ids'] ) ) : [],
293 'paginationEnabled' => \SureCart::account()->isConnected() ? wp_validate_boolean( $attributes['pagination_enabled'] ) : false,
294 'ajaxPagination' => wp_validate_boolean( $attributes['ajax_pagination'] ),
295 'paginationAutoScroll' => $attributes['pagination_auto_scroll'],
296 'searchEnabled' => \SureCart::account()->isConnected() ? wp_validate_boolean( $attributes['search_enabled'] ) : false,
297 'sortEnabled' => \SureCart::account()->isConnected() ? wp_validate_boolean( $attributes['sort_enabled'] ) : false,
298 'featured' => 'featured' === $attributes['type'],
299 'products' => ! \SureCart::account()->isConnected() ? $this->getDummyProducts( $attributes['limit'] ) : $products,
300 'collectionEnabled' => \SureCart::account()->isConnected() ? wp_validate_boolean( $attributes['collection_enabled'] ) : false,
301 'pageTitle' => get_the_title(),
302 ]
303 );
304
305 return '<sc-product-item-list id="' . esc_attr( self::$instance ) . '"></sc-product-item-list>';
306 }
307
308 /**
309 * Get the query for the products.
310 *
311 * @param array $attributes Block attributes.
312 *
313 * @return array
314 */
315 public function getQuery( $attributes ) {
316 $query = [
317 'expand' => [ 'prices', 'featured_product_media', 'product_medias', 'product_media.media', 'variants' ],
318 'archived' => false,
319 'status' => [ 'published' ],
320 'sort' => 'cataloged_at:desc',
321 ];
322
323 if ( 'featured' === ( $attributes['type'] ?? '' ) ) {
324 $query['featured'] = true;
325 }
326
327 if ( 'custom' === ( $attributes['type'] ?? '' ) ) {
328 $query['ids'] = array_values( array_filter( $attributes['ids'] ?? [] ) );
329 }
330
331 return $query;
332 }
333 }
334