Coder Social home page Coder Social logo

arafatkn / wrest Goto Github PK

View Code? Open in Web Editor NEW
8.0 1.0 0.0 31 KB

WREST - easy to use fluent REST API wrapper for WordPress

Home Page: https://packagist.org/packages/arafatkn/wrest

License: MIT License

PHP 100.00%
wordpress wp wp-rest wp-rest-api

wrest's Introduction

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.

wrest's People

Contributors

arafatkn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

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.