| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Services\ShortCodeParser\Parsers; |
| 4 |
|
| 5 |
use FluentCart\App\Services\DateTime\DateFormatter; |
| 6 |
use FluentCart\Framework\Support\Arr; |
| 7 |
|
| 8 |
class LicenseParser extends BaseParser |
| 9 |
{ |
| 10 |
private $license; |
| 11 |
|
| 12 |
/** |
| 13 |
* Only used as the timezone context for DateFormatter — the order carries the |
| 14 |
* user_tz captured at checkout. Null when the shortcode runs without one, in |
| 15 |
* which case DateFormatter falls back to UTC. |
| 16 |
*/ |
| 17 |
private $order; |
| 18 |
|
| 19 |
public function __construct($data) |
| 20 |
{ |
| 21 |
$this->license = Arr::get($data, 'license'); |
| 22 |
$this->order = Arr::get($data, 'order'); |
| 23 |
parent::__construct($data); |
| 24 |
} |
| 25 |
|
| 26 |
public function parse($accessor = null, $code = null, $transformer = null): ?string |
| 27 |
{ |
| 28 |
$license = $this->license; |
| 29 |
|
| 30 |
if (!$license) { |
| 31 |
return ''; |
| 32 |
} |
| 33 |
|
| 34 |
switch ($accessor) { |
| 35 |
case 'sl': |
| 36 |
return (string) (isset($license->sl) ? $license->sl : ''); |
| 37 |
case 'key': |
| 38 |
return esc_html($license->license_key); |
| 39 |
case 'status': |
| 40 |
return esc_html($license->status); |
| 41 |
case 'product_name': |
| 42 |
return esc_html($license->product ? $license->product->post_title : ''); |
| 43 |
case 'variant': |
| 44 |
return esc_html($license->productVariant ? $license->productVariant->variation_title : ''); |
| 45 |
case 'limit': |
| 46 |
return $license->limit ? esc_html($license->limit) : __('Unlimited', 'fluent-cart'); |
| 47 |
case 'activation_count': |
| 48 |
return esc_html($license->activation_count); |
| 49 |
case 'expiration_date': |
| 50 |
return $license->expiration_date |
| 51 |
? esc_html(DateFormatter::format($license->expiration_date, false, $this->order)) |
| 52 |
: __('Never', 'fluent-cart'); |
| 53 |
default: |
| 54 |
return Arr::get((array) $license, $accessor, ''); |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|