Skip to content

arafatkn/wrest

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WREST

WREST (WordPress REST)

WREST - easy to use fluent REST API wrapper for WordPress.

Latest Stable Version PHP Version Require License Total Downloads

Installation

Requirements

  • PHP >= 5.6
  • WordPress >= 4.4

You can install wRest in two ways, via composer and manually.

1. Composer Installation

Add dependency in your project (theme/plugin):

composer require arafatkn/wrest

Now add autoload.php in your file if you haven't done already.

require __DIR__ . '/vendor/autoload.php';

2. Manual Installation

Not Available Yet.

Usage

WordPress API needs a namespace, so you have to set a namespace first.

One way is to set a default namespace before creating routes.

wrest()->setNamespace('my-plugin/v1');

wrest()->get('hello', function() {
    return 'Hello world';
});

or you can set namespace for a group of routes.

wrest()->usingNamespace('my-plugin/v1', function($wrest) {
    // You can use both $wrest or wrest() here
    $wrest->get('greeting', function(WP_REST_Request $req) {
        return 'Hello world';
    });
});

Passed callback will get a WP_REST_Request object as a parameter.

wrest()->get('greeting', function(WP_REST_Request $req) {
    return 'Hello world';
});

More Examples

wrest()->get('posts', $callback);
wrest()->post('posts', [$postController, 'store']);
wrest()->put($uri, $callback);
wrest()->patch($uri, $callback);
wrest()->delete($uri, $callback);
wrest()->any($uri, $callback); // All Routes GET, POST, PUT, PATCH, DELETE
wrest()->match(['GET', 'POST'], $uri, $callback);

Permission Management

Passing a capability

wrest()->get('greeting', function() {
    return 'Hello world';
})->permission('manage_options');

Passing a callback

wrest()->get('greeting', function() {
    return 'Hello world';
})->permission(function(WP_REST_Request $req) {
    return is_user_logged_in();
});

Parameters passing

wrest()->get('/posts/{slug}', function(WP_REST_Request $request, $slug) {
    //
})->param('slug', '[A-Za-z]+');

wrest()->get('/user/{id}/{name}', function ($request, $id, $name) {
    //
})->param('id', '[0-9]+')->param('name', '[a-z]+');

wrest()->get('/user/{id}/{name}', function ($request, $id, $name) {
    //
})->param(['id' => '[0-9]+', 'name' => '[a-z]+']);

If you do not pass a regex for a param then [^/]+ will be used as default.

wrest()->get('/posts/{slug}', function(WP_REST_Request $request, $slug) {
    // Also Works. slug will contain all the characters between posts/ and next /.
});

Route Action

Action can be a callback, a class method, a static class method or a non-static class method and can be passed as below.

wrest()->get('posts', function() => {});
wrest()->get('posts', 'getAllPosts'); // getAllPosts is a function.
wrest()->get('posts', 'PostController@getAll'); // getAll is static function.
wrest()->get('pages', [$pageController, 'getAll']); // getAll is non-static function.
wrest()->get('authors', [CommentController::class, 'getAll']); // getAll is static function.

All the functions will get a WP_REST_Request object as a parameter.

Supported Features

  • Namespaces for All Routes.
  • Normal Routes.
  • Routes with Parameters.
  • Route Actions.
  • Permission Management.

Upcoming Features

  • Add support for route groups.
  • Add support for namespace on the fly.
  • Add support for namespace groups.
  • Add support for resource routes.
  • Add support for schema.
  • Add support route redirection.
  • Add support for passing matched parameters directly to actions.

Credits

Special thanks to Tareq Hasan for this awesome idea.

Contribution Guide

This is still in beta, though I have a confidence that it will work as expected. You can contribute by reporting bugs, fixing bugs, reviewing pull requests and more ways. Go to issues section, and you can start working on a issue immediately. If you want to add or fix something, open a pull request.

License

The MIT License (MIT). Please see License File for more information.