mirror of
https://gitlab.com/ArkHost/WP-Security-Pack.git
synced 2026-09-19 17:37:30 +02:00
v1.0
This commit is contained in:
@@ -464,6 +464,55 @@ class WPSP_Helper {
|
||||
return $slug;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the server's public IP address.
|
||||
*
|
||||
* Uses external service to determine the server's outbound IP.
|
||||
* Result is cached for 1 hour to avoid excessive external requests.
|
||||
*
|
||||
* @return string|null Server IP or null on failure.
|
||||
*/
|
||||
public static function get_server_ip() {
|
||||
$cached = get_transient( 'wpsp_server_ip' );
|
||||
if ( false !== $cached ) {
|
||||
return $cached;
|
||||
}
|
||||
|
||||
// Try multiple services for reliability.
|
||||
$services = array(
|
||||
'https://api.ipify.org',
|
||||
'https://ifconfig.me/ip',
|
||||
'https://icanhazip.com',
|
||||
);
|
||||
|
||||
$ip = null;
|
||||
|
||||
foreach ( $services as $service ) {
|
||||
$response = wp_remote_get(
|
||||
$service,
|
||||
array(
|
||||
'timeout' => 5,
|
||||
'sslverify' => true,
|
||||
)
|
||||
);
|
||||
|
||||
if ( ! is_wp_error( $response ) && 200 === wp_remote_retrieve_response_code( $response ) ) {
|
||||
$body = trim( wp_remote_retrieve_body( $response ) );
|
||||
if ( filter_var( $body, FILTER_VALIDATE_IP ) ) {
|
||||
$ip = $body;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( $ip ) {
|
||||
// Cache for 1 hour.
|
||||
set_transient( 'wpsp_server_ip', $ip, HOUR_IN_SECONDS );
|
||||
}
|
||||
|
||||
return $ip;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get country flag emoji from country code.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user