Making WordPress.org

Changeset 1544


Ignore:
Timestamp:
05/05/2015 11:22:45 PM (11 years ago)
Author:
iandunn
Message:

WordCamp JSON API: Create whitelisted endpoint array from scratch.

This essentially reverts [wordcamp1972], so that we are creating a new array and populating it with whitelisted items, instead of traversing the original array and removing blacklisted items.

The previous approach was taken because at the time it appeared we would need to access endpoint variables that were not available globally -- such as $wp_json_post_meta -- but it turns out that we don't, and this approach is much simpler. In order to add new endpoints like posts/types, which have a different array structure than the currently supported ones, it would have been necessary to significantly increase the complexity of the traversal logic. With this approach, adding new endpoints is straightforward, regardless of structural inconsistencies.

File:
1 edited

Legend:

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

    r1534 r1544  
    2323 */
    2424function wcorg_json_whitelist_endpoints( $endpoints ) {
     25    global $wp_json_server, $wp_json_posts;
     26
    2527    $whitelisted_endpoints = array(
    26         '/posts'             => array( 'get_posts' ),
    27         '/posts/(?P<id>\d+)' => array( 'get_post'  ),
     28        '/' => array( array( $wp_json_server, 'get_index' ),  WP_JSON_Server::READABLE ),
     29
     30        // Posts
     31        '/posts' => array(
     32            array( array( $wp_json_posts, 'get_posts' ),      WP_JSON_Server::READABLE ),
     33        ),
     34        '/posts/(?P<id>\d+)' => array(
     35            array( array( $wp_json_posts, 'get_post' ),       WP_JSON_Server::READABLE ),
     36        ),
     37
    2838        // todo Add /posts/types too, because it's useful for debugging and there's no harm. It has a different array structure than the current ones, though, so this will need some work.
    2939    );
    3040
    31     foreach ( $endpoints as $endpoint => $endpoint_data ) {
    32         /*
    33          * Don't attempt to scan '/' because it has a different array structure than normal endpoints and is
    34          * unlikely to expose anything sensitive.
    35          */
    36         if ( '/' == $endpoint ) {
    37             continue;
    38         }
    39 
    40         if ( array_key_exists( $endpoint, $whitelisted_endpoints ) ) {
    41             // Allow the endpoint, but remove any of its callbacks that aren't whitelisted and read-only
    42 
    43             foreach ( $endpoint_data as $callback_index => $callback ) {
    44                 $callback_name        = $callback[0][1];
    45                 $callback_permissions = $callback[1];
    46 
    47                 if ( ! in_array( $callback_name, $whitelisted_endpoints[ $endpoint ] ) || WP_JSON_Server::READABLE != $callback_permissions ) {
    48                     unset( $endpoints[ $endpoint ][ $callback_index ] );
    49                 }
    50             }
    51         } else {
    52             // Remove endpoints that aren't whitelisted
    53 
    54             unset( $endpoints[ $endpoint ] );
    55         }
    56     }
    57 
    58     return $endpoints;
     41    return $whitelisted_endpoints;
    5942}
    6043add_filter( 'json_endpoints', 'wcorg_json_whitelist_endpoints', 999 );
Note: See TracChangeset for help on using the changeset viewer.