CreateUserFromDonor.php
2 years ago
LoadDonorDetailsAssets.php
1 year ago
LoadDonorOptions.php
1 year ago
LoadDonorsListTableAssets.php
1 year ago
RegisterDonorEntity.php
1 year ago
SendDonorUserRegistrationNotification.php
2 years ago
UpdateAdminDonorDetails.php
2 years ago
LoadDonorOptions.php
84 lines
| 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.4.0 |
| 13 | */ |
| 14 | class LoadDonorOptions |
| 15 | { |
| 16 | public function __invoke() |
| 17 | { |
| 18 | wp_register_script('give-donor-options', false); |
| 19 | wp_localize_script('give-donor-options', 'GiveDonorOptions', $this->getDonorOptions()); |
| 20 | wp_enqueue_script('give-donor-options'); |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Get all donor options for localization |
| 25 | * |
| 26 | * @return array |
| 27 | * @since 4.4.0 |
| 28 | */ |
| 29 | private function getDonorOptions(): array |
| 30 | { |
| 31 | $isAdmin = is_admin(); |
| 32 | |
| 33 | return [ |
| 34 | 'isAdmin' => $isAdmin, |
| 35 | 'adminUrl' => admin_url(), |
| 36 | 'apiRoot' => rest_url(DonorRoute::NAMESPACE . '/' . DonorRoute::BASE), |
| 37 | 'apiNonce' => wp_create_nonce('wp_rest'), |
| 38 | 'donorsAdminUrl' => admin_url('edit.php?post_type=give_forms&page=give-donors'), |
| 39 | 'currency' => give_get_currency(), |
| 40 | 'currencySymbol' => give_currency_symbol(), |
| 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 | 'admin' => $isAdmin ? [] : null, |
| 47 | 'mode' => give_is_test_mode() ? 'test' : 'live' |
| 48 | ]; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Get states data with decoded HTML entities |
| 53 | * |
| 54 | * @since 4.4.0 |
| 55 | */ |
| 56 | private function getStatesData(): array |
| 57 | { |
| 58 | return [ |
| 59 | 'list' => $this->decodeHtmlEntities(give_states_list(), true), |
| 60 | 'labels' => give_get_states_label(), |
| 61 | 'noStatesCountries' => array_keys(give_no_states_country_list()), |
| 62 | 'statesNotRequiredCountries' => array_keys(give_states_not_required_country_list()), |
| 63 | ]; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Decode HTML entities from an array of strings or nested arrays |
| 68 | * |
| 69 | * @since 4.4.0 |
| 70 | */ |
| 71 | private function decodeHtmlEntities(array $data, bool $isNested = false): array |
| 72 | { |
| 73 | if ($isNested) { |
| 74 | return array_map(function($nestedData) { |
| 75 | return $this->decodeHtmlEntities($nestedData); |
| 76 | }, $data); |
| 77 | } |
| 78 | |
| 79 | return array_map(function($item) { |
| 80 | return html_entity_decode($item, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, 'UTF-8'); |
| 81 | }, $data); |
| 82 | } |
| 83 | } |
| 84 |