Skip to content

Commit

Permalink
Update calling code handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mikejolley committed Mar 6, 2019
1 parent fa2515b commit 4330b2e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
1 change: 1 addition & 0 deletions i18n/phone.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@
'DO' => array(
'+1809',
'+1829',
'+1849',
),
'DM' => '+1767',
'DJ' => '+253',
Expand Down
12 changes: 9 additions & 3 deletions includes/class-wc-countries.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,21 @@ public function get_continent_code_for_country( $cc ) {
* @param string $cc Country code.
* @return string
*/
public function get_calling_code_for_country( $cc ) {
public function get_country_calling_code( $cc ) {
$codes = wp_cache_get( 'calling-codes', 'countries' );

if ( ! $codes ) {
$codes = json_decode( file_get_contents( WC()->plugin_path() . '/i18n/phone.json' ), true ); // Data from http://country.io/phone.json.
$codes = include WC()->plugin_path() . '/i18n/phone.php';
wp_cache_set( 'calling-codes', $codes, 'countries' );
}

return isset( $codes[ $cc ] ) ? $codes[ $cc ] : '';
$calling_code = isset( $codes[ $cc ] ) ? $codes[ $cc ] : '';

if ( is_array( $calling_code ) ) {
$calling_code = $calling_code[0];
}

return $calling_code;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ protected function get_phone_number_args( $order ) {
'night_phone_c' => substr( $phone_number, 6, 4 ),
);
} else {
$calling_code = WC()->countries->get_calling_code_for_country( $order->get_billing_country() );
$calling_code = WC()->countries->get_country_calling_code( $order->get_billing_country() );
$phone_number = $calling_code ? preg_replace( '/^0/', '', $order->get_billing_phone() ) : $order->get_billing_phone();
$phone_args = array(
'night_phone_a' => $calling_code,
Expand Down

0 comments on commit 4330b2e

Please sign in to comment.