GeneratePurchaseKey.php
4 years ago
LoadDonationAdminOptions.php
11 months ago
LoadDonationDetailsAssets.php
11 months ago
LoadDonationsListTableAssets.php
11 months ago
RegisterDonationEntity.php
11 months ago
LoadDonationAdminOptions.php
193 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 | * @return array |
| 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 | 'campaignsWithForms' => $this->getCampaignsWithForms(), |
| 51 | 'donors' => $this->getDonors(), |
| 52 | 'isRecurringEnabled' => defined('GIVE_RECURRING_VERSION') ? GIVE_RECURRING_VERSION : null, |
| 53 | 'admin' => $isAdmin ? [] : null, |
| 54 | 'eventTicketsEnabled' => FeatureFlag::eventTickets(), |
| 55 | 'isFeeRecoveryEnabled' => defined('GIVE_FEE_RECOVERY_VERSION'), |
| 56 | 'mode' => give_is_test_mode() ? 'test' : 'live', |
| 57 | 'gateways' => $this->getGateways(), |
| 58 | ]; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Get states data with decoded HTML entities |
| 63 | * |
| 64 | * @since 4.6.0 |
| 65 | */ |
| 66 | private function getStatesData(): array |
| 67 | { |
| 68 | return [ |
| 69 | 'list' => $this->decodeHtmlEntities(give_states_list(), true), |
| 70 | 'labels' => give_get_states_label(), |
| 71 | 'noStatesCountries' => array_keys(give_no_states_country_list()), |
| 72 | 'statesNotRequiredCountries' => array_keys(give_states_not_required_country_list()), |
| 73 | ]; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Get campaigns with their forms using Campaign query builder |
| 78 | * |
| 79 | * @since 4.6.0 |
| 80 | */ |
| 81 | private function getCampaignsWithForms(): array |
| 82 | { |
| 83 | $campaignsWithForms = []; |
| 84 | |
| 85 | $results = DB::table('give_campaigns', 'campaigns') |
| 86 | ->select( |
| 87 | ['campaigns.id', 'campaignId'], |
| 88 | ['campaigns.campaign_title', 'campaignTitle'], |
| 89 | ['campaigns.form_id', 'defaultFormId'], |
| 90 | ['posts.ID', 'formId'], |
| 91 | ['posts.post_title', 'formTitle'] |
| 92 | ) |
| 93 | ->leftJoin('give_campaign_forms', 'campaigns.id', 'campaign_forms.campaign_id', 'campaign_forms') |
| 94 | ->leftJoin('posts', 'campaign_forms.form_id', 'posts.ID', 'posts') |
| 95 | ->where('posts.post_type', 'give_forms') |
| 96 | ->orderBy('campaigns.id', 'DESC') |
| 97 | ->orderBy('posts.ID', 'DESC') |
| 98 | ->getAll(ARRAY_A); |
| 99 | |
| 100 | foreach ($results as $row) { |
| 101 | [ |
| 102 | 'campaignId' => $campaignId, |
| 103 | 'campaignTitle' => $campaignTitle, |
| 104 | 'defaultFormId' => $defaultFormId, |
| 105 | 'formId' => $formId, |
| 106 | 'formTitle' => $formTitle |
| 107 | ] = $row; |
| 108 | |
| 109 | if (!isset($campaignsWithForms[$campaignId])) { |
| 110 | $campaignsWithForms[$campaignId] = [ |
| 111 | 'title' => $campaignTitle, |
| 112 | 'defaultFormId' => $defaultFormId, |
| 113 | 'forms' => [] |
| 114 | ]; |
| 115 | } |
| 116 | |
| 117 | if ($formId && $formTitle) { |
| 118 | $campaignsWithForms[$campaignId]['forms'][$formId] = $formTitle; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | return $campaignsWithForms; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Decode HTML entities from an array of strings or nested arrays |
| 127 | * |
| 128 | * @since 4.6.0 |
| 129 | */ |
| 130 | private function decodeHtmlEntities(array $data, bool $isNested = false): array |
| 131 | { |
| 132 | if ($isNested) { |
| 133 | return array_map(function($nestedData) { |
| 134 | return $this->decodeHtmlEntities($nestedData); |
| 135 | }, $data); |
| 136 | } |
| 137 | |
| 138 | return array_map(function($item) { |
| 139 | return html_entity_decode($item, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, 'UTF-8'); |
| 140 | }, $data); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Get donor ids with their names |
| 145 | * |
| 146 | * @since 4.6.0 |
| 147 | */ |
| 148 | private function getDonors(): array |
| 149 | { |
| 150 | $results = DB::table('give_donors', 'donors') |
| 151 | ->select( |
| 152 | ['donors.id', 'donorId'], |
| 153 | ['donors.name', 'donorName'], |
| 154 | ['donors.email', 'donorEmail'], |
| 155 | ) |
| 156 | ->orderBy('donors.id', 'DESC') |
| 157 | ->getAll(ARRAY_A); |
| 158 | |
| 159 | $donors = []; |
| 160 | |
| 161 | foreach ($results as $row) { |
| 162 | $donors[$row['donorId']] = $row['donorName'] . ' (' . $row['donorEmail'] . ')'; |
| 163 | } |
| 164 | |
| 165 | return $donors; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Get gateways |
| 170 | * |
| 171 | * @since 4.6.0 |
| 172 | */ |
| 173 | private function getGateways(): array |
| 174 | { |
| 175 | $enabledGateways = array_keys(give_get_enabled_payment_gateways()); |
| 176 | |
| 177 | $gateways = array_map(static function ($gatewayClass) use ($enabledGateways) { |
| 178 | /** @var PaymentGateway $gateway */ |
| 179 | $gateway = give($gatewayClass); |
| 180 | |
| 181 | return [ |
| 182 | 'id' => $gateway::id(), |
| 183 | 'enabled' => in_array($gateway::id(), $enabledGateways, true), |
| 184 | 'label' => $gateway->getName(), |
| 185 | 'supportsSubscriptions' => $gateway->supportsSubscriptions(), |
| 186 | 'supportsRefund' => $gateway->supportsRefund(), |
| 187 | ]; |
| 188 | }, give()->gateways->getPaymentGateways()); |
| 189 | |
| 190 | return array_values($gateways); |
| 191 | } |
| 192 | } |
| 193 |