Make WordPress Core


Ignore:
Timestamp:
05/19/2017 05:48:01 AM (9 years ago)
Author:
azaozz
Message:

Dashboard: Improve the handling of locations determined by geolocating the IP address and by entering a city name. Fix couple of edge cases, and some names.

Props iandunn coreymckrill.
Fixes #40702.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/class-wp-community-events.php

    r40781 r40790  
    9595                }
    9696
    97                 $request_url    = $this->get_request_url( $location_search, $timezone );
    98                 $response       = wp_remote_get( $request_url );
     97                $api_url        = 'https://api.wordpress.org/events/1.0/';
     98                $request_args   = $this->get_request_args( $location_search, $timezone );
     99                $response       = wp_remote_get( $api_url, $request_args );
    99100                $response_code  = wp_remote_retrieve_response_code( $response );
    100101                $response_body  = json_decode( wp_remote_retrieve_body( $response ), true );
    101102                $response_error = null;
    102                 $debugging_info = compact( 'request_url', 'response_code', 'response_body' );
     103                $debugging_info = compact( 'api_url', 'request_args', 'response_code', 'response_body' );
    103104
    104105                if ( is_wp_error( $response ) ) {
     
    129130                        }
    130131
     132                        /*
     133                         * The IP in the response is usually the same as the one that was sent
     134                         * in the request, but in some cases it is different. In those cases,
     135                         * it's important to reset it back to the IP from the request.
     136                         *
     137                         * For example, if the IP sent in the request is private (e.g., 192.168.1.100),
     138                         * then the API will ignore that and use the corresponding public IP instead,
     139                         * and the public IP will get returned. If the public IP were saved, though,
     140                         * then get_cached_events() would always return `false`, because the transient
     141                         * would be generated based on the public IP when saving the cache, but generated
     142                         * based on the private IP when retrieving the cache.
     143                         */
     144                        if ( ! empty( $response_body['location']['ip'] ) ) {
     145                                $response_body['location']['ip'] = $request_args['body']['ip'];
     146                        }
     147
     148                        /*
     149                         * The API doesn't return a description for latitude/longitude requests,
     150                         * but the description is already saved in the user location, so that
     151                         * one can be used instead.
     152                         */
     153                        if ( $this->coordinates_match( $request_args['body'], $response_body['location'] ) && empty( $response_body['location']['description'] ) ) {
     154                                $response_body['location']['description'] = $this->user_location['description'];
     155                        }
     156
    131157                        $this->cache_events( $response_body, $expiration );
    132158
     
    144170
    145171        /**
    146          * Builds a URL for requests to the w.org Events API.
     172         * Builds an array of args to use in an HTTP request to the w.org Events API.
    147173         *
    148174         * @access protected
     
    151177         * @param  string $search   Optional. City search string. Default empty string.
    152178         * @param  string $timezone Optional. Timezone string. Default empty string.
    153          * @return string The request URL.
    154          */
    155         protected function get_request_url( $search = '', $timezone = '' ) {
    156                 $api_url = 'https://api.wordpress.org/events/1.0/';
    157                 $args    = array(
     179         * @return @return array The request args.
     180         */
     181        protected function get_request_args( $search = '', $timezone = '' ) {
     182                $args = array(
    158183                        'number' => 5, // Get more than three in case some get trimmed out.
    159                         'ip'     => $this->get_client_ip(),
     184                        'ip'     => self::get_unsafe_client_ip(),
    160185                );
    161186
    162187                /*
    163                  * Send the minimal set of necessary arguments, in order to increase the
     188                 * Include the minimal set of necessary arguments, in order to increase the
    164189                 * chances of a cache-hit on the API side.
    165190                 */
     
    179204                }
    180205
    181                 return add_query_arg( $args, $api_url );
     206                // Wrap the args in an array compatible with the second parameter of `wp_remote_get()`.
     207                return array(
     208                        'body' => $args
     209                );
    182210        }
    183211
     
    208236         *                      or false on failure.
    209237         */
    210         protected function get_client_ip() {
     238        public static function get_unsafe_client_ip() {
    211239                $client_ip = false;
    212240
     
    251279
    252280        /**
     281         * Test if two pairs of latitude/longitude coordinates match each other.
     282         *
     283         * @since 4.8.0
     284         * @access protected
     285         *
     286         * @param array $a The first pair, with indexes 'latitude' and 'longitude'.
     287         * @param array $b The second pair, with indexes 'latitude' and 'longitude'.
     288         * @return bool True if they match, false if they don't.
     289         */
     290        protected function coordinates_match( $a, $b ) {
     291                if ( ! isset( $a['latitude'], $a['longitude'], $b['latitude'], $b['longitude'] ) ) {
     292                        return false;
     293                }
     294
     295                return $a['latitude'] === $b['latitude'] && $a['longitude'] === $b['longitude'];
     296        }
     297
     298        /**
    253299         * Generates a transient key based on user location.
    254300         *
     
    267313                $key = false;
    268314
    269                 if ( isset( $location['latitude'], $location['longitude'] ) ) {
     315                if ( isset( $location['ip'] ) ) {
     316                        $key = 'community-events-' . md5( $location['ip'] );
     317                } else if ( isset( $location['latitude'], $location['longitude'] ) ) {
    270318                        $key = 'community-events-' . md5( $location['latitude'] . $location['longitude'] );
    271319                }
Note: See TracChangeset for help on using the changeset viewer.