-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: add phone number to donor profile (#7331)
- Loading branch information
1 parent
5386b28
commit 4174ebf
Showing
12 changed files
with
330 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2624,3 +2624,117 @@ function give_get_page_by_title(string $page_title, string $output = OBJECT, str | |
|
||
return get_post($pages[0], $output); | ||
} | ||
|
||
/** | ||
* @see https://github.com/jackocnr/intl-tel-input | ||
* | ||
* @unreleased | ||
*/ | ||
function give_get_intl_tel_input(string $value, string $id, string $class = '', string $name = ''):string { | ||
|
||
if (empty($name)) { | ||
$name = $id; | ||
} | ||
|
||
$styleUrl = 'https://cdn.jsdelivr.net/npm/[email protected]/build/css/intlTelInput.css'; | ||
$scriptUrl = 'https://cdn.jsdelivr.net/npm/[email protected]/build/js/intlTelInput.min.js'; | ||
$utilsScriptUrl = 'https://cdn.jsdelivr.net/npm/[email protected]/build/js/utils.js'; | ||
|
||
ob_start(); | ||
|
||
?> | ||
<script src="<?php echo $scriptUrl; ?>"></script> | ||
<link rel="stylesheet" href="<?php echo $styleUrl; ?>"> | ||
|
||
<input id="<?php echo $id; ?>" class="<?php echo $class; ?>" name="<?php echo $name; ?>" value="<?php echo $value; ?>" type='text'> | ||
<span id="<?php echo $id . '--error-msg'; ?>" class="give-intl-tel-input-hide" style="color:red;"></span> | ||
|
||
<style> | ||
.give-intl-tel-input-hide { | ||
display: none !important; | ||
} | ||
.give-intl-tel-input-error { | ||
border: 1px solid red !important; | ||
} | ||
</style> | ||
<script> | ||
if (document.readyState !== 'loading') { | ||
readyHandler(); | ||
} else { | ||
document.addEventListener('DOMContentLoaded', readyHandler); | ||
} | ||
function readyHandler() { | ||
const input = document.querySelector("#<?php echo $id; ?>"); | ||
const iti = window.intlTelInput(input, { | ||
utilsScript: "<?php echo $utilsScriptUrl; ?>", | ||
hiddenInput: function(telInputName) { | ||
return { | ||
phone: "<?php echo $id . '--international-format'; ?>", | ||
country: "<?php echo $id . '--country-code'; ?>" | ||
}; | ||
}, | ||
initialCountry: "<?php echo strtolower(give_get_country()); ?>", | ||
showSelectedDialCode: true, | ||
strictMode: false, | ||
i18n: <?php echo give_get_intl_tel_input_i18n_json_object(); ?> | ||
}); | ||
|
||
const errorMsg = document.querySelector("#<?php echo $id . '--error-msg'; ?>"); | ||
const errorMap = [ | ||
"<?php echo __('Invalid number', 'give'); ?>", | ||
"<?php echo __('Invalid country code', 'give'); ?>", | ||
"<?php echo __('Too short', 'give'); ?>", | ||
"<?php echo __('Too long', 'give'); ?>", | ||
"<?php echo __('Invalid number', 'give'); ?>", | ||
]; | ||
|
||
const reset = () => { | ||
input.classList.remove("give-intl-tel-input-error"); | ||
errorMsg.innerHTML = ""; | ||
errorMsg.classList.add("give-intl-tel-input-hide"); | ||
}; | ||
|
||
const showError = (msg) => { | ||
input.classList.add("give-intl-tel-input-error"); | ||
errorMsg.innerHTML = msg; | ||
errorMsg.classList.remove("give-intl-tel-input-hide"); | ||
}; | ||
|
||
input.addEventListener('change', reset); | ||
input.addEventListener('keyup', reset); | ||
input.form.addEventListener("submit", function(e) { | ||
if (input.value.trim() && !iti.isValidNumber()) { | ||
e.preventDefault(); | ||
const errorCode = iti.getValidationError(); | ||
const msg = errorMap[errorCode] || errorMap[0]; | ||
showError(msg); | ||
return false; | ||
} | ||
}); | ||
} | ||
</script> | ||
<?php | ||
|
||
return ob_get_clean(); | ||
} | ||
|
||
/** | ||
* @unreleased | ||
*/ | ||
function give_get_intl_tel_input_i18n_json_object() { | ||
|
||
$countryList = array_change_key_case(give_get_country_list()); | ||
array_shift($countryList); // Remove first empty item from the country list | ||
|
||
$i18n = array_merge($countryList, [ | ||
'selectedCountryAriaLabel' => __('Selected country', 'give'), // Aria label for the selected country element | ||
'noCountrySelected' => __('No country selected', 'give'), // Screen reader text for when no country is selected | ||
'countryListAriaLabel' => __('List of countries', 'give'), // Aria label for the country list element | ||
'searchPlaceholder' => __('Search', 'give'), // Placeholder for the search input in the dropdown (when countrySearch enabled) | ||
'zeroSearchResults' => __('No results found', 'give'), // Screen reader text for when the search produces no results | ||
'oneSearchResult' => __('1 result found', 'give'), // Screen reader text for when the search produces 1 result | ||
'multipleSearchResults' => __('${count} results found', 'give'), // Screen reader text for when the search produces multiple results, where ${count} will be replaced by the count | ||
]); | ||
|
||
return json_encode($i18n); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Give\Donors\Actions; | ||
|
||
use Exception; | ||
use Give\Donors\Models\Donor; | ||
|
||
/** | ||
* This class allows us to use the Donor model to update data from the donor profile admin legacy page without adding new code directly to the legacy codebase. | ||
* | ||
* @unreleased | ||
*/ | ||
class UpdateAdminDonorDetails | ||
{ | ||
/** | ||
* @unreleased | ||
* | ||
* @throws Exception | ||
*/ | ||
public function __invoke(array $args, int $donorId) | ||
{ | ||
$donor = Donor::find($donorId); | ||
$donor->phone = $args['give_donor_phone_number--international-format']; | ||
$donor->save(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace Give\Donors\Migrations; | ||
|
||
use Give\Framework\Database\Exceptions\DatabaseQueryException; | ||
use Give\Framework\Migrations\Contracts\Migration; | ||
use Give\Framework\Migrations\Exceptions\DatabaseMigrationException; | ||
|
||
/** | ||
* @unreleased | ||
*/ | ||
class AddPhoneColumn extends Migration | ||
{ | ||
/** | ||
* @unreleased | ||
* | ||
* @throws DatabaseMigrationException | ||
*/ | ||
public function run() | ||
{ | ||
global $wpdb; | ||
|
||
$donorsTableName = "{$wpdb->prefix}give_donors"; | ||
|
||
try { | ||
maybe_add_column( | ||
$donorsTableName, | ||
'phone', | ||
"ALTER TABLE `$donorsTableName` ADD COLUMN `phone` varchar(50) NOT NULL DEFAULT '' AFTER `name`" | ||
); | ||
} catch (DatabaseQueryException $exception) { | ||
throw new DatabaseMigrationException('An error occurred adding the phone column to the donors table', | ||
0, $exception); | ||
} | ||
} | ||
|
||
/** | ||
* @unreleased | ||
*/ | ||
public static function id(): string | ||
{ | ||
return 'donors-add-phone-column'; | ||
} | ||
|
||
/** | ||
* @unreleased | ||
*/ | ||
public static function title(): string | ||
{ | ||
return 'Add phone column to donors table'; | ||
} | ||
|
||
/** | ||
* @unreleased | ||
*/ | ||
public static function timestamp() | ||
{ | ||
return strtotime('2024-26-03'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.