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
ProductVariantsMigrationService.php
94 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\BlockLibrary; |
| 4 | |
| 5 | /** |
| 6 | * Provide the migration service for the product variant block. |
| 7 | */ |
| 8 | class ProductVariantsMigrationService { |
| 9 | /** |
| 10 | * Attributes |
| 11 | * |
| 12 | * @var array |
| 13 | */ |
| 14 | public $attributes = array(); |
| 15 | |
| 16 | /** |
| 17 | * Block. |
| 18 | * |
| 19 | * @var object |
| 20 | */ |
| 21 | public ?object $block = null; |
| 22 | |
| 23 | /** |
| 24 | * Block HTML. |
| 25 | * |
| 26 | * @var string |
| 27 | */ |
| 28 | public string $block_html = ''; |
| 29 | |
| 30 | /** |
| 31 | * Constructor |
| 32 | * |
| 33 | * @param array $attributes Attributes. |
| 34 | * @param object $block Block. |
| 35 | */ |
| 36 | public function __construct( $attributes = array(), $block = null ) { |
| 37 | $this->attributes = $attributes; |
| 38 | $this->block = $block; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Render the variant pill |
| 43 | */ |
| 44 | public function renderVariantPill() { |
| 45 | $block_attributes = array_merge( |
| 46 | $this->attributes, |
| 47 | array( |
| 48 | 'highlight_text' => '#ffffff', |
| 49 | 'highlight_background' => '#000000', |
| 50 | 'highlight_border' => '#000000', |
| 51 | ) |
| 52 | ); |
| 53 | |
| 54 | // remove the margin since it is used for the parent block. |
| 55 | $block_attributes['style']['spacing'] = array( |
| 56 | 'padding' => $block_attributes['style']['spacing']['padding'] ?? '', |
| 57 | ); |
| 58 | |
| 59 | $this->block_html .= '<!-- wp:surecart/product-variant-pill ' . wp_json_encode( $block_attributes ) . ' /-->'; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Render the product variant block. |
| 64 | * |
| 65 | * @return void |
| 66 | */ |
| 67 | public function renderProductVariants() { |
| 68 | $wrapper_attributes['style']['spacing']['margin'] = $this->attributes['style']['spacing']['margin'] ?? ''; |
| 69 | |
| 70 | $this->block_html = '<!-- wp:surecart/product-variant-pills ' . wp_json_encode( $wrapper_attributes ) . ' -->'; |
| 71 | $this->renderVariantPill(); |
| 72 | $this->block_html .= '<!-- /wp:surecart/product-variant-pills -->'; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Render the blocks. |
| 77 | * |
| 78 | * @return string |
| 79 | */ |
| 80 | public function doBlocks() { |
| 81 | return do_blocks( $this->block_html ); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Render the new variants block. |
| 86 | * |
| 87 | * @return string |
| 88 | */ |
| 89 | public function render() { |
| 90 | $this->renderProductVariants(); |
| 91 | return $this->doBlocks(); |
| 92 | } |
| 93 | } |
| 94 |