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
ProductPriceChoicesMigrationService.php
1 year ago
ProductSelectedPriceMigrationService.php
1 year ago
ProductVariantsMigrationService.php
1 year ago
URLParamService.php
1 year ago
ProductListMigrationService.php
343 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 && empty( $this->attributes['collection_id'] ) ) { |
| 147 | $this->block_html .= '<!-- wp:surecart/product-list-filter {"collection_id":"' . esc_attr( $collection_id ) . '"} /-->'; |
| 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 = array( |
| 205 | 'useFeaturedImage' => true, |
| 206 | 'dimRatio' => 0, |
| 207 | 'isUserOverlayColor' => true, |
| 208 | 'focalPoint' => array( |
| 209 | 'x' => 0.5, |
| 210 | 'y' => 0.5, |
| 211 | ), |
| 212 | 'contentPosition' => 'top right', |
| 213 | 'isDark' => false, |
| 214 | 'style' => array( |
| 215 | 'dimensions' => array( 'aspectRatio' => '3/4' ), |
| 216 | 'layout' => array( |
| 217 | 'selfStretch' => 'fit', |
| 218 | 'flexSize' => null, |
| 219 | ), |
| 220 | 'spacing' => array( 'margin' => array( 'bottom' => '15px' ) ), |
| 221 | 'border' => array( 'radius' => '10px' ), |
| 222 | ), |
| 223 | ); |
| 224 | |
| 225 | $image = '<!-- wp:cover ' . wp_json_encode( |
| 226 | array_merge( |
| 227 | $product_image_attrs, |
| 228 | $this->getChildBlocksAttributes( 'surecart/product-item-image' ), |
| 229 | ) |
| 230 | ) . ' -->'; |
| 231 | $image .= '<div class="wp-block-cover is-light has-custom-content-position is-position-top-right" style="border-radius:10px;margin-bottom:15px">'; |
| 232 | $image .= '<span aria-hidden="true" class="wp-block-cover__background has-background-dim-0 has-background-dim"></span>'; |
| 233 | $image .= '<div class="wp-block-cover__inner-container">'; |
| 234 | $image .= '</div>'; |
| 235 | $image .= '</div>'; |
| 236 | $image .= '<!-- /wp:cover -->'; |
| 237 | |
| 238 | $this->block_html .= $image; |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Render the product price. |
| 243 | * |
| 244 | * @return void |
| 245 | */ |
| 246 | public function renderPrice(): void { |
| 247 | $product_price_attrs = wp_json_encode( $this->getChildBlocksAttributes( 'surecart/product-item-price' ), JSON_FORCE_OBJECT ); |
| 248 | $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"}} -->'; |
| 249 | $this->block_html .= '<div class="wp-block-group">'; |
| 250 | $this->block_html .= '<!-- wp:surecart/product-list-price ' . $product_price_attrs . ' /-->'; |
| 251 | $this->block_html .= '<!-- wp:surecart/product-list-price-sale ' . $product_price_attrs . ' /-->'; |
| 252 | $this->block_html .= '</div>'; |
| 253 | $this->block_html .= '<!-- /wp:group -->'; |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Render the product template. |
| 258 | * |
| 259 | * @return void |
| 260 | */ |
| 261 | public function renderProductTemplate(): void { |
| 262 | $product_template_attrs = array_merge( |
| 263 | $this->attributes ?? array(), |
| 264 | array( |
| 265 | 'style' => array( |
| 266 | 'spacing' => array( |
| 267 | 'blockGap' => '30px', |
| 268 | ), |
| 269 | ), |
| 270 | 'layout' => array( |
| 271 | 'type' => 'grid', |
| 272 | 'columnCount' => $this->attributes['columns'] ?? 3, |
| 273 | ), |
| 274 | ) |
| 275 | ); |
| 276 | |
| 277 | $this->block_html .= '<!-- wp:surecart/product-template ' . wp_json_encode( $product_template_attrs ) . ' -->'; |
| 278 | $group_attrs = ! empty( $this->inner_blocks[0]['attrs'] ) ? wp_json_encode( $this->inner_blocks[0]['attrs'] ) : '{}'; |
| 279 | $group_block = parse_blocks( '<!-- wp:group ' . $group_attrs . ' -->' )[0]; |
| 280 | $group_styles = sc_get_block_styles( true, $group_block ); |
| 281 | $group_classnames = ! empty( $group_styles['classnames'] ) ? $group_styles['classnames'] : ''; |
| 282 | $group_css = ! empty( $group_styles['css'] ) ? $group_styles['css'] : ''; |
| 283 | $this->block_html .= '<!-- wp:group {"style":{"spacing":{"blockGap":"5px"}},"layout":{"type":"flex","orientation":"vertical","justifyContent":"stretch"}} -->'; |
| 284 | $this->block_html .= '<div class="wp-block-group ' . $group_classnames . '" style="' . $group_css . '">'; |
| 285 | // Render according to the inner blocks order in old block. |
| 286 | if ( ! empty( $this->inner_blocks[0]['innerBlocks'] ) ) { |
| 287 | foreach ( $this->inner_blocks[0]['innerBlocks'] as $inner_block ) { |
| 288 | switch ( $inner_block['blockName'] ) { |
| 289 | case 'surecart/product-item-image': |
| 290 | $this->renderImage(); |
| 291 | break; |
| 292 | case 'surecart/product-item-title': |
| 293 | $this->renderTitle(); |
| 294 | break; |
| 295 | case 'surecart/product-item-price': |
| 296 | $this->renderPrice(); |
| 297 | break; |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | $this->block_html .= '</div><!-- /wp:group -->'; |
| 302 | $this->block_html .= '<!-- /wp:surecart/product-template -->'; |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Render the pagination. |
| 307 | * |
| 308 | * @return void |
| 309 | */ |
| 310 | public function renderPagination(): void { |
| 311 | $pagination_enabled = wp_validate_boolean( $this->attributes['pagination_enabled'] ?? true ); |
| 312 | |
| 313 | if ( ! $pagination_enabled ) { |
| 314 | return; |
| 315 | } |
| 316 | |
| 317 | $this->block_html .= '<!-- wp:surecart/product-pagination -->'; |
| 318 | $this->block_html .= '<!-- wp:surecart/product-pagination-previous /-->'; |
| 319 | $this->block_html .= '<!-- wp:surecart/product-pagination-numbers /-->'; |
| 320 | $this->block_html .= '<!-- wp:surecart/product-pagination-next /-->'; |
| 321 | $this->block_html .= '<!-- /wp:surecart/product-pagination -->'; |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * Render the blocks. |
| 326 | * |
| 327 | * @return string |
| 328 | */ |
| 329 | public function doBlocks(): string { |
| 330 | return do_blocks( $this->block_html ); |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Render the new product list. |
| 335 | * |
| 336 | * @return string |
| 337 | */ |
| 338 | public function render(): string { |
| 339 | $this->renderProductList(); |
| 340 | return $this->doBlocks(); |
| 341 | } |
| 342 | } |
| 343 |