Making WordPress.org


Ignore:
Timestamp:
02/03/2017 05:22:51 PM (10 years ago)
Author:
tellyworth
Message:

Events: add crude IP location guessing.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/api.wordpress.org/public_html/events/1.0/index.php

    r4865 r4869  
    5858}
    5959
     60function guess_location_from_ip( $dotted_ip, $timezone, $country ) {
     61        global $wpdb;
     62
     63        $long_ip = ip2long( $dotted_ip );
     64        if ( $long_ip === false )
     65                return;
     66
     67        $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM ip2location WHERE ip_to >= %d ORDER BY ip_to ASC LIMIT 1", $long_ip ) );
     68        return $row;
     69}
    6070
    6171function get_location( $args = array() ) {
     
    90100                                        'latitude' => $guess->latitude,
    91101                                        'longitude' => $guess->longitude,
     102                                        'country' => $guess->country,
    92103                                );
     104                }
     105
     106                // IP:
     107                if ( isset( $args['location_data']['ip'] ) ) {
     108                        $guess = guess_location_from_ip( $args['location_data']['ip'], $args['location_data']['timezone'] ?? '', $country_code );
     109                        if ( $guess ) {
     110                                return array(
     111                                        'description' => $guess->ip_city,
     112                                        'latitude' => $guess->ip_latitude,
     113                                        'longitude' => $guess->ip_longitude,
     114                                        'country' => $guess->country_short,
     115                                );
     116                        }
    93117                }
    94118                               
Note: See TracChangeset for help on using the changeset viewer.