← Back
Editing: IntlTelInput.php
<?php namespace Give\Helpers; /** * @since 3.9.0 * * @see https://github.com/jackocnr/intl-tel-input */ class IntlTelInput { /** * @since 4.19.1 */ private const VERSION = '21.2.4'; /** * @since 4.20.0 Serve the file from the plugin's build folder instead of the jsDelivr CDN. * @since 3.9.0 */ public static function getCssUrl(): string { return GIVE_PLUGIN_URL . 'build/assets/dist/css/intl-tel-input.css'; } /** * @since 4.20.0 Serve the file from the plugin's build folder instead of the jsDelivr CDN. * @since 3.9.0 */ public static function getScriptUrl(): string { return GIVE_PLUGIN_URL . 'build/assets/dist/js/intl-tel-input/intlTelInput.min.js'; } /** * @since 4.20.0 Serve the file from the plugin's build folder instead of the jsDelivr CDN, with the library version as a query string. * @since 3.9.0 */ public static function getUtilsScriptUrl(): string { return GIVE_PLUGIN_URL . 'build/assets/dist/js/intl-tel-input/utils.js?ver=' . self::VERSION; } /** * @since 3.9.0 */ public static function getI18n(): array { $countryList = array_change_key_case(give_get_country_list()); array_shift($countryList); // Remove first empty item from the country list $i18n = array_merge($countryList, [ // Aria label for the selected country element 'selectedCountryAriaLabel' => __('Selected country', 'give'), // Screen reader text for when no country is selected 'noCountrySelected' => __('No country selected', 'give'), // Aria label for the country list element 'countryListAriaLabel' => __('List of countries', 'give'), // Placeholder for the search input in the dropdown (when countrySearch enabled) 'searchPlaceholder' => __('Search', 'give'), // Screen reader text for when the search produces no results 'zeroSearchResults' => __('No results found', 'give'), // Screen reader text for when the search produces 1 result 'oneSearchResult' => __('1 result found', 'give'), // Screen reader text for when the search produces multiple results, where ${count} will be replaced by the count 'multipleSearchResults' => __('${count} results found', 'give'), ]); return $i18n; } /** * @since 3.9.0 */ public static function getErrorMap(): array { return [ __('Invalid number.', 'give'), __('Invalid country code.', 'give'), __('Invalid number: too short.', 'give'), __('Invalid number: too long.', 'give'), __('Invalid number.', 'give'), ]; } /** * @since 3.9.0 */ public static function getInitialCountry(): string { return strtolower(give_get_country()); } /** * @since 3.9.0 */ public static function getShowSelectedDialCode(): bool { return true; } /** * @since 3.9.0 */ public static function getStrictMode(): bool { return true; } /** * @since 3.9.0 */ public static function getUseFullscreenPopup(): bool { return false; } /** * @since 3.9.0 */ public static function getSettings(): array { return [ 'initialCountry' => self::getInitialCountry(), 'showSelectedDialCode' => self::getShowSelectedDialCode(), 'strictMode' => self::getStrictMode(), 'i18n' => self::getI18n(), 'cssUrl' => self::getCssUrl(), 'scriptUrl' => self::getScriptUrl(), 'utilsScriptUrl' => self::getUtilsScriptUrl(), 'errorMap' => self::getErrorMap(), 'useFullscreenPopup' => self::getUseFullscreenPopup(), ]; } /** * @since 4.19.1 Escape output. Load the script and style with the WordPress loader. * @since 4.16.4 Escaped the value attribute output. * @since 3.9.0 */ public static function getHtmlInput(string $value, string $id, string $class = '', string $name = ''): string { if (empty($name)) { $name = $id; } self::enqueueAssets($id); ob_start(); ?> <input id="<?php echo esc_attr($id . '--intl_tel_input'); ?>" class="<?php echo esc_attr($class); ?>" name="<?php echo esc_attr($name); ?>" value="<?php echo esc_attr($value); ?>" type='text'> <span id="<?php echo esc_attr($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> <?php return ob_get_clean(); } /** * Enqueues the library and adds the script that sets up the input. * * @since 4.19.1 */ private static function enqueueAssets(string $id): void { $handle = 'givewp-intl-tel-input'; wp_enqueue_style($handle, self::getCssUrl(), [], self::VERSION); wp_enqueue_script($handle, self::getScriptUrl(), [], self::VERSION, true); wp_add_inline_script($handle, self::getInitScript($id)); } /** * @since 4.19.1 */ private static function getInitScript(string $id): string { ob_start(); ?> (function () { if (document.readyState !== 'loading') { readyHandler(); } else { document.addEventListener('DOMContentLoaded', readyHandler); } function readyHandler() { const input = document.querySelector("#<?php echo esc_js($id . '--intl_tel_input'); ?>"); const intl = window.intlTelInput(input, { utilsScript: "<?php echo esc_url(self::getUtilsScriptUrl()); ?>", hiddenInput: function (telInputName) { return { phone: <?php echo wp_json_encode($id, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT); ?>, }; }, initialCountry: "<?php echo esc_js(self::getInitialCountry()); ?>", showSelectedDialCode: Boolean(<?php echo (int) self::getShowSelectedDialCode(); ?>), useFullscreenPopup: Boolean(<?php echo (int) self::getUseFullscreenPopup(); ?>), strictMode: Boolean(<?php echo (int) self::getStrictMode(); ?>), i18n: <?php echo json_encode(self::getI18n()); ?>, }); const errorMsg = document.querySelector("#<?php echo esc_js($id . '--error-msg'); ?>"); const errorMap = <?php echo json_encode(self::getErrorMap()); ?>; const resetErrorMessage = () => { input.classList.remove("give-intl-tel-input-error"); errorMsg.innerHTML = ""; errorMsg.classList.add("give-intl-tel-input-hide"); }; const showErrorMessage = (msg) => { input.classList.add("give-intl-tel-input-error"); errorMsg.innerHTML = msg; errorMsg.classList.remove("give-intl-tel-input-hide"); }; input.addEventListener('change', resetErrorMessage); input.addEventListener('keyup', resetErrorMessage); input.form.addEventListener("submit", function (e) { if (input.value.trim() && !intl.isValidNumber()) { e.preventDefault(); const errorCode = intl.getValidationError(); const msg = errorMap[errorCode] || errorMap[0]; showErrorMessage(msg); return false; } }); } })(); <?php return ob_get_clean(); } }
Save File
Cancel