| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Donors\Actions; |
| 4 |
|
| 5 |
use Give\API\REST\V3\Routes\Donors\ValueObjects\DonorRoute; |
| 6 |
use Give\Helpers\IntlTelInput; |
| 7 |
|
| 8 |
/** |
| 9 |
* The purpose of this action is to have a centralized place for localizing options used on many different places |
| 10 |
* by donor scripts (list tables, blocks, etc.) |
| 11 |
* |
| 12 |
* @since 4.6.1 Rename to LoadDonorAdminOptions |
| 13 |
* @since 4.4.0 |
| 14 |
*/ |
| 15 |
class LoadDonorAdminOptions |
| 16 |
{ |
| 17 |
public function __invoke() |
| 18 |
{ |
| 19 |
wp_register_script('give-donor-options', false); |
| 20 |
wp_localize_script('give-donor-options', 'GiveDonorOptions', $this->getDonorOptions()); |
| 21 |
wp_enqueue_script('give-donor-options'); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Get all donor options for localization |
| 26 |
* |
| 27 |
* @return array |
| 28 |
* @since 4.4.0 |
| 29 |
*/ |
| 30 |
private function getDonorOptions(): array |
| 31 |
{ |
| 32 |
$isAdmin = is_admin(); |
| 33 |
|
| 34 |
return [ |
| 35 |
'isAdmin' => $isAdmin, |
| 36 |
'adminUrl' => admin_url(), |
| 37 |
'apiRoot' => rest_url(DonorRoute::NAMESPACE . '/' . DonorRoute::BASE), |
| 38 |
'apiNonce' => wp_create_nonce('wp_rest'), |
| 39 |
'donorsAdminUrl' => admin_url('edit.php?post_type=give_forms&page=give-donors'), |
| 40 |
'currency' => give_get_currency(), |
| 41 |
'intlTelInputSettings' => IntlTelInput::getSettings(), |
| 42 |
'nameTitlePrefixes' => give_get_option('title_prefixes', array_values(give_get_default_title_prefixes())), |
| 43 |
'countries' => $this->decodeHtmlEntities(give_get_country_list()), |
| 44 |
'states' => $this->getStatesData(), |
| 45 |
'isRecurringEnabled' => defined('GIVE_RECURRING_VERSION') ? GIVE_RECURRING_VERSION : null, |
| 46 |
'mode' => give_is_test_mode() ? 'test' : 'live' |
| 47 |
]; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Get states data with decoded HTML entities |
| 52 |
* |
| 53 |
* @since 4.4.0 |
| 54 |
*/ |
| 55 |
private function getStatesData(): array |
| 56 |
{ |
| 57 |
return [ |
| 58 |
'list' => $this->decodeHtmlEntities(give_states_list(), true), |
| 59 |
'labels' => give_get_states_label(), |
| 60 |
'noStatesCountries' => array_keys(give_no_states_country_list()), |
| 61 |
'statesNotRequiredCountries' => array_keys(give_states_not_required_country_list()), |
| 62 |
]; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Decode HTML entities from an array of strings or nested arrays |
| 67 |
* |
| 68 |
* @since 4.4.0 |
| 69 |
*/ |
| 70 |
private function decodeHtmlEntities(array $data, bool $isNested = false): array |
| 71 |
{ |
| 72 |
if ($isNested) { |
| 73 |
return array_map(function ($nestedData) { |
| 74 |
return $this->decodeHtmlEntities($nestedData); |
| 75 |
}, $data); |
| 76 |
} |
| 77 |
|
| 78 |
return array_map(function ($item) { |
| 79 |
return html_entity_decode($item, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, 'UTF-8'); |
| 80 |
}, $data); |
| 81 |
} |
| 82 |
} |
| 83 |
|