| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Hooks\Handlers\BlockEditors\Checkout; |
| 4 |
|
| 5 |
|
| 6 |
use FluentCart\Api\CurrencySettings; |
| 7 |
use FluentCart\App\Helpers\CartHelper; |
| 8 |
use FluentCart\App\Helpers\CurrenciesHelper; |
| 9 |
use FluentCart\App\Hooks\Handlers\BlockEditors\BlockEditor; |
| 10 |
use FluentCart\App\Hooks\Handlers\BlockEditors\Checkout\InnerBlocks\InnerBlocks; |
| 11 |
use FluentCart\App\Modules\Templating\AssetLoader; |
| 12 |
use FluentCart\App\Services\Renderer\CartRenderer; |
| 13 |
use FluentCart\App\Services\Renderer\CheckoutRenderer; |
| 14 |
use FluentCart\App\Services\Renderer\RenderHelper; |
| 15 |
use FluentCart\App\Services\Translations\TransStrings; |
| 16 |
use FluentCart\App\Vite; |
| 17 |
use FluentCart\Framework\Support\Arr; |
| 18 |
|
| 19 |
|
| 20 |
class CheckoutBlockEditor extends BlockEditor |
| 21 |
{ |
| 22 |
protected static string $editorName = 'checkout'; |
| 23 |
|
| 24 |
public function getScripts(): array |
| 25 |
{ |
| 26 |
return [ |
| 27 |
[ |
| 28 |
'source' => 'admin/BlockEditor/Checkout/CheckoutBlockEditor.jsx', |
| 29 |
'dependencies' => ['wp-blocks', 'wp-components'] |
| 30 |
] |
| 31 |
]; |
| 32 |
} |
| 33 |
|
| 34 |
public function getStyles(): array |
| 35 |
{ |
| 36 |
return [ |
| 37 |
'admin/BlockEditor/Checkout/style/checkout-block-editor.scss', |
| 38 |
'public/checkout/style/checkout.scss' |
| 39 |
]; |
| 40 |
} |
| 41 |
|
| 42 |
public function init(): void |
| 43 |
{ |
| 44 |
parent::init(); |
| 45 |
|
| 46 |
$this->registerInnerBlocks(); |
| 47 |
} |
| 48 |
|
| 49 |
public function registerInnerBlocks() |
| 50 |
{ |
| 51 |
InnerBlocks::register(); |
| 52 |
} |
| 53 |
|
| 54 |
public function localizeData(): array |
| 55 |
{ |
| 56 |
|
| 57 |
return [ |
| 58 |
$this->getLocalizationKey() => [ |
| 59 |
'slug' => $this->slugPrefix, |
| 60 |
'name' => static::getEditorName(), |
| 61 |
'title' => __('Checkout Page', 'fluent-cart'), |
| 62 |
'description' => __('This block will display the checkout page.', 'fluent-cart'), |
| 63 |
'placeholder_image' => Vite::getAssetUrl('images/placeholder.svg'), |
| 64 |
], |
| 65 |
'fluent_cart_block_translation' => TransStrings::blockStrings() |
| 66 |
]; |
| 67 |
} |
| 68 |
|
| 69 |
protected function skipInnerBlocks(): bool |
| 70 |
{ |
| 71 |
return true; |
| 72 |
} |
| 73 |
|
| 74 |
public function render(array $shortCodeAttribute, $block = null, $content = null): string |
| 75 |
{ |
| 76 |
AssetLoader::loadCheckoutAssets(); |
| 77 |
|
| 78 |
// return '[fluent_cart_checkout]'; |
| 79 |
global $wp; |
| 80 |
$current_url = home_url(add_query_arg([], $wp->request)); |
| 81 |
$current_url = add_query_arg(array_map('sanitize_text_field', wp_unslash($_GET)), $current_url); |
| 82 |
$cart = CartHelper::getCart(); |
| 83 |
|
| 84 |
if (!$cart || empty(Arr::get($cart, 'cart_data', []))) { |
| 85 |
ob_start(); |
| 86 |
(new CartRenderer())->renderEmpty(); |
| 87 |
return ob_get_clean(); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
$classNames = [ |
| 92 |
'fluent-cart-checkout-page', |
| 93 |
'fct-checkout alignwide', |
| 94 |
'fct-checkout-type-' . $cart->cart_group |
| 95 |
]; |
| 96 |
|
| 97 |
$atts = [ |
| 98 |
'class' => implode(' ', $classNames), |
| 99 |
'data-fluent-cart-checkout-page' => '', |
| 100 |
]; |
| 101 |
|
| 102 |
$formAttributes = [ |
| 103 |
'method' => 'POST', |
| 104 |
'data-fluent-cart-checkout-page-checkout-form' => '', |
| 105 |
'class' => 'fct_checkout fluent-cart-checkout-page-checkout-form', |
| 106 |
'action' => $current_url, |
| 107 |
'enctype' => 'multipart/form-data', |
| 108 |
]; |
| 109 |
|
| 110 |
|
| 111 |
ob_start(); |
| 112 |
(new CheckoutRenderer($cart))->wrapperStart(); |
| 113 |
$wrapperStartHtml = ob_get_clean(); |
| 114 |
|
| 115 |
$innerBlocksContent = ''; |
| 116 |
|
| 117 |
if (empty($block->inner_blocks) && version_compare( FLUENTCART_VERSION, '1.3.0', '>' )) { |
| 118 |
return '[fluent_cart_checkout]'; |
| 119 |
} else if ($block instanceof \WP_Block && !empty($block->inner_blocks)) { |
| 120 |
ob_start(); |
| 121 |
?> |
| 122 |
<form <?php RenderHelper::renderAtts($formAttributes); ?>> |
| 123 |
<div class="fct_block_checkout"> |
| 124 |
<?php |
| 125 |
foreach ($block->inner_blocks as $inner_block) { |
| 126 |
if (isset($inner_block->parsed_block)) { ?> |
| 127 |
<div class="fluent-cart-checkout-block-child-wrap"> |
| 128 |
<?php echo $inner_block->render(); ?> |
| 129 |
</div> |
| 130 |
<?php |
| 131 |
} |
| 132 |
} |
| 133 |
?> |
| 134 |
</div> |
| 135 |
</form> |
| 136 |
<?php |
| 137 |
$innerBlocksContent = ob_get_clean(); |
| 138 |
} |
| 139 |
|
| 140 |
ob_start(); |
| 141 |
(new CheckoutRenderer($cart))->wrapperEnd(); |
| 142 |
$wrapperEndHtml = ob_get_clean(); |
| 143 |
|
| 144 |
return $wrapperStartHtml . $innerBlocksContent . $wrapperEndHtml; |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Returns the default `addressModal` |
| 149 |
* |
| 150 |
* @return array |
| 151 |
*/ |
| 152 |
public static function getDefaultAddressModal(): array |
| 153 |
{ |
| 154 |
return [ |
| 155 |
'billingAddress' => __('Billing Address', 'fluent-cart'), |
| 156 |
'shippingAddress' => __('Shipping Address', 'fluent-cart'), |
| 157 |
'openButtonText' => __('Change', 'fluent-cart'), |
| 158 |
'addButtonText' => __('Add Address', 'fluent-cart'), |
| 159 |
'applyButtonText' => __('Apply', 'fluent-cart'), |
| 160 |
'submitButtonText' => __('Submit', 'fluent-cart'), |
| 161 |
'cancelButtonText' => __('Cancel', 'fluent-cart') |
| 162 |
]; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Returns the default `SippingMethods` |
| 167 |
* |
| 168 |
* @return array |
| 169 |
*/ |
| 170 |
public static function getDefaultSippingMethods(): array |
| 171 |
{ |
| 172 |
return [ |
| 173 |
'heading' => __('Shipping Method', 'fluent-cart') |
| 174 |
]; |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Returns the default `PaymentMethods` |
| 179 |
* |
| 180 |
* @return array |
| 181 |
*/ |
| 182 |
public static function getDefaultPaymentMethods(): array |
| 183 |
{ |
| 184 |
return [ |
| 185 |
'heading' => __('Payment', 'fluent-cart') |
| 186 |
]; |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Returns the default `orderSummary` |
| 191 |
* |
| 192 |
* @return array |
| 193 |
*/ |
| 194 |
public static function getDefaultOrderSummary(): array |
| 195 |
{ |
| 196 |
return [ |
| 197 |
'toggleButtonText' => __('View Items', 'fluent-cart'), |
| 198 |
'removeButtonText' => __('Remove', 'fluent-cart'), |
| 199 |
'totalText' => __('Total', 'fluent-cart'), |
| 200 |
'heading' => __('Summary', 'fluent-cart'), |
| 201 |
'maxVisibleItems' => 2, |
| 202 |
'showRemoveButton' => true, |
| 203 |
'coupons' => self::getDefaultCoupons() |
| 204 |
]; |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Returns the default `coupons` |
| 209 |
* |
| 210 |
* @return array |
| 211 |
*/ |
| 212 |
public static function getDefaultCoupons(): array |
| 213 |
{ |
| 214 |
return [ |
| 215 |
'iconVisibility' => true, |
| 216 |
'placeholder' => __('Apply Here', 'fluent-cart'), |
| 217 |
'applyButton' => __('Apply', 'fluent-cart'), |
| 218 |
'label' => __('Have a Coupon?', 'fluent-cart'), |
| 219 |
'collapsible' => true |
| 220 |
]; |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* Returns the default `submitButton` |
| 225 |
* |
| 226 |
* @return array |
| 227 |
*/ |
| 228 |
public static function getDefaultSubmitButton(): array |
| 229 |
{ |
| 230 |
return [ |
| 231 |
'text' => __('Place Order', 'fluent-cart'), |
| 232 |
'alignment' => 'left', |
| 233 |
'size' => 'large', |
| 234 |
'full' => true |
| 235 |
]; |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Returns the default `AllowCreateAccount` |
| 240 |
* |
| 241 |
* @return array |
| 242 |
*/ |
| 243 |
public static function getDefaultAllowCreateAccount(): array |
| 244 |
{ |
| 245 |
return [ |
| 246 |
'label' => __('Create my user account', 'fluent-cart'), |
| 247 |
'infoText' => __('By checking this box, you agree to create an account with us to manage your subscription and order details. This is mandatory for subscription-based purchases.', 'fluent-cart') |
| 248 |
]; |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Safely encode JSON strings if needed |
| 253 |
* |
| 254 |
* @param mixed $value The value to process |
| 255 |
* @return mixed The processed value (encoded if it was an array or object) |
| 256 |
*/ |
| 257 |
protected function maybeEncodeJson($value) |
| 258 |
{ |
| 259 |
if (is_array($value) || is_object($value)) { |
| 260 |
return json_encode($value); |
| 261 |
} |
| 262 |
return $value; |
| 263 |
} |
| 264 |
|
| 265 |
} |
| 266 |
|