Make WordPress Core

Ticket #40702: 40702-geoip.2.diff

File 40702-geoip.2.diff, 4.8 KB (added by iandunn, 7 years ago)

Work in progress to provide better UX for geolocated IP locations

  • src/wp-admin/includes/class-wp-community-events.php

    diff --git src/wp-admin/includes/class-wp-community-events.php src/wp-admin/includes/class-wp-community-events.php
    index d3c6f08699..49c2af8c8e 100644
    class WP_Community_Events { 
    143143         */
    144144        protected function get_request_url( $search = '', $timezone = '' ) {
    145145                $api_url = 'https://api.wordpress.org/events/1.0/';
    146                 $args    = array( 'number' => 5 ); // Get more than three in case some get trimmed out.
     146                $args    = array(
     147                        // Get more than three in case some get trimmed out.
     148                        'number' => 5,
     149
     150                        // Anonymize the IP to protect user privacy.
     151                        'ip' => $this->maybe_anonymize_ip_address( $this->get_unsafe_client_ip() ),
     152                );
    147153
    148154                /*
    149155                 * Send the minimal set of necessary arguments, in order to increase the
    class WP_Community_Events { 
    161167
    162168                        if ( $search ) {
    163169                                $args['location'] = $search;
    164                         } else {
    165                                 /*
    166                                  * Protect the user's privacy by anonymizing their IP before sending
    167                                  * it to w.org, and only send it when necessary.
    168                                  *
    169                                  * The w.org API endpoint only uses the IP address when a location
    170                                  * query is not provided, so we can safely avoid sending it when
    171                                  * there is a query.
    172                                  */
    173                                 $args['ip'] = $this->maybe_anonymize_ip_address( $this->get_unsafe_client_ip() );
    174170                        }
    175171                }
    176172
    class WP_Community_Events { 
    275271        protected function get_events_transient_key( $location ) {
    276272                $key = false;
    277273
    278                 if ( isset( $location['latitude'], $location['longitude'] ) ) {
     274                if ( isset( $location['ip'] ) ) {
     275                        /*
     276                         * Use the IP that was sent in the API request, not the IP that the
     277                         * the API responded with. If the sent IP was private, the API will
     278                         * use the corresponding public IP for geolocation, and return that.
     279                         * If that public IP were used when setting the transient, then the
     280                         * transient would never be successfully retrieved, because
     281                         * get_unsafe_client_ip() will always return the private IP instead.
     282                         */
     283                        $key = 'community-events-' . md5( $this->maybe_anonymize_ip_address( $this->get_unsafe_client_ip() ) );
     284                } else if ( isset( $location['latitude'], $location['longitude'] ) ) {
    279285                        $key = 'community-events-' . md5( $location['latitude'] . $location['longitude'] );
    280286                }
    281287
  • src/wp-admin/includes/dashboard.php

    diff --git src/wp-admin/includes/dashboard.php src/wp-admin/includes/dashboard.php
    index 0f0058127f..f23fbde49d 100644
    function wp_print_community_events_templates() { 
    12471247                ); ?>
    12481248        </script>
    12491249
     1250        <script id="tmpl-community-events-attend-event-near-generic" type="text/template">
     1251                <?php _e( 'Attend an upcoming event near you.' ); ?>
     1252        </script>
     1253
    12501254        <script id="tmpl-community-events-could-not-locate" type="text/template">
    12511255                <?php printf(
    12521256                        $script_data['l10n']['could_not_locate_city'],
    function wp_print_community_events_templates() { 
    12861290                </li>
    12871291        </script>
    12881292
     1293        <script id="tmpl-community-events-no-upcoming-events-generic" type="text/template">
     1294                <li class="event-none">
     1295                        <?php printf(
     1296                                /* translators: meetup organization documentation URL. */
     1297                                __( 'There aren&#8217;t any events scheduled near you at the moment. Would you like to <a href="%s">organize one</a>?' ),
     1298                                __( 'https://make.wordpress.org/community/handbook/meetup-organizer/welcome/' )
     1299                        ); ?>
     1300                </li>
     1301        </script>
    12891302        <?php
    12901303}
    12911304
  • src/wp-admin/js/dashboard.js

    diff --git src/wp-admin/js/dashboard.js src/wp-admin/js/dashboard.js
    index 6a9b5033d4..8d2994696c 100644
    jQuery( function( $ ) { 
    369369                         * Determine which templates should be rendered and which elements
    370370                         * should be displayed.
    371371                         */
    372                         if ( templateParams.location ) {
     372                        if ( templateParams.location.ip ) {
     373                                /*
     374                                 * If the API determined the location by geolocating an IP, it will
     375                                 * provide events, but not a specific location.
     376                                 */
     377                                template = wp.template( 'community-events-attend-event-near-generic' );
     378                                $locationMessage.html( template( templateParams ) );
     379
     380                                if ( templateParams.events.length ) {
     381                                        template = wp.template( 'community-events-event-list' );
     382                                        $results.html( template( templateParams ) );
     383                                } else {
     384                                        template = wp.template( 'community-events-no-upcoming-events-generic' );
     385                                        $results.html( template( templateParams ) );
     386                                }
     387
     388                                elementVisibility['#community-events-location-message'] = true;
     389                                elementVisibility['.community-events-toggle-location']  = true;
     390                                elementVisibility['.community-events-results']          = true;
     391
     392                        } else if ( templateParams.location.description ) {
    373393                                template = wp.template( 'community-events-attend-event-near' );
    374394                                $locationMessage.html( template( templateParams ) );
    375395