Coder Social home page Coder Social logo

wp-middleware-plugin's Introduction

Wordpress REST API Middleware


WP Middleware is a middleware package for creating middleware checks for the REST API. It passes a request to a series of callbacks against which you can check.

Basic example

middleware()->get('/wp/v2/posts', ['check_foo', 'check_bar']);

// callback check
function check_foo($request) {
    if($request->get_param('foo') != 'foo' ){
        return reject();
    }
}

// callback check
function check_bar($request) {
    if($request->get_param('bar') != 'bar' ){
        return reject();
    }
}

middleware()->get() checks all GET requests to that given route. To check against all HTTP methods, use middleware()-guard().

Callback Parameters

All callbacks must have at least 1 request parameter, which will recieve an instance of the WP_REST_Request class. You will be able to use all of the methods of this class within your callback to perform checks.

Optional You may also accept a second parameter, which is the outgoing response if you choose to modify it in any way based on the inbound request. This second parameter will be given an instance of WP_HTTP_Response.

Protecting Multiple Routes

To protect multiple routes with the same series of checks

$middlewareStack = ['check_foo', 'check_bar'];

middleware()->guard([
    '/wp/v2/posts',
    '/wp/v2/users' ], 
    $middlewareStack);

Protecting Multiple HTTP Methods

If you would like to protect multiple HTTP Methods in one call, you can use the methods() helper and pass in an array of methods to target:

middleware()->methods(['POST', 'PUT'], '/wp/v2/users', $middlewareStack);

Customize Rejection Message and Status

You can return a rejection in any callback using the reject() function. If you would like to pass a custom message or status, you can pass those in as paramaters as below: return reject("Failed this specific check.", 400);

The default status is 401 but you may override as you wish.

Reject all requests

While Wordpress includes core functions to achieve this too, if you would like to keep turning on / off endpoints within the middleware logic area, you can also use as below:

middleware()->reject('/wp/v2/users');

Available HTTP Method Helpers

All method helpers require two parameters:

  • string|array $routeInput
  • array $callbacks = []
Avaiable methods
  • guard() - Protects all HTTP methods
  • get()
  • post()
  • put()
  • patch()
  • delete()
  • head()

Other helper methods

  • methods() - Protect multiple HTTP Methods
  • reject() - Reject all requests to this route.

wp-middleware-plugin's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

wp-middleware-plugin's Issues

Cannot match Parameterized Routes

I am using this middleware in conjunction with Woocommerce and Wordpress. It works great, thanks! One issue I have found though, I cannot match routes which are parameterized.

Routes like /wc/v2/orders can be matched with middleware()->guard() but routes like /wc/v2/orders/21 cannot. Is there a way that I can match these kinds of routes using this plugin?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.