| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Services\ShortCodeParser\Parsers; |
| 4 |
|
| 5 |
use FluentCart\App\Helpers\Helper; |
| 6 |
use FluentCart\App\Services\DateTime\DateFormatter; |
| 7 |
use FluentCart\Framework\Support\Arr; |
| 8 |
|
| 9 |
class SubscriptionParser extends BaseParser |
| 10 |
{ |
| 11 |
private $subscription; |
| 12 |
|
| 13 |
/** |
| 14 |
* Only used as the timezone context for DateFormatter — the order carries the |
| 15 |
* user_tz captured at checkout. Null when the shortcode runs without one, in |
| 16 |
* which case DateFormatter falls back to UTC. |
| 17 |
*/ |
| 18 |
private $order; |
| 19 |
|
| 20 |
public function __construct($data) |
| 21 |
{ |
| 22 |
$this->subscription = Arr::get($data, 'subscription'); |
| 23 |
$this->order = Arr::get($data, 'order'); |
| 24 |
parent::__construct($data); |
| 25 |
} |
| 26 |
|
| 27 |
public function parse($accessor = null, $code = null, $transformer = null): ?string |
| 28 |
{ |
| 29 |
$subscription = $this->subscription; |
| 30 |
|
| 31 |
if (!$subscription) { |
| 32 |
return ''; |
| 33 |
} |
| 34 |
|
| 35 |
switch ($accessor) { |
| 36 |
case 'sl': |
| 37 |
return (string) (isset($subscription->sl) ? $subscription->sl : ''); |
| 38 |
case 'item_name': |
| 39 |
return esc_html($subscription->display_item_name); |
| 40 |
case 'status': |
| 41 |
return esc_html($subscription->status); |
| 42 |
case 'billing_interval': |
| 43 |
return esc_html($subscription->billing_interval); |
| 44 |
case 'recurring_amount': |
| 45 |
return $subscription->recurring_amount ? (string) ($subscription->recurring_amount / 100) : '0'; |
| 46 |
case 'recurring_amount_formatted': |
| 47 |
return esc_html(Helper::toDecimal($subscription->recurring_amount)); |
| 48 |
case 'payment_info': |
| 49 |
return wp_kses_post($subscription->payment_info); |
| 50 |
case 'next_billing_date': |
| 51 |
return $subscription->next_billing_date |
| 52 |
? esc_html(DateFormatter::format($subscription->next_billing_date, false, $this->order)) |
| 53 |
: __('N/A', 'fluent-cart'); |
| 54 |
case 'bill_times': |
| 55 |
return $subscription->bill_times |
| 56 |
? esc_html($subscription->bill_times) |
| 57 |
: __('Unlimited', 'fluent-cart'); |
| 58 |
case 'bill_count': |
| 59 |
return esc_html($subscription->bill_count); |
| 60 |
case 'trial_days': |
| 61 |
return esc_html($subscription->trial_days ?: '0'); |
| 62 |
case 'expire_at': |
| 63 |
return $subscription->expire_at |
| 64 |
? esc_html(DateFormatter::format($subscription->expire_at, false, $this->order)) |
| 65 |
: __('Never', 'fluent-cart'); |
| 66 |
default: |
| 67 |
return Arr::get((array) $subscription, $accessor, ''); |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|