Making WordPress.org

Changeset 7188


Ignore:
Timestamp:
05/09/2018 06:06:54 PM (8 years ago)
Author:
coreymckrill
Message:

Events: Add WP15 promo when no local WP15 events

  • Add function to detect WP15 events in a list
  • Add function to check conditions and add promo item if conditions are met
  • Add unit tests
Location:
sites/trunk/api.wordpress.org/public_html/events/1.0
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/api.wordpress.org/public_html/events/1.0/index.php

    r7145 r7188  
    165165
    166166        $events = get_events( $event_args );
     167        $events = maybe_add_wp15_promo( $events, $_SERVER['HTTP_USER_AGENT'], time() );
    167168        $events = add_regional_wordcamps( $events, $_SERVER['HTTP_USER_AGENT'] );
    168169
     
    982983
    983984/**
     985 * Add a special WP15 meetup event to the Events Widget in Core if the user location doesn't already have a
     986 * WP15 event in the list.
     987 *
     988 * @param array  $local_events The list of events for a particular location.
     989 * @param string $user_agent   The User Agent string of the client requesting the events.
     990 * @param int    $time         Unix timestamp.
     991 *
     992 * @return array
     993 */
     994function maybe_add_wp15_promo( $local_events, $user_agent, $time ) {
     995    if ( ! is_client_core( $user_agent ) ) {
     996        return $local_events;
     997    }
     998
     999    if ( $time < strtotime( 'April 27th, 2018' ) || $time > strtotime( 'May 28th, 2018' ) ) {
     1000        return $local_events;
     1001    }
     1002
     1003    $wp15_events = array_filter( $local_events, function( $event ) {
     1004        return is_wp15_event( $event['title'] );
     1005    } );
     1006
     1007    if ( empty( $wp15_events ) ) {
     1008        $promo = array(
     1009            'type'       => 'meetup',
     1010            'title'      => 'WP15',
     1011            'url'        => 'https://wordpress.org/news/2018/04/celebrate-the-wordpress-15th-anniversary-on-may-27/',
     1012            'meetup'     => null,
     1013            'meetup_url' => null,
     1014            'date'       => '2018-05-27 12:00:00',
     1015            'location'   => array(
     1016                'location' => 'Everywhere',
     1017            ),
     1018        );
     1019
     1020        array_unshift( $local_events, $promo );
     1021    }
     1022
     1023    return $local_events;
     1024}
     1025
     1026/**
     1027 * Determine if a meetup event is a WP15 celebration, based on the event title.
     1028 *
     1029 * Note that unlike the version of this function in the `wp15-meetup-events` plugin, the event data we're parsing here
     1030 * doesn't include a meetup event ID or a description, so we must rely on the title. In testing, this resulted in about
     1031 * 10% fewer events identified.
     1032 *
     1033 * @see https://meta.trac.wordpress.org/browser/sites/trunk/wp15.wordpress.net/public_html/content/plugins/wp15-meetup-events/wp15-meetup-events.php#L141
     1034 *
     1035 * @param string $title
     1036 *
     1037 * @return bool
     1038 */
     1039function is_wp15_event( $title ) {
     1040    $match    = false;
     1041    $keywords = array(
     1042        'wp15', '15 year', '15 ano', '15 año', '15 candeline', 'wordt 15',
     1043        'anniversary', 'aniversário', 'aniversario', 'birthday', 'cumpleaños',
     1044        'Tanti auguri'
     1045    );
     1046
     1047    foreach ( $keywords as $keyword ) {
     1048        if ( false !== stripos( $title, $keyword ) ) {
     1049            $match = true;
     1050            break;
     1051        }
     1052    }
     1053
     1054    return $match;
     1055}
     1056
     1057/**
    9841058 * Create a bounded latitude/longitude box of x KM around specific coordinates.
    9851059 *
  • sites/trunk/api.wordpress.org/public_html/events/1.0/tests/test-index.php

    r7145 r7188  
    2323    $tests_failed += test_get_events_country_restriction();
    2424    $tests_failed += test_add_regional_wordcamps();
     25    $tests_failed += test_maybe_add_wp15_promo();
    2526    $tests_failed += test_build_response();
    2627    $tests_failed += test_is_client_core();
     
    12071208
    12081209/**
     1210 * Test `maybe_add_wp15_promo()`
     1211 *
     1212 * @return int
     1213 */
     1214function test_maybe_add_wp15_promo() {
     1215    $failed = 0;
     1216
     1217    $local_events_yes_wp15 = array(
     1218        array(
     1219            'type'       => 'meetup',
     1220            'title'      => 'WordPress 15th Anniversary Celebration',
     1221            'url'        => 'https://www.meetup.com/pdx-wp/events/250109566/',
     1222            'meetup'     => 'Portland WordPress Meetup',
     1223            'meetup_url' => 'https://www.meetup.com/pdx-wp/',
     1224            'date'       => '2018-05-27 12:00:00',
     1225            'location'   => array(
     1226                'location'  => 'Portland, OR, USA',
     1227                'country'   => 'us',
     1228                'latitude'  => 45.540115,
     1229                'longitude' => - 122.630699,
     1230            ),
     1231        ),
     1232        array(
     1233            'type'       => 'wordcamp',
     1234            'title'      => 'WordCamp Portland, Oregon, USA',
     1235            'url'        => 'https://2018.portland.wordcamp.org',
     1236            'meetup'     => null,
     1237            'meetup_url' => null,
     1238            'date'       => '2018-11-03 00:00:00',
     1239            'location'   => array(
     1240                'location'  => 'Portland, OR, USA',
     1241                'country'   => 'us',
     1242                'latitude'  => 45.540115,
     1243                'longitude' => - 122.630699,
     1244            ),
     1245        ),
     1246    );
     1247
     1248    $local_events_no_wp15 = array(
     1249        array(
     1250            'type'       => 'meetup',
     1251            'title'      => 'Kickoff: Meet and greet, roundtable discussion',
     1252            'url'        => 'https://www.meetup.com/Corvallis-WordPress-Meetup/events/250327006/',
     1253            'meetup'     => 'Corvallis WordPress Meetup',
     1254            'meetup_url' => 'https://www.meetup.com/Corvallis-WordPress-Meetup/',
     1255            'date'       => '2018-05-22 18:30:00',
     1256            'location'   => array(
     1257                'location'  => 'Corvallis, OR, USA',
     1258                'country'   => 'us',
     1259                'latitude'  => 44.563564,
     1260                'longitude' => - 123.26095,
     1261            ),
     1262        ),
     1263        array(
     1264            'type'       => 'wordcamp',
     1265            'title'      => 'WordCamp Portland, Oregon, USA',
     1266            'url'        => 'https://2018.portland.wordcamp.org',
     1267            'meetup'     => null,
     1268            'meetup_url' => null,
     1269            'date'       => '2018-11-03 00:00:00',
     1270            'location'   => array(
     1271                'location'  => 'Portland, OR, USA',
     1272                'country'   => 'us',
     1273                'latitude'  => 45.540115,
     1274                'longitude' => - 122.630699,
     1275            ),
     1276        ),
     1277    );
     1278
     1279    $local_events_added_wp15 = array(
     1280        array(
     1281            'type'       => 'meetup',
     1282            'title'      => 'WP15',
     1283            'url'        => 'https://wordpress.org/news/2018/04/celebrate-the-wordpress-15th-anniversary-on-may-27/',
     1284            'meetup'     => null,
     1285            'meetup_url' => null,
     1286            'date'       => '2018-05-27 12:00:00',
     1287            'location'   => array(
     1288                'location' => 'Everywhere',
     1289            ),
     1290        ),
     1291        array(
     1292            'type'       => 'meetup',
     1293            'title'      => 'Kickoff: Meet and greet, roundtable discussion',
     1294            'url'        => 'https://www.meetup.com/Corvallis-WordPress-Meetup/events/250327006/',
     1295            'meetup'     => 'Corvallis WordPress Meetup',
     1296            'meetup_url' => 'https://www.meetup.com/Corvallis-WordPress-Meetup/',
     1297            'date'       => '2018-05-22 18:30:00',
     1298            'location'   => array(
     1299                'location'  => 'Corvallis, OR, USA',
     1300                'country'   => 'us',
     1301                'latitude'  => 44.563564,
     1302                'longitude' => - 123.26095,
     1303            ),
     1304        ),
     1305        array(
     1306            'type'       => 'wordcamp',
     1307            'title'      => 'WordCamp Portland, Oregon, USA',
     1308            'url'        => 'https://2018.portland.wordcamp.org',
     1309            'meetup'     => null,
     1310            'meetup_url' => null,
     1311            'date'       => '2018-11-03 00:00:00',
     1312            'location'   => array(
     1313                'location'  => 'Portland, OR, USA',
     1314                'country'   => 'us',
     1315                'latitude'  => 45.540115,
     1316                'longitude' => - 122.630699,
     1317            ),
     1318        ),
     1319    );
     1320
     1321    $user_agent = 'WordPress/4.9; https://example.org';
     1322
     1323    $time_before_date_range = 1523295832;
     1324    $time_during_date_range = 1525887832;
     1325
     1326    printf( "\n\nRunning %d maybe_add_wp15_promo() tests\n", 4 );
     1327
     1328    // Test that the promo is added if there is not already a WP15 event.
     1329    $events_promo_added = maybe_add_wp15_promo( $local_events_no_wp15, $user_agent, $time_during_date_range );
     1330
     1331    if ( $events_promo_added !== $local_events_added_wp15 ) {
     1332        $failed++;
     1333        output_results( 'needs-promo', false, $local_events_added_wp15, $events_promo_added );
     1334    }
     1335
     1336    // Test that no promo is added if there is already a WP15 event.
     1337    $events_already_has_one = maybe_add_wp15_promo( $local_events_yes_wp15, $user_agent, $time_during_date_range );
     1338
     1339    if ( $events_already_has_one !== $local_events_yes_wp15 ) {
     1340        $failed++;
     1341        output_results( 'already-has-event', false, $local_events_yes_wp15, $events_already_has_one );
     1342    }
     1343
     1344    // Test that no changes are made if the user agent isn't Core.
     1345    $events_no_user_agent = maybe_add_wp15_promo( $local_events_no_wp15, '', $time_during_date_range );
     1346
     1347    if ( $events_no_user_agent !== $local_events_no_wp15 ) {
     1348        $failed++;
     1349        output_results( 'no-user-agent', false, $local_events_no_wp15, $events_no_user_agent );
     1350    }
     1351
     1352    // Test that no promo is added if the time is outside the date range.
     1353    $events_outside_date_range = maybe_add_wp15_promo( $local_events_no_wp15, $user_agent, $time_before_date_range );
     1354
     1355    if ( $events_outside_date_range !== $local_events_no_wp15 ) {
     1356        $failed++;
     1357        output_results( 'outside-date-range', false, $local_events_no_wp15, $events_outside_date_range );
     1358    }
     1359
     1360    return $failed;
     1361}
     1362
     1363/**
    12091364 * Stub to simulate cache misses, so that the tests always get fresh results
    12101365 *
Note: See TracChangeset for help on using the changeset viewer.