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
ProductSelectedPriceMigrationService.php
223 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\BlockLibrary; |
| 4 | |
| 5 | /** |
| 6 | * Provide the migration service for the product price block. |
| 7 | */ |
| 8 | class ProductSelectedPriceMigrationService { |
| 9 | /** |
| 10 | * Attributes |
| 11 | * |
| 12 | * @var array |
| 13 | */ |
| 14 | public array $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 | * Get content alignment |
| 43 | * |
| 44 | * @return string |
| 45 | */ |
| 46 | public function getContentAlignment() { |
| 47 | return $this->attributes['alignment'] ?? 'left'; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Render the sales badge. |
| 52 | * |
| 53 | * @return void |
| 54 | */ |
| 55 | public function renderSalesBadge() { |
| 56 | $badge_attributes = array( |
| 57 | 'text' => $this->attributes['sale_text'] ?? __( 'Sale', 'surecart' ), |
| 58 | 'style' => array( |
| 59 | 'border' => array( |
| 60 | 'radius' => '15px', |
| 61 | ), |
| 62 | 'typography' => array( |
| 63 | 'fontSize' => '12px', |
| 64 | ), |
| 65 | 'layout' => array( |
| 66 | 'selfStretch' => 'fit', |
| 67 | 'flexSize' => null, |
| 68 | ), |
| 69 | ), |
| 70 | ); |
| 71 | |
| 72 | $this->block_html .= '<!-- wp:surecart/product-sale-badge ' . wp_json_encode( $badge_attributes ) . ' /-->'; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Render the price scratch amount, selected price and sales badge. |
| 77 | * |
| 78 | * @return void |
| 79 | */ |
| 80 | public function renderAmountLine() { |
| 81 | $scratch_block_attributes = array_merge( |
| 82 | $this->attributes, |
| 83 | array( |
| 84 | 'style' => array( |
| 85 | 'typography' => array( |
| 86 | 'textDecoration' => 'line-through', |
| 87 | 'fontSize' => '24px', |
| 88 | 'lineHeight' => '1.4', |
| 89 | ), |
| 90 | 'color' => array( |
| 91 | 'text' => $attributes['textColor'] ?? '#8a8a8a', |
| 92 | ), |
| 93 | ), |
| 94 | ) |
| 95 | ); |
| 96 | $amount_block_attributes = array_merge( |
| 97 | $this->attributes, |
| 98 | array( |
| 99 | 'style' => array( |
| 100 | 'typography' => array( |
| 101 | 'fontSize' => '24px', |
| 102 | 'lineHeight' => '1.4', |
| 103 | ), |
| 104 | 'color' => array( |
| 105 | 'text' => $attributes['textColor'] ?? '#8a8a8a', |
| 106 | ), |
| 107 | ), |
| 108 | ) |
| 109 | ); |
| 110 | |
| 111 | $interval_block_attributes = array_merge( |
| 112 | $this->attributes, |
| 113 | array( |
| 114 | 'style' => array( |
| 115 | 'typography' => array( |
| 116 | 'fontSize' => '18px', |
| 117 | 'lineHeight' => '1.8', |
| 118 | ), |
| 119 | 'color' => array( |
| 120 | 'text' => $attributes['textColor'] ?? '#8a8a8a', |
| 121 | ), |
| 122 | ), |
| 123 | ) |
| 124 | ); |
| 125 | |
| 126 | $this->block_html .= '<!-- wp:group {"style":{"spacing":{"blockGap":"0.5em"}},"layout":{"type":"flex","flexWrap":"nowrap","justifyContent":"' . $this->getContentAlignment() . '","verticalAlignment":"bottom"}} -->'; |
| 127 | $this->block_html .= '<div class="wp-block-group">'; |
| 128 | $this->block_html .= '<!-- wp:surecart/product-selected-price-scratch-amount ' . wp_json_encode( $scratch_block_attributes ) . ' /-->'; |
| 129 | $this->block_html .= '<!-- wp:surecart/product-selected-price-amount ' . wp_json_encode( $amount_block_attributes ) . ' /-->'; |
| 130 | $this->block_html .= '<!-- wp:surecart/product-selected-price-interval ' . wp_json_encode( $interval_block_attributes ) . ' /-->'; |
| 131 | |
| 132 | $this->renderSalesBadge(); |
| 133 | $this->block_html .= '</div>'; |
| 134 | $this->block_html .= '<!-- /wp:group -->'; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Render selected price trial and fees. |
| 139 | * |
| 140 | * @return void |
| 141 | */ |
| 142 | public function renderSelectedPriceTrialAndFees() { |
| 143 | $child_block_attributes = array_merge( |
| 144 | $this->attributes, |
| 145 | array( |
| 146 | 'style' => array( |
| 147 | 'color' => array( |
| 148 | 'text' => $attributes['textColor'] ?? '#8a8a8a', |
| 149 | ), |
| 150 | 'elements' => array( |
| 151 | 'link' => array( |
| 152 | 'color' => array( |
| 153 | 'text' => $attributes['textColor'] ?? '#8a8a8a', |
| 154 | ), |
| 155 | ), |
| 156 | ), |
| 157 | ), |
| 158 | ) |
| 159 | ); |
| 160 | |
| 161 | $this->block_html .= '<!-- wp:group {"style":{"spacing":{"blockGap":"0.5em"}},"layout":{"type":"flex","flexWrap":"wrap","justifyContent":"' . $this->getContentAlignment() . '"}} -->'; |
| 162 | $this->block_html .= '<div class="wp-block-group" >'; |
| 163 | $this->block_html .= '<!-- wp:surecart/product-selected-price-trial ' . wp_json_encode( $child_block_attributes ) . ' /-->'; |
| 164 | $this->block_html .= '<!-- wp:surecart/product-selected-price-fees ' . wp_json_encode( $child_block_attributes ) . ' /-->'; |
| 165 | $this->block_html .= '</div>'; |
| 166 | $this->block_html .= '<!-- /wp:group -->'; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Render the price. |
| 171 | * |
| 172 | * @return void |
| 173 | */ |
| 174 | public function renderPrice() { |
| 175 | $group_attributes = array( |
| 176 | 'style' => array( |
| 177 | 'spacing' => $this->attributes['style']['spacing'] ?? array( |
| 178 | 'blockGap' => '0px', |
| 179 | ), |
| 180 | ), |
| 181 | ); |
| 182 | |
| 183 | if ( ! empty( $this->attributes['backgroundColor'] ) ) { |
| 184 | $group_attributes['backgroundColor'] = $this->attributes['backgroundColor']; |
| 185 | } |
| 186 | |
| 187 | if ( ! empty( $this->attributes['style']['color']['background'] ) ) { |
| 188 | $group_attributes['style']['color']['background'] = $this->attributes['style']['color']['background']; |
| 189 | } |
| 190 | |
| 191 | $group_block = parse_blocks( '<!-- wp:group ' . wp_json_encode( $group_attributes ) . ' -->' ) [0] ?? null; |
| 192 | $group_styles = sc_get_block_styles( true, $group_block ); |
| 193 | $group_classnames = $group_styles['classnames'] ?? ''; |
| 194 | $group_css = $group_styles['css'] ?? ''; |
| 195 | |
| 196 | $this->block_html .= '<!-- wp:group ' . wp_json_encode( $group_attributes ) . ' -->'; |
| 197 | $this->block_html .= '<div class="wp-block-group ' . $group_classnames . '" style="' . $group_css . '">'; |
| 198 | $this->renderAmountLine(); |
| 199 | $this->renderSelectedPriceTrialAndFees(); |
| 200 | $this->block_html .= '</div>'; |
| 201 | $this->block_html .= '<!-- /wp:group -->'; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Render blocks. |
| 206 | * |
| 207 | * @return string |
| 208 | */ |
| 209 | public function doBlocks() { |
| 210 | return do_blocks( $this->block_html ); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Render the new price. |
| 215 | * |
| 216 | * @return string |
| 217 | */ |
| 218 | public function render() { |
| 219 | $this->renderPrice(); |
| 220 | return $this->doBlocks(); |
| 221 | } |
| 222 | } |
| 223 |