'376', 'AE' => '971', 'AF' => '93', 'AG' => '1', 'AI' => '1', 'AL' => '355', 'AM' => '374', 'AO' => '244', 'AR' => '54', 'AS' => '1', 'AT' => '43', 'AU' => '61', 'AW' => '297', 'AZ' => '994', 'BA' => '387', 'BB' => '1', 'BD' => '880', 'BE' => '32', 'BF' => '226', 'BG' => '359', 'BH' => '973', 'BI' => '257', 'BJ' => '229', 'BM' => '1', 'BN' => '673', 'BO' => '591', 'BQ' => '599', 'BR' => '55', 'BS' => '1', 'BT' => '975', 'BW' => '267', 'BY' => '375', 'BZ' => '501', 'CA' => '1', 'CD' => '243', 'CF' => '236', 'CG' => '242', 'CH' => '41', 'CI' => '225', 'CK' => '682', 'CL' => '56', 'CM' => '237', 'CN' => '86', 'CO' => '57', 'CR' => '506', 'CU' => '53', 'CV' => '238', 'CW' => '599', 'CY' => '357', 'CZ' => '420', 'DE' => '49', 'DJ' => '253', 'DK' => '45', 'DM' => '1', 'DO' => '1', 'DZ' => '213', 'EC' => '593', 'EE' => '372', 'EG' => '20', 'ER' => '291', 'ES' => '34', 'ET' => '251', 'FI' => '358', 'FJ' => '679', 'FO' => '298', 'FR' => '33', 'GA' => '241', 'GB' => '44', 'GD' => '1', 'GE' => '995', 'GF' => '594', 'GH' => '233', 'GI' => '350', 'GL' => '299', 'GM' => '220', 'GN' => '224', 'GP' => '590', 'GQ' => '240', 'GR' => '30', 'GT' => '502', 'GU' => '1', 'GW' => '245', 'GY' => '592', 'HK' => '852', 'HN' => '504', 'HR' => '385', 'HT' => '509', 'HU' => '36', 'ID' => '62', 'IE' => '353', 'IL' => '972', 'IN' => '91', 'IQ' => '964', 'IR' => '98', 'IS' => '354', 'IT' => '39', 'JM' => '1', 'JO' => '962', 'JP' => '81', 'KE' => '254', 'KG' => '996', 'KH' => '855', 'KN' => '1', 'KR' => '82', 'KW' => '965', 'KY' => '1', 'KZ' => '7', 'LA' => '856', 'LB' => '961', 'LC' => '1', 'LI' => '423', 'LK' => '94', 'LR' => '231', 'LS' => '266', 'LT' => '370', 'LU' => '352', 'LV' => '371', 'LY' => '218', 'MA' => '212', 'MC' => '377', 'MD' => '373', 'ME' => '382', 'MG' => '261', 'MK' => '389', 'ML' => '223', 'MM' => '95', 'MN' => '976', 'MO' => '853', 'MQ' => '596', 'MR' => '222', 'MT' => '356', 'MU' => '230', 'MV' => '960', 'MW' => '265', 'MX' => '52', 'MY' => '60', 'MZ' => '258', 'NA' => '264', 'NC' => '687', 'NE' => '227', 'NG' => '234', 'NI' => '505', 'NL' => '31', 'NO' => '47', 'NP' => '977', 'NZ' => '64', 'OM' => '968', 'PA' => '507', 'PE' => '51', 'PF' => '689', 'PG' => '675', 'PH' => '63', 'PK' => '92', 'PL' => '48', 'PR' => '1', 'PS' => '970', 'PT' => '351', 'PY' => '595', 'QA' => '974', 'RE' => '262', 'RO' => '40', 'RS' => '381', 'RU' => '7', 'RW' => '250', 'SA' => '966', 'SC' => '248', 'SD' => '249', 'SE' => '46', 'SG' => '65', 'SI' => '386', 'SK' => '421', 'SL' => '232', 'SM' => '378', 'SN' => '221', 'SO' => '252', 'SR' => '597', 'SV' => '503', 'SX' => '1', 'SY' => '963', 'TC' => '1', 'TG' => '228', 'TH' => '66', 'TJ' => '992', 'TN' => '216', 'TR' => '90', 'TT' => '1', 'TW' => '886', 'TZ' => '255', 'UA' => '380', 'UG' => '256', 'US' => '1', 'UY' => '598', 'UZ' => '998', 'VC' => '1', 'VE' => '58', 'VG' => '1', 'VI' => '1', 'VN' => '84', 'YE' => '967', 'ZA' => '27', 'ZM' => '260', 'ZW' => '263', ); /** Countries whose national numbers keep their leading 0 after the country code. */ const KEEP_ZERO = array( 'IT', 'SM', 'VA' ); /** As typed, with the country code shown when it had to be added: "62172024" → "+507 62172024". */ public static function display( $phone, $country = '' ) { $phone = trim( (string) $phone ); $intl = self::international( $phone, $country ); if ( '' === $intl || '+' === substr( $phone, 0, 1 ) || 0 === strpos( preg_replace( '/\D/', '', $phone ), '00' ) ) { return $phone; } $national = ltrim( preg_replace( '/\D/', '', $phone ), '0' ); if ( $intl === preg_replace( '/\D/', '', $phone ) ) { return '+' . $phone; // Typed with the country code but without "+". } return '+' . substr( $intl, 0, strlen( $intl ) - strlen( $national ) ) . ' ' . ltrim( $phone, '0' ); } /** The site's own country: WooCommerce store country, else the site language's region (es_PA → PA). */ public static function site_country() { $c = ''; $woo = (string) get_option( 'woocommerce_default_country', '' ); if ( '' !== $woo ) { $c = strtoupper( substr( $woo, 0, 2 ) ); } elseif ( 'en_US' !== get_locale() && preg_match( '/^[a-z]{2,3}_([A-Z]{2})/', (string) get_locale(), $m ) ) { // en_US is WordPress's default, not a statement about where the business is. $c = $m[1]; } return (string) apply_filters( 'nodebook_default_country', $c ); } /** * Digits in international form without "+" (e.g. "50762172024"), or '' when it can't be made * reliable (too short, or no country code known for a national number). */ public static function international( $phone, $country = '' ) { $raw = trim( (string) $phone ); if ( '' === $raw ) { return ''; } $digits = preg_replace( '/\D/', '', $raw ); if ( strlen( $digits ) < 6 || strlen( $digits ) > 15 ) { return ''; } if ( '+' === $raw[0] ) { return $digits; } if ( 0 === strpos( $digits, '00' ) ) { return substr( $digits, 2 ); } $country = strtoupper( (string) $country ); if ( ! isset( self::CODES[ $country ] ) ) { $country = self::site_country(); } if ( ! isset( self::CODES[ $country ] ) ) { return ''; } $code = self::CODES[ $country ]; // Already includes the country code (e.g. "507 6217 2024" typed without +). if ( 0 === strpos( $digits, $code ) && strlen( $digits ) - strlen( $code ) >= 7 && strlen( $digits ) >= 10 ) { return $digits; } if ( '0' === $digits[0] && ! in_array( $country, self::KEEP_ZERO, true ) ) { $digits = ltrim( $digits, '0' ); } // North American numbers are always 10 digits; anything else is not one of them. if ( '1' === $code && 10 !== strlen( $digits ) ) { return ''; } return $code . $digits; } }