customer_ip_address = new Customer_Ip_Address(); $this->helper = new Plugin_Helper(); } /** * Gets the country of the customer from ip Address * * @since 1.5.0 * * @return string Country of the customer */ public function get_country_iso_code() { $country_code = ""; $reader = $this->get_reader(); if ( $reader ) { try { $ip = $this->customer_ip_address->get(); $record = $reader->country( $ip ); $country_code = $record->country->isoCode; //In the ISO code list, "GB" is used to refer to "UK" but since in Amazon we call it UK, so override that $country_code = $country_code === "GB" ? "UK" : $country_code; } catch ( \Exception $e ) { error_log( "Customer_Country:get_country_iso_code failed." . $e->getMessage() ); } $reader->close(); } return $country_code; } /** * Gets the instance of reader class of maxmind with GeoLiteCountryDB * * @since 1.5.0 * * @return Reader Instance of Reader class of Maxmind */ private function get_reader() { $reader = null; try { $maxmind_db_file_path = $this->get_maxmind_db_file_path(); if ( ! empty( $maxmind_db_file_path ) ) { $reader = new Reader( $maxmind_db_file_path ); } } catch ( \Exception $e ) { error_log( "Customer_Country:get_reader failed." . $e->getMessage() ); } return $reader; } /** * Gets the maxmind db file name with complete path * * @since 1.5.0 * * @return String Maxmind db file name with complete path if file is readable else null */ private function get_maxmind_db_file_path() { $maxmind_db_file_path = get_option( Db_Constants::CUSTOM_UPLOAD_PATH ) . Plugin_Constants::MAXMIND_DATA_FILENAME; return ( file_exists( $maxmind_db_file_path ) && is_readable( $maxmind_db_file_path ) ) ? $maxmind_db_file_path : null; } } ?>