PluginProbe
ezCache / 1.2
ezCache v1.2
2.6.5 2.6.4 2.6.2 2.6.3 2.6.1 2.6.0 2.5.6 2.5.5 2.5.4 2.5.3 2.5.2 2.5.1 2.5 2.2.1 2.2.2 trunk 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.3 1.3.1 1.3.10 1.3.11 All 49 releases
ezcache / includes / RestApi.php

RestApi.php in ezCache 1.2, at includes/RestApi.php

69 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Upress\EzCache;
3
4 class RestApi {
5 private $plugin;
6 private $rest_routes = [];
7
8 function __construct($plugin) {
9 $this->plugin = $plugin;
10
11 $this->get( '/settings', '\Upress\EzCache\Rest\SettingsController@show' );
12 $this->patch( '/settings', '\Upress\EzCache\Rest\SettingsController@update' );
13 $this->get( '/cache', '\Upress\EzCache\Rest\CacheController@show' );
14 $this->delete( '/cache', '\Upress\EzCache\Rest\CacheController@destroy' );
15 $this->get( '/status', '\Upress\EzCache\Rest\StatusController@show' );
16 $this->get( '/license', '\Upress\EzCache\Rest\LicenseController@show' );
17 $this->patch( '/license', '\Upress\EzCache\Rest\LicenseController@update' );
18 $this->delete( '/license', '\Upress\EzCache\Rest\LicenseController@destroy' );
19
20 add_action( 'rest_api_init', [ $this, 'rest_api_init' ] );
21 }
22
23 function rest_api_init() {
24 foreach ( $this->rest_routes as $rest ) {
25 register_rest_route( "/ezcache/v1", $rest['route'], $rest['params'] );
26 }
27 }
28
29 function get( $route, $handler, $capabilities = 'manage_options' ) { $this->register_route( 'GET', $route, $handler, $capabilities ); }
30 function post( $route, $handler, $capabilities = 'manage_options' ) { $this->register_route( 'POST', $route, $handler, $capabilities ); }
31 function put( $route, $handler, $capabilities = 'manage_options' ) { $this->register_route( 'PUT', $route, $handler, $capabilities ); }
32 function patch( $route, $handler, $capabilities = 'manage_options' ) { $this->register_route( 'PATCH', $route, $handler, $capabilities ); }
33 function delete( $route, $handler, $capabilities = 'manage_options' ) { $this->register_route( 'DELETE', $route, $handler, $capabilities ); }
34
35 /**
36 * Register a REST route
37 * @param string|string[] $http_method
38 * @param string $route
39 * @param callable|string $handler
40 * @param callable|string $capability
41 */
42 protected function register_route( $http_method, $route, $handler, $capability = 'manage_options' ) {
43 if ( is_callable( $handler ) || strpos( $handler, '::' ) !== false ) {
44 $callback = $handler;
45 } else {
46 list( $class, $method ) = explode( '@', $handler );
47 $class = new $class;
48 $callback = [ $class, $method ];
49 }
50
51 if ( is_callable( $capability ) ) {
52 $permission_callback = $capability;
53 } else {
54 $permission_callback = function () use ($capability) {
55 return current_user_can( $capability );
56 };
57 }
58
59 $this->rest_routes[] = [
60 'route' => $route,
61 'params' => [
62 'methods' => $http_method,
63 'callback' => $callback,
64 'permission_callback' => $permission_callback,
65 ]
66 ];
67 }
68 }
69