Index: /sites/trunk/api.wordpress.org/public_html/events/1.0/index.php
===================================================================
--- /sites/trunk/api.wordpress.org/public_html/events/1.0/index.php (revision 9749)
+++ /sites/trunk/api.wordpress.org/public_html/events/1.0/index.php (revision 9750)
@@ -182,4 +182,6 @@
$location
);
+
+ $events = remove_duplicate_events( $events );
// Internal location data cannot be exposed in the response, see get_location().
@@ -1136,18 +1138,6 @@
}
}
- }
-
- /**
- * Remove duplicates events.
- * Favor the regional event since it'll be pinned to the top.
- */
- foreach ( $regional_wordcamps as $regional_event ) {
- $local_events = array_filter( $local_events, function( $local_event ) use ( $regional_event ) {
- if ( parse_url( $regional_event['url'], PHP_URL_HOST ) === parse_url( $local_event['url'], PHP_URL_HOST ) ) {
- return false;
- }
-
- return true;
- } );
+
+
}
@@ -1274,4 +1264,36 @@
/**
+ * Remove duplicate events, based on the URL.
+ *
+ * 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()`.
+ *
+ * @param array $events
+ *
+ * @return array
+ */
+function remove_duplicate_events( $events ) {
+ // Re-index them by a URL to overwrite duplicates
+ $unique_events = array();
+
+ foreach ( $events as $event ) {
+ $parsed_url = parse_url( $event['url'] );
+
+ // Take all essential parts to cover current URL structures, and potential future ones.
+ $normalized_url = sprintf(
+ '%s%s%s',
+ $parsed_url['host'],
+ $parsed_url['path'],
+ $parsed_url['query']
+ );
+ $normalized_url = str_replace( '/', '', $normalized_url );
+
+ $unique_events[ $normalized_url ] = $event;
+ }
+
+ // Restore integer keys.
+ return array_values( $unique_events );
+}
+
+/**
* Create a bounded latitude/longitude box of x KM around specific coordinates.
*
Index: /sites/trunk/api.wordpress.org/public_html/events/1.0/tests/test-index.php
===================================================================
--- /sites/trunk/api.wordpress.org/public_html/events/1.0/tests/test-index.php (revision 9749)
+++ /sites/trunk/api.wordpress.org/public_html/events/1.0/tests/test-index.php (revision 9750)
@@ -27,4 +27,5 @@
$tests_failed += test_is_client_core();
$tests_failed += test_get_iso_3166_2_country_codes();
+ $tests_failed += test_remove_duplicate_events();
$query_count = count( $wpdb->queries );
@@ -1258,21 +1259,8 @@
$tests_expect_changes['core-user-agent'] = maybe_add_regional_wordcamps( $local_events, $region_data, $core_user_agent, $time_during_promo_phase_1, $location_country_within_region );
- // There should only be one entry for an event, even if the local event array already contains the regional event.
- $tests_expect_no_changes['duplicate-event'] = maybe_add_regional_wordcamps( array( $region_data['us']['event'] ), $region_data, $core_user_agent, $time_during_promo_phase_1, $location_country_within_region );
-
foreach ( $tests_expect_no_changes as $name => $result ) {
- switch ( $name ) {
- case 'duplicate-event':
- if ( $result !== array( $region_data['us']['event'] ) ) {
- $failed++;
- output_results( $name, false, array( $region_data['us']['event'] ), $result );
- }
- break;
- default:
- if ( $result !== $local_events ) {
- $failed++;
- output_results( $name, false, $local_events, $result );
- }
- break;
+ if ( $result !== $local_events ) {
+ $failed++;
+ output_results( $name, false, $local_events, $result );
}
}
@@ -1482,4 +1470,44 @@
/**
+ * Test `remove_duplicate_events()`.
+ */
+function test_remove_duplicate_events() {
+ $duplicate_events = array(
+ // Each of these represents an event; extraneous fields have been removed for readability.
+ array (
+ 'url' => 'https://2020.us.wordcamp.org/',
+ ),
+
+ array (
+ 'url' => 'https://2020.detroit.wordcamp.org/',
+ ),
+
+ array(
+ // Intentionally missing the trailing slash, to account for inconsistencies in data.
+ 'url' => 'https://2020.us.wordcamp.org',
+ )
+ );
+
+ printf( "\n\nRunning 1 remove_duplicate_events() test\n" );
+
+ $expected_result = array(
+ array (
+ 'url' => 'https://2020.us.wordcamp.org',
+ ),
+
+ array (
+ 'url' => 'https://2020.detroit.wordcamp.org/',
+ ),
+ );
+
+ $actual_result = remove_duplicate_events( $duplicate_events );
+ $passed = $expected_result === $actual_result;
+
+ output_results( 'remove duplicate events', $passed, $expected_result, $actual_result );
+
+ return $passed ? 0 : 1;
+}
+
+/**
* Stub to simulate cache misses, so that the tests always get fresh results
*