BlockAnchorSupportService.php
1 year ago
BlockPatternsService.php
1 year ago
BlockService.php
1 year ago
BlockServiceProvider.php
1 year ago
BlockStylesService.php
1 year ago
BlockValidationService.php
2 years ago
CartMenuIconMigrationService.php
1 year ago
CartMigrationService.php
1 year ago
FormModeSwitcherService.php
1 year ago
ProductCollectionBadgesMigrationService.php
1 year ago
ProductListMigrationService.php
1 year ago
ProductListService.php
1 year ago
ProductPageBlocksMigrationService.php
1 year ago
ProductPriceChoicesMigrationService.php
1 year ago
ProductSelectedPriceMigrationService.php
1 year ago
ProductVariantsMigrationService.php
1 year ago
URLParamService.php
1 year ago
ProductListMigrationService.php
387 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 | if ( 'surecart/product-item-price' === $block_name && isset( $block['attrs']['range'] ) ) { |
| 86 | $block['attrs']['show_range'] = $block['attrs']['range']; |
| 87 | } |
| 88 | return $block['attrs']; |
| 89 | } |
| 90 | } |
| 91 | return array(); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Render the product list. |
| 96 | * |
| 97 | * @return void |
| 98 | */ |
| 99 | public function renderProductList(): void { |
| 100 | if ( ! isset( $this->attributes['query'] ) ) { |
| 101 | $this->attributes['query'] = []; |
| 102 | } |
| 103 | $this->attributes['query']['perPage'] = $this->attributes['limit'] ?? 8; |
| 104 | |
| 105 | $this->block_html .= '<!-- wp:surecart/product-list' . ( ! empty( $this->attributes ) ? ' ' . wp_json_encode( $this->attributes ) : '' ) . ' -->'; |
| 106 | |
| 107 | $this->renderSortFilterAndSearch(); |
| 108 | |
| 109 | $this->renderFilterTags(); |
| 110 | |
| 111 | $this->renderProductTemplate(); |
| 112 | |
| 113 | $this->renderPagination(); |
| 114 | |
| 115 | $this->block_html .= '<!-- /wp:surecart/product-list -->'; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Render the sort, filter and search. |
| 120 | * |
| 121 | * @return void |
| 122 | */ |
| 123 | public function renderSortFilterAndSearch(): void { |
| 124 | // we don't have any of these items enabled, so we don't need to render anything. |
| 125 | $sort_enabled = wp_validate_boolean( $this->attributes['sort_enabled'] ?? true ); |
| 126 | $collection_enabled = wp_validate_boolean( $this->attributes['collection_enabled'] ?? true ); |
| 127 | $search_enabled = wp_validate_boolean( $this->attributes['search_enabled'] ?? true ); |
| 128 | if ( ! $sort_enabled && ! $collection_enabled && ! $search_enabled ) { |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | $this->block_html .= '<!-- wp:group {"style":{"spacing":{"margin":{"bottom":"10px"}}},"layout":{"type":"flex","justifyContent":"space-between"}} -->'; |
| 133 | $this->block_html .= '<div class="wp-block-group" style="margin-bottom:10px">'; |
| 134 | |
| 135 | $this->renderSortAndFilter(); |
| 136 | |
| 137 | $this->renderSearch(); |
| 138 | |
| 139 | $this->block_html .= '</div><!-- /wp:group -->'; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Render the sort and filter. |
| 144 | * |
| 145 | * @return void |
| 146 | */ |
| 147 | public function renderSortAndFilter(): void { |
| 148 | $sort_enabled = wp_validate_boolean( $this->attributes['sort_enabled'] ?? true ); |
| 149 | $collection_enabled = wp_validate_boolean( $this->attributes['collection_enabled'] ?? true ); |
| 150 | |
| 151 | if ( ! $sort_enabled && ! $collection_enabled ) { |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | $this->block_html .= '<!-- wp:group {"layout":{"type":"flex","flexWrap":"nowrap"}} -->'; |
| 156 | $this->block_html .= '<div class="wp-block-group">'; |
| 157 | |
| 158 | if ( $sort_enabled ) { |
| 159 | $this->block_html .= '<!-- wp:surecart/product-list-sort /-->'; |
| 160 | } |
| 161 | |
| 162 | if ( $collection_enabled ) { |
| 163 | $this->block_html .= '<!-- wp:surecart/product-list-filter /-->'; |
| 164 | } |
| 165 | |
| 166 | $this->block_html .= '</div><!-- /wp:group -->'; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Render the search. |
| 171 | * |
| 172 | * @return void |
| 173 | */ |
| 174 | public function renderSearch(): void { |
| 175 | $search_enabled = wp_validate_boolean( $this->attributes['search_enabled'] ?? true ); |
| 176 | |
| 177 | if ( ! $search_enabled ) { |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | $this->block_html .= '<!-- wp:surecart/product-list-search {"style":{"layout":{"selfStretch":"fixed","flexSize":"250px"}}} /-->'; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Render the filter tags. |
| 186 | * |
| 187 | * @return void |
| 188 | */ |
| 189 | public function renderFilterTags(): void { |
| 190 | $collection_enabled = wp_validate_boolean( $this->attributes['collection_enabled'] ?? true ); |
| 191 | |
| 192 | if ( ! $collection_enabled ) { |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | $this->block_html .= '<!-- wp:group {"style":{"spacing":{"margin":{"bottom":"10px"}}},"layout":{"type":"flex","flexWrap":"nowrap"}} -->'; |
| 197 | $this->block_html .= '<div class="wp-block-group" style="margin-bottom:10px">'; |
| 198 | $this->block_html .= '<!-- wp:surecart/product-list-filter-tags -->'; |
| 199 | $this->block_html .= '<!-- wp:surecart/product-list-filter-tag /-->'; |
| 200 | $this->block_html .= '</div><!-- /wp:group -->'; |
| 201 | $this->block_html .= '</div><!-- /wp:group -->'; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Render the product title. |
| 206 | * |
| 207 | * @return void |
| 208 | */ |
| 209 | public function renderTitle(): void { |
| 210 | $product_title_attrs = $this->getChildBlocksAttributes( 'surecart/product-item-title' ); |
| 211 | if ( ! isset( $product_title_attrs['style'] ) ) { |
| 212 | $product_title_attrs['style'] = array(); |
| 213 | } |
| 214 | if ( ! isset( $product_title_attrs['style']['spacing'] ) ) { |
| 215 | $product_title_attrs['style']['spacing'] = array(); |
| 216 | } |
| 217 | |
| 218 | // this is the default. |
| 219 | if ( isset( $product_title_attrs['style']['spacing']['padding']['top'] ) && $product_title_attrs['style']['spacing']['padding']['top'] === '10px' ) { |
| 220 | $product_title_attrs['style']['spacing']['margin'] = array_merge( |
| 221 | $product_title_attrs['style']['spacing']['margin'] ?? array(), |
| 222 | array( |
| 223 | 'bottom' => $product_title_attrs['style']['spacing']['margin']['bottom'] ?? '0px', |
| 224 | 'top' => '0px', |
| 225 | ), |
| 226 | ); |
| 227 | } |
| 228 | |
| 229 | $this->block_html .= '<!-- wp:surecart/product-title ' . wp_json_encode( $product_title_attrs, JSON_FORCE_OBJECT ) . ' /-->'; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Render the product image. |
| 234 | * |
| 235 | * @return void |
| 236 | */ |
| 237 | public function renderImage(): void { |
| 238 | $attributes = $this->getChildBlocksAttributes( 'surecart/product-item-image' ); |
| 239 | $product_image_attrs = array( |
| 240 | 'useFeaturedImage' => true, |
| 241 | 'minHeight' => 0, |
| 242 | 'dimRatio' => 0, |
| 243 | 'isUserOverlayColor' => true, |
| 244 | 'focalPoint' => array( |
| 245 | 'x' => 0.5, |
| 246 | 'y' => 0.5, |
| 247 | ), |
| 248 | 'contentPosition' => 'top right', |
| 249 | 'isDark' => false, |
| 250 | 'style' => array( |
| 251 | 'dimensions' => array( 'aspectRatio' => ! empty( $attributes['ratio'] ) ? $attributes['ratio'] : '3/4' ), |
| 252 | 'layout' => array( |
| 253 | 'selfStretch' => 'fit', |
| 254 | 'flexSize' => null, |
| 255 | ), |
| 256 | 'spacing' => array( 'margin' => array( 'bottom' => '15px' ) ), |
| 257 | 'border' => array( 'radius' => '10px' ), |
| 258 | ), |
| 259 | ); |
| 260 | |
| 261 | $image = '<!-- wp:group {"style":{"color":{"background":"#0000000d"},"border":{"radius":"10px"},"spacing":{"padding":{"top":"0px","bottom":"0px","left":"0px","right":"0px"},"margin":{"top":"0px","bottom":"0px"}}},"layout":{"type":"constrained"}} -->'; |
| 262 | $image .= '<div class="wp-block-group has-background" style="border-radius:10px;background-color:#0000000d;margin-top:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px">'; |
| 263 | $image .= '<!-- wp:cover ' . wp_json_encode( array_replace_recursive( $product_image_attrs, [] ) ) . ' -->'; |
| 264 | $image .= '<div class="wp-block-cover is-light has-custom-content-position is-position-top-right" style="border-radius:10px;">'; |
| 265 | $image .= '<span aria-hidden="true" class="wp-block-cover__background has-background-dim-0 has-background-dim"></span>'; |
| 266 | $image .= '<div class="wp-block-cover__inner-container">'; |
| 267 | $image .= '<!-- wp:surecart/product-sale-badge {"style":{"typography":{"fontSize":"12px"},"border":{"radius":"100px"}}} /-->'; |
| 268 | $image .= '</div>'; |
| 269 | $image .= '</div>'; |
| 270 | $image .= '<!-- /wp:cover -->'; |
| 271 | $image .= '</div>'; |
| 272 | $image .= '<!-- /wp:group -->'; |
| 273 | |
| 274 | $this->block_html .= $image; |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Render the product price. |
| 279 | * |
| 280 | * @return void |
| 281 | */ |
| 282 | public function renderPrice(): void { |
| 283 | $product_price_attrs = wp_json_encode( $this->getChildBlocksAttributes( 'surecart/product-item-price' ), JSON_FORCE_OBJECT ); |
| 284 | $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"}} -->'; |
| 285 | $this->block_html .= '<div class="wp-block-group" style="margin-top:0;">'; |
| 286 | $this->block_html .= '<!-- wp:surecart/product-list-price ' . $product_price_attrs . ' /-->'; |
| 287 | $this->block_html .= '<!-- wp:surecart/product-scratch-price ' . $product_price_attrs . ' /-->'; |
| 288 | $this->block_html .= '</div>'; |
| 289 | $this->block_html .= '<!-- /wp:group -->'; |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Render the product template. |
| 294 | * |
| 295 | * @return void |
| 296 | */ |
| 297 | public function renderProductTemplate(): void { |
| 298 | $product_template_attrs = array_merge( |
| 299 | array( |
| 300 | 'style' => array( |
| 301 | 'spacing' => array( |
| 302 | 'blockGap' => '30px', |
| 303 | ), |
| 304 | ), |
| 305 | 'layout' => array( |
| 306 | 'type' => 'grid', |
| 307 | 'columnCount' => $this->attributes['columns'] ?? 3, |
| 308 | ), |
| 309 | ), |
| 310 | $this->attributes ?? array(), |
| 311 | ); |
| 312 | |
| 313 | $this->block_html .= '<!-- wp:surecart/product-template ' . wp_json_encode( $product_template_attrs ) . ' -->'; |
| 314 | $group_attrs = ! empty( $this->inner_blocks[0]['attrs'] ) ? wp_json_encode( $this->inner_blocks[0]['attrs'] ) : '{}'; |
| 315 | $group_block = parse_blocks( '<!-- wp:group ' . $group_attrs . ' -->' )[0]; |
| 316 | $group_styles = sc_get_block_styles( true, $group_block ); |
| 317 | $group_classnames = ! empty( $group_styles['classnames'] ) ? $group_styles['classnames'] : ''; |
| 318 | $group_css = ! empty( $group_styles['css'] ) ? $group_styles['css'] : ''; |
| 319 | $this->block_html .= '<!-- wp:group {"style":{"spacing":{"blockGap":"5px"}} -->'; |
| 320 | $this->block_html .= '<div class="wp-block-group ' . $group_classnames . '" style="' . $group_css . '">'; |
| 321 | // Render according to the inner blocks order in old block. |
| 322 | if ( ! empty( $this->inner_blocks[0]['innerBlocks'] ) ) { |
| 323 | foreach ( $this->inner_blocks[0]['innerBlocks'] as $inner_block ) { |
| 324 | switch ( $inner_block['blockName'] ) { |
| 325 | case 'surecart/product-item-image': |
| 326 | $this->renderImage(); |
| 327 | break; |
| 328 | case 'surecart/product-item-title': |
| 329 | $this->renderTitle(); |
| 330 | break; |
| 331 | case 'surecart/product-item-price': |
| 332 | $this->renderPrice(); |
| 333 | break; |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | $this->block_html .= '</div><!-- /wp:group -->'; |
| 338 | $this->block_html .= '<!-- /wp:surecart/product-template -->'; |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Render the pagination. |
| 343 | * |
| 344 | * @return void |
| 345 | */ |
| 346 | public function renderPagination(): void { |
| 347 | $pagination_enabled = wp_validate_boolean( $this->attributes['pagination_enabled'] ?? true ); |
| 348 | |
| 349 | if ( ! $pagination_enabled ) { |
| 350 | return; |
| 351 | } |
| 352 | |
| 353 | $attributes = $this->attributes['pagination_size'] ? [ |
| 354 | 'style' => [ |
| 355 | 'typography' => [ |
| 356 | 'fontSize' => $this->attributes['pagination_size'], |
| 357 | ], |
| 358 | ], |
| 359 | ] : null; |
| 360 | |
| 361 | $this->block_html .= '<!-- wp:surecart/product-pagination ' . ( ! empty( $attributes ) ? wp_json_encode( $attributes ) : '' ) . ' -->'; |
| 362 | $this->block_html .= '<!-- wp:surecart/product-pagination-previous /-->'; |
| 363 | $this->block_html .= '<!-- wp:surecart/product-pagination-numbers /-->'; |
| 364 | $this->block_html .= '<!-- wp:surecart/product-pagination-next /-->'; |
| 365 | $this->block_html .= '<!-- /wp:surecart/product-pagination -->'; |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * Render the blocks. |
| 370 | * |
| 371 | * @return string |
| 372 | */ |
| 373 | public function doBlocks(): string { |
| 374 | return do_blocks( $this->block_html ); |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * Render the new product list. |
| 379 | * |
| 380 | * @return string |
| 381 | */ |
| 382 | public function render(): string { |
| 383 | $this->renderProductList(); |
| 384 | return $this->doBlocks(); |
| 385 | } |
| 386 | } |
| 387 |