PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.7.0
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.7.0
1.7.0 1.6.6 1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 All 50 releases
fluent-cart / app / Hooks / Handlers / BlockEditors / Checkout / CheckoutBlockEditor.php

CheckoutBlockEditor.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.7.0, at app/Hooks/Handlers/BlockEditors/Checkout/CheckoutBlockEditor.php

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