<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[40790] trunk/src: Dashboard: Improve the handling of locations determined by geolocating the IP address and by entering a city name.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/40790">40790</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/40790","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>azaozz</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2017-05-19 05:48:01 +0000 (Fri, 19 May 2017)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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 <a href="https://core.trac.wordpress.org/ticket/40702">#40702</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminincludesajaxactionsphp">trunk/src/wp-admin/includes/ajax-actions.php</a></li>
<li><a href="#trunksrcwpadminincludesclasswpcommunityeventsphp">trunk/src/wp-admin/includes/class-wp-community-events.php</a></li>
<li><a href="#trunksrcwpadminincludesdashboardphp">trunk/src/wp-admin/includes/dashboard.php</a></li>
<li><a href="#trunksrcwpadminjsdashboardjs">trunk/src/wp-admin/js/dashboard.js</a></li>
<li><a href="#trunksrcwpincludesscriptloaderphp">trunk/src/wp-includes/script-loader.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminincludesajaxactionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/ajax-actions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/ajax-actions.php      2017-05-19 04:00:32 UTC (rev 40789)
+++ trunk/src/wp-admin/includes/ajax-actions.php        2017-05-19 05:48:01 UTC (rev 40790)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -312,14 +312,33 @@
</span><span class="cx" style="display: block; padding: 0 10px">        $saved_location = get_user_option( 'community-events-location', $user_id );
</span><span class="cx" style="display: block; padding: 0 10px">        $events_client  = new WP_Community_Events( $user_id, $saved_location );
</span><span class="cx" style="display: block; padding: 0 10px">        $events         = $events_client->get_events( $search, $timezone );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        $ip_changed     = false;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        if ( is_wp_error( $events ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                wp_send_json_error( array(
</span><span class="cx" style="display: block; padding: 0 10px">                        'error' => $events->get_error_message(),
</span><span class="cx" style="display: block; padding: 0 10px">                ) );
</span><span class="cx" style="display: block; padding: 0 10px">        } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( isset( $events['location'] ) ) {
-                       // Store the location network-wide, so the user doesn't have to set it on each site.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( empty( $saved_location['ip'] ) && ! empty( $events['location']['ip'] ) ) {
+                       $ip_changed = true;
+               } elseif ( isset( $saved_location['ip'] ) && ! empty( $events['location']['ip'] ) && $saved_location['ip'] !== $events['location']['ip'] ) {
+                       $ip_changed = true;
+               }
+
+               /*
+                * The location should only be updated when it changes. The API doesn't always return
+                * a full location; sometimes it's missing the description or country. The location
+                * that was saved during the initial request is known to be good and complete, though.
+                * It should be left in tact until the user explicitly changes it (either by manually
+                * searching for a new location, or by changing their IP address).
+                *
+                * If the location were updated with an incomplete response from the API, then it could
+                * break assumptions that the UI makes (e.g., that there will always be a description
+                * that corresponds to a latitude/longitude location).
+                *
+                * The location is stored network-wide, so that the user doesn't have to set it on each site.
+                */
+               if ( $ip_changed || $search ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         update_user_option( $user_id, 'community-events-location', $events['location'], true );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="trunksrcwpadminincludesclasswpcommunityeventsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/class-wp-community-events.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/class-wp-community-events.php 2017-05-19 04:00:32 UTC (rev 40789)
+++ trunk/src/wp-admin/includes/class-wp-community-events.php   2017-05-19 05:48:01 UTC (rev 40790)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -94,12 +94,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        return $cached_events;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $request_url    = $this->get_request_url( $location_search, $timezone );
-               $response       = wp_remote_get( $request_url );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $api_url        = 'https://api.wordpress.org/events/1.0/';
+               $request_args   = $this->get_request_args( $location_search, $timezone );
+               $response       = wp_remote_get( $api_url, $request_args );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $response_code  = wp_remote_retrieve_response_code( $response );
</span><span class="cx" style="display: block; padding: 0 10px">                $response_body  = json_decode( wp_remote_retrieve_body( $response ), true );
</span><span class="cx" style="display: block; padding: 0 10px">                $response_error = null;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $debugging_info = compact( 'request_url', 'response_code', 'response_body' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $debugging_info = compact( 'api_url', 'request_args', 'response_code', 'response_body' );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( is_wp_error( $response ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $response_error = $response;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -128,6 +129,31 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                unset( $response_body['ttl'] );
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        /*
+                        * The IP in the response is usually the same as the one that was sent
+                        * in the request, but in some cases it is different. In those cases,
+                        * it's important to reset it back to the IP from the request.
+                        *
+                        * For example, if the IP sent in the request is private (e.g., 192.168.1.100),
+                        * then the API will ignore that and use the corresponding public IP instead,
+                        * and the public IP will get returned. If the public IP were saved, though,
+                        * then get_cached_events() would always return `false`, because the transient
+                        * would be generated based on the public IP when saving the cache, but generated
+                        * based on the private IP when retrieving the cache.
+                        */
+                       if ( ! empty( $response_body['location']['ip'] ) ) {
+                               $response_body['location']['ip'] = $request_args['body']['ip'];
+                       }
+
+                       /*
+                        * The API doesn't return a description for latitude/longitude requests,
+                        * but the description is already saved in the user location, so that
+                        * one can be used instead.
+                        */
+                       if ( $this->coordinates_match( $request_args['body'], $response_body['location'] ) && empty( $response_body['location']['description'] ) ) {
+                               $response_body['location']['description'] = $this->user_location['description'];
+                       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                         $this->cache_events( $response_body, $expiration );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        $response_body = $this->trim_events( $response_body );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -143,24 +169,23 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * Builds a URL for requests to the w.org Events API.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * Builds an array of args to use in an HTTP request to the w.org Events API.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @access protected
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 4.8.0
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @param  string $search   Optional. City search string. Default empty string.
</span><span class="cx" style="display: block; padding: 0 10px">         * @param  string $timezone Optional. Timezone string. Default empty string.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @return string The request URL.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @return @return array The request args.
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        protected function get_request_url( $search = '', $timezone = '' ) {
-               $api_url = 'https://api.wordpress.org/events/1.0/';
-               $args    = array(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ protected function get_request_args( $search = '', $timezone = '' ) {
+               $args = array(
</ins><span class="cx" style="display: block; padding: 0 10px">                         'number' => 5, // Get more than three in case some get trimmed out.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        'ip'     => $this->get_client_ip(),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 'ip'     => self::get_unsafe_client_ip(),
</ins><span class="cx" style="display: block; padding: 0 10px">                 );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                /*
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                 * Send the minimal set of necessary arguments, in order to increase the
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+          * Include the minimal set of necessary arguments, in order to increase the
</ins><span class="cx" style="display: block; padding: 0 10px">                  * chances of a cache-hit on the API side.
</span><span class="cx" style="display: block; padding: 0 10px">                 */
</span><span class="cx" style="display: block; padding: 0 10px">                if ( empty( $search ) && isset( $this->user_location['latitude'], $this->user_location['longitude'] ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -178,7 +203,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                return add_query_arg( $args, $api_url );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Wrap the args in an array compatible with the second parameter of `wp_remote_get()`.
+               return array(
+                       'body' => $args
+               );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -207,7 +235,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @return false|string The anonymized address on success; the given address
</span><span class="cx" style="display: block; padding: 0 10px">         *                      or false on failure.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        protected function get_client_ip() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public static function get_unsafe_client_ip() {
</ins><span class="cx" style="display: block; padding: 0 10px">                 $client_ip = false;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // In order of preference, with the best ones for this purpose first.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -250,6 +278,24 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Test if two pairs of latitude/longitude coordinates match each other.
+        *
+        * @since 4.8.0
+        * @access protected
+        *
+        * @param array $a The first pair, with indexes 'latitude' and 'longitude'.
+        * @param array $b The second pair, with indexes 'latitude' and 'longitude'.
+        * @return bool True if they match, false if they don't.
+        */
+       protected function coordinates_match( $a, $b ) {
+               if ( ! isset( $a['latitude'], $a['longitude'], $b['latitude'], $b['longitude'] ) ) {
+                       return false;
+               }
+
+               return $a['latitude'] === $b['latitude'] && $a['longitude'] === $b['longitude'];
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Generates a transient key based on user location.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * This could be reduced to a one-liner in the calling functions, but it's
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -266,7 +312,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">        protected function get_events_transient_key( $location ) {
</span><span class="cx" style="display: block; padding: 0 10px">                $key = false;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( isset( $location['latitude'], $location['longitude'] ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( isset( $location['ip'] ) ) {
+                       $key = 'community-events-' . md5( $location['ip'] );
+               } else if ( isset( $location['latitude'], $location['longitude'] ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         $key = 'community-events-' . md5( $location['latitude'] . $location['longitude'] );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="trunksrcwpadminincludesdashboardphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/dashboard.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/dashboard.php 2017-05-19 04:00:32 UTC (rev 40789)
+++ trunk/src/wp-admin/includes/dashboard.php   2017-05-19 05:48:01 UTC (rev 40790)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1234,15 +1234,23 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        <script id="tmpl-community-events-no-upcoming-events" type="text/template">
</span><span class="cx" style="display: block; padding: 0 10px">                <li class="event-none">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        <?php printf(
-                               /* translators: 1: the city the user searched for, 2: meetup organization documentation URL */
-                               __( 'There aren&#8217;t any events scheduled near %1$s at the moment. Would you like to <a href="%2$s">organize one</a>?' ),
-                               '{{ data.location.description }}',
-                               __( 'https://make.wordpress.org/community/handbook/meetup-organizer/welcome/' )
-                       ); ?>
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 <# if ( data.location.description ) { #>
+                               <?php printf(
+                                       /* translators: 1: the city the user searched for, 2: meetup organization documentation URL */
+                                       __( 'There aren&#8217;t any events scheduled near %1$s at the moment. Would you like to <a href="%2$s">organize one</a>?' ),
+                                       '{{ data.location.description }}',
+                                       __( 'https://make.wordpress.org/community/handbook/meetup-organizer/welcome/' )
+                               ); ?>
+
+                       <# } else { #>
+                               <?php printf(
+                                       /* translators: meetup organization documentation URL. */
+                                       __( 'There aren&#8217;t any events scheduled near you at the moment. Would you like to <a href="%s">organize one</a>?' ),
+                                       __( 'https://make.wordpress.org/community/handbook/meetup-organizer/welcome/' )
+                               ); ?>
+                       <# } #>
</ins><span class="cx" style="display: block; padding: 0 10px">                 </li>
</span><span class="cx" style="display: block; padding: 0 10px">        </script>
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
</del><span class="cx" style="display: block; padding: 0 10px">         <?php
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="trunksrcwpadminjsdashboardjs"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/js/dashboard.js</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/js/dashboard.js        2017-05-19 04:00:32 UTC (rev 40789)
+++ trunk/src/wp-admin/js/dashboard.js  2017-05-19 05:48:01 UTC (rev 40790)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -385,7 +385,26 @@
</span><span class="cx" style="display: block; padding: 0 10px">                         * Determine which templates should be rendered and which elements
</span><span class="cx" style="display: block; padding: 0 10px">                         * should be displayed.
</span><span class="cx" style="display: block; padding: 0 10px">                         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        if ( templateParams.location ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 if ( templateParams.location.ip ) {
+                               /*
+                                * If the API determined the location by geolocating an IP, it will
+                                * provide events, but not a specific location.
+                                */
+                               $locationMessage.text( communityEventsData.l10n.attend_event_near_generic );
+
+                               if ( templateParams.events.length ) {
+                                       template = wp.template( 'community-events-event-list' );
+                                       $results.html( template( templateParams ) );
+                               } else {
+                                       template = wp.template( 'community-events-no-upcoming-events' );
+                                       $results.html( template( templateParams ) );
+                               }
+
+                               elementVisibility['#community-events-location-message'] = true;
+                               elementVisibility['.community-events-toggle-location']  = true;
+                               elementVisibility['.community-events-results']          = true;
+
+                       } else if ( templateParams.location.description ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                                 template = wp.template( 'community-events-attend-event-near' );
</span><span class="cx" style="display: block; padding: 0 10px">                                $locationMessage.html( template( templateParams ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -435,7 +454,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        $toggleButton.attr( 'aria-expanded', elementVisibility['.community-events-toggle-location'] );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        if ( templateParams.location ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 if ( templateParams.location && ( templateParams.location.ip || templateParams.location.latitude ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                                 // Hide the form when there's a valid location.
</span><span class="cx" style="display: block; padding: 0 10px">                                app.toggleLocationForm( 'hide' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="trunksrcwpincludesscriptloaderphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/script-loader.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/script-loader.php   2017-05-19 04:00:32 UTC (rev 40789)
+++ trunk/src/wp-includes/script-loader.php     2017-05-19 05:48:01 UTC (rev 40790)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1012,10 +1012,25 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        require_once( ABSPATH . 'wp-admin/includes/class-wp-community-events.php' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $user_id       = get_current_user_id();
-       $user_location = get_user_option( 'community-events-location', $user_id );
-       $events_client = new WP_Community_Events( $user_id, $user_location );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $user_id            = get_current_user_id();
+       $saved_location     = get_user_option( 'community-events-location', $user_id );
+       $saved_ip_address   = isset( $saved_location['ip'] ) ? $saved_location['ip'] : false;
+       $current_ip_address = WP_Community_Events::get_unsafe_client_ip();
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        /*
+        * If the user's location is based on their IP address, then update their
+        * location when their IP address changes. This allows them to see events
+        * in their current city when travelling. Otherwise, they would always be
+        * shown events in the city where they were when they first loaded the
+        * Dashboard, which could have been months or years ago.
+        */
+       if ( $saved_ip_address && $current_ip_address && $current_ip_address !== $saved_ip_address ) {
+               $saved_location['ip'] = $current_ip_address;
+               update_user_option( $user_id, 'community-events-location', $saved_location, true );
+       }
+
+       $events_client = new WP_Community_Events( $user_id, $saved_location );
+
</ins><span class="cx" style="display: block; padding: 0 10px">         wp_localize_script( 'dashboard', 'communityEventsData', array(
</span><span class="cx" style="display: block; padding: 0 10px">                'nonce' => wp_create_nonce( 'community_events' ),
</span><span class="cx" style="display: block; padding: 0 10px">                'cache' => $events_client->get_cached_events(),
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1023,6 +1038,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                'l10n' => array(
</span><span class="cx" style="display: block; padding: 0 10px">                        'enter_closest_city' => __( 'Enter your closest city to find nearby events.' ),
</span><span class="cx" style="display: block; padding: 0 10px">                        'error_occurred_please_try_again' => __( 'An error occurred. Please try again.' ),
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        'attend_event_near_generic' => __( 'Attend an upcoming event near you.' ),
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        /*
</span><span class="cx" style="display: block; padding: 0 10px">                         * These specific examples were chosen to highlight the fact that a
</span></span></pre>
</div>
</div>

</body>
</html>