CalculateProjectedAnnualRevenue.php
9 months ago
GenerateNextRenewalForSubscription.php
3 years ago
LoadSubscriptionAdminOptions.php
9 months ago
LoadSubscriptionDetailsAssets.php
9 months ago
LoadSubscriptionsListTableAssets.php
8 months ago
LoadSubscriptionAdminOptions.php
78 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Subscriptions\Actions; |
| 4 | |
| 5 | use Give\API\REST\V3\Routes\Donations\ValueObjects\DonationRoute; |
| 6 | use Give\BetaFeatures\Facades\FeatureFlag; |
| 7 | use Give\Framework\PaymentGateways\PaymentGateway; |
| 8 | use Give\Subscriptions\ValueObjects\SubscriptionStatus; |
| 9 | |
| 10 | /** |
| 11 | * The purpose of this action is to have a centralized place for localizing options used on many different places |
| 12 | * by donation scripts (list tables, blocks, etc.) |
| 13 | * |
| 14 | * @since 4.8.0 |
| 15 | */ |
| 16 | class LoadSubscriptionAdminOptions |
| 17 | { |
| 18 | public function __invoke() |
| 19 | { |
| 20 | wp_register_script('give-subscription-options', false); |
| 21 | wp_localize_script('give-subscription-options', 'GiveSubscriptionOptions', $this->getSubscriptionOptions()); |
| 22 | wp_enqueue_script('give-subscription-options'); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Get all donation options for localization |
| 27 | * |
| 28 | * @since 4.8.0 |
| 29 | */ |
| 30 | private function getSubscriptionOptions(): array |
| 31 | { |
| 32 | $isAdmin = is_admin(); |
| 33 | |
| 34 | return [ |
| 35 | 'isAdmin' => $isAdmin, |
| 36 | 'adminUrl' => admin_url(), |
| 37 | 'pluginUrl' => GIVE_PLUGIN_URL, |
| 38 | 'apiRoot' => rest_url(DonationRoute::NAMESPACE), |
| 39 | 'legacyApiRoot' => esc_url_raw(rest_url('give-api/v2/admin')), |
| 40 | 'apiNonce' => wp_create_nonce('wp_rest'), |
| 41 | 'syncSubscriptionNonce' => wp_create_nonce( 'sync-subscription-details' ), |
| 42 | 'subscriptionsAdminUrl' => admin_url('edit.php?post_type=give_forms&page=give-subscriptions'), |
| 43 | 'currency' => give_get_currency(), |
| 44 | 'subscriptionStatuses' => SubscriptionStatus::labels(), |
| 45 | 'isRecurringEnabled' => defined('GIVE_RECURRING_VERSION') ? GIVE_RECURRING_VERSION : null, |
| 46 | 'eventTicketsEnabled' => FeatureFlag::eventTickets(), |
| 47 | 'isFeeRecoveryEnabled' => defined('GIVE_FEE_RECOVERY_VERSION'), |
| 48 | 'mode' => give_is_test_mode() ? 'test' : 'live', |
| 49 | 'gateways' => $this->getGateways(), |
| 50 | ]; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Get gateways |
| 55 | * |
| 56 | * @since 4.8.0 |
| 57 | */ |
| 58 | private function getGateways(): array |
| 59 | { |
| 60 | $enabledGateways = array_keys(give_get_enabled_payment_gateways()); |
| 61 | |
| 62 | $gateways = array_map(static function ($gatewayClass) use ($enabledGateways) { |
| 63 | /** @var PaymentGateway $gateway */ |
| 64 | $gateway = give($gatewayClass); |
| 65 | |
| 66 | return [ |
| 67 | 'id' => $gateway::id(), |
| 68 | 'enabled' => in_array($gateway::id(), $enabledGateways, true), |
| 69 | 'label' => $gateway->getName(), |
| 70 | 'supportsSubscriptions' => $gateway->supportsSubscriptions(), |
| 71 | 'supportsRefund' => $gateway->supportsRefund(), |
| 72 | ]; |
| 73 | }, give()->gateways->getPaymentGateways()); |
| 74 | |
| 75 | return array_values($gateways); |
| 76 | } |
| 77 | } |
| 78 |