| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\Api; |
| 4 |
|
| 5 |
use FluentCart\App\Modules\PaymentMethods\Core\AbstractPaymentGateway; |
| 6 |
use FluentCart\App\Modules\PaymentMethods\Core\GatewayManager; |
| 7 |
use FluentCart\Framework\Support\Arr; |
| 8 |
|
| 9 |
class FluentCartGeneralApi |
| 10 |
{ |
| 11 |
/** |
| 12 |
* @var FluentCartGeneralApi |
| 13 |
*/ |
| 14 |
private static $instance; |
| 15 |
|
| 16 |
public static function getInstance() |
| 17 |
{ |
| 18 |
if (!self::$instance) { |
| 19 |
self::$instance = new self(); |
| 20 |
} |
| 21 |
|
| 22 |
return self::$instance; |
| 23 |
} |
| 24 |
|
| 25 |
public function addCustomerDashboardEndpoint($slug, $args = []) |
| 26 |
{ |
| 27 |
if (!$slug) { |
| 28 |
throw new \Exception(esc_html__('The endpoint slug cannot be empty.', 'fluent-cart')); |
| 29 |
} |
| 30 |
|
| 31 |
$reserved = ['dashboard', 'purchase-history', 'subscriptions', 'licenses', 'downloads', 'profile']; |
| 32 |
|
| 33 |
if (in_array($slug, $reserved)) { |
| 34 |
throw new \Exception( |
| 35 |
sprintf( |
| 36 |
/* translators: %s: The reserved endpoint slug. */ |
| 37 |
esc_html__( |
| 38 |
'The endpoint slug "%s" is reserved and cannot be used.', |
| 39 |
'fluent-cart' |
| 40 |
), |
| 41 |
esc_html($slug) |
| 42 |
) |
| 43 |
); |
| 44 |
|
| 45 |
} |
| 46 |
|
| 47 |
if (!isset($args['render_callback']) && !isset($args['page_id'])) { |
| 48 |
throw new \Exception(esc_html__('You must provide either a render callback or a page ID for the endpoint.', 'fluent-cart')); |
| 49 |
} |
| 50 |
|
| 51 |
add_filter('fluent_cart/global_customer_menu_items', function ($items) use ($slug, $args) { |
| 52 |
// Add this new item just before the 'profile' item |
| 53 |
$profileKey = array_search('profile', array_keys($items)); |
| 54 |
if ($profileKey !== false) { |
| 55 |
$items = array_slice($items, 0, $profileKey, true) + |
| 56 |
[$slug => [ |
| 57 |
'label' => Arr::get($args, 'title'), |
| 58 |
'css_class' => 'fct-menu-item-' . $slug, |
| 59 |
'link' => \FluentCart\App\Services\URL::getCustomerDashboardUrl($slug), |
| 60 |
'icon_svg' => Arr::get($args, 'icon_svg'), |
| 61 |
'icon_url' => Arr::get($args, 'icon_url'), |
| 62 |
]] + |
| 63 |
array_slice($items, $profileKey, null, true); |
| 64 |
} else { |
| 65 |
// If 'profile' is not found, just append it at the end |
| 66 |
if (!isset($items[$slug])) { |
| 67 |
$items[$slug] = [ |
| 68 |
'label' => Arr::get($args, 'title'), |
| 69 |
'css_class' => 'fct-menu-item-' . $slug, |
| 70 |
'link' => \FluentCart\App\Services\URL::getCustomerDashboardUrl($slug), |
| 71 |
'icon_svg' => Arr::get($args, 'icon_svg'), |
| 72 |
'icon_url' => Arr::get($args, 'icon_url'), |
| 73 |
]; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
return $items; |
| 78 |
}); |
| 79 |
|
| 80 |
add_filter('fluent_cart/customer_portal/custom_endpoints', function ($endPoints) use ($slug, $args) { |
| 81 |
|
| 82 |
if (isset($args['render_callback'])) { |
| 83 |
$endPoints[$slug] = [ |
| 84 |
'render_callback' => $args['render_callback'], |
| 85 |
]; |
| 86 |
} else if (isset($args['page_id'])) { |
| 87 |
$endPoints[$slug] = [ |
| 88 |
'page_id' => $args['page_id'], |
| 89 |
]; |
| 90 |
} |
| 91 |
|
| 92 |
return $endPoints; |
| 93 |
}); |
| 94 |
} |
| 95 |
|
| 96 |
public function registerCustomPaymentMethod($name, $paymentGatewayInstance) |
| 97 |
{ |
| 98 |
if(! $paymentGatewayInstance instanceof AbstractPaymentGateway) { |
| 99 |
throw new \Exception( |
| 100 |
sprintf( |
| 101 |
/* translators: %s: The name of the invalid payment gateway class. */ |
| 102 |
esc_html__( |
| 103 |
'The payment gateway class "%s" is not valid. It must extend AbstractPaymentGateway.', |
| 104 |
'fluent-cart' |
| 105 |
), |
| 106 |
esc_html($paymentGatewayClass) |
| 107 |
) |
| 108 |
); |
| 109 |
|
| 110 |
} |
| 111 |
|
| 112 |
(GatewayManager::getInstance())->register($name, $paymentGatewayInstance); |
| 113 |
} |
| 114 |
|
| 115 |
} |
| 116 |
|