PluginProbe ʕ •ᴥ•ʔ
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 3.0.0-beta1
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v3.0.0-beta1
4.7.0 4.6.6 4.6.5 4.6.4 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 / app / src / BlockLibrary / ProductListMigrationService.php
surecart / app / src / BlockLibrary Last commit date
BlockAnchorSupportService.php 2 years ago BlockPatternsService.php 2 years ago BlockService.php 2 years ago BlockServiceProvider.php 2 years ago BlockStylesService.php 2 years ago BlockValidationService.php 2 years ago CartMenuIconMigrationService.php 2 years ago CartMigrationService.php 2 years ago FormModeSwitcherService.php 2 years ago ProductCollectionBadgesMigrationService.php 2 years ago ProductListMigrationService.php 2 years ago ProductListService.php 2 years ago ProductPriceChoicesMigrationService.php 2 years ago ProductSelectedPriceMigrationService.php 2 years ago ProductVariantsMigrationService.php 2 years ago URLParamService.php 2 years ago
ProductListMigrationService.php
296 lines
1 <?php
2
3 namespace SureCart\BlockLibrary;
4
5 /**
6 * Provide product list migration functionality.
7 */
8 class ProductListMigrationService {
9 /**
10 * Attributes.
11 *
12 * @var array
13 */
14 public array $attributes = array();
15
16 /**
17 * Block.
18 *
19 * @var \WP_Block_Type;
20 */
21 public ?object $block;
22
23 /**
24 * Block HTML.
25 *
26 * @var string
27 */
28 public string $block_html = '';
29
30 /**
31 * Inner Blocks.
32 *
33 * @var array
34 */
35 public array $inner_blocks = array();
36
37 /**
38 * Set the initial variables.
39 *
40 * @param array $attributes Attributes.
41 * @param object $block Block.
42 */
43 public function __construct( $attributes = array(), $block = null ) {
44 $this->attributes = $attributes;
45 $this->block = $block;
46 $default_inner_blocks = array(
47 array(
48 'blockName' => 'surecart/product-item',
49 'attrs' => array(),
50 'innerBlocks' => array(
51 array(
52 'blockName' => 'surecart/product-item-image',
53 'attrs' => array(),
54 ),
55 array(
56 'blockName' => 'surecart/product-item-price',
57 'attrs' => array(),
58 ),
59 array(
60 'blockName' => 'surecart/product-item-title',
61 'attrs' => array(),
62 ),
63 ),
64 ),
65 ); // For Shortcodes.
66 $this->inner_blocks = $block->parsed_block['innerBlocks'] ?? $default_inner_blocks;
67 }
68
69 /**
70 * Get the Child Blocks Attributes.
71 *
72 * @param string $block_name Block Name.
73 * @return array Child Blocks Attributes.
74 */
75 public function getChildBlocksAttributes( $block_name ) {
76 if ( empty( $this->inner_blocks ) || empty( $this->inner_blocks[0]['innerBlocks'] ) ) {
77 return array();
78 }
79
80 foreach ( $this->inner_blocks[0]['innerBlocks'] as $block ) {
81 if ( $block['blockName'] === $block_name ) {
82 if ( 'surecart/product-item-title' === $block_name ) {
83 $block['attrs']['level'] = 0;
84 }
85 return $block['attrs'];
86 }
87 }
88 return array();
89 }
90
91 /**
92 * Render the product list.
93 *
94 * @return void
95 */
96 public function renderProductList(): void {
97 $this->block_html .= '<!-- wp:surecart/product-list' . ( ! empty( $this->attributes ) ? ' ' . wp_json_encode( $this->attributes ) : '' ) . ' -->';
98
99 $this->renderSortFilterAndSearch();
100
101 $this->renderFilterTags();
102
103 $this->renderProductTemplate();
104
105 $this->renderPagination();
106
107 $this->block_html .= '<!-- /wp:surecart/product-list -->';
108 }
109
110 /**
111 * Render the sort, filter and search.
112 *
113 * @return void
114 */
115 public function renderSortFilterAndSearch(): void {
116 $this->block_html .= '<!-- wp:group {"style":{"spacing":{"margin":{"bottom":"10px"}}},"layout":{"type":"flex","justifyContent":"space-between"}} -->';
117 $this->block_html .= '<div class="wp-block-group" style="margin-bottom:10px">';
118
119 $this->renderSortAndFilter();
120
121 $this->renderSearch();
122
123 $this->block_html .= '</div><!-- /wp:group -->';
124 }
125
126 /**
127 * Render the sort and filter.
128 *
129 * @return void
130 */
131 public function renderSortAndFilter(): void {
132 $sort_enabled = wp_validate_boolean( $this->attributes['sort_enabled'] ?? true );
133 $collection_enabled = wp_validate_boolean( $this->attributes['collection_enabled'] ?? true );
134
135 if ( ! $sort_enabled && ! $collection_enabled ) {
136 return;
137 }
138
139 $this->block_html .= '<!-- wp:group {"layout":{"type":"flex","flexWrap":"nowrap"}} -->';
140 $this->block_html .= '<div class="wp-block-group">';
141
142 if ( $sort_enabled ) {
143 $this->block_html .= '<!-- wp:surecart/product-list-sort /-->';
144 }
145
146 if ( $collection_enabled ) {
147 $this->block_html .= '<!-- wp:surecart/product-list-filter /-->';
148 }
149
150 $this->block_html .= '</div><!-- /wp:group -->';
151 }
152
153 /**
154 * Render the search.
155 *
156 * @return void
157 */
158 public function renderSearch(): void {
159 $search_enabled = wp_validate_boolean( $this->attributes['search_enabled'] ?? true );
160
161 if ( ! $search_enabled ) {
162 return;
163 }
164
165 $this->block_html .= '<!-- wp:surecart/product-list-search {"style":{"layout":{"selfStretch":"fixed","flexSize":"250px"}}} /-->';
166 }
167
168 /**
169 * Render the filter tags.
170 *
171 * @return void
172 */
173 public function renderFilterTags(): void {
174 $collection_enabled = wp_validate_boolean( $this->attributes['collection_enabled'] ?? true );
175
176 if ( ! $collection_enabled ) {
177 return;
178 }
179
180 $this->block_html .= '<!-- wp:group {"style":{"spacing":{"margin":{"bottom":"10px"}}},"layout":{"type":"flex","flexWrap":"nowrap"}} -->';
181 $this->block_html .= '<div class="wp-block-group" style="margin-bottom:10px">';
182 $this->block_html .= '<!-- wp:surecart/product-list-filter-tags -->';
183 $this->block_html .= '<!-- wp:surecart/product-list-filter-tag /-->';
184 $this->block_html .= '</div><!-- /wp:group -->';
185 $this->block_html .= '</div><!-- /wp:group -->';
186 }
187
188 /**
189 * Render the product title.
190 *
191 * @return void
192 */
193 public function renderTitle(): void {
194 $product_title_attrs = wp_json_encode( $this->getChildBlocksAttributes( 'surecart/product-item-title' ), JSON_FORCE_OBJECT );
195 $this->block_html .= '<!-- wp:surecart/product-title ' . $product_title_attrs . ' /-->';
196 }
197
198 /**
199 * Render the product image.
200 *
201 * @return void
202 */
203 public function renderImage(): void {
204 $product_image_attrs = wp_json_encode( $this->getChildBlocksAttributes( 'surecart/product-item-image' ), JSON_FORCE_OBJECT );
205 $this->block_html .= '<!-- wp:surecart/product-image ' . $product_image_attrs . ' /-->';
206 }
207
208 /**
209 * Render the product price.
210 *
211 * @return void
212 */
213 public function renderPrice(): void {
214 $product_price_attrs = wp_json_encode( $this->getChildBlocksAttributes( 'surecart/product-item-price' ), JSON_FORCE_OBJECT );
215 $this->block_html .= '<!-- wp:group {"style":{"spacing":{"blockGap":"0.5em","margin":{"top":"0px","bottom":"0px"}},"margin":{"top":"0px","bottom":"0px"}},"layout":{"type":"flex","flexWrap":"nowrap"}} -->';
216 $this->block_html .= '<div class="wp-block-group">';
217 $this->block_html .= '<!-- wp:surecart/product-list-price ' . $product_price_attrs . ' /-->';
218 $this->block_html .= '<!-- wp:surecart/product-list-price-sale ' . $product_price_attrs . ' /-->';
219 $this->block_html .= '</div>';
220 $this->block_html .= '<!-- /wp:group -->';
221 }
222
223 /**
224 * Render the product template.
225 *
226 * @return void
227 */
228 public function renderProductTemplate(): void {
229 $columns = $this->attributes['columns'] ?? 3;
230 $this->block_html .= '<!-- wp:surecart/product-template {"style":{"spacing":{"blockGap":"30px"}},"layout":{"type":"grid","columnCount":' . $columns . '}} -->';
231 $group_attrs = ! empty( $this->inner_blocks[0]['attrs'] ) ? wp_json_encode( $this->inner_blocks[0]['attrs'] ) : '{}';
232 $group_block = parse_blocks( '<!-- wp:group ' . $group_attrs . ' -->' )[0];
233 $group_styles = sc_get_block_styles( true, $group_block );
234 $group_classnames = ! empty( $group_styles['classnames'] ) ? $group_styles['classnames'] : '';
235 $group_css = ! empty( $group_styles['css'] ) ? $group_styles['css'] : '';
236 $this->block_html .= '<!-- wp:group {"style":{"spacing":{"blockGap":"5px"}},"layout":{"type":"flex","orientation":"vertical"}} -->';
237 $this->block_html .= '<div class="wp-block-group ' . $group_classnames . '" style="' . $group_css . '">';
238 // Render according to the inner blocks order in old block.
239 if ( ! empty( $this->inner_blocks[0]['innerBlocks'] ) ) {
240 foreach ( $this->inner_blocks[0]['innerBlocks'] as $inner_block ) {
241 switch ( $inner_block['blockName'] ) {
242 case 'surecart/product-item-image':
243 $this->renderImage();
244 break;
245 case 'surecart/product-item-title':
246 $this->renderTitle();
247 break;
248 case 'surecart/product-item-price':
249 $this->renderPrice();
250 break;
251 }
252 }
253 }
254 $this->block_html .= '</div><!-- /wp:group -->';
255 $this->block_html .= '<!-- /wp:surecart/product-template -->';
256 }
257
258 /**
259 * Render the pagination.
260 *
261 * @return void
262 */
263 public function renderPagination(): void {
264 $pagination_enabled = wp_validate_boolean( $this->attributes['pagination_enabled'] ?? true );
265
266 if ( ! $pagination_enabled ) {
267 return;
268 }
269
270 $this->block_html .= '<!-- wp:surecart/product-pagination -->';
271 $this->block_html .= '<!-- wp:surecart/product-pagination-previous /-->';
272 $this->block_html .= '<!-- wp:surecart/product-pagination-numbers /-->';
273 $this->block_html .= '<!-- wp:surecart/product-pagination-next /-->';
274 $this->block_html .= '<!-- /wp:surecart/product-pagination -->';
275 }
276
277 /**
278 * Render the blocks.
279 *
280 * @return string
281 */
282 public function doBlocks(): string {
283 return do_blocks( $this->block_html );
284 }
285
286 /**
287 * Render the new product list.
288 *
289 * @return string
290 */
291 public function render(): string {
292 $this->renderProductList();
293 return $this->doBlocks();
294 }
295 }
296