<!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>[9100] sites/trunk/wordpress.org/public_html/wp-content/plugins/official-wordpress-events: Official WordPress Events: Refactor meetup fetching for upcoming API changes</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/9100">9100</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/9100","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-13 21:08:02 +0000 (Tue, 13 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'>Official WordPress Events: Refactor meetup fetching for upcoming API changes

The Meetup.com API announced changes that will take effect on 2019-08-15. The
API key method of authentication will no longer work, and the entire v2 of the
API will be deactivated in favor of v3. So:

* Add an OAuth2 client for authentication (copied from WordCamp)
* Copy updated version of Meetup_Client from WordCamp and refactor usage of its
  `get_events` method to reflect the change to v3 endpoints.
* Add a generic API_Client class (copied from WordCamp) for making repeated
  remote requests. This is already used by the Meetup_Client class, but now
  it'll be used for the requests to the WordCamp REST API endpoint and Google
  Maps as well.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsofficialwordpresseventsmeetupclassmeetupclientphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/official-wordpress-events/meetup/class-meetup-client.php</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsofficialwordpresseventsofficialwordpresseventsphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/official-wordpress-events/official-wordpress-events.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsofficialwordpresseventsmeetupclassapiclientphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/official-wordpress-events/meetup/class-api-client.php</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsofficialwordpresseventsmeetupclassmeetupoauth2clientphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/official-wordpress-events/meetup/class-meetup-oauth2-client.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsofficialwordpresseventsmeetupclassapiclientphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: sites/trunk/wordpress.org/public_html/wp-content/plugins/official-wordpress-events/meetup/class-api-client.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/official-wordpress-events/meetup/class-api-client.php                            (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/official-wordpress-events/meetup/class-api-client.php      2019-08-13 21:08:02 UTC (rev 9100)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,238 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+namespace WordCamp\Utilities;
+
+use WP_Error;
+
+defined( 'WPINC' ) || die();
+
+/**
+ * Class API_Client
+ *
+ * A generic, extendable class for making requests to an API.
+ *
+ * Important: This class is used in multiple locations in the WordPress/WordCamp ecosystem. Because of complexities
+ * around SVN externals and the reliability of GitHub's SVN bridge during deploys, it was decided to maintain multiple
+ * copies of this file rather than have SVN externals pointing to one canonical source.
+ *
+ * If you make changes to this file, make sure they are propagated to the other locations:
+ *
+ * - wordcamp: wp-content/mu-plugins/utilities
+ * - wporg: wp-content/plugins/official-wordpress-events/meetup
+ *
+ * @package WordCamp\Utilities
+ */
+class API_Client {
+       /**
+        * @var WP_Error|null Container for errors.
+        */
+       public $error = null;
+
+       /**
+        * @var string|array A function to call to determine whether to throttle requests to the API.
+        */
+       protected $throttle_callback = '';
+
+       /*
+        * @var array A list of integer response codes that should break the "tenacious" remote request loop.
+        */
+       protected $breaking_response_codes = [];
+
+       /**
+        * API_Client constructor.
+        *
+        * @param array $settings {
+        *     Optional. Settings for the client.
+        *
+        *     @type callable $throttle_callback       A function to call to determine whether to throttle requests to
+        *                                             the API.
+        *     @type array    $breaking_response_codes A list of integer response codes that should break the "tenacious"
+        *                                             remote request loop.
+        * }
+        */
+       public function __construct( array $settings = [] ) {
+               $this->error = new WP_Error();
+
+               $defaults = [
+                       'throttle_callback'       => '',
+                       'breaking_response_codes' => [ 400, 401, 404, 429 ],
+               ];
+
+               $settings = wp_parse_args( $settings, $defaults );
+
+               $this->throttle_callback       = $settings['throttle_callback'];
+               $this->breaking_response_codes = $settings['breaking_response_codes'];
+       }
+
+       /**
+        * Wrapper for `wp_remote_get` to retry requests that fail temporarily for various reasons.
+        *
+        * One common example of a reason a request would fail, but later succeed, is when the first request times out.
+        *
+        * Based on `wcorg_redundant_remote_get`.
+        *
+        * @param string $url
+        * @param array  $args
+        *
+        * @return array|WP_Error
+        */
+       protected function tenacious_remote_request( $url, array $args = [] ) {
+               $attempt_count  = 0;
+               $max_attempts   = 3;
+               $breaking_codes = $this->breaking_response_codes;
+
+               // The default of 5 seconds often results in frequent timeouts.
+               if ( empty( $args['timeout'] ) ) {
+                       $args['timeout'] = 15;
+               }
+
+               while ( $attempt_count < $max_attempts ) {
+                       $response      = wp_remote_request( $url, $args );
+                       $response_code = wp_remote_retrieve_response_code( $response );
+
+                       // This is called before breaking in case a new request is made immediately.
+                       $this->maybe_throttle( $response );
+
+                       if ( in_array( $response_code, $breaking_codes, true ) ) {
+                               break;
+                       }
+
+                       /*
+                        * Sometimes an API inexplicably returns a success code with an empty body, but will return a valid
+                        * response if the exact request is retried.
+                        */
+                       if ( 200 === $response_code && ! empty( wp_remote_retrieve_body( $response ) ) ) {
+                               break;
+                       }
+
+                       $attempt_count++;
+
+                       /**
+                        * Action: Fires when tenacious_remote_request fails a request attempt.
+                        *
+                        * Note that the request parameter includes the request URL which may contain sensitive information such as
+                        * an API key. This should be redacted before outputting anywhere public.
+                        *
+                        * @param array $response
+                        * @param array $request
+                        * @param int   $attempt_count
+                        * @param int   $max_attempts
+                        */
+                       do_action( 'api_client_tenacious_remote_request_attempt', $response, compact( 'url', 'args' ), $attempt_count, $max_attempts );
+
+                       if ( $attempt_count < $max_attempts ) {
+                               $retry_after = wp_remote_retrieve_header( $response, 'retry-after' ) ?: 5;
+                               $wait        = min( $retry_after * $attempt_count, 30 );
+
+                               self::cli_message( "\nRequest failed $attempt_count times. Pausing for $wait seconds before retrying." );
+
+                               sleep( $wait );
+                       }
+               }
+
+               if ( $attempt_count === $max_attempts && ( 200 !== $response_code || is_wp_error( $response ) ) ) {
+                       self::cli_message( "\nRequest failed $attempt_count times. Giving up." );
+               }
+
+               return $response;
+       }
+
+       /**
+        * Wrapper method for a request using the GET method.
+        *
+        * @param $url
+        * @param array $args
+        *
+        * @return array|WP_Error
+        */
+       public function tenacious_remote_get( $url, array $args = [] ) {
+               $args['method'] = 'GET';
+
+               return $this->tenacious_remote_request( $url, $args );
+       }
+
+       /**
+        * Wrapper method for a request using the POST method.
+        *
+        * @param $url
+        * @param array $args
+        *
+        * @return array|WP_Error
+        */
+       public function tenacious_remote_post( $url, array $args = [] ) {
+               $args['method'] = 'POST';
+
+               return $this->tenacious_remote_request( $url, $args );
+       }
+
+       /**
+        * Check the rate limit status in an API response and delay further execution if necessary.
+        *
+        * @param array $response
+        */
+       private function maybe_throttle( $response ) {
+               if ( ! is_callable( $this->throttle_callback ) ) {
+                       if ( ! empty( $this->throttle_callback ) ) {
+                               $this->error->add(
+                                       'invalid_throttle_callback',
+                                       'The specified throttle callback is not callable.'
+                               );
+                       }
+
+                       return;
+               }
+
+               call_user_func( $this->throttle_callback, $response );
+       }
+
+       /**
+        * Extract error information from an API response and add it to our error handler.
+        *
+        * This is just a stub. Extending classes should define their own method that handles the error codes and
+        * messages specific to the API they are dealing with.
+        *
+        * @param array|WP_Error $response     The response or error generated from the request.
+        * @param string         $request_url  Optional.
+        * @param array          $request_args Optional.
+        *
+        * @return bool True if the error was handled.
+        */
+       public function handle_error_response( $response, $request_url = '', $request_args = array() ) {
+               /**
+                * Action: Fires when a remote response is suspected to be an error.
+                *
+                * @param array|WP_Error $response
+                * @param string         $request_url
+                * @param array          $request_args
+                */
+               do_action( 'api_client_handle_error_response', $response, $request_url, $request_args );
+
+               if ( is_wp_error( $response ) ) {
+                       $codes = $response->get_error_codes();
+
+                       foreach ( $codes as $code ) {
+                               $messages = $response->get_error_messages( $code );
+
+                               foreach ( $messages as $message ) {
+                                       $this->error->add( $code, $message );
+                               }
+                       }
+
+                       return true;
+               }
+
+               return false;
+       }
+
+       /**
+        * Outputs a message when the command is run from the PHP command line.
+        *
+        * @param string $message
+        *
+        * @return void
+        */
+       protected static function cli_message( $message ) {
+               if ( 'cli' === php_sapi_name() ) {
+                       echo $message;
+               }
+       }
+}
</ins></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsofficialwordpresseventsmeetupclassmeetupclientphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordpress.org/public_html/wp-content/plugins/official-wordpress-events/meetup/class-meetup-client.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/official-wordpress-events/meetup/class-meetup-client.php 2019-08-10 16:00:56 UTC (rev 9099)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/official-wordpress-events/meetup/class-meetup-client.php   2019-08-13 21:08:02 UTC (rev 9100)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1,6 +1,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> <?php
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+namespace WordCamp\Utilities;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-namespace WordCamp\Utilities;
</del><span class="cx" style="display: block; padding: 0 10px"> use WP_Error;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> defined( 'WPINC' ) || die();
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -8,9 +8,17 @@
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px">  * Class Meetup_Client
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * TODO Refactor this to use the API_Client base class.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Important: This class and its dependency classes are used in multiple locations in the WordPress/WordCamp
+ * ecosystem. Because of complexities around SVN externals and the reliability of GitHub's SVN bridge during deploys,
+ * it was decided to maintain multiple copies of these files rather than have SVN externals pointing to one canonical
+ * source.
+ *
+ * If you make changes to this file, make sure they are propagated to the other locations:
+ *
+ * - wordcamp: wp-content/mu-plugins/utilities
+ * - wporg: wp-content/plugins/official-wordpress-events/meetup
</ins><span class="cx" style="display: block; padding: 0 10px">  */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-class Meetup_Client {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+class Meetup_Client extends API_Client {
</ins><span class="cx" style="display: block; padding: 0 10px">         /**
</span><span class="cx" style="display: block; padding: 0 10px">         * @var string The base URL for the API endpoints.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -17,36 +25,59 @@
</span><span class="cx" style="display: block; padding: 0 10px">        protected $api_base = 'https://api.meetup.com/';
</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">-         * @var string The API key.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @var Meetup_OAuth2_Client|null
</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 $api_key = '';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ protected $oauth_client = null;
</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">         * @var bool If true, the client will fetch fewer results, for faster debugging.
</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 $debug_mode;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ protected $debug = false;
</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">-         * @var WP_Error|null Container for errors.
-        */
-       public $error = null;
-
-       /**
</del><span class="cx" style="display: block; padding: 0 10px">          * Meetup_Client constructor.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         *
+        * @param array $settings {
+        *     Optional. Settings for the client.
+        *
+        *     @type bool $debug If true, the client will fetch fewer results, for faster debugging.
+        * }
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        public function __construct() {
-               $this->error = new WP_Error();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function __construct( array $settings = [] ) {
+               parent::__construct( array(
+                       /*
+                        * Response codes that should break the request loop.
+                        *
+                        * See https://www.meetup.com/meetup_api/docs/#errors.
+                        *
+                        * `200` (ok) is not in the list, because it needs to be handled conditionally.
+                        *  See API_Client::tenacious_remote_request.
+                        *
+                        * `400` (bad request) is not in the list, even though it seems like it _should_ indicate an unrecoverable
+                        * error. In practice we've observed that it's common for a seemingly valid request to be rejected with
+                        * a `400` response, but then get a `200` response if that exact same request is retried.
+                        */
+                       'breaking_response_codes' => array(
+                               401, // Unauthorized (invalid key).
+                               429, // Too many requests (rate-limited).
+                               404, // Unable to find group
+                       ),
+                       'throttle_callback'       => array( __CLASS__, 'throttle' ),
+               ) );
</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 ( defined( 'MEETUP_API_KEY' ) ) {
-                       $this->api_key = MEETUP_API_KEY;
-               } else {
-                       $this->error->add(
-                               'api_key_undefined',
-                               'The Meetup.com API Key is undefined.'
-                       );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $settings = wp_parse_args(
+                       $settings,
+                       array(
+                               'debug' => false,
+                       )
+               );
+
+               $this->oauth_client = new Meetup_OAuth2_Client;
+               $this->debug        = $settings['debug'];
+
+               if ( $this->debug ) {
+                       self::cli_message( "Meetup Client debug is ON. Results will be truncated." );
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
-               $this->debug_mode = apply_filters( 'wcmc_debug_mode', false );
</del><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">@@ -67,10 +98,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                ), $request_url );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                while ( $request_url ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $request_url = $this->sign_request_url( $request_url );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $response = $this->tenacious_remote_get( $request_url, $this->get_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">-                        $response = $this->tenacious_remote_get( $request_url );
-
</del><span class="cx" style="display: block; padding: 0 10px">                         if ( 200 === wp_remote_retrieve_response_code( $response ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                $body = json_decode( wp_remote_retrieve_body( $response ), true );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -97,7 +126,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                break;
</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 ( $this->debug_mode ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 if ( $this->debug ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                                 break;
</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">@@ -124,10 +153,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        'page' => 1,
</span><span class="cx" style="display: block; padding: 0 10px">                ), $request_url );
</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->sign_request_url( $request_url );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $response = $this->tenacious_remote_get( $request_url, $this->get_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">-                $response = $this->tenacious_remote_get( $request_url );
-
</del><span class="cx" style="display: block; padding: 0 10px">                 if ( 200 === wp_remote_retrieve_response_code( $response ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $count_header = wp_remote_retrieve_header( $response, 'X-Total-Count' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -151,112 +178,22 @@
</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">-         * Wrapper for `wp_remote_get` to retry requests that fail temporarily for various reasons.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * Generate headers to use in a request.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * One common example of a reason a request would fail, but later succeed, is when the first request times out.
-        *
-        * Based on `wcorg_redundant_remote_get`.
-        *
-        * @param string $url
-        * @param array  $args
-        *
-        * @return array|WP_Error
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @return 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">-        protected function tenacious_remote_get( $url, $args = array() ) {
-               $attempt_count = 0;
-               $max_attempts  = 3;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ protected function get_request_args() {
+               $oauth_token = $this->oauth_client->get_oauth_token();
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                /*
-                * Response codes that should break the loop.
-                *
-                * See https://www.meetup.com/meetup_api/docs/#errors.
-                *
-                * `200` (ok) is not in the list, because it needs to be handled conditionally. See below.
-                *
-                * `400` (bad request) is not in the list, even though it seems like it _should_ indicate an unrecoverable
-                * error. In practice we've observed that it's common for a seemingly valid request to be rejected with
-                * a `400` response, but then get a `200` response if that exact same request is retried.
-                */
-               $breaking_codes = array(
-                       401, // Unauthorized (invalid key).
-                       429, // Too many requests (rate-limited).
-                       404, // Unable to find group
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         return array(
+                       'headers' => array(
+                               'Accept'        => 'application/json',
+                               'Authorization' => "Bearer $oauth_token",
+                       ),
</ins><span class="cx" style="display: block; padding: 0 10px">                 );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
-               // The default of 5 seconds often results in frequent timeouts.
-               if ( empty( $args['timeout'] ) ) {
-                       $args['timeout'] = 15;
-               }
-
-               while ( $attempt_count < $max_attempts ) {
-                       $response      = wp_remote_get( $url, $args );
-                       $response_code = wp_remote_retrieve_response_code( $response );
-
-                       $this->maybe_throttle( wp_remote_retrieve_headers( $response ) );
-
-                       /*
-                        * Sometimes their API inexplicably returns a success code with an empty body, but will return a valid
-                        * response if the exact request is retried.
-                        */
-                       if ( 200 === $response_code && ! empty( wp_remote_retrieve_body( $response ) ) ) {
-                               break;
-                       }
-
-                       if ( in_array( $response_code, $breaking_codes, true ) ) {
-                               break;
-                       }
-
-                       $attempt_count++;
-
-                       /**
-                        * Action: Fires when tenacious_remote_get fails a request attempt.
-                        *
-                        * Note that the request parameter includes the request URL that contains a query string for the API key.
-                        * This should be redacted before outputting anywhere public.
-                        *
-                        * @param array $response
-                        * @param array $request
-                        * @param int   $attempt_count
-                        * @param int   $max_attempts
-                        */
-                       do_action( 'meetup_client_tenacious_remote_get_attempt', $response, compact( 'url', 'args' ), $attempt_count, $max_attempts );
-
-                       if ( $attempt_count < $max_attempts ) {
-                               $retry_after = wp_remote_retrieve_header( $response, 'retry-after' ) ?: 5;
-                               $wait        = min( $retry_after * $attempt_count, 30 );
-
-                               if ( 'cli' === php_sapi_name() ) {
-                                       echo "\nRequest failed $attempt_count times. Pausing for $wait seconds before retrying.";
-                               }
-
-                               sleep( $wait );
-                       }
-               }
-
-               if ( $attempt_count === $max_attempts && 'cli' === php_sapi_name() ) {
-                       if ( 200 !== $response_code || is_wp_error( $response ) ) {
-                               echo "\nRequest failed $attempt_count times. Giving up.";
-                       }
-               }
-
-               return $response;
</del><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">-         * Sign a request URL with our API key.
-        *
-        * @param string $request_url
-        *
-        * @return string
-        */
-       protected function sign_request_url( $request_url ) {
-               return add_query_arg( array(
-                       'sign' => true,
-                       'key'  => $this->api_key,
-               ), $request_url );
-       }
-
-       /**
</del><span class="cx" style="display: block; padding: 0 10px">          * Get the URL for the next page of results from a paginated API response.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @param array $response
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -297,27 +234,35 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * Check the rate limit status in an API response and delay further execution if necessary.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @param array $headers
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         *
+        * @return void
</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 maybe_throttle( $headers ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ protected static function throttle( $response ) {
+               $headers = wp_remote_retrieve_headers( $response );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( ! isset( $headers['x-ratelimit-remaining'], $headers['x-ratelimit-reset'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        return;
</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">                $remaining = absint( $headers['x-ratelimit-remaining'] );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $period    = absint( $headers['x-ratelimit-reset'    ] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $period    = absint( $headers['x-ratelimit-reset'] );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Pause more frequently than we need to, and for longer, just to be safe.
-               if ( $remaining > 2 ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         /**
+                * Don't throttle if we have sufficient requests remaining.
+                *
+                * We don't let this number get to 0, though, because there are scenarios where multiple processes are using
+                * the API at the same time, and there's no way for them to be aware of each other.
+                */
+               if ( $remaining > 3 ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         return;
</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">+                // Pause for longer than we need to, just to be safe.
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( $period < 2 ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $period = 2;
</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 ( 'cli' === php_sapi_name() ) {
-                       echo "\nPausing for $period seconds to avoid rate-limiting.";
-               }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         self::cli_message( "\nPausing for $period seconds to avoid rate-limiting." );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                sleep( $period );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -325,22 +270,15 @@
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="cx" style="display: block; padding: 0 10px">         * Extract error information from an API response and add it to our error handler.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Make sure you don't include the full $response in the error as data, as that could expose sensitive information
+        * from the request payload.
+        *
</ins><span class="cx" style="display: block; padding: 0 10px">          * @param array|WP_Error $response
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @return void
</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 handle_error_response( $response ) {
-               if ( is_wp_error( $response ) ) {
-                       $codes = $response->get_error_codes();
-
-                       foreach ( $codes as $code ) {
-                               $messages = $response->get_error_messages( $code );
-
-                               foreach ( $messages as $message ) {
-                                       $this->error->add( $code, $message );
-                               }
-                       }
-
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function handle_error_response( $response ) {
+               if ( parent::handle_error_response( $response ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         return;
</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">@@ -349,10 +287,16 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( isset( $data['errors'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        foreach ( $data['errors'] as $error ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $this->error->add( $error['code'], $error['message'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $this->error->add(
+                                       $error['code'],
+                                       $error['message']
+                               );
</ins><span class="cx" style="display: block; padding: 0 10px">                         }
</span><span class="cx" style="display: block; padding: 0 10px">                } elseif ( isset( $data['code'] ) && isset( $data['details'] ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $this->error->add( $data['code'], $data['details'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $this->error->add(
+                               $data['code'],
+                               $data['details']
+                       );
</ins><span class="cx" style="display: block; padding: 0 10px">                 } elseif ( $response_code ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $this->error->add(
</span><span class="cx" style="display: block; padding: 0 10px">                                'http_response_code',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -359,7 +303,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                sprintf( 'HTTP Status: %d', absint( $response_code ) )
</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">-                        $this->error->add( 'unknown_error', 'There was an unknown error.' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $this->error->add(
+                               'unknown_error',
+                               'There was an unknown error.'
+                       );
</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">@@ -384,33 +331,40 @@
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="cx" style="display: block; padding: 0 10px">         * Retrieve data about events associated with a set of groups.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * This automatically breaks up requests into chunks of 50 groups to avoid overloading the API.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * Because of the way that the Meetup API v3 endpoints are structured, we unfortunately have to make one request
+        * (or more, if there's pagination) for each group that we want events for. When there are hundreds of groups, and
+        * we are throttling to make sure we don't get rate-limited, this process can literally take several minutes.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @param array $group_ids The IDs of the groups to get events for.
-        * @param array $args      Optional. Additional request parameters.
-        *                         See https://www.meetup.com/meetup_api/docs/2/events/.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * So, when building the array for the $group_slugs parameter, it's important to filter out groups that you know
+        * will not provide relevant results. For example, if you want all events during a date range in the past, you can
+        * filter out groups that didn't join the chapter program until after your date range.
</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 when using date/time related parameters in the $args array, unlike other endpoints and fields in the
+        * Meetup API which use an epoch timestamp in milliseconds, this one requires a date/time string formatted in
+        * ISO 8601, without the timezone part. Because consistency is overrated.
+        *
+        * @param array $group_slugs The URL slugs of each group to retrieve events for. Also known as `urlname`.
+        * @param array $args        Optional. Additional request parameters.
+        *                           See https://www.meetup.com/meetup_api/docs/:urlname/events/#list
+        *
</ins><span class="cx" style="display: block; padding: 0 10px">          * @return array|WP_Error
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        public function get_events( array $group_ids, array $args = array() ) {
-               $url_base     = $this->api_base . '2/events';
-               $group_chunks = array_chunk( $group_ids, 50, true ); // Meetup API sometimes throws an error with chunk size larger than 50.
-               $events       = array();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function get_events( array $group_slugs, array $args = array() ) {
+               $events = 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">-                foreach ( $group_chunks as $chunk ) {
-                       $query_args = array_merge( array(
-                               'group_id' => implode( ',', $chunk ),
-                       ), $args );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( $this->debug ) {
+                       $chunked     = array_chunk( $group_slugs, 10 );
+                       $group_slugs = $chunked[0];
+               }
</ins><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 = add_query_arg( $query_args, $url_base );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         foreach ( $group_slugs as $group_slug ) {
+                       $response = $this->get_group_events( $group_slug, $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">-                        $data = $this->send_paginated_request( $request_url );
-
-                       if ( is_wp_error( $data ) ) {
-                               return $data;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 if ( is_wp_error( $response ) ) {
+                               return $response;
</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">-                        $events = array_merge( $events, $data );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $events = array_merge( $events, $response );
</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 $events;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -417,15 +371,16 @@
</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">-         * Retrieve data about the group. Calls https://www.meetup.com/meetup_api/docs/:urlname/#get
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * Retrieve details about a group.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @param string $group_slug The slug/urlname of a group.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @param array $args Optional. Additional request parameters.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @param array  $args       Optional. Additional request parameters.
+        *                           See https://www.meetup.com/meetup_api/docs/:urlname/#get
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @return array|WP_Error
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        public function get_group_details ( $group_slug, $args = array() ) {
-               $request_url = $this->api_base . "$group_slug";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function get_group_details( $group_slug, $args = array() ) {
+               $request_url = $this->api_base . sanitize_key( $group_slug );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! empty( $args ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $request_url = add_query_arg( $args, $request_url );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -435,15 +390,16 @@
</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">-         * Retrieve group members. Calls https://www.meetup.com/meetup_api/docs/:urlname/members/#list
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * Retrieve details about group members.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @param string $group_slug The slug/urlname of a group.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @param array $args Optional. Additional request parameters.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @param array  $args       Optional. Additional request parameters.
+        *                           See https://www.meetup.com/meetup_api/docs/:urlname/members/#list
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @return array|WP_Error
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        public function get_group_members ( $group_slug, $args = array() ) {
-               $request_url = $this->api_base . "$group_slug/members";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function get_group_members( $group_slug, $args = array() ) {
+               $request_url = $this->api_base . sanitize_key( $group_slug ) . '/members';
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! empty( $args ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $request_url = add_query_arg( $args, $request_url );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -457,12 +413,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @param string $group_slug The slug/urlname of a group.
</span><span class="cx" style="display: block; padding: 0 10px">         * @param array  $args       Optional. Additional request parameters.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         *                           See https://www.meetup.com/meetup_api/docs/:urlname/events/.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  *                           See https://www.meetup.com/meetup_api/docs/:urlname/events/#list
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @return array|WP_Error
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function get_group_events( $group_slug, array $args = array() ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $request_url = $this->api_base . "$group_slug/events";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $request_url = $this->api_base . sanitize_key( $group_slug ) . '/events';
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! empty( $args ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $request_url = add_query_arg( $args, $request_url );
</span></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsofficialwordpresseventsmeetupclassmeetupoauth2clientphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: sites/trunk/wordpress.org/public_html/wp-content/plugins/official-wordpress-events/meetup/class-meetup-oauth2-client.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/official-wordpress-events/meetup/class-meetup-oauth2-client.php                          (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/official-wordpress-events/meetup/class-meetup-oauth2-client.php    2019-08-13 21:08:02 UTC (rev 9100)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,407 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+namespace WordCamp\Utilities;
+
+use WP_Error;
+
+defined( 'WPINC' ) || die();
+
+/**
+ * Class Meetup_OAuth2_Client
+ *
+ * Implements the "Server Flow with User Credentials" for Meetup's OAuth2 authorization scheme.
+ *
+ * @see https://www.meetup.com/meetup_api/auth/#oauth2servercredentials
+ *
+ * @todo Maybe make some of the credential strings like consumer key, etc, class parameters that map to private
+ *       properties instead of constants. That way the client could be reused elsewhere in the Dotorg ecosystem with
+ *       different credentials, instead of being tightly coupled to WordCamp.
+ *
+ * Important: This class is used in multiple locations in the WordPress/WordCamp ecosystem. Because of complexities
+ * around SVN externals and the reliability of GitHub's SVN bridge during deploys, it was decided to maintain multiple
+ * copies of this file rather than have SVN externals pointing to one canonical source.
+ *
+ * If you make changes to this file, make sure they are propagated to the other locations:
+ *
+ * - wordcamp: wp-content/mu-plugins/utilities
+ * - wporg: wp-content/plugins/official-wordpress-events/meetup
+ *
+ * @package WordCamp\Utilities
+ */
+class Meetup_OAuth2_Client extends API_Client {
+       /**
+        * @var string
+        */
+       const CONSUMER_KEY = MEETUP_OAUTH_CONSUMER_KEY;
+
+       /**
+        * @var string
+        */
+       const CONSUMER_SECRET = MEETUP_OAUTH_CONSUMER_SECRET;
+
+       /**
+        * @var string
+        */
+       const REDIRECT_URI = MEETUP_OAUTH_CONSUMER_REDIRECT_URI;
+
+       /**
+        * @var string
+        */
+       const EMAIL = MEETUP_USER_EMAIL;
+
+       /**
+        * @var string
+        */
+       const PASSWORD = MEETUP_USER_PASSWORD;
+
+       /**
+        * @var string
+        */
+       const URL_AUTHORIZE = 'https://secure.meetup.com/oauth2/authorize';
+
+       /**
+        * @var string
+        */
+       const URL_ACCESS_TOKEN = 'https://secure.meetup.com/oauth2/access';
+
+       /**
+        * @var string
+        */
+       const URL_OAUTH_TOKEN = 'https://api.meetup.com/sessions';
+
+       /**
+        * @var string
+        */
+       const SITE_OPTION_KEY_OAUTH = 'meetup_oauth_token';
+
+       /**
+        * @var array
+        */
+       protected $oauth_token = array();
+
+       /**
+        * Meetup_OAuth2_Client constructor.
+        */
+       public function __construct() {
+               parent::__construct( array(
+                       /**
+                        * Response codes that should break the request loop.
+                        *
+                        * `200` (ok) is not in the list, because it needs to be handled conditionally.
+                        *  See API_Client::tenacious_remote_request.
+                        */
+                       'breaking_response_codes' => array(
+                               400, // Bad request.
+                               401, // Unauthorized (invalid key).
+                               429, // Too many requests (rate-limited).
+                               404, // Unable to find group
+                       ),
+               ) );
+       }
+
+       /**
+        * Generate the headers for a request.
+        *
+        * @param string $access_token Optional. Providing an access token will add an extra header.
+        *
+        * @return array
+        */
+       protected function get_headers( $access_token = '' ) {
+               $headers = array(
+                       'Accept' => 'application/json',
+               );
+
+               if ( $access_token ) {
+                       $headers['Authorization'] = "Bearer $access_token";
+               }
+
+               return $headers;
+       }
+
+       /**
+        * Step 1 in "Server Flow with User Credentials".
+        *
+        * @see https://www.meetup.com/meetup_api/auth/#oauth2servercredentials-auth
+        *
+        * @return array
+        */
+       protected function request_authorization_code() {
+               $authorization_code = array();
+
+               $request = array(
+                       'client_id'     => self::CONSUMER_KEY,
+                       'redirect_uri'  => self::REDIRECT_URI,
+                       'response_type' => 'anonymous_code',
+               );
+
+               $args = array(
+                       'headers' => $this->get_headers(),
+                       'body'    => $request,
+               );
+
+               $response = $this->tenacious_remote_post( self::URL_AUTHORIZE, $args );
+
+               if ( 200 === wp_remote_retrieve_response_code( $response ) ) {
+                       $body = json_decode( wp_remote_retrieve_body( $response ), true );
+
+                       if ( ! empty( $body['code'] ) ) {
+                               $authorization_code = $body;
+                       } else {
+                               $this->error->add(
+                                       'unexpected_oauth_authorization_code_response',
+                                       'The Meetup OAuth API response did not provide the expected data.'
+                               );
+                       }
+               } else {
+                       $this->handle_error_response( $response );
+               }
+
+               return $authorization_code;
+       }
+
+       /**
+        * Request one of various types of tokens from the Meetup OAuth API.
+        *
+        * Setting $type to 'server_token' is for Step 2 in "Server Flow with User Credentials". This gets the "server
+        * access token" which is then used to request the "oauth access token".
+        * @see https://www.meetup.com/meetup_api/auth/#oauth2servercredentials-access
+        *
+        * Setting $type to 'oauth_token' is for Step 3 in "Server Flow with User Credentials". Technically in the
+        * documentation, this token is also called an "access token", but here we're calling it the "oauth access token" to
+        * differentiate it from the "server access token". Also, why are two separate access tokens necessary??
+        * @see https://www.meetup.com/meetup_api/auth/#oauth2servercredentials-accesspro
+        *
+        * Setting $type to 'refresh_token' will request a new server access token for Step 2. This is for when the oauth
+        * token from Step 3 is expired. The refreshed server token can then be used to obtain a new Step 3
+        * oauth token. This skips the authorization code request (Step 1), but seems largely superfluous since a second
+        * oauth token request must still be made with the new server token. Also, the refresh_token string used to refresh
+        * the server token needs to come from the oauth token array, **not the server token array**. This is not what the
+        * documentation implies. Why is this so terrible??
+        * @see https://www.meetup.com/meetup_api/auth/#oauth2server-refresh
+        *
+        * Check the `get_oauth_token` method to see how these token request flows work.
+        *
+        * @param string $type The type of token request. 'server_token', 'refresh_token', or 'oauth_token'.
+        * @param array  $args The pieces of data required to make the given type of request.
+        *
+        * @return array|mixed|object
+        */
+       protected function request_token( $type, array $args = array() ) {
+               $token = array();
+
+               $request_url     = '';
+               $request_headers = $this->get_headers();
+               $request_body    = array();
+
+               switch( $type ) {
+                       case 'server_token': // Request a new server access token.
+                               $args = wp_parse_args( $args, array(
+                                       'code' => '',
+                               ) );
+
+                               $request_url  = self::URL_ACCESS_TOKEN;
+                               $request_body = array(
+                                       'client_id'     => self::CONSUMER_KEY,
+                                       'client_secret' => self::CONSUMER_SECRET,
+                                       'redirect_uri'  => self::REDIRECT_URI,
+                                       'code'          => $args['code'],
+                                       'grant_type'    => 'anonymous_code',
+                               );
+                               break;
+                       case 'refresh_token': // Refresh a server access token.
+                               $args = wp_parse_args( $args, array(
+                                       'refresh_token' => '',
+                               ) );
+
+                               $request_url  = self::URL_ACCESS_TOKEN;
+                               $request_body = array(
+                                       'client_id'     => self::CONSUMER_KEY,
+                                       'client_secret' => self::CONSUMER_SECRET,
+                                       'refresh_token' => $args['refresh_token'],
+                                       'grant_type'    => 'refresh_token',
+                               );
+                               break;
+                       case 'oauth_token': // Request a new oauth token.
+                               $args = wp_parse_args( $args, array(
+                                       'access_token' => '',
+                               ) );
+
+                               $request_url     = self::URL_OAUTH_TOKEN;
+                               $request_headers = $this->get_headers( $args['access_token'] );
+                               $request_body    = array(
+                                       'email'    => self::EMAIL,
+                                       'password' => self::PASSWORD,
+                               );
+                               break;
+               }
+
+               $request_args = array(
+                       'headers' => $request_headers,
+                       'body'    => $request_body,
+               );
+
+               $response = $this->tenacious_remote_post( $request_url, $request_args );
+
+               if ( 200 === wp_remote_retrieve_response_code( $response ) ) {
+                       $body = json_decode( wp_remote_retrieve_body( $response ), true );
+
+                       if ( $this->is_valid_token( $body, $type ) ) {
+                               $token = $body;
+
+                               $token['timestamp'] = time();
+                       } else {
+                               $this->error->add(
+                                       // Don't include the entire response in the error data, because it might include sensitive
+                                       // data from the request payload.
+                                       'unexpected_oauth_token_response',
+                                       'The Meetup OAuth API response did not provide the expected data.'
+                               );
+                       }
+               } else {
+                       $this->handle_error_response( $response );
+               }
+
+               return $token;
+       }
+
+       /**
+        * Get the oauth access token needed to make requests to the Meetup API.
+        *
+        * This encompasses all three of the steps in the "Server Flow with User Credentials" flow, so it's the only
+        * method that should be called directly.
+        *
+        * @see https://www.meetup.com/meetup_api/auth/#oauth2servercredentials
+        *
+        * This stores a successfully retrieved token array to the database for repeated use, until it expires.
+        *
+        * @return string
+        */
+       public function get_oauth_token() {
+               if ( $this->oauth_token && ! $this->is_expired_token( $this->oauth_token ) ) {
+                       return $this->oauth_token['oauth_token'];
+               }
+
+               $oauth_token   = '';
+               $needs_caching = false;
+
+               $token = get_site_option( self::SITE_OPTION_KEY_OAUTH, array() );
+
+               if ( ! $this->is_valid_token( $token, 'oauth_token' ) ) {
+                       $authorization = $this->request_authorization_code(); // Step 1.
+                       $server_token  = $this->request_token( 'server_token', $authorization ); // Step 2.
+                       $token         = $this->request_token( 'oauth_token', $server_token ); // Step 3.
+                       $needs_caching = true;
+               } elseif ( $this->is_expired_token( $token ) ) {
+                       $server_token  = $this->request_token( 'refresh_token', $token ); // Alternate for Steps 1 & 2.
+                       $token         = $this->request_token( 'oauth_token', $server_token ); // Step 3.
+                       $needs_caching = true;
+               }
+
+               if ( $this->is_valid_token( $token, 'oauth_token' ) ) {
+                       $this->oauth_token = $token;
+
+                       if ( $needs_caching ) {
+                               update_site_option( self::SITE_OPTION_KEY_OAUTH, $token );
+                       }
+
+                       $oauth_token = $this->oauth_token['oauth_token'];
+               }
+
+               return $oauth_token;
+       }
+
+       /**
+        * Check if a token array has the required keys.
+        *
+        * @param array  $token The token array to check.
+        * @param string $type  The type of token. Either 'access' or 'oauth'.
+        *
+        * @return bool
+        */
+       protected function is_valid_token( $token, $type ) {
+               $valid_types = array( 'server_token', 'refresh_token', 'oauth_token' );
+
+               if ( ! is_array( $token ) || ! in_array( $type, $valid_types, true ) ) {
+                       return false;
+               }
+
+               switch ( $type ) {
+                       case 'server_token':
+                       case 'refresh_token':
+                               $required_properties = array(
+                                       'access_token' => '',
+                               );
+                               break;
+                       case 'oauth_token':
+                       default:
+                               $required_properties = array(
+                                       'oauth_token'   => '',
+                                       'refresh_token' => '',
+                                       'expires_in'    => '',
+                               );
+                               break;
+               }
+
+               $missing_properties = array_diff_key( $required_properties, $token );
+
+               return empty( $missing_properties );
+       }
+
+       /**
+        * Check if a token has expired since it was retrieved from the OAuth API.
+        *
+        * @param array $token
+        *
+        * @return bool
+        */
+       protected function is_expired_token( array $token ) {
+               return absint( $token['timestamp'] ) + absint( $token['expires_in'] ) <= time();
+       }
+
+       /**
+        * Extract error information from an API response and add it to our error handler.
+        *
+        * Make sure you don't include the full $response in the error as data, as that could expose sensitive information
+        * from the request payload.
+        *
+        * @param array|WP_Error $response
+        *
+        * @return void
+        */
+       public function handle_error_response( $response ) {
+               if ( parent::handle_error_response( $response ) ) {
+                       return;
+               }
+
+               $response_code = wp_remote_retrieve_response_code( $response );
+               $data          = json_decode( wp_remote_retrieve_body( $response ), true );
+
+               if ( isset( $data['errors'] ) ) {
+                       foreach ( $data['errors'] as $error ) {
+                               $this->error->add(
+                                       $error['code'],
+                                       $error['message']
+                               );
+                       }
+               } elseif ( isset( $data['error'] ) ) {
+                       $this->error->add(
+                               'oauth_error',
+                               $data['error']
+                       );
+               } elseif ( isset( $data['code'] ) && isset( $data['details'] ) ) {
+                       $this->error->add(
+                               $data['code'],
+                               $data['details']
+                       );
+               } elseif ( $response_code ) {
+                       $this->error->add(
+                               'http_response_code',
+                               sprintf( 'HTTP Status: %d', absint( $response_code ) )
+                       );
+               } else {
+                       $this->error->add(
+                               'unknown_error',
+                               'There was an unknown error.'
+                       );
+               }
+       }
+}
</ins></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsofficialwordpresseventsofficialwordpresseventsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordpress.org/public_html/wp-content/plugins/official-wordpress-events/official-wordpress-events.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/official-wordpress-events/official-wordpress-events.php  2019-08-10 16:00:56 UTC (rev 9099)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/official-wordpress-events/official-wordpress-events.php    2019-08-13 21:08:02 UTC (rev 9100)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -7,6 +7,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> Author:      WordPress.org Meta Team
</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">+use WordCamp\Utilities\API_Client;
</ins><span class="cx" style="display: block; padding: 0 10px"> use WordCamp\Utilities\Meetup_Client;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> class Official_WordPress_Events {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -37,9 +38,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * Constructor
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function __construct() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                add_action( 'wp_enqueue_scripts',           array( $this, 'enqueue_scripts'    ) );
-               add_action( 'owpe_prime_events_cache',      array( $this, 'prime_events_cache' ) );
-               add_action( 'owpe_mark_deleted_meetups',    array( $this, 'mark_deleted_meetups' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         add_action( 'wp_enqueue_scripts',               array( $this, 'enqueue_scripts'       ) );
+               add_action( 'owpe_prime_events_cache',          array( $this, 'prime_events_cache'    ) );
+               add_action( 'owpe_mark_deleted_meetups',        array( $this, 'mark_deleted_meetups'  ) );
+               add_action( 'api_client_handle_error_response', array( $this, 'handle_error_response' ), 10, 3 );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 add_shortcode( 'official_wordpress_events', array( $this, 'render_events'      ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! wp_next_scheduled( 'owpe_prime_events_cache' ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -52,6 +55,25 @@
</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">+         * Get an instance of the generic API Client, loading files first as necessary.
+        *
+        * @return API_Client
+        */
+       protected function get_api_client() {
+               if ( ! class_exists( '\WordCamp\Utilities\API_Client' ) ) {
+                       $files = array(
+                               'class-api-client.php',
+                       );
+
+                       foreach ( $files as $file ) {
+                               require_once trailingslashit( __DIR__ ) . "meetup/$file";
+                       }
+               }
+
+               return new API_Client();
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Get an instance of the Meetup Client, loading files first as necessary.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @return Meetup_Client
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -59,6 +81,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">        protected function get_meetup_client() {
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! class_exists( '\WordCamp\Utilities\Meetup_Client' ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $files = array(
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                'class-api-client.php',
+                               'class-meetup-oauth2-client.php',
</ins><span class="cx" style="display: block; padding: 0 10px">                                 'class-meetup-client.php',
</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">@@ -208,8 +232,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @return array
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        protected function fetch_upcoming_events() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $events = array_merge( $this->get_wordcamp_events(), $this->get_meetup_events() );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $wordcamp_events = $this->get_wordcamp_events();
+               $meetup_events   = $this->get_meetup_events();
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $events = array_merge( $wordcamp_events, $meetup_events );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 return $events;
</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">@@ -238,16 +265,20 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @return array
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        protected function get_wordcamp_events() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $request_params = array(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $api_client = $this->get_api_client();
+
+               // Note: With the number of WordCamps per year growing fast, we may need to batch requests in the near future, like we do for meetups.
+               $request_url = add_query_arg( array(
</ins><span class="cx" style="display: block; padding: 0 10px">                         'status'   => array( 'wcpt-scheduled', 'wcpt-cancelled' ),
</span><span class="cx" style="display: block; padding: 0 10px">                        'per_page' => 100,
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // Note: With the number of WordCamps per year growing fast, we may need to batch requests in the near future, like we do for meetups
-               );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         ), self::WORDCAMP_API_BASE_URL . 'wp/v2/wordcamps' );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $endpoint = add_query_arg( $request_params, self::WORDCAMP_API_BASE_URL . 'wp/v2/wordcamps' );
-               $response = $this->remote_get( esc_url_raw( $endpoint ) );
-               $events   = $this->parse_wordcamp_events( $response );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $response = $api_client->tenacious_remote_get( $request_url );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $api_client->handle_error_response( $response, $request_url );
+
+               $events = $this->parse_wordcamp_events( $response );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->log( sprintf( 'returning %d events', count( $events ) ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                return $events;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -338,75 +369,108 @@
</span><span class="cx" style="display: block; padding: 0 10px">        protected function get_meetup_events() {
</span><span class="cx" style="display: block; padding: 0 10px">                $events = 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">-                $client = $this->get_meetup_client();
-               if ( ! empty( $client->error->errors ) ) {
-                       $this->log( 'Failed to instantiate meetup client: ' . wp_json_encode( $client->error ), true );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Fetching events for a large number of groups from the Meetup API is currently a very inefficient process.
+               ini_set( 'memory_limit', '900M' );
+               ini_set( 'max_execution_time', 500 );
+
+               $meetup_client = $this->get_meetup_client();
+               if ( ! empty( $meetup_client->error->errors ) ) {
+                       $this->log( 'Failed to instantiate meetup client: ' . wp_json_encode( $meetup_client->error ), true );
</ins><span class="cx" style="display: block; padding: 0 10px">                         return $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">-                $groups = $client->get_groups();
-               if ( ! empty( $client->error->errors ) ) {
-                       $this->log( 'Failed to fetch groups: ' . wp_json_encode( $client->error ), true );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $groups = $meetup_client->get_groups();
+               if ( ! empty( $meetup_client->error->errors ) ) {
+                       $this->log( 'Failed to fetch groups: ' . wp_json_encode( $meetup_client->error ), true );
</ins><span class="cx" style="display: block; padding: 0 10px">                         return $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">-                $meetups = $client->get_events( wp_list_pluck( $groups, 'id' ) );
-               if ( ! empty( $client->error->errors ) ) {
-                       $this->log( 'Failed to fetch meetups: ' . wp_json_encode( $client->error ), true );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $yesterday    = date( 'c', strtotime( '-1 day' ) );
+               $one_year_out = date( 'c', strtotime( '+1 year' ) );
+               $meetups      = $meetup_client->get_events(
+                       wp_list_pluck( $groups, 'urlname' ),
+                       array(
+                               // We want cancelled events too so they will be updated in our database table.
+                               'status'          => 'upcoming,cancelled',
+                               // We don't want cancelled events in the past, but need some leeway here for timezones.
+                               'no_earlier_than' => substr( $yesterday, 0, strpos( $yesterday, '+' ) ),
+                               // We don't need to cache events happening more than a year from now.
+                               'no_later_than'   => substr( $one_year_out, 0, strpos( $one_year_out, '+' ) ),
+                       )
+               );
+               if ( ! empty( $meetup_client->error->errors ) ) {
+                       $this->log( 'Failed to fetch meetups: ' . wp_json_encode( $meetup_client->error ), true );
</ins><span class="cx" style="display: block; padding: 0 10px">                         return $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">-                                        foreach ( $meetups as $meetup ) {
-                                               if ( empty( $meetup['id'] ) || empty( $meetup['name'] ) ) {
-                                                       $this->log( 'Malformed meetup: ' . wp_json_encode( $meetup ) );
-                                                       continue;
-                                               }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $events = $this->parse_meetup_events( $meetups );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                                $start_timestamp = ( $meetup['time'] / 1000 ) + ( $meetup['utc_offset'] / 1000 );    // convert to seconds
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->log( sprintf( 'returning %d events', count( $events ) ) );
</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 ( isset( $meetup['venue'] ) ) {
-                                                       $location = $this->format_meetup_venue_location( $meetup['venue'] );
-                                               } else {
-                                                       $geocoded_location = $this->reverse_geocode( $meetup['group']['group_lat'], $meetup['group']['group_lon'] );
-                                                       $location_parts    = $this->parse_reverse_geocode_address( $geocoded_location );
-                                                       $location          = sprintf(
-                                                               '%s%s%s',
-                                                               $location_parts['city'] ?? '',
-                                                               empty( $location_parts['state'] )        ? '' : ', ' . $location_parts['state'],
-                                                               empty( $location_parts['country_name'] ) ? '' : ', ' . $location_parts['country_name']
-                                                       );
-                                                       $location         = trim( $location, ", \t\n\r\0\x0B" );
-                                               }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         return $events;
+       }
</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 ( ! empty( $meetup['venue']['country'] ) ) {
-                                                       $country_code = $meetup['venue']['country'];
-                                               } elseif ( ! empty( $location_parts['country_code'] ) ) {
-                                                       $country_code = $location_parts['country_code'];
-                                               } else {
-                                                       $country_code = '';
-                                               }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+        * Parse meetup events out of a response from the Meetup API.
+        *
+        * @param array $meetups
+        *
+        * @return array
+        */
+       protected function parse_meetup_events( $meetups ) {
+               $events = 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">-                                                $events[] = new Official_WordPress_Event( array(
-                                                       'type'            => 'meetup',
-                                                       'source_id'       => $meetup['id'],
-                                                       'status'          => 'upcoming' === $meetup['status'] ? 'scheduled' : 'cancelled',
-                                                       'title'           => $meetup['name'],
-                                                       'url'             => $meetup['event_url'],
-                                                       'meetup_name'     => $meetup['group']['name'],
-                                                       'meetup_url'      => sprintf( 'https://www.meetup.com/%s/', $meetup['group']['urlname'] ),
-                                                       'description'     => $meetup['description'] ?? '',
-                                                       'num_attendees'   => $meetup['yes_rsvp_count'],
-                                                       'start_timestamp' => $start_timestamp,
-                                                       'end_timestamp'   => ( empty ( $meetup['duration'] ) ? $start_timestamp : $start_timestamp + ( $meetup['duration'] / 1000 ) ), // convert to seconds
-                                                       'location'        => $location,
-                                                       'country_code'    => $country_code,
-                                                       'latitude'        => empty( $meetup['venue']['lat'] ) ? $meetup['group']['group_lat'] : $meetup['venue']['lat'],
-                                                       'longitude'       => empty( $meetup['venue']['lon'] ) ? $meetup['group']['group_lon'] : $meetup['venue']['lon'],
-                                               ) );
-                                       }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         foreach ( $meetups as $meetup ) {
+                       if ( empty( $meetup['id'] ) || empty( $meetup['name'] ) ) {
+                               $this->log( 'Malformed meetup: ' . wp_json_encode( $meetup ) );
+                               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">-                $this->log( sprintf( 'returning %d events', count( $events ) ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $start_timestamp = ( $meetup['time'] / 1000 ) + ( $meetup['utc_offset'] / 1000 ); // convert to seconds
+                       $latitude        = ! empty( $meetup['venue']['lat'] ) ? $meetup['venue']['lat'] : $meetup['group']['lat'];
+                       $longitude       = ! empty( $meetup['venue']['lon'] ) ? $meetup['venue']['lon'] : $meetup['group']['lon'];
</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 ( isset( $meetup['venue'] ) ) {
+                               $location = $this->format_meetup_venue_location( $meetup['venue'] );
+                       } else {
+                               $geocoded_location = $this->reverse_geocode( $latitude, $longitude );
+                               $location_parts    = $this->parse_reverse_geocode_address( $geocoded_location );
+                               $location          = sprintf(
+                                       '%s%s%s',
+                                       $location_parts['city'] ?? '',
+                                       empty( $location_parts['state'] )        ? '' : ', ' . $location_parts['state'],
+                                       empty( $location_parts['country_name'] ) ? '' : ', ' . $location_parts['country_name']
+                               );
+                               $location          = trim( $location, ", \t\n\r\0\x0B" );
+                       }
+
+                       if ( ! empty( $meetup['venue']['country'] ) ) {
+                               $country_code = $meetup['venue']['country'];
+                       } elseif ( ! empty( $location_parts['country_code'] ) ) {
+                               $country_code = $location_parts['country_code'];
+                       } else {
+                               $country_code = '';
+                       }
+
+                       $events[] = new Official_WordPress_Event( array(
+                               'type'            => 'meetup',
+                               'source_id'       => $meetup['id'],
+                               'status'          => 'upcoming' === $meetup['status'] ? 'scheduled' : 'cancelled',
+                               'title'           => $meetup['name'],
+                               'url'             => $meetup['link'],
+                               'meetup_name'     => $meetup['group']['name'],
+                               'meetup_url'      => sprintf( 'https://www.meetup.com/%s/', $meetup['group']['urlname'] ),
+                               'description'     => $meetup['description'] ?? '',
+                               'num_attendees'   => $meetup['yes_rsvp_count'],
+                               'start_timestamp' => $start_timestamp,
+                               'end_timestamp'   => ( empty ( $meetup['duration'] ) ? $start_timestamp : $start_timestamp + ( $meetup['duration'] / 1000 ) ), // convert to seconds
+                               'location'        => $location,
+                               'country_code'    => $country_code,
+                               'latitude'        => $latitude,
+                               'longitude'       => $longitude,
+                       ) );
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 return $events;
</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">@@ -430,14 +494,17 @@
</span><span class="cx" style="display: block; padding: 0 10px">                // Rough attempt at avoiding rate limit.
</span><span class="cx" style="display: block; padding: 0 10px">                usleep( 75000 );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $response = $this->remote_get( sprintf(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $api_client  = $this->get_api_client();
+               $request_url = sprintf(
</ins><span class="cx" style="display: block; padding: 0 10px">                         'https://maps.googleapis.com/maps/api/geocode/json?latlng=%s,%s&sensor=false&key=%s',
</span><span class="cx" style="display: block; padding: 0 10px">                        $latitude,
</span><span class="cx" style="display: block; padding: 0 10px">                        $longitude,
</span><span class="cx" style="display: block; padding: 0 10px">                        OFFICIAL_WP_EVENTS_GOOGLE_MAPS_API_KEY
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                ) );
-               $body = json_decode( wp_remote_retrieve_body( $response ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $response = $api_client->tenacious_remote_get( $request_url );
+               $body     = json_decode( wp_remote_retrieve_body( $response ) );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( ! is_wp_error( $response ) && isset( $body->results ) && empty( $body->error_message ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $this->log( 'geocode successful' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -447,6 +514,7 @@
</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">                else {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        $api_client->handle_error_response( $response, $request_url );
</ins><span class="cx" style="display: block; padding: 0 10px">                         $this->log( 'geocode failed: ' . wp_json_encode( $response ) );
</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">@@ -570,116 +638,76 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                // The event is missing from a valid response, so assume that it's been deleted.
</span><span class="cx" style="display: block; padding: 0 10px">                                $wpdb->update( self::EVENTS_TABLE, array( 'status' => 'deleted' ), array( 'id' => $db_event->id ) );
</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 ( 'cli' === php_sapi_name() ) {
-                                       echo "\nMarked {$db_event->source_id} as deleted.";
-                               }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $this->log( "Marked {$db_event->source_id} as deleted." );
</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="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">-         * Wrapper for wp_remote_get()
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * Error logging and notification.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * This adds error logging/notification.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * Hooked to `api_client_handle_error_response`.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @param string $url
-        * @param array  $args
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @param array|WP_Error $response
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @return false|array|WP_Error False if a valid $url was not passed; otherwise the results from wp_remote_get()
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @return void
</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 remote_get( $url, $args = array() ) {
-               $response = $error = false;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ protected function handle_error_response( $response, $request_url, $request_args ) {
+               $error = null;
</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 ( $url ) {
-                       $args['timeout'] = 30;
-                       $response        = wp_remote_get( $url, $args );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $response_code    = wp_remote_retrieve_response_code( $response );
+               $response_message = wp_remote_retrieve_response_message( $response );
+               $response_body    = wp_remote_retrieve_body( $response );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $this->maybe_pause( wp_remote_retrieve_headers( $response ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( is_wp_error( $response ) ) {
+                       $error_messages = implode( ', ', $response->get_error_messages() );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $response_code    = wp_remote_retrieve_response_code( $response );
-                       $response_message = wp_remote_retrieve_response_message( $response );
-                       $response_body    = wp_remote_retrieve_body( $response );
-
-                       if ( is_wp_error( $response ) ) {
-                               $error_messages = implode( ', ', $response->get_error_messages() );
-
-                               if ( false === strpos( $error_messages, 'Operation timed out' ) ) {
-                                       $error = sprintf(
-                                               'Received WP_Error message: %s; Request was to %s; Arguments were: %s',
-                                               $error_messages,
-                                               $url,
-                                               print_r( $args, true )
-                                       );
-                               }
-                       } elseif ( 200 != $response_code ) {
-                               // trigger_error() has a message limit of 1024 bytes, so we truncate $response['body'] to make sure that $body doesn't get truncated.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 if ( false === strpos( $error_messages, 'Operation timed out' ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                                 $error = sprintf(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        "HTTP Code: %d\nMessage: %s\nBody: %s\nRequest URL: %s\nArgs: %s",
-                                       $response_code,
-                                       sanitize_text_field( $response_message ),
-                                       substr( sanitize_text_field( $response_body ), 0, 500 ),
-                                       $url,
-                                       print_r( $args, true )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 'Received WP_Error message: %s; Request was to %s; Arguments were: %s',
+                                       $error_messages,
+                                       $request_url,
+                                       print_r( $request_args, true )
</ins><span class="cx" style="display: block; padding: 0 10px">                                 );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
-                               $response = new WP_Error( 'owe_invalid_http_response', 'Invalid HTTP response code', $response );
</del><span class="cx" style="display: block; padding: 0 10px">                         }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
-                       if ( $error ) {
-                               $error = preg_replace( '/&key=[a-z0-9]+/i', '&key=[redacted]', $error );
-                               trigger_error( sprintf(
-                                       '%s error for %s: %s',
-                                       __METHOD__,
-                                       parse_url( site_url(), PHP_URL_HOST ),
-                                       sanitize_text_field( $error )
-                               ), E_USER_WARNING );
-
-                               $to = apply_filters( 'owe_error_email_addresses', array() );
-                               if ( $to && ( ! defined( 'WPORG_SANDBOXED_REQUEST' ) || ! WPORG_SANDBOXED_REQUEST ) ) {
-                                       wp_mail(
-                                               $to,
-                                               sprintf(
-                                                       '%s error for %s',
-                                                       __METHOD__,
-                                                       parse_url( site_url(), PHP_URL_HOST )
-                                               ),
-                                               sanitize_text_field( $error )
-                                       );
-                               }
-                       }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         } elseif ( 200 !== $response_code ) {
+                       // trigger_error() has a message limit of 1024 bytes, so we truncate $response['body'] to make sure that $body doesn't get truncated.
+                       $error = sprintf(
+                               "HTTP Code: %d\nMessage: %s\nBody: %s\nRequest URL: %s\nArgs: %s",
+                               $response_code,
+                               sanitize_text_field( $response_message ),
+                               substr( sanitize_text_field( $response_body ), 0, 500 ),
+                               $request_url,
+                               print_r( $request_args, true )
+                       );
</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">-                return $response;
-       }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( $error ) {
+                       $error = preg_replace( '/&key=[a-z0-9]+/i', '&key=[redacted]', $error );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        /**
-        * Maybe pause the script to avoid rate limiting
-        *
-        * @param array $headers
-        */
-       protected function maybe_pause( $headers ) {
-               if ( ! isset( $headers['x-ratelimit-remaining'], $headers['x-ratelimit-reset'] ) ) {
-                       return;
-               }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $this->log( sanitize_text_field( $error ), true );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $remaining = absint( $headers['x-ratelimit-remaining'] );
-               $period    = absint( $headers['x-ratelimit-reset'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 trigger_error( sprintf(
+                               '%s error for %s: %s',
+                               __METHOD__,
+                               parse_url( site_url(), PHP_URL_HOST ),
+                               sanitize_text_field( $error )
+                       ), E_USER_WARNING );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Pause more frequently than we need to, and for longer, just to be safe
-               if ( $remaining > 2 ) {
-                       return;
-               }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $to = apply_filters( 'owe_error_email_addresses', 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 ( $period < 2 ) {
-                       $period = 2;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 if ( $to && ( ! defined( 'WPORG_SANDBOXED_REQUEST' ) || ! WPORG_SANDBOXED_REQUEST ) ) {
+                               wp_mail(
+                                       $to,
+                                       sprintf(
+                                               '%s error for %s',
+                                               __METHOD__,
+                                               parse_url( site_url(), PHP_URL_HOST )
+                                       ),
+                                       sanitize_text_field( $error )
+                               );
+                       }
</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 ( 'cli' == php_sapi_name() ) {
-                       echo "\nPausing for $period seconds to avoid rate-limiting.";
-               }
-
-               $this->log( 'sleeping to avoid api rate limit' );
-               sleep( $period );
</del><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">@@ -697,6 +725,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $limit = 500;
</span><span class="cx" style="display: block; padding: 0 10px">                $api_keys = array( MEETUP_API_KEY, OFFICIAL_WP_EVENTS_GOOGLE_MAPS_API_KEY );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                if ( 'cli' === php_sapi_name() ) {
+                       echo $message;
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( $write_to_disk ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        error_log( sprintf(
</span><span class="cx" style="display: block; padding: 0 10px">                                /*
</span></span></pre>
</div>
</div>

</body>
</html>