Making WordPress.org


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

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

File:
1 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    }
Note: See TracChangeset for help on using the changeset viewer.