Make WordPress Core

Changeset 38689


Ignore:
Timestamp:
09/30/2016 08:11:10 PM (8 years ago)
Author:
joehoyle
Message:

REST API: Add filters to allow creating REST API middleware plugins.

Introduce two new filters: rest_request_before_callbacks and rest_request_after_callbacks to
assist REST API middleware plugins to perform pre-callback and cleanup hooks such as switch_to_blog()
or caching implementations.

Props jnylen0.
Fixes #35590.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/class-wp-rest-server.php

    r38601 r38689  
    874874                    }
    875875                }
     876
     877                /**
     878                 * Call a filter before executing any REST API callbacks.
     879                 *
     880                 * Allows plugins to perform additional validation after a
     881                 * request is initialized and matched to a registered route,
     882                 * but before it is executed.
     883                 *
     884                 * Note that this filter will not be called for requests that
     885                 * fail to authenticate or match to a registered route.
     886                 *
     887                 * @since 4.7.0
     888                 *
     889                 * @param WP_HTTP_Response $response Result to send to the client. Usually a WP_REST_Response.
     890                 * @param WP_REST_Server   $handler  ResponseHandler instance (usually WP_REST_Server).
     891                 * @param WP_REST_Request  $request  Request used to generate the response.
     892                 */
     893                $response = apply_filters( 'rest_request_before_callbacks', $response, $handler, $request );
    876894
    877895                if ( ! is_wp_error( $response ) ) {
     
    911929                    }
    912930                }
     931
     932                /**
     933                 * Call a filter immediately after executing any REST API
     934                 * callbacks.
     935                 *
     936                 * Allows plugins to perform any needed cleanup, for example,
     937                 * to undo changes made during `rest_request_before_callbacks`.
     938                 *
     939                 * Note that this filter will not be called for requests that
     940                 * fail to authenticate or match to a registered route.
     941                 *
     942                 * Note that an endpoint's `permission_callback` can still be
     943                 * called after this filter - see the `rest_send_allow_header`
     944                 * function.
     945                 *
     946                 * @since 4.7.0
     947                 *
     948                 * @param WP_HTTP_Response $response Result to send to the client. Usually a WP_REST_Response.
     949                 * @param WP_REST_Server   $handler  ResponseHandler instance (usually WP_REST_Server).
     950                 * @param WP_REST_Request  $request  Request used to generate the response.
     951                 */
     952                $response = apply_filters( 'rest_request_after_callbacks', $response, $handler, $request );
    913953
    914954                if ( is_wp_error( $response ) ) {
Note: See TracChangeset for help on using the changeset viewer.