<!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>[6275] sites/trunk/api.wordpress.org/public_html/events/1.0: Events: Stick an upcoming WordCamp to the response to improve visibility.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://meta.trac.wordpress.org/changeset/6275">6275</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/6275","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>iandunn</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2017-12-14 23:45:15 +0000 (Thu, 14 Dec 2017)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Events: Stick an upcoming WordCamp to the response to improve visibility.
Fixes <a href="http://meta.trac.wordpress.org/ticket/2994">#2994</a>
Props metalandcoffee for the initial patch</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunkapiwordpressorgpublic_htmlevents10indexphp">sites/trunk/api.wordpress.org/public_html/events/1.0/index.php</a></li>
<li><a href="#sitestrunkapiwordpressorgpublic_htmlevents10teststestindexphp">sites/trunk/api.wordpress.org/public_html/events/1.0/tests/test-index.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkapiwordpressorgpublic_htmlevents10indexphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/api.wordpress.org/public_html/events/1.0/index.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/api.wordpress.org/public_html/events/1.0/index.php 2017-12-14 23:33:51 UTC (rev 6274)
+++ sites/trunk/api.wordpress.org/public_html/events/1.0/index.php 2017-12-14 23:45:15 UTC (rev 6275)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -14,6 +14,15 @@
</span><span class="cx" style="display: block; padding: 0 10px"> /*
</span><span class="cx" style="display: block; padding: 0 10px"> * Short-circuit some requests if a traffic spike is larger than we can handle.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * THROTTLE_STICKY_WORDCAMPS prevents the additional `SELECT` query in `get_sticky_wordcamp()`. This is the
+ * least intrusive throttle for users, and should be tried first.
+ *
+ * If that doesn't help enough, then start throttling ip2location, since those happen automatically
+ * and are therefore less likely to be noticed by users. Throttling Geonames should be a last
+ * resort, since users will notice those the most, and will sometimes retry their requests,
+ * which makes the problem worse.
+ *
+ * THROTTLE_{ GEONAMES | IP2LOCATION }
</ins><span class="cx" style="display: block; padding: 0 10px"> * - A value of `0` means that 0% of requests will be throttled.
</span><span class="cx" style="display: block; padding: 0 10px"> * - A value of `100` means that all cache-miss requests will be short-circuited with an error.
</span><span class="cx" style="display: block; padding: 0 10px"> * - Any value `n` between `0` and `100` means that `n%` of cache-miss requests will be short-circuited.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -21,10 +30,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * In all of the above scenarios, requests that have cached results will always be served.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ define( 'THROTTLE_STICKY_WORDCAMPS', false );
</ins><span class="cx" style="display: block; padding: 0 10px"> define( 'THROTTLE_GEONAMES', 0 );
</span><span class="cx" style="display: block; padding: 0 10px"> define( 'THROTTLE_IP2LOCATION', 0 );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> defined( 'DAY_IN_SECONDS' ) or define( 'DAY_IN_SECONDS', 60 * 60 * 24 );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ defined( 'WEEK_IN_SECONDS' ) or define( 'WEEK_IN_SECONDS', 7 * DAY_IN_SECONDS );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // The test suite just needs the functions defined and doesn't want any headers or output
</span><span class="cx" style="display: block; padding: 0 10px"> if ( defined( 'RUNNING_TESTS' ) && RUNNING_TESTS ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -131,7 +142,9 @@
</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"> if ( $location ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $event_args = array();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $event_args = array(
+ 'is_client_core' => is_client_core( $_SERVER['HTTP_USER_AGENT'] ),
+ );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( isset( $_REQUEST['number'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $event_args['number'] = $_REQUEST['number'];
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -641,6 +654,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> ), 'ARRAY_A' );
</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 upcoming events for the requested location.
+ *
+ * @param array $args
+ *
+ * @return array
+ */
</ins><span class="cx" style="display: block; padding: 0 10px"> function get_events( $args = array() ) {
</span><span class="cx" style="display: block; padding: 0 10px"> global $wpdb, $cache_life, $cache_group;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -651,6 +671,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $args['number'] = $args['number'] ?? 10;
</span><span class="cx" style="display: block; padding: 0 10px"> $args['number'] = max( 0, min( $args['number'], 100 ) );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Distances in kilometers
+ $event_distances = array(
+ 'meetup' => 100,
+ 'wordcamp' => 400,
+ );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $cache_key = 'events:' . md5( serialize( $args ) );
</span><span class="cx" style="display: block; padding: 0 10px"> if ( false !== ( $data = wp_cache_get( $cache_key, $cache_group ) ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return $data;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -664,11 +690,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // If we want nearby events, create a WHERE based on a bounded box of lat/long co-ordinates.
</span><span class="cx" style="display: block; padding: 0 10px"> if ( !empty( $args['nearby'] ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Distances in kilometers
- $event_distances = array(
- 'meetup' => 100,
- 'wordcamp' => 400,
- );
</del><span class="cx" style="display: block; padding: 0 10px"> $nearby_where = array();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( $event_distances as $type => $distance ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -695,6 +716,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Just show upcoming events
</span><span class="cx" style="display: block; padding: 0 10px"> $wheres[] = '`date_utc` >= %s';
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> // Dates are in local-time not UTC, so the API output will contain events that have already happened in some parts of the world.
</span><span class="cx" style="display: block; padding: 0 10px"> // TODO update this when the UTC dates are stored.
</span><span class="cx" style="display: block; padding: 0 10px"> $sql_values[] = gmdate( 'Y-m-d', time() - DAY_IN_SECONDS );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -723,6 +745,15 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $sql_values
</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">+ if ( should_stick_wordcamp( $args, $raw_events ) ) {
+ $sticky_wordcamp = get_sticky_wordcamp( $args, $event_distances['wordcamp'] );
+
+ if ( $sticky_wordcamp ) {
+ array_pop( $raw_events );
+ array_push( $raw_events, $sticky_wordcamp );
+ }
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $events = array();
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( $raw_events as $event ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $events[] = array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -742,10 +773,128 @@
</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"> wp_cache_set( $cache_key, $events, $cache_group, $cache_life );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</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="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Determine if conditions for sticking a WordCamp event to the response are met.
+ *
+ * @param array $request_args
+ * @param array $raw_events
+ *
+ * @return bool
+ */
+function should_stick_wordcamp( $request_args, $raw_events ) {
+ if ( THROTTLE_STICKY_WORDCAMPS ) {
+ return false;
+ }
+
+ // $raw_events already contains all the events that are coming up
+ if ( count( $raw_events ) < $request_args['number'] ) {
+ return false;
+ }
+
+ if ( ! $request_args['is_client_core'] ) {
+ return false;
+ }
+
+ $event_types = array_column( $raw_events, 'type' );
+
+ if ( in_array( 'wordcamp', $event_types, true ) ) {
+ return false;
+ }
+
+ return true;
+}
+
+/**
+ * Get the WordCamp that should be stuck to the response.
+ *
+ * WordCamps are large, all-day (or multi-day) events that require more of attendees that meetups do. Attendees
+ * need to have more advanced notice of when they're occurring. In a city with an active meetup, the camp
+ * might not show up in the Events Widget until a week or two before it happens, which isn't enough time.
+ *
+ * @param array $request_args
+ * @param int $distance
+ *
+ * @return object|false A database row on success; `false` on failure.
+ */
+function get_sticky_wordcamp( $request_args, $distance ) {
+ global $wpdb;
+
+ $sticky_wordcamp_query = build_sticky_wordcamp_query( $request_args, $distance );
+ $sticky_wordcamp = $wpdb->get_results( $wpdb->prepare(
+ $sticky_wordcamp_query['query'],
+ $sticky_wordcamp_query['values']
+ ) );
+
+ if ( ! empty( $sticky_wordcamp[0]->type ) ) {
+ return $sticky_wordcamp[0];
+ }
+
+ return false;
+}
+
+/**
+ * Build the database query for fetching the WordCamp to stick to the response.
+ *
+ * @param array $request_args
+ * @param int $distance
+ *
+ * @return array
+ */
+function build_sticky_wordcamp_query( $request_args, $distance ) {
+ $where = $values = array();
+
+ /*
+ * How far ahead the query should search for an upcoming camp. It should be high enough that attendees have
+ * enough time to prepare for the event, but low enough that it doesn't crowd out meetups that are happening
+ * in the mean-time, or make the content of the Events Widget feel less dynamic. Always having fresh content
+ * there is one of the things that makes the widget engaging.
+ */
+ $date_upper_bound = 6 * WEEK_IN_SECONDS;
+
+ if ( ! empty( $request_args['nearby'] ) ) {
+ $bounded_box = get_bounded_coordinates( $request_args['nearby']['latitude'], $request_args['nearby']['longitude'], $distance );
+ $where[] = '( `latitude` BETWEEN %f AND %f AND `longitude` BETWEEN %f AND %f )';
+ $values[] = $bounded_box['latitude']['min'];
+ $values[] = $bounded_box['latitude']['max'];
+ $values[] = $bounded_box['longitude']['min'];
+ $values[] = $bounded_box['longitude']['max'];
+ }
+
+ // Allow queries for limiting to specific countries.
+ if ( ! empty( $request_args['country'] ) && preg_match( '![a-z]{2}!i', $request_args['country'] ) ) {
+ $where[] = '`country` = %s';
+ $values[] = $request_args['country'];
+ }
+
+ $where = implode( ' AND ', $where );
+
+ $query = "
+ SELECT
+ `type`, `title`, `url`,
+ `meetup`, `meetup_url`,
+ `date_utc`, `date_utc_offset`,
+ `location`, `country`, `latitude`, `longitude`
+ FROM `wporg_events`
+ WHERE
+ `type` = 'wordcamp' AND
+ $where AND
+ `date_utc` >= %s AND
+ `date_utc` <= %s
+ ORDER BY `date_utc` ASC
+ LIMIT 1"
+ ;
+
+ $values[] = gmdate( 'Y-m-d', time() - DAY_IN_SECONDS );
+ $values[] = gmdate( 'Y-m-d', time() + $date_upper_bound );
+
+ return compact( 'query', 'values' );
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Add regional WordCamps to the Events Widget in Core for extra promotion.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param array $local_events
</span></span></pre></div>
<a id="sitestrunkapiwordpressorgpublic_htmlevents10teststestindexphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/api.wordpress.org/public_html/events/1.0/tests/test-index.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/api.wordpress.org/public_html/events/1.0/tests/test-index.php 2017-12-14 23:33:51 UTC (rev 6274)
+++ sites/trunk/api.wordpress.org/public_html/events/1.0/tests/test-index.php 2017-12-14 23:45:15 UTC (rev 6275)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -600,7 +600,6 @@
</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">-
</del><span class="cx" style="display: block; padding: 0 10px"> /*
</span><span class="cx" style="display: block; padding: 0 10px"> * A combination of city, region, and country are given, along with the locale and timezone
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1131,14 +1130,14 @@
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @return false
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function wp_cache_get() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function wp_cache_get( $key, $group = '', $force = false, &$found = null ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> return false;
</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"> * Stub to simulate cache misses, so that the tests always get fresh results
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function wp_cache_set() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function wp_cache_set( $key, $data, $group = '', $expire = 0 ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> // Intentionally empty
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre>
</div>
</div>
</body>
</html>