PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.6.4
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.6.4
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 1.3.19 trunk 1.2.0 All 47 releases
fluent-cart / app / Services / ShortCodeParser / ShortcodeParser.php

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

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