$parsed['device_type'], 'browser_name' => $parsed['browser_name'], 'visitor_country' => $geo['visitor_country'], 'visitor_region' => $geo['visitor_region'], ); } public static function get_client_ip() { $candidates = array( 'HTTP_CF_CONNECTING_IP', 'HTTP_X_REAL_IP', 'HTTP_X_FORWARDED_FOR', 'REMOTE_ADDR', ); foreach ( $candidates as $key ) { if ( empty( $_SERVER[ $key ] ) ) { continue; } $raw = sanitize_text_field( wp_unslash( $_SERVER[ $key ] ) ); if ( 'HTTP_X_FORWARDED_FOR' === $key ) { $parts = array_map( 'trim', explode( ',', $raw ) ); foreach ( $parts as $part ) { if ( self::is_public_ip( $part ) ) { return $part; } } continue; } if ( self::is_public_ip( $raw ) ) { return $raw; } } return ''; } public static function parse_user_agent( $user_agent ) { $user_agent = (string) $user_agent; if ( '' === $user_agent ) { return array( 'device_type' => '', 'browser_name' => '', ); } $device = 'desktop'; if ( preg_match( '/ipad|tablet|playbook|silk|(android(?!.*mobile))/i', $user_agent ) ) { $device = 'tablet'; } elseif ( preg_match( '/mobile|iphone|ipod|android.*mobile|windows phone|blackberry/i', $user_agent ) ) { $device = 'mobile'; } $browser = 'Unknown'; if ( preg_match( '/EdgA|EdgiOS|Edg\//i', $user_agent ) ) { $browser = ( 'mobile' === $device ) ? 'Edge Mobile' : 'Edge'; } elseif ( preg_match( '/OPR\/|Opera/i', $user_agent ) ) { $browser = ( 'mobile' === $device ) ? 'Opera Mobile' : 'Opera'; } elseif ( preg_match( '/SamsungBrowser/i', $user_agent ) ) { $browser = 'Samsung Internet'; } elseif ( preg_match( '/CriOS/i', $user_agent ) ) { $browser = 'Chrome Mobile'; } elseif ( preg_match( '/Chrome/i', $user_agent ) ) { $browser = ( 'mobile' === $device || 'tablet' === $device ) ? 'Chrome Mobile' : 'Chrome'; } elseif ( preg_match( '/FxiOS/i', $user_agent ) ) { $browser = 'Firefox Mobile'; } elseif ( preg_match( '/Firefox/i', $user_agent ) ) { $browser = ( 'mobile' === $device ) ? 'Firefox Mobile' : 'Firefox'; } elseif ( preg_match( '/iPhone|iPad|iPod/i', $user_agent ) && preg_match( '/Safari/i', $user_agent ) ) { $browser = ( 'desktop' === $device ) ? 'Safari' : 'Mobile Safari'; } elseif ( preg_match( '/Safari/i', $user_agent ) ) { $browser = 'Safari'; } return array( 'device_type' => $device, 'browser_name' => $browser, ); } /** * Resolves the visitor's country from the Cloudflare geo header, when present. * * This reads the inbound HTTP_CF_IPCOUNTRY request header only -- it never sends * the visitor's IP (or any data) to a third-party service. Region is not * available from a request header, so it is always empty. */ public static function get_geo() { $country = ''; if ( ! empty( $_SERVER['HTTP_CF_IPCOUNTRY'] ) ) { $country = self::country_code_to_name( sanitize_text_field( wp_unslash( $_SERVER['HTTP_CF_IPCOUNTRY'] ) ) ); } return array( 'visitor_country' => $country, 'visitor_region' => '', ); } private static function country_code_to_name( $code ) { $code = strtoupper( trim( (string) $code ) ); if ( '' === $code || 'XX' === $code || 'T1' === $code ) { return ''; } if ( function_exists( 'locale_get_display_region' ) ) { $name = locale_get_display_region( 'und_' . $code, 'en' ); if ( $name && $name !== $code ) { return $name; } } $map = array( 'AE' => 'United Arab Emirates', 'SA' => 'Saudi Arabia', 'OM' => 'Oman', 'QA' => 'Qatar', 'BH' => 'Bahrain', 'KW' => 'Kuwait', 'US' => 'United States', 'GB' => 'United Kingdom', 'IN' => 'India', 'PK' => 'Pakistan', ); return $map[ $code ] ?? $code; } private static function is_public_ip( $ip ) { return (bool) filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE ); } }