Making WordPress.org


Ignore:
Timestamp:
04/20/2020 10:28:21 PM (4 years ago)
Author:
iandunn
Message:

Events: Remove duplicate events after all events have been added.

pin_next_online_wordcamp() will be added in a subsequent commit, and at that point this function will need to be called after all events from been added dynamically.

File:
1 edited

Legend:

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

    r9749 r9750  
    182182            $location
    183183        );
     184
     185        $events = remove_duplicate_events( $events );
    184186
    185187        // Internal location data cannot be exposed in the response, see get_location().
     
    11361138            }
    11371139        }
    1138     }
    1139 
    1140     /**
    1141      * Remove duplicates events.
    1142      * Favor the regional event since it'll be pinned to the top.
    1143      */
    1144     foreach ( $regional_wordcamps as $regional_event ) {
    1145         $local_events = array_filter( $local_events, function( $local_event ) use ( $regional_event ) {
    1146             if ( parse_url( $regional_event['url'], PHP_URL_HOST ) === parse_url( $local_event['url'], PHP_URL_HOST ) ) {
    1147                 return false;
    1148             }
    1149 
    1150             return true;
    1151         } );
     1140
     1141
    11521142    }
    11531143
     
    12741264
    12751265/**
     1266 * Remove duplicate events, based on the URL.
     1267 *
     1268 * This is needed because sometimes an event gets added from multiple functions; e.g., `get_events()` and also `maybe_add_regional_wordcamps()` and/or `pin_next_online_wordcamp()`.
     1269 *
     1270 * @param array $events
     1271 *
     1272 * @return array
     1273 */
     1274function remove_duplicate_events( $events ) {
     1275    // Re-index them by a URL to overwrite duplicates
     1276    $unique_events = array();
     1277
     1278    foreach ( $events as $event ) {
     1279        $parsed_url = parse_url( $event['url'] );
     1280
     1281        // Take all essential parts to cover current URL structures, and potential future ones.
     1282        $normalized_url = sprintf(
     1283            '%s%s%s',
     1284            $parsed_url['host'],
     1285            $parsed_url['path'],
     1286            $parsed_url['query']
     1287        );
     1288        $normalized_url = str_replace( '/', '', $normalized_url );
     1289
     1290        $unique_events[ $normalized_url ] = $event;
     1291    }
     1292
     1293    // Restore integer keys.
     1294    return array_values( $unique_events );
     1295}
     1296
     1297/**
    12761298 * Create a bounded latitude/longitude box of x KM around specific coordinates.
    12771299 *
Note: See TracChangeset for help on using the changeset viewer.