-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple-rest-router.php
37 lines (31 loc) · 1.16 KB
/
simple-rest-router.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
/*
Plugin Name: Simpler Rest Router
Plugin URI: https://github.com/suomato/simpler-rest-route
Description: This plugin helps you to make WordPress REST endpoints in a simpler way.
Version: 0.1.0
Author: Toni Suomalainen
Author URI: https://github.com/suomato/simpler-rest-route
License: MIT
License URI: https://opensource.org/licenses/MIT
*/
namespace Suomato;
defined('ABSPATH') or die('Not allowed');
if ( ! class_exists('Suomato\Router')) {
class Router
{
public static function register($route)
{
add_action('rest_api_init', function () use ($route) {
$route['base'] = str_replace('{', '(?P<', $route['base']);
$route['base'] = str_replace('}', '>\w+)', $route['base']);
register_rest_route($route['namespace'] . '/', $route['base'], [
'methods' => $route['methods'],
'callback' => $route['callback'],
'permission_callback' => $route['permission_callback'] ?? null,
'args' => $route['params'] ?? [],
]);
});
}
}
}