<!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>[9108] sites/trunk/api.wordpress.org/public_html/events/1.0: Events API: Update logic for pinning regional camps</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 { white-space: pre-line; 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="http://meta.trac.wordpress.org/changeset/9108">9108</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://meta.trac.wordpress.org/changeset/9108","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>coreymckrill</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2019-08-17 00:50:17 +0000 (Sat, 17 Aug 2019)</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'>Events API: Update logic for pinning regional camps

* Pinning now has three phases of two weeks each. First phase is global, second
  is regional, third is just within the country where the event is being held.
* The pinning process is abstracted to work with any regional camp definition,
  which includes the necessary data about the event and a start date for the
  promotion.
* Regional camp definitions have been added for WCUS, WCEU, and WC Asia. Only
  WCUS is currently complete, as it is the next pending event.
* Unit tests also updated and passing to reflect this new functionality.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunkapiwordpressorgpublic_htmlevents10indexphp">sites/trunk/api.wordpress.org/public_html/events/1.0/index.php</a></li>
<li><a href="#sitestrunkapiwordpressorgpublic_htmlevents10teststestindexphp">sites/trunk/api.wordpress.org/public_html/events/1.0/tests/test-index.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkapiwordpressorgpublic_htmlevents10indexphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/api.wordpress.org/public_html/events/1.0/index.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/api.wordpress.org/public_html/events/1.0/index.php    2019-08-17 00:35:04 UTC (rev 9107)
+++ sites/trunk/api.wordpress.org/public_html/events/1.0/index.php      2019-08-17 00:50:17 UTC (rev 9108)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -164,9 +164,17 @@
</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">                $events = get_events( $event_args );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $events = maybe_add_wp15_promo( $events, $_SERVER['HTTP_USER_AGENT'], time() );
-               $events = add_regional_wordcamps( $events, $_SERVER['HTTP_USER_AGENT'] );
</del><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                //$events = maybe_add_wp15_promo( $events, $_SERVER['HTTP_USER_AGENT'], time() );
+
+               $events = maybe_add_regional_wordcamps(
+                       $events,
+                       get_regional_wordcamp_data(),
+                       $_SERVER['HTTP_USER_AGENT'],
+                       time(),
+                       $location
+               );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 // Internal location data cannot be exposed in the response, see get_location().
</span><span class="cx" style="display: block; padding: 0 10px">                if ( isset( $location['internal'] ) && $location['internal'] ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        // Let the client know that a location was successfully determined based on their IP
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -176,8 +184,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $error = 'no_location_available';
</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 compact( 'error', 'location', 'events' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $sandboxed = ( defined( 'WPORG_SANDBOXED' ) ) ? WPORG_SANDBOXED : null;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        return compact( 'sandboxed', 'error', 'location', 'events' );
</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">@@ -915,55 +924,125 @@
</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">- * Add regional WordCamps to the Events Widget in Core for extra promotion.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * The data for upcoming regional WordCamps.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Externalizing this makes it easier to test the `maybe_add_regional_wordcamps` function.
+ *
+ * @return array
+ */
+function get_regional_wordcamp_data() {
+       return array(
+               // WordCamp Asia.
+               'asia'   => array(
+                       'promo_start'        => 0, // todo
+                       'regional_countries' => array(
+                               // todo
+                       ),
+                       'event'              => array(
+                               'type'       => 'wordcamp',
+                               'title'      => 'WordCamp Asia',
+                               'url'        => 'https://2020.asia.wordcamp.org/',
+                               'meetup'     => '',
+                               'meetup_url' => '',
+                               'date'       => '2020-02-21 00:00:00',
+                               'location'   => array(
+                                       'location'  => 'Bangkok, Thailand',
+                                       'country'   => 'TH',
+                                       'latitude'  => 13.7248934,
+                                       'longitude' => 100.492683,
+                               ),
+                       ),
+               ),
+               // WordCamp Europe.
+               'europe' => array(
+                       'promo_start'        => 0, // todo
+                       'regional_countries' => array(
+                               // todo
+                       ),
+                       'event'              => array(
+                               'type'       => 'wordcamp',
+                               'title'      => 'WordCamp Europe',
+                               'url'        => 'https://2020.europe.wordcamp.org/',
+                               'meetup'     => '',
+                               'meetup_url' => '',
+                               'date'       => '', // todo
+                               'location' => array(
+                                       'location'  => 'Porto, Portugal',
+                                       'country'   => 'PT',
+                                       'latitude'  => 41.1622022,
+                                       'longitude' => -8.6570588,
+                               ),
+                       ),
+               ),
+               // WordCamp US.
+               'us'     => array(
+                       'promo_start'        => strtotime( '2019-08-16 00:00:00' ),
+                       'regional_countries' => array(
+                               'us', 'ca', 'bz', 'cr', 'sv', 'gt', 'hn', 'mx', 'ni', 'pa',
+                               'ar', 'bo', 'br', 'cl', 'co', 'ec', 'gf', 'gy', 'py', 'pe',
+                               'sr', 'uy', 've', 'ag', 'aw', 'bs', 'bb', 'ky', 'cu', 'dm',
+                               'do', 'gd', 'ht', 'jm', 'kn', 'lc', 'vc', 'tt',
+                       ),
+                       'event'              => array(
+                               'type'       => 'wordcamp',
+                               'title'      => 'WordCamp US',
+                               'url'        => 'https://2019.us.wordcamp.org/',
+                               'meetup'     => '',
+                               'meetup_url' => '',
+                               'date'       => '2019-11-01 00:00:00',
+                               'location'   => array(
+                                       'location'  => 'St. Louis, MO, USA',
+                                       'country'   => 'US',
+                                       'latitude'  => 38.6532135,
+                                       'longitude' => -90.3136733,
+                               ),
+                       ),
+               ),
+       );
+}
+
+/**
+ * Add time- and location-relevant regional WordCamps to the Events Widget in Core.
+ *
</ins><span class="cx" style="display: block; padding: 0 10px">  * @param array  $local_events
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param array  $region_data
</ins><span class="cx" style="display: block; padding: 0 10px">  * @param string $user_agent
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param int    $current_time
+ * @param array  $location
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @return array
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function add_regional_wordcamps( $local_events, $user_agent ) {
-       $time               = time();
-       $regional_wordcamps = array();
-
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function maybe_add_regional_wordcamps( $local_events, $region_data, $user_agent, $current_time, $location ) {
</ins><span class="cx" style="display: block; padding: 0 10px">         if ( ! is_client_core( $user_agent ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                return $local_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">-        if ( $time > strtotime( 'November 7th, 2018' ) && $time < strtotime( 'December 10th, 2018' ) ) {
-               $regional_wordcamps[] = array(
-                       'type'       => 'wordcamp',
-                       'title'      => 'WordCamp US',
-                       'url'        => 'https://2018.us.wordcamp.org/',
-                       'meetup'     => '',
-                       'meetup_url' => '',
-                       'date'       => '2018-12-07 00:00:00',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $regional_wordcamps = array();
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        'location' => array(
-                               'location'  => 'Nashville, TN, USA',
-                               'country'   => 'US',
-                               'latitude'  => 36.1566085,
-                               'longitude' => -86.7784909,
-                       )
-               );
-       }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ foreach ( $region_data as $region => $data ) {
+               if ( empty( $data['promo_start'] ) ) {
+                       continue;
+               }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( $time > strtotime( 'May 20th, 2019' ) && $time < strtotime( 'June 23rd, 2019' ) ) {
-               $regional_wordcamps[] = array(
-                       'type'       => 'wordcamp',
-                       'title'      => 'WordCamp Europe',
-                       'url'        => 'https://2019.europe.wordcamp.org/',
-                       'meetup'     => '',
-                       'meetup_url' => '',
-                       'date'       => '2019-06-20 00:00:00',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $start = $data['promo_start'];
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        'location' => array(
-                               'location'  => 'Berlin, Germany',
-                               'country'   => 'DE',
-                               'latitude'  => 52.50697,
-                               'longitude' => 13.2843064,
-                       )
-               );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         /**
+                * The targeted area of the regional camp promotion "zooms in" over the course of 6 weeks.
+                */
+               if ( is_within_date_range( $current_time, $start, strtotime( '+ 2 weeks', $start ) ) ) {
+                       // Phase 1: Show worldwide for first two weeks.
+                       $regional_wordcamps[] = $data['event'];
+               } elseif ( is_within_date_range( $current_time, strtotime( '+ 2 weeks', $start ), strtotime( '+ 4 weeks', $start ) ) ) {
+                       // Phase 2: Show within regional countries for next two weeks.
+                       if ( ! empty( $location['country'] ) && in_array( strtolower( $location['country'] ), $data['regional_countries'], true ) ) {
+                               $regional_wordcamps[] = $data['event'];
+                       }
+               } elseif ( is_within_date_range( $current_time, strtotime( '+ 4 weeks', $start ), strtotime( '+ 6 weeks', $start ) ) ) {
+                       // Phase 3: Show only within the event country for the last two weeks.
+                       if ( ! empty( $location['country'] ) && strtolower( $data['event']['location']['country'] ) === strtolower( $location['country'] ) ) {
+                               $regional_wordcamps[] = $data['event'];
+                       }
+               }
</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">@@ -971,11 +1050,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * Favor the regional event since it'll be pinned to the top.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        foreach ( $regional_wordcamps as $regional_event ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                foreach ( $local_events as $local_key => $local_event ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $local_events = array_filter( $local_events, function( $local_event ) use ( $regional_event ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         if ( parse_url( $regional_event['url'], PHP_URL_HOST ) === parse_url( $local_event['url'], PHP_URL_HOST ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                unset( $local_events[ $local_key ] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         return false;
</ins><span class="cx" style="display: block; padding: 0 10px">                         }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                       return true;
+               } );
</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">        return array_merge( $regional_wordcamps, $local_events );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -982,6 +1063,27 @@
</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">+ * Determine if a given Unix timestamp is within a date range.
+ *
+ * @param int    $time        A Unix timestamp.
+ * @param string $range_start A date/time string compatible with strtotime.
+ * @param string $range_end   A date/time string compatible with strtotime.
+ *
+ * @return bool
+ */
+function is_within_date_range( $time, $range_start, $range_end ) {
+       if ( ! is_int( $range_start ) ) {
+               $range_start = strtotime( $range_start );
+       }
+
+       if ( ! is_int( $range_end ) ) {
+               $range_end = strtotime( $range_end );
+       }
+
+       return $time > $range_start && $time < $range_end;
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px">  * Add a special WP15 meetup event to the Events Widget in Core if the user location doesn't already have a
</span><span class="cx" style="display: block; padding: 0 10px">  * WP15 event in the list.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span></span></pre></div>
<a id="sitestrunkapiwordpressorgpublic_htmlevents10teststestindexphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/api.wordpress.org/public_html/events/1.0/tests/test-index.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/api.wordpress.org/public_html/events/1.0/tests/test-index.php 2019-08-17 00:35:04 UTC (rev 9107)
+++ sites/trunk/api.wordpress.org/public_html/events/1.0/tests/test-index.php   2019-08-17 00:50:17 UTC (rev 9108)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -21,7 +21,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">        $tests_failed += test_get_location();
</span><span class="cx" style="display: block; padding: 0 10px">        $tests_failed += test_get_events();
</span><span class="cx" style="display: block; padding: 0 10px">        $tests_failed += test_get_events_country_restriction();
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $tests_failed += test_add_regional_wordcamps();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $tests_failed += test_maybe_add_regional_wordcamps();
</ins><span class="cx" style="display: block; padding: 0 10px">         $tests_failed += test_maybe_add_wp15_promo();
</span><span class="cx" style="display: block; padding: 0 10px">        $tests_failed += test_build_response();
</span><span class="cx" style="display: block; padding: 0 10px">        $tests_failed += test_is_client_core();
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -731,8 +731,11 @@
</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">-                 * Only the IPv4 address is given
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+          * Only the IPv4 address is given.
</ins><span class="cx" style="display: block; padding: 0 10px">                  *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 * Note that IP locations change frequently, so some of these expected results will inevitably become outdated
+                * and cause tests to fail.
+                *
</ins><span class="cx" style="display: block; padding: 0 10px">                  * See https://awebanalysis.com/en/ipv4-directory/
</span><span class="cx" style="display: block; padding: 0 10px">                 */
</span><span class="cx" style="display: block; padding: 0 10px">                'ip-africa' => array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -749,9 +752,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                'ip-asia' => array(
</span><span class="cx" style="display: block; padding: 0 10px">                        'input' => array( 'ip' => '86.108.55.28' ),
</span><span class="cx" style="display: block; padding: 0 10px">                        'expected' => array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                'description' => 'amman',
-                               'latitude'    => '31.955',
-                               'longitude'   => '35.945',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         'description' => 'hakama',
+                               'latitude'    => '32.594',
+                               'longitude'   => '35.884',
</ins><span class="cx" style="display: block; padding: 0 10px">                                 'country'     => 'JO',
</span><span class="cx" style="display: block; padding: 0 10px">                                'internal'    => true,
</span><span class="cx" style="display: block; padding: 0 10px">                        ),
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -760,9 +763,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                'ip-europe' => array(
</span><span class="cx" style="display: block; padding: 0 10px">                        'input' => array( 'ip' => '80.95.186.144' ),
</span><span class="cx" style="display: block; padding: 0 10px">                        'expected' => array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                'description' => 'belfast',
-                               'latitude'    => '54.583',
-                               'longitude'   => '-5.933',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         'description' => 'antrim',
+                               'latitude'    => '54.700',
+                               'longitude'   => '-6.200',
</ins><span class="cx" style="display: block; padding: 0 10px">                                 'country'     => 'GB',
</span><span class="cx" style="display: block; padding: 0 10px">                                'internal'    => true,
</span><span class="cx" style="display: block; padding: 0 10px">                        ),
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -771,9 +774,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                'ip-north-america' => array(
</span><span class="cx" style="display: block; padding: 0 10px">                        'input' => array( 'ip' => '189.147.186.0' ),
</span><span class="cx" style="display: block; padding: 0 10px">                        'expected' => array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                'description' => 'cuauhtemoc',
-                               'latitude'    => '19.417',
-                               'longitude'   => '-99.157',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         'description' => 'mexico city',
+                               'latitude'    => '19.428',
+                               'longitude'   => '-99.128',
</ins><span class="cx" style="display: block; padding: 0 10px">                                 'country'     => 'MX',
</span><span class="cx" style="display: block; padding: 0 10px">                                'internal'    => true,
</span><span class="cx" style="display: block; padding: 0 10px">                        ),
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -802,8 +805,11 @@
</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">-                 * Only an IPv6 address is given
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+          * Only an IPv6 address is given.
</ins><span class="cx" style="display: block; padding: 0 10px">                  *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 * Note that IP locations change frequently, so some of these expected results will inevitably become outdated
+                * and cause tests to fail.
+                *
</ins><span class="cx" style="display: block; padding: 0 10px">                  * See https://www.google.com/intl/en/ipv6/statistics.html#tab=per-country-ipv6-adoption&tab=per-country-ipv6-adoption
</span><span class="cx" style="display: block; padding: 0 10px">                 * See https://awebanalysis.com/en/ipv6-directory/
</span><span class="cx" style="display: block; padding: 0 10px">                 * See https://www.google.com/search?q=australia+site%3Ahttps%3A%2F%2Fawebanalysis.com%2Fen%2Fipv6-directory%2F
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -866,9 +872,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                'ipv6-south-america' => array(
</span><span class="cx" style="display: block; padding: 0 10px">                        'input'    => array( 'ip' => '2001:1388:6643:2736:10f1:897c:428c:1b3b' ),
</span><span class="cx" style="display: block; padding: 0 10px">                        'expected' => array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                'description' => 'ayacucho',
-                               'latitude'    => '-13.158',
-                               'longitude'   => '-74.224',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         'description' => 'lima',
+                               'latitude'    => '-12.043',
+                               'longitude'   => '-77.028',
</ins><span class="cx" style="display: block; padding: 0 10px">                                 'country'     => 'PE',
</span><span class="cx" style="display: block; padding: 0 10px">                                'internal'    => true,
</span><span class="cx" style="display: block; padding: 0 10px">                        ),
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1161,7 +1167,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @return int
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function test_add_regional_wordcamps() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function test_maybe_add_regional_wordcamps() {
</ins><span class="cx" style="display: block; padding: 0 10px">         $failed = 0;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        $local_events = get_events( array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1172,35 +1178,115 @@
</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">+        $region_data = array(
+               'us' => array(
+                       'promo_start'        => strtotime( '2019-08-16 00:00:00' ),
+                       'regional_countries' => array(
+                               'us', 'ca', 'bz', 'cr', 'sv', 'gt', 'hn', 'mx', 'ni', 'pa',
+                               'ar', 'bo', 'br', 'cl', 'co', 'ec', 'gf', 'gy', 'py', 'pe',
+                               'sr', 'uy', 've', 'ag', 'aw', 'bs', 'bb', 'ky', 'cu', 'dm',
+                               'do', 'gd', 'ht', 'jm', 'kn', 'lc', 'vc', 'tt',
+                       ),
+                       'event'              => array(
+                               'type'       => 'wordcamp',
+                               'title'      => 'WordCamp US',
+                               'url'        => 'https://2019.us.wordcamp.org/',
+                               'meetup'     => '',
+                               'meetup_url' => '',
+                               'date'       => '2019-11-01 00:00:00',
+                               'location'   => array(
+                                       'location'  => 'St. Louis, MO, USA',
+                                       'country'   => 'US',
+                                       'latitude'  => 38.6532135,
+                                       'longitude' => -90.3136733,
+                               ),
+                       ),
+               ),
+       );
+
+       $core_user_agent  = 'WordPress/5.2; https://example.org';
+       $other_user_agent = 'Smith';
+
+       $time_before_promo         = strtotime( '2019-08-15 00:00:00' );
+       $time_during_promo_phase_1 = strtotime( '+ 1 day', $region_data['us']['promo_start'] );
+       $time_during_promo_phase_2 = strtotime( '+ 2 weeks + 1 day', $region_data['us']['promo_start'] );
+       $time_during_promo_phase_3 = strtotime( '+ 4 weeks + 1 day', $region_data['us']['promo_start'] );
+       $time_after_promo          = strtotime( '+ 6 weeks + 1 day', $region_data['us']['promo_start'] );
+
+       $location_country_within_region = array(
+               'country' => 'us',
+       );
+
+       $location_country_outside_region = array(
+               'country' => 'es',
+       );
+
+       $location_ip_only = array(
+               'ip' => '8.8.8.8',
+       );
+
</ins><span class="cx" style="display: block; padding: 0 10px">         // Make sure there's at least one event, otherwise there could be false positives.
</span><span class="cx" style="display: block; padding: 0 10px">        if ( ! $local_events ) {
</span><span class="cx" style="display: block; padding: 0 10px">                $local_events[] = array( 'title' => 'Mock Event' );
</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">-        printf( "\n\nRunning %d add_regional_wordcamps() tests\n", 2 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ printf( "\n\nRunning %d add_regional_wordcamps() tests\n", 13 );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        // Test that no changes were made if the user agent isn't Core.
-       $events_no_user_agent = add_regional_wordcamps( $local_events, '' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $tests_expect_no_changes = array();
+       $tests_expect_changes    = array();
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( $events_no_user_agent !== $local_events ) {
-               $failed++;
-               output_results( 'no-user-agent', false, $local_events, $events_no_user_agent );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // No regional camps should be added if before the promo start date or after the promo window is past (6 weeks).
+       $tests_expect_no_changes['before-promo'] = maybe_add_regional_wordcamps( $local_events, $region_data, $core_user_agent, $time_before_promo, $location_country_within_region );
+       $tests_expect_no_changes['before-promo'] = maybe_add_regional_wordcamps( $local_events, $region_data, $core_user_agent, $time_after_promo, $location_country_within_region );
+
+       // Regional camp should be added if it's within phase 1 of the promo, regardless of location.
+       $tests_expect_changes['promo-phase-1-within-region'] = maybe_add_regional_wordcamps( $local_events, $region_data, $core_user_agent, $time_during_promo_phase_1, $location_country_within_region );
+       $tests_expect_changes['promo-phase-1-outside-region'] = maybe_add_regional_wordcamps( $local_events, $region_data, $core_user_agent, $time_during_promo_phase_1, $location_country_outside_region );
+
+       // Regional camp should only be added during phase 2 of promo if location is within region.
+       $tests_expect_changes['promo-phase-2-within-region'] = maybe_add_regional_wordcamps( $local_events, $region_data, $core_user_agent, $time_during_promo_phase_2, $location_country_within_region );
+       $tests_expect_no_changes['promo-phase-2-outside-region'] = maybe_add_regional_wordcamps( $local_events, $region_data, $core_user_agent, $time_during_promo_phase_2, $location_country_outside_region );
+       $tests_expect_no_changes['promo-phase-2-ip-only'] = maybe_add_regional_wordcamps( $local_events, $region_data, $core_user_agent, $time_during_promo_phase_2, $location_ip_only );
+
+       // Regional camp should only be added during phase 3 of promo if location is within event country.
+       $tests_expect_changes['promo-phase-3-within-event-country'] = maybe_add_regional_wordcamps( $local_events, $region_data, $core_user_agent, $time_during_promo_phase_3, $location_country_within_region );
+       $tests_expect_no_changes['promo-phase-3-outside-event-country'] = maybe_add_regional_wordcamps( $local_events, $region_data, $core_user_agent, $time_during_promo_phase_3, $location_country_outside_region );
+       $tests_expect_no_changes['promo-phase-3-ip-only'] = maybe_add_regional_wordcamps( $local_events, $region_data, $core_user_agent, $time_during_promo_phase_3, $location_ip_only );
+
+       // Regional camp should only be added if the user agent is Core.
+       $tests_expect_no_changes['other-user-agent'] = maybe_add_regional_wordcamps( $local_events, $region_data, $other_user_agent, $time_during_promo_phase_1, $location_country_within_region );
+       $tests_expect_changes['core-user-agent'] = maybe_add_regional_wordcamps( $local_events, $region_data, $core_user_agent, $time_during_promo_phase_1, $location_country_within_region );
+
+       // There should only be one entry for an event, even if the local event array already contains the regional event.
+       $tests_expect_no_changes['duplicate-event'] = maybe_add_regional_wordcamps( array( $region_data['us']['event'] ), $region_data, $core_user_agent, $time_during_promo_phase_1, $location_country_within_region );
+
+       foreach ( $tests_expect_no_changes as $name => $result ) {
+               switch ( $name ) {
+                       case 'duplicate-event':
+                               if ( $result !== array( $region_data['us']['event'] ) ) {
+                                       $failed++;
+                                       output_results( $name, false, array( $region_data['us']['event'] ), $result );
+                               }
+                               break;
+                       default:
+                               if ( $result !== $local_events ) {
+                                       $failed++;
+                                       output_results( $name, false, $local_events, $result );
+                               }
+                               break;
+               }
</ins><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">-        /*
-        * Test that local events were unharmed if the user agent is Core.
-        *
-        * There isn't an easy way to mock time(), so this doesn't test that the events were added
-        * correctly. It just makes sure that local events weren't removed.
-        */
-       $events_core_user_agent = add_regional_wordcamps( $local_events, 'WordPress/4.9; https://example.org' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $unchanged_count = count( $local_events );
+       $expected_count  = $unchanged_count + 1;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if (
-               count( $events_core_user_agent ) < count( $local_events ) ||
-               ! in_array( $local_events[0], $events_core_user_agent, true )
-       ) {
-               $failed++;
-               output_results( 'core-user-agent', false, 'local events were not affected', $events_core_user_agent );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ foreach ( $tests_expect_changes as $name => $result ) {
+               $actual_count = count( $result );
+
+               if ( $actual_count !== $expected_count ) {
+                       $failed++;
+                       output_results( $name, false, $expected_count, $actual_count );
+               }
</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">        return $failed;
</span></span></pre>
</div>
</div>

</body>
</html>