WC_Geolocation::get_external_ip_address()
Get user IP Address using an external service. This can be used as a fallback for users on localhost where get_ip_address() will be a local IP and non-geolocatable.
Method of the class: WC_Geolocation{}
Hooks from the method
Return
String
.
Usage
$result = WC_Geolocation::get_external_ip_address();
WC_Geolocation::get_external_ip_address() WC Geolocation::get external ip address code WC 7.5.0
public static function get_external_ip_address() { $external_ip_address = '0.0.0.0'; if ( '' !== self::get_ip_address() ) { $transient_name = 'external_ip_address_' . self::get_ip_address(); $external_ip_address = get_transient( $transient_name ); } if ( false === $external_ip_address ) { $external_ip_address = '0.0.0.0'; $ip_lookup_services = apply_filters( 'woocommerce_geolocation_ip_lookup_apis', self::$ip_lookup_apis ); $ip_lookup_services_keys = array_keys( $ip_lookup_services ); shuffle( $ip_lookup_services_keys ); foreach ( $ip_lookup_services_keys as $service_name ) { $service_endpoint = $ip_lookup_services[ $service_name ]; $response = wp_safe_remote_get( $service_endpoint, array( 'timeout' => 2, 'user-agent' => 'WooCommerce/' . wc()->version, ) ); if ( ! is_wp_error( $response ) && rest_is_ip_address( $response['body'] ) ) { $external_ip_address = apply_filters( 'woocommerce_geolocation_ip_lookup_api_response', wc_clean( $response['body'] ), $service_name ); break; } } set_transient( $transient_name, $external_ip_address, DAY_IN_SECONDS ); } return $external_ip_address; }