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

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

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