WordPress.org

Making WordPress.org

Changeset 6208


Ignore:
Timestamp:
12/03/2017 05:28:28 PM (6 months ago)
Author:
iandunn
Message:

Events: Abstract the Core user agent check, to allow reuse.

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

    r6179 r6208  
    161161    return compact( 'error', 'location', 'events' );
    162162
     163}
     164
     165/**
     166 * Determine if the client making the API request is WordPress Core.
     167 *
     168 * This can be used to limit the effects of some data processing to just the Events Widget in
     169 * Core. Otherwise, those changes would result in unexpected data for other clients, like
     170 * having WordCamps stuck to the end of the request by `stick_wordcamps()`.
     171 *
     172 * Ideally this would be isolated to Core itself, and exclude plugins using `wp_remote_get()`.
     173 * There isn't a good way to do that, though, so plugins will still get unexpected results.
     174 * They can set a custom user agent to get the raw data, though.
     175 *
     176 * @param string $user_agent
     177 *
     178 * @return bool
     179 */
     180function is_client_core( $user_agent ) {
     181    // This doesn't simply return the value of `strpos()` because `0` means `true` in this context
     182    if ( false === strpos( $user_agent, 'WordPress/' ) ) {
     183        return false;
     184    }
     185
     186    return true;
    163187}
    164188
     
    732756    $regional_wordcamps = array();
    733757
    734     /*
    735      * Limit effects to the Events Widget in Core.
    736      * Otherwise this would return unexpected results to other clients.
    737      *
    738      * This is the closest we can get to detecting Core, so it'll still distort results for any
    739      * plugins that are fetching events with `wp_remote_get()`.
    740      */
    741     if ( false === strpos( $user_agent, 'WordPress/' ) ) {
     758    if ( ! is_client_core( $user_agent ) ) {
    742759        return $local_events;
    743760    }
  • sites/trunk/api.wordpress.org/public_html/events/1.0/tests/test-index.php

    r6180 r6208  
    2222    $tests_failed += test_add_regional_wordcamps();
    2323    $tests_failed += test_build_response();
     24    $tests_failed += test_is_client_core();
    2425    $query_count  = count( $wpdb->queries );
    2526    $query_time   = array_sum( array_column( $wpdb->queries, 1 ) );
     
    10211022
    10221023/**
     1024 * Test `is_client_core()`.
     1025 *
     1026 * @return int
     1027 */
     1028function test_is_client_core() {
     1029    $failed = 0;
     1030    $cases  = array(
     1031        ''                                   => false,
     1032        'Contains WordPress but no slash'    => false,
     1033        'WordPress/4.9; https://example.org' => true,
     1034        'WordPress/10.0'                     => true,
     1035    );
     1036
     1037    printf( "\n\nRunning %d is_client_core() tests\n", count( $cases ) );
     1038
     1039    foreach ( $cases as $user_agent => $expected_result ) {
     1040        $actual_result = is_client_core( $user_agent );
     1041
     1042        $passed = $expected_result === $actual_result;
     1043
     1044        output_results( $user_agent, $passed, $expected_result, $actual_result );
     1045
     1046        if ( ! $passed ) {
     1047            $failed++;
     1048        }
     1049    }
     1050
     1051    return $failed;
     1052}
     1053
     1054/**
    10231055 * Test `add_regional_events()`
    10241056 *
Note: See TracChangeset for help on using the changeset viewer.