Making WordPress.org


Ignore:
Timestamp:
08/17/2019 12:50:17 AM (7 years ago)
Author:
coreymckrill
Message:

Events API: Update logic for pinning regional camps

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

Legend:

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

    r7932 r9108  
    165165
    166166        $events = get_events( $event_args );
    167         $events = maybe_add_wp15_promo( $events, $_SERVER['HTTP_USER_AGENT'], time() );
    168         $events = add_regional_wordcamps( $events, $_SERVER['HTTP_USER_AGENT'] );
     167
     168        //$events = maybe_add_wp15_promo( $events, $_SERVER['HTTP_USER_AGENT'], time() );
     169
     170        $events = maybe_add_regional_wordcamps(
     171            $events,
     172            get_regional_wordcamp_data(),
     173            $_SERVER['HTTP_USER_AGENT'],
     174            time(),
     175            $location
     176        );
    169177
    170178        // Internal location data cannot be exposed in the response, see get_location().
     
    177185    }
    178186
    179     return compact( 'error', 'location', 'events' );
    180 
     187    $sandboxed = ( defined( 'WPORG_SANDBOXED' ) ) ? WPORG_SANDBOXED : null;
     188
     189    return compact( 'sandboxed', 'error', 'location', 'events' );
    181190}
    182191
     
    916925
    917926/**
    918  * Add regional WordCamps to the Events Widget in Core for extra promotion.
     927 * The data for upcoming regional WordCamps.
     928 *
     929 * Externalizing this makes it easier to test the `maybe_add_regional_wordcamps` function.
     930 *
     931 * @return array
     932 */
     933function get_regional_wordcamp_data() {
     934    return array(
     935        // WordCamp Asia.
     936        'asia'   => array(
     937            'promo_start'        => 0, // todo
     938            'regional_countries' => array(
     939                // todo
     940            ),
     941            'event'              => array(
     942                'type'       => 'wordcamp',
     943                'title'      => 'WordCamp Asia',
     944                'url'        => 'https://2020.asia.wordcamp.org/',
     945                'meetup'     => '',
     946                'meetup_url' => '',
     947                'date'       => '2020-02-21 00:00:00',
     948                'location'   => array(
     949                    'location'  => 'Bangkok, Thailand',
     950                    'country'   => 'TH',
     951                    'latitude'  => 13.7248934,
     952                    'longitude' => 100.492683,
     953                ),
     954            ),
     955        ),
     956        // WordCamp Europe.
     957        'europe' => array(
     958            'promo_start'        => 0, // todo
     959            'regional_countries' => array(
     960                // todo
     961            ),
     962            'event'              => array(
     963                'type'       => 'wordcamp',
     964                'title'      => 'WordCamp Europe',
     965                'url'        => 'https://2020.europe.wordcamp.org/',
     966                'meetup'     => '',
     967                'meetup_url' => '',
     968                'date'       => '', // todo
     969                'location' => array(
     970                    'location'  => 'Porto, Portugal',
     971                    'country'   => 'PT',
     972                    'latitude'  => 41.1622022,
     973                    'longitude' => -8.6570588,
     974                ),
     975            ),
     976        ),
     977        // WordCamp US.
     978        'us'     => array(
     979            'promo_start'        => strtotime( '2019-08-16 00:00:00' ),
     980            'regional_countries' => array(
     981                'us', 'ca', 'bz', 'cr', 'sv', 'gt', 'hn', 'mx', 'ni', 'pa',
     982                'ar', 'bo', 'br', 'cl', 'co', 'ec', 'gf', 'gy', 'py', 'pe',
     983                'sr', 'uy', 've', 'ag', 'aw', 'bs', 'bb', 'ky', 'cu', 'dm',
     984                'do', 'gd', 'ht', 'jm', 'kn', 'lc', 'vc', 'tt',
     985            ),
     986            'event'              => array(
     987                'type'       => 'wordcamp',
     988                'title'      => 'WordCamp US',
     989                'url'        => 'https://2019.us.wordcamp.org/',
     990                'meetup'     => '',
     991                'meetup_url' => '',
     992                'date'       => '2019-11-01 00:00:00',
     993                'location'   => array(
     994                    'location'  => 'St. Louis, MO, USA',
     995                    'country'   => 'US',
     996                    'latitude'  => 38.6532135,
     997                    'longitude' => -90.3136733,
     998                ),
     999            ),
     1000        ),
     1001    );
     1002}
     1003
     1004/**
     1005 * Add time- and location-relevant regional WordCamps to the Events Widget in Core.
    9191006 *
    9201007 * @param array  $local_events
     1008 * @param array  $region_data
    9211009 * @param string $user_agent
     1010 * @param int    $current_time
     1011 * @param array  $location
    9221012 *
    9231013 * @return array
    9241014 */
    925 function add_regional_wordcamps( $local_events, $user_agent ) {
    926     $time               = time();
    927     $regional_wordcamps = array();
    928 
     1015function maybe_add_regional_wordcamps( $local_events, $region_data, $user_agent, $current_time, $location ) {
    9291016    if ( ! is_client_core( $user_agent ) ) {
    9301017        return $local_events;
    9311018    }
    9321019
    933     if ( $time > strtotime( 'November 7th, 2018' ) && $time < strtotime( 'December 10th, 2018' ) ) {
    934         $regional_wordcamps[] = array(
    935             'type'       => 'wordcamp',
    936             'title'      => 'WordCamp US',
    937             'url'        => 'https://2018.us.wordcamp.org/',
    938             'meetup'     => '',
    939             'meetup_url' => '',
    940             'date'       => '2018-12-07 00:00:00',
    941 
    942             'location' => array(
    943                 'location'  => 'Nashville, TN, USA',
    944                 'country'   => 'US',
    945                 'latitude'  => 36.1566085,
    946                 'longitude' => -86.7784909,
    947             )
    948         );
    949     }
    950 
    951     if ( $time > strtotime( 'May 20th, 2019' ) && $time < strtotime( 'June 23rd, 2019' ) ) {
    952         $regional_wordcamps[] = array(
    953             'type'       => 'wordcamp',
    954             'title'      => 'WordCamp Europe',
    955             'url'        => 'https://2019.europe.wordcamp.org/',
    956             'meetup'     => '',
    957             'meetup_url' => '',
    958             'date'       => '2019-06-20 00:00:00',
    959 
    960             'location' => array(
    961                 'location'  => 'Berlin, Germany',
    962                 'country'   => 'DE',
    963                 'latitude'  => 52.50697,
    964                 'longitude' => 13.2843064,
    965             )
    966         );
     1020    $regional_wordcamps = array();
     1021
     1022    foreach ( $region_data as $region => $data ) {
     1023        if ( empty( $data['promo_start'] ) ) {
     1024            continue;
     1025        }
     1026
     1027        $start = $data['promo_start'];
     1028
     1029        /**
     1030         * The targeted area of the regional camp promotion "zooms in" over the course of 6 weeks.
     1031         */
     1032        if ( is_within_date_range( $current_time, $start, strtotime( '+ 2 weeks', $start ) ) ) {
     1033            // Phase 1: Show worldwide for first two weeks.
     1034            $regional_wordcamps[] = $data['event'];
     1035        } elseif ( is_within_date_range( $current_time, strtotime( '+ 2 weeks', $start ), strtotime( '+ 4 weeks', $start ) ) ) {
     1036            // Phase 2: Show within regional countries for next two weeks.
     1037            if ( ! empty( $location['country'] ) && in_array( strtolower( $location['country'] ), $data['regional_countries'], true ) ) {
     1038                $regional_wordcamps[] = $data['event'];
     1039            }
     1040        } elseif ( is_within_date_range( $current_time, strtotime( '+ 4 weeks', $start ), strtotime( '+ 6 weeks', $start ) ) ) {
     1041            // Phase 3: Show only within the event country for the last two weeks.
     1042            if ( ! empty( $location['country'] ) && strtolower( $data['event']['location']['country'] ) === strtolower( $location['country'] ) ) {
     1043                $regional_wordcamps[] = $data['event'];
     1044            }
     1045        }
    9671046    }
    9681047
     
    9721051     */
    9731052    foreach ( $regional_wordcamps as $regional_event ) {
    974         foreach ( $local_events as $local_key => $local_event ) {
     1053        $local_events = array_filter( $local_events, function( $local_event ) use ( $regional_event ) {
    9751054            if ( parse_url( $regional_event['url'], PHP_URL_HOST ) === parse_url( $local_event['url'], PHP_URL_HOST ) ) {
    976                 unset( $local_events[ $local_key ] );
     1055                return false;
    9771056            }
    978         }
     1057
     1058            return true;
     1059        } );
    9791060    }
    9801061
    9811062    return array_merge( $regional_wordcamps, $local_events );
     1063}
     1064
     1065/**
     1066 * Determine if a given Unix timestamp is within a date range.
     1067 *
     1068 * @param int    $time        A Unix timestamp.
     1069 * @param string $range_start A date/time string compatible with strtotime.
     1070 * @param string $range_end   A date/time string compatible with strtotime.
     1071 *
     1072 * @return bool
     1073 */
     1074function is_within_date_range( $time, $range_start, $range_end ) {
     1075    if ( ! is_int( $range_start ) ) {
     1076        $range_start = strtotime( $range_start );
     1077    }
     1078
     1079    if ( ! is_int( $range_end ) ) {
     1080        $range_end = strtotime( $range_end );
     1081    }
     1082
     1083    return $time > $range_start && $time < $range_end;
    9821084}
    9831085
Note: See TracChangeset for help on using the changeset viewer.