Making WordPress.org

Changeset 6078


Ignore:
Timestamp:
11/06/2017 09:03:28 PM (6 years ago)
Author:
iandunn
Message:

Events: Add regional WordCamps to the Core widget for extra promotion.

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

    r5559 r6078  
    148148
    149149        $events = get_events( $event_args );
     150        $events = add_regional_wordcamps( $events, $_SERVER['HTTP_USER_AGENT'] );
    150151
    151152        /*
     
    785786
    786787/**
     788 * Add regional WordCamps to the Events Widget in Core for extra promotion.
     789 *
     790 * @param array  $local_events
     791 * @param string $user_agent
     792 *
     793 * @return array
     794 */
     795function add_regional_wordcamps( $local_events, $user_agent ) {
     796    $time = time();
     797    $regional_wordcamps = array();
     798
     799    /*
     800     * Limit effects to the Events Widget in Core.
     801     * Otherwise this would return unexpected results to other clients.
     802     *
     803     * This is the closest we can get to detecting Core, so it'll still distort results for any
     804     * plugins that are fetching events with `wp_remote_get()`.
     805     */
     806    if ( false === strpos( $user_agent, 'WordPress/' ) ) {
     807        return $local_events;
     808    }
     809
     810    if ( $time <= strtotime( 'December 2nd, 2017' ) ) {
     811        $regional_wordcamps[] = array(
     812            'type'       => 'wordcamp',
     813            'title'      => 'WordCamp US',
     814            'url'        => 'https://2017.us.wordcamp.org/',
     815            'meetup'     => '',
     816            'meetup_url' => '',
     817            'date'       => '2017-12-01 00:00:00',
     818
     819            'location' => array(
     820                'location'  => 'Nashville, TN, USA',
     821                'country'   => 'US',
     822                'latitude'  => 36.1566085,
     823                'longitude' => -86.7784909,
     824            )
     825        );
     826    }
     827
     828    if ( $time >= strtotime( 'May 14th, 2018' ) && $time <= strtotime( 'June 15th, 2018' ) ) {
     829        $regional_wordcamps[] = array(
     830            'type'       => 'wordcamp',
     831            'title'      => 'WordCamp Europe',
     832            'url'        => 'https://2018.europe.wordcamp.org/',
     833            'meetup'     => '',
     834            'meetup_url' => '',
     835            'date'       => '2018-06-14 00:00:00',
     836
     837            'location' => array(
     838                'location'  => 'Belgrade, Serbia',
     839                'country'   => 'RS',
     840                'latitude'  => 44.808497,
     841                'longitude' => 20.432285,
     842            )
     843        );
     844    }
     845
     846    /**
     847     * Remove duplicates events.
     848     * Favor the regional event since it'll be pinned to the top.
     849     */
     850    foreach ( $regional_wordcamps as $regional_event ) {
     851        foreach ( $local_events as $local_key => $local_event ) {
     852            if ( parse_url( $regional_event['url'], PHP_URL_HOST ) === parse_url( $local_event['url'], PHP_URL_HOST ) ) {
     853                unset( $local_events[ $local_key ] );
     854            }
     855        }
     856    }
     857
     858    return array_merge( $regional_wordcamps, $local_events );
     859}
     860
     861/**
    787862 * Create a bounded latitude/longitude box of x KM around specific coordinates.
    788863 *
  • sites/trunk/api.wordpress.org/public_html/events/1.0/tests/test-index.php

    r6076 r6078  
    2020    $tests_failed += test_get_location();
    2121    $tests_failed += test_get_events();
     22    $tests_failed += test_add_regional_wordcamps();
    2223    $tests_failed += test_build_response();
    2324    $query_count  = count( $wpdb->queries );
     
    10191020
    10201021/**
     1022 * Test `add_regional_events()`
     1023 *
     1024 * @return int
     1025 */
     1026function test_add_regional_wordcamps() {
     1027    $failed = 0;
     1028
     1029    $local_events = get_events( array(
     1030        'number' => '5',
     1031        'nearby' => array(
     1032            'latitude'  => '-33.849951',
     1033            'longitude' => '18.426246',
     1034        ),
     1035    ) );
     1036
     1037    // Make sure there's at least one event, otherwise there could be false positives.
     1038    if ( ! $local_events ) {
     1039        $local_events[] = array( 'title' => 'Mock Event' );
     1040    }
     1041
     1042    printf( "\n\nRunning %d add_regional_wordcamps() tests\n", 2 );
     1043
     1044    // Test that no changes were made if the user agent isn't Core.
     1045    $events_no_user_agent = add_regional_wordcamps( $local_events, '' );
     1046
     1047    if ( $events_no_user_agent !== $local_events ) {
     1048        $failed++;
     1049        output_results( 'no-user-agent', false, $local_events, $events_no_user_agent );
     1050    }
     1051
     1052    /*
     1053     * Test that local events were unharmed if the user agent is Core.
     1054     *
     1055     * There isn't an easy way to mock time(), so this doesn't test that the events were added
     1056     * correctly. It just makes sure that local events weren't removed.
     1057     */
     1058    $events_core_user_agent = add_regional_wordcamps( $local_events, 'WordPress/4.9; https://example.org' );
     1059
     1060    if (
     1061        count( $events_core_user_agent ) < count( $local_events ) ||
     1062        ! in_array( $local_events[0], $events_core_user_agent, true )
     1063    ) {
     1064        $failed++;
     1065        output_results( 'core-user-agent', false, 'local events were not affected', $events_core_user_agent );
     1066    }
     1067
     1068    return $failed;
     1069}
     1070
     1071/**
    10211072 * Stub to simulate cache misses, so that the tests always get fresh results
    10221073 *
Note: See TracChangeset for help on using the changeset viewer.