headers as $header ) { if ( ! isset( $_SERVER[ $header ] ) ) { continue; } $ip = trim( sanitize_text_field( wp_unslash( $_SERVER[ $header ] ) ) ); if ( empty( $ip ) ) { continue; } // If contains multiple IPs, get the first one. $ips = explode( ',', $ip ); $ip = trim( $ips[0] ); if ( filter_var( $ip, FILTER_VALIDATE_IP ) ) { return $ip; } } return ''; } /** * Check if an IP address is private * * Determines whether the given IP address is in a private range * (including RFC1918 private ranges and reserved ranges). * * @param string $ip The IP address to check. * @return bool True if the IP is private, false otherwise */ public function is_private_ip( string $ip ): bool { return ! filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE ); } }