GeneratePurchaseKey.php
4 years ago
LoadDonationAdminOptions.php
8 months ago
LoadDonationDetailsAssets.php
11 months ago
LoadDonationsListTableAssets.php
8 months ago
LoadDonationAdminOptions.php
117 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Donations\Actions; |
| 4 | |
| 5 | use Give\API\REST\V3\Routes\Donations\ValueObjects\DonationRoute; |
| 6 | use Give\Donations\ValueObjects\DonationStatus; |
| 7 | use Give\Helpers\IntlTelInput; |
| 8 | use Give\BetaFeatures\Facades\FeatureFlag; |
| 9 | use Give\Framework\Database\DB; |
| 10 | use Give\Framework\PaymentGateways\PaymentGateway; |
| 11 | |
| 12 | /** |
| 13 | * The purpose of this action is to have a centralized place for localizing options used on many different places |
| 14 | * by donation scripts (list tables, blocks, etc.) |
| 15 | * |
| 16 | * @since 4.6.1 Rename to LoadDonationAdminOptions |
| 17 | * @since 4.6.0 |
| 18 | */ |
| 19 | class LoadDonationAdminOptions |
| 20 | { |
| 21 | public function __invoke() |
| 22 | { |
| 23 | wp_register_script('give-donation-options', false); |
| 24 | wp_localize_script('give-donation-options', 'GiveDonationOptions', $this->getDonationOptions()); |
| 25 | wp_enqueue_script('give-donation-options'); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Get all donation options for localization |
| 30 | * |
| 31 | * @since 4.7.0 removed donors from the options |
| 32 | * @since 4.6.0 |
| 33 | */ |
| 34 | private function getDonationOptions(): array |
| 35 | { |
| 36 | $isAdmin = is_admin(); |
| 37 | |
| 38 | return [ |
| 39 | 'isAdmin' => $isAdmin, |
| 40 | 'adminUrl' => admin_url(), |
| 41 | 'apiRoot' => rest_url(DonationRoute::NAMESPACE), |
| 42 | 'apiNonce' => wp_create_nonce('wp_rest'), |
| 43 | 'donationsAdminUrl' => admin_url('edit.php?post_type=give_forms&page=give-payment-history'), |
| 44 | 'currency' => give_get_currency(), |
| 45 | 'intlTelInputSettings' => IntlTelInput::getSettings(), |
| 46 | 'nameTitlePrefixes' => give_get_option('title_prefixes', array_values(give_get_default_title_prefixes())), |
| 47 | 'countries' => $this->decodeHtmlEntities(give_get_country_list()), |
| 48 | 'states' => $this->getStatesData(), |
| 49 | 'donationStatuses' => DonationStatus::labels(), |
| 50 | 'isRecurringEnabled' => defined('GIVE_RECURRING_VERSION') ? GIVE_RECURRING_VERSION : null, |
| 51 | 'admin' => $isAdmin ? [] : null, |
| 52 | 'eventTicketsEnabled' => FeatureFlag::eventTickets(), |
| 53 | 'isFeeRecoveryEnabled' => defined('GIVE_FEE_RECOVERY_VERSION'), |
| 54 | 'mode' => give_is_test_mode() ? 'test' : 'live', |
| 55 | 'gateways' => $this->getGateways(), |
| 56 | ]; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Get states data with decoded HTML entities |
| 61 | * |
| 62 | * @since 4.6.0 |
| 63 | */ |
| 64 | private function getStatesData(): array |
| 65 | { |
| 66 | return [ |
| 67 | 'list' => $this->decodeHtmlEntities(give_states_list(), true), |
| 68 | 'labels' => give_get_states_label(), |
| 69 | 'noStatesCountries' => array_keys(give_no_states_country_list()), |
| 70 | 'statesNotRequiredCountries' => array_keys(give_states_not_required_country_list()), |
| 71 | ]; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Decode HTML entities from an array of strings or nested arrays |
| 76 | * |
| 77 | * @since 4.6.0 |
| 78 | */ |
| 79 | private function decodeHtmlEntities(array $data, bool $isNested = false): array |
| 80 | { |
| 81 | if ($isNested) { |
| 82 | return array_map(function($nestedData) { |
| 83 | return $this->decodeHtmlEntities($nestedData); |
| 84 | }, $data); |
| 85 | } |
| 86 | |
| 87 | return array_map(function($item) { |
| 88 | return html_entity_decode($item, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, 'UTF-8'); |
| 89 | }, $data); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Get gateways |
| 94 | * |
| 95 | * @since 4.6.0 |
| 96 | */ |
| 97 | private function getGateways(): array |
| 98 | { |
| 99 | $enabledGateways = array_keys(give_get_enabled_payment_gateways()); |
| 100 | |
| 101 | $gateways = array_map(static function ($gatewayClass) use ($enabledGateways) { |
| 102 | /** @var PaymentGateway $gateway */ |
| 103 | $gateway = give($gatewayClass); |
| 104 | |
| 105 | return [ |
| 106 | 'id' => $gateway::id(), |
| 107 | 'enabled' => in_array($gateway::id(), $enabledGateways, true), |
| 108 | 'label' => $gateway->getName(), |
| 109 | 'supportsSubscriptions' => $gateway->supportsSubscriptions(), |
| 110 | 'supportsRefund' => $gateway->supportsRefund(), |
| 111 | ]; |
| 112 | }, give()->gateways->getPaymentGateways()); |
| 113 | |
| 114 | return array_values($gateways); |
| 115 | } |
| 116 | } |
| 117 |