Making WordPress.org


Ignore:
Timestamp:
05/09/2016 09:29:51 PM (8 years ago)
Author:
iandunn
Message:

WordCamp JSON API: Automatically route all v2 requests to the Core handler.

See #1459
Props prettyboymp

File:
1 edited

Legend:

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

    r3101 r3104  
    349349 * JSON REST API v1 and Core/v2 compatibility.
    350350 *
    351  * All routes in $v2_routes are routed to the core handler.
     351 * All v2 routes are routed to the Core handler, instead of the json-rest-api plugin.
    352352 *
    353353 * @param WP $request
    354354 */
    355355function wcorg_json_v2_compat( $request ) {
    356     $v2_routes = array(
    357         '/wp-json/wordcamp-qbo',
    358         '/wp-json/wordcamp-letsencrypt',
    359     );
    360 
    361     if ( strpos( $_SERVER['REQUEST_URI'], '/wp-json' ) !== 0 ) {
     356    $rest_prefix = rest_get_url_prefix();
     357
     358    // Skip non-API requests
     359    if ( 0 !== strpos( $_SERVER[ 'REQUEST_URI' ], "/$rest_prefix" ) ) {
    362360        return;
    363361    }
    364362
     363    // Determine if it's a v2 request
    365364    $is_route_v2 = false;
    366     foreach ( $v2_routes as $route ) {
    367         if ( strpos( $_SERVER['REQUEST_URI'], $route ) === 0 ) {
     365    foreach ( rest_get_server()->get_namespaces() as $namespace ) {
     366        if ( 0 === strpos( $_SERVER[ 'REQUEST_URI' ], "/$rest_prefix/$namespace" ) ) {
    368367            $is_route_v2 = true;
    369368            break;
     
    371370    }
    372371
     372    // Skip API v1 requests
    373373    if ( ! $is_route_v2 ) {
    374374        return;
    375375    }
    376376
     377    // Route v2 requests to Core handler
    377378    $request->query_vars['rest_route'] = $request->query_vars['json_route'];
    378379    unset( $request->query_vars['json_route'] );
Note: See TracChangeset for help on using the changeset viewer.