Making WordPress.org


Ignore:
Timestamp:
01/22/2016 07:02:40 PM (9 years ago)
Author:
kovshenin
Message:

WordCamp.org: Allow some routes to skip the JSON REST API v1 plugin.

Some of our newer API routes are meant to work with Core's handler,
but we're still dependant on the v1 of the JSON REST API plugin, so
for backwards compatibility this way we can add new routes without
breaking old ones.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordcamp.org/public_html/wp-content/mu-plugins/wcorg-json-api.php

    r2246 r2358  
    2020add_action( 'wp_json_server_before_serve', 'wcorg_json_avoid_nested_callback_conflicts', 11     );    // after the default endpoints are added in `json_api_default_filters()`
    2121add_filter( 'wp_cache_eof_tags',           'wcorg_json_cache_requests'                          );
     22
     23// Allow some routes to skip the JSON REST API v1 plugin.
     24add_action( 'parse_request', 'wcorg_json_v2_compat', 9 );
    2225
    2326/**
     
    333336    return $eof_pattern;
    334337}
     338
     339/**
     340 * JSON REST API v1 and Core/v2 compatibility.
     341 *
     342 * All routes in $v2_routes are routed to the core handler.
     343 *
     344 * @param WP $request
     345 */
     346function wcorg_json_v2_compat( $request ) {
     347    $v2_routes = array(
     348        '/wp-json/wordcamp-qbo',
     349        '/wp-json/wordcamp-letsencrypt',
     350    );
     351
     352    if ( strpos( $_SERVER['REQUEST_URI'], '/wp-json' ) !== 0 ) {
     353        return;
     354    }
     355
     356    $is_route_v2 = false;
     357    foreach ( $v2_routes as $route ) {
     358        if ( strpos( $_SERVER['REQUEST_URI'], $route ) === 0 ) {
     359            $is_route_v2 = true;
     360            break;
     361        }
     362    }
     363
     364    if ( ! $is_route_v2 ) {
     365        return;
     366    }
     367
     368    $request->query_vars['rest_route'] = $request->query_vars['json_route'];
     369    unset( $request->query_vars['json_route'] );
     370    $request->matched_query = preg_replace( '#^json_route=(.+)$#', 'rest_route=$1', $request->matched_query );
     371
     372    return;
     373}
Note: See TracChangeset for help on using the changeset viewer.