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 / Services / ShortCodeParser / ShortcodeParser.php

ShortcodeParser.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.7.0, at app/Services/ShortCodeParser/ShortcodeParser.php

431 lines 16.0 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\Services\ShortCodeParser;
4
5 use FluentCart\Api\StoreSettings;
6 use FluentCart\App\Helpers\Helper;
7 use FluentCart\App\Models\Order;
8 use FluentCart\App\Services\Payments\PaymentHelper;
9 use FluentCart\App\Services\Payments\PaymentReceipt;
10 use FluentCart\App\Services\WpMetaHelper;
11 use FluentCart\Framework\Support\Arr;
12
13 class ShortcodeParser
14 {
15
16 /*
17 *
18 * @param $content string|array
19 * @param $order object with customer FluentCart\Api\Orders; getOrderWithCustomer;
20 * @return string|array
21 */
22 public static function parse($content, $parsable)
23 {
24
25 $transaction = Arr::get($parsable, 'transaction', []);
26 $order = Arr::get($parsable, 'order', []);
27
28 if (is_array($content)) {
29 return static::arrayIterator($content, $parsable);
30 }
31
32 $parsableItems = self::nestedArrayItems($content);
33
34 if (!$parsableItems) {
35 return $content;
36 }
37
38 if (!is_array($parsableItems)) {
39 return $parsableItems;
40 }
41
42 $formattedParsables = array();
43
44 if (!is_array($parsableItems)) {
45 return $parsableItems;
46 }
47
48 foreach ($parsableItems as $parsableKey => $parsableItem) {
49 // Get Parsed Group
50 $group = strtok($parsableItem, '.:');
51 $itemExt = str_replace(array($group . '.', $group . ':'), '', $parsableItem);
52 $formattedParsables[$group][$parsableKey] = $itemExt;
53 }
54
55 $wpParser = Arr::only(
56 $formattedParsables,
57 array(
58 'wp',
59 'other'
60 )
61 );
62
63 $formattedInputs = Arr::only(
64 $formattedParsables,
65 array(
66 'order',
67 )
68 );
69
70 $orderAddressCodes = Arr::only(
71 $formattedParsables,
72 array(
73 'billing',
74 'shipping'
75 )
76 );
77
78 $otherShortCodes = Arr::only(
79 $formattedParsables,
80 array(
81 'payment',
82 )
83 );
84
85 $userShortCodes = Arr::only(
86 $formattedParsables,
87 array(
88 'user'
89 )
90 );
91
92 $transactionShortCodes = Arr::only(
93 $formattedParsables,
94 array(
95 'transaction'
96 )
97 );
98
99 $customerShortCodes = Arr::only(
100 $formattedParsables,
101 ['customer']
102 );
103
104 $parsedOthers = static::parseOthers($otherShortCodes, $order);
105 $parsedInputs = static::parseInputFields($formattedInputs, $order);
106 $parsedWp = isset($formattedParsables['wp']) ? static::parseWPFields($wpParser, $order) : [];
107 $parsedAddress = static::parseAddressFields($orderAddressCodes, Arr::get($order, 'customer', []));
108 $parsedUser = static::parseUserFields($userShortCodes, Arr::get($parsable, 'user'));
109 $parsedTransaction = isset($formattedParsables['transaction']) ? static::parseTransaction($transactionShortCodes, $transaction) : [];
110 $parsedCustomer = isset($formattedParsables['customer']) ? static::parseCustomerFields($customerShortCodes, Arr::get($order, 'customer', [])) : [];
111
112 $parsedData = array_merge(
113 $parsedInputs,
114 $parsedWp,
115 $parsedAddress,
116 $parsedOthers,
117 $parsedUser,
118 $parsedTransaction,
119 $parsedCustomer
120 );
121
122 $formattedParsedItems = [];
123 foreach ($parsedData as $parsedKey => $parseItem) {
124 if (is_array($parseItem)) {
125 $parseItem = implode(', ', $parseItem);
126 }
127 $formattedParsedItems[$parsedKey] = $parseItem;
128 }
129
130 return self::replaceValue($content, $formattedParsedItems);
131 }
132
133 public static function arrayIterator($contentArray, $order)
134 {
135 foreach ($contentArray as $key => $val) {
136 if (!is_array($val)) {
137 $contentArray[$key] = static::parse($val, $order);
138 } else {
139 $contentArray[$key] = static::arrayIterator($val, $order);
140 }
141 }
142 return $contentArray;
143 }
144
145 public static function parseUserFields($userShortCodes, $user = null): array
146 {
147 $customData = [
148 'password_reset_link' => function ($user) {
149 $user_data = get_userdata($user->ID);
150 $user_login = $user_data->user_login;
151 $key = get_password_reset_key($user_data);
152 if (is_wp_error($key)) {
153 return __("Error generating password reset key", 'fluent-cart');
154 }
155
156 $locale = get_user_locale($user_data);
157
158 return '<a href="' . network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user_login), 'login') . '&wp_lang=' . $locale . '"> ' . __('Reset', 'fluent-cart') . ' </a>';
159
160 }
161 ];
162 global $current_user;
163
164 if (!($user instanceof \WP_User)) {
165 $user = $current_user;
166 }
167
168 $parsedData = array();
169 if (!$user) {
170 return $parsedData;
171 }
172 foreach ($userShortCodes as $groupKey => $values) {
173 foreach ($values as $placeholder => $targetItem) {
174 if (isset($customData[$targetItem])) {
175 $parsedData[$placeholder] = $customData[$targetItem]($user);
176 } else {
177 $parsedData[$placeholder] = $user->{$targetItem};
178 }
179
180 }
181 }
182 return $parsedData;
183
184 }
185
186 public static function parseCustomerFields($customerShortCodes, $customer): array
187 {
188 $parsedData = array();
189 foreach ($customerShortCodes as $groupKey => $values) {
190 foreach ($values as $placeholder => $targetItem) {
191 if ($targetItem === 'first_name') {
192 $val = Arr::get($customer, 'first_name', null);
193 }
194 if ($targetItem === 'last_name') {
195 $val = Arr::get($customer, 'last_name', null);
196 }
197 $parsedData[$placeholder] = $val;
198 }
199 }
200 return $parsedData;
201 }
202
203 public static function parseOthers($otherShortCodes, $order): array
204 {
205 $parsedData = array();
206 foreach ($otherShortCodes as $groupKey => $values) {
207 foreach ($values as $placeholder => $targetItem) {
208 if ($targetItem === 'summary') {
209 $val = static::getSummary($order);
210 }
211 if ($targetItem === 'receipt') {
212 $val = static::getReceipt($order);
213 }
214
215 if (!empty($val)) {
216 $parsedData[$placeholder] = $val;
217 }
218
219 }
220 }
221 return $parsedData;
222 }
223
224 public static function parseTransaction($transactionShortCodes, $transaction): array
225 {
226 $parsedData = [];
227 foreach ($transactionShortCodes as $groupKey => $values) {
228 foreach ($values as $placeholder => $targetItem) {
229 if ($targetItem === 'refund_amount') {
230 $refund_amount = Helper::toDecimal(Arr::get($transaction, 'refund_amount', false));
231 $val = Helper::toDecimal($refund_amount);
232 }
233 if ($targetItem === 'created_at') {
234 $val = $transaction['created_at'];
235 }
236
237 if (!empty($val)) {
238 $parsedData[$placeholder] = $val;
239 }
240
241 }
242 }
243 return $parsedData;
244 }
245
246 public static function getSummary($order)
247 {
248 $paymentReceipt = new PaymentReceipt($order);
249 ob_start();
250 do_action('fluent_cart/views/checkout_order_summary', compact('order', 'paymentReceipt'));
251 return ob_get_clean();
252 }
253
254 public static function getReceipt($order)
255 {
256 $paymentReceipt = new PaymentReceipt($order);
257 ob_start();
258 do_action('fluent_cart/views/checkout_order_receipt', compact('order', 'paymentReceipt'));
259 return ob_get_clean();
260 }
261
262 public static function replaceValue($parsedItems, $shortcodeValues)
263 {
264 if (is_array($parsedItems)) {
265 foreach ($parsedItems as $key => $value) {
266 if (!is_array($value)) {
267 $parsedItems[$key] = str_replace(array_keys($shortcodeValues), array_values($shortcodeValues), $value);
268 } else {
269 $parsedItems[$key] = self::replaceValue($value, $shortcodeValues);
270 }
271 }
272 } else {
273 $parsedItems = str_replace(array_keys($shortcodeValues), array_values($shortcodeValues), $parsedItems);
274 }
275 return $parsedItems;
276 }
277
278 public static function parseInputFields($placeholders, $order): array
279 {
280 $order = $order->load('customer', 'billing_address', 'shipping_address')->toArray();
281 $order['billing'] = Arr::get($order, 'billing_address');
282 $order['shipping'] = Arr::get($order, 'shipping_address');
283
284 unset($order['billing_address']);
285 unset($order['shipping_address']);
286
287 $storeSettings = new StoreSettings();
288 $profilePage = $storeSettings->getCustomerProfilePage();
289 $parsedData = [];
290
291 foreach ($placeholders as $groupKey => $values) {
292 foreach ($values as $placeholder => $targetItem) {
293 if ($groupKey == 'order') {
294 if ($targetItem === 'customer_dashboard_link') {
295 if (!empty($profilePage)) {
296 $parsedData[$placeholder] = "<a style='color: #017EF3; text-decoration: none;' href='" . "$profilePage#/order/" . Arr::get($order, 'uuid') . "'>" . Arr::get($order, 'id') . "</a>";
297 } else {
298 $parsedData[$placeholder] = Arr::get($order, 'id');
299 }
300 } elseif ($targetItem === 'payment_link') {
301 $parsedData[$placeholder] = PaymentHelper::getCustomPaymentLink(Arr::get($order, 'uuid'));
302 } elseif ($targetItem === 'review_url') {
303 // Resolved here rather than through OrderParser: this
304 // parser handles every order.* code itself and never
305 // consults that class, so a methodMap entry alone would
306 // render an empty string in a real email.
307 $reviewHash = (string) Arr::get($order, 'uuid', '');
308 if ($reviewHash !== '') {
309 $parsedData[$placeholder] = Order::reviewUrlForHash($reviewHash);
310 } else {
311 $reviewOrder = Order::query()->find(Arr::get($order, 'id'));
312 $parsedData[$placeholder] = $reviewOrder instanceof Order ? $reviewOrder->getOrderReviewUrl() : '';
313 }
314 } else if ($targetItem === 'total_amount') {
315 $parsedData[$placeholder] = Helper::toDecimal(Arr::get($order, 'total_amount'), false);
316 } elseif ($targetItem === 'total_paid') {
317 $parsedData[$placeholder] = Helper::toDecimal(Arr::get($order, 'total_paid'), false);
318 } else if ($targetItem === 'tax_total') {
319 $parsedData[$placeholder] = Helper::toDecimal(Arr::get($order, 'tax_total'), false);
320 } else if ($targetItem === 'shipping_total') {
321 $parsedData[$placeholder] = Helper::toDecimal(Arr::get($order, 'shipping_total'), false);
322 } elseif ($targetItem === 'manual_discount_total') {
323 $parsedData[$placeholder] = Helper::toDecimal(Arr::get($order, 'manual_discount_total'), false);
324 } elseif ($targetItem === 'coupon_discount_total') {
325 $parsedData[$placeholder] = Helper::toDecimal(Arr::get($order, 'coupon_discount_total'), false);
326 } elseif ($targetItem === 'shipping_tax') {
327 $parsedData[$placeholder] = Helper::toDecimal(Arr::get($order, 'shipping_tax'), false);
328 } elseif ($targetItem === 'total_refund') {
329 $parsedData[$placeholder] = Helper::toDecimal(Arr::get($order, 'total_refund'), false);
330 } else {
331 $parsedData[$placeholder] = Arr::get($order, $targetItem);
332 }
333
334 } elseif ($groupKey == 'customer') {
335 $parsedData[$placeholder] = Arr::get($order, 'customer.' . $targetItem, []);
336 } elseif ($groupKey == 'order_items') {
337 $parsedData[$placeholder] = Arr::get($order, 'order_items.' . $targetItem);
338 }
339 }
340 }
341 return $parsedData;
342 }
343
344 public static function parseShortcode($parsableItems): array
345 {
346 $parsable = [];
347 preg_replace_callback('/{+(.*?)}/', function ($matches) use (&$parsable) {
348 $parsable[$matches[0]] = $matches[1];
349 }, $parsableItems);
350 return $parsable;
351 }
352
353 public static function nestedArrayItems($parsableItems): array
354 {
355 if (!is_array($parsableItems)) {
356 return self::parseShortcode($parsableItems);
357 }
358
359 $parsableData = [];
360 // checking item has child array elements
361 foreach ($parsableItems as $parsable) {
362 // checking if there is another child array, checking 2 levels deep
363 if (is_array($parsable) && count($parsable)) {
364 foreach ($parsable as $parsableItem) {
365 $arrayParsed = self::parseShortcode($parsableItem);
366 $parsableData = array_merge($parsable, $arrayParsed);
367 }
368 } else {
369 $arrayParsed = self::parseShortcode($parsable);
370 $parsableData = array_merge($parsable, $arrayParsed);
371 }
372 }
373
374 return $parsableData;
375 }
376
377 public static function parseAddressFields($parsable, $data): array
378 {
379
380 if (empty($data)) {
381 return [];
382 }
383 $billing = Arr::get($data, 'billing_address.0');
384 $shipping = Arr::get($data, 'shipping_address.0');
385
386 $parsedData = [];
387 foreach ($parsable as $groupKey => $values) {
388 foreach ($values as $placeholder => $targetItem) {
389 if ($groupKey === 'billing' && isset($billing)) {
390 $val = Arr::get($billing, $targetItem);
391 if ($targetItem === 'first_name' || $targetItem === 'last_name') {
392 $val = static::splitName($billing, $targetItem);
393 }
394 $parsedData[$placeholder] = $val;
395 } elseif ($groupKey === 'shipping' && isset($shipping)) {
396 $val = Arr::get($shipping, $targetItem);
397 if ($targetItem === 'first_name' || $targetItem === 'last_name') {
398 $val = static::splitName($shipping, $targetItem);
399 }
400 $parsedData[$placeholder] = $val;
401 }
402 }
403 }
404 return $parsedData;
405 }
406
407 public static function splitName($data, $key = 'first_name')
408 {
409 $names = explode(" ", Arr::get($data, 'name'));
410 return $key === 'first_name' ? $names[0] : $names[1];
411 }
412
413 public static function parseWPFields($placeHolders, $order): array
414 {
415 $parsedData = array();
416 $metaData = new WpMetaHelper($order);
417 foreach ($placeHolders as $groupKey => $values) {
418 foreach ($values as $placeholder => $targetItem) {
419 if ($groupKey == 'wp') {
420 $parsedData[$placeholder] = $metaData->getWPValues($targetItem);
421 } elseif ($groupKey == 'user_meta') {
422 $parsedData[$placeholder] = $metaData->getuserMeta($targetItem);
423 } elseif ($groupKey == 'other') {
424 $parsedData[$placeholder] = $metaData->getOtherData($targetItem);
425 }
426 }
427 }
428 return $parsedData;
429 }
430 }
431