PluginProbe
ezCache / trunk
ezCache vtrunk
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 trunk, at includes/RestApi.php

106 lines 4.7 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->delete( '/settings', '\Upress\EzCache\Rest\SettingsController@destroy' );
14 $this->get( '/cache', '\Upress\EzCache\Rest\CacheController@show' );
15 $this->delete( '/cache', '\Upress\EzCache\Rest\CacheController@destroy' );
16 $this->post( '/webp', '\Upress\EzCache\Rest\WebpController@process' );
17 $this->delete( '/webp', '\Upress\EzCache\Rest\WebpController@destroy' );
18 $this->get( '/webp/status', '\Upress\EzCache\Rest\WebpController@status' );
19 $this->post( '/webp/scan', '\Upress\EzCache\Rest\WebpController@scan' );
20 $this->get( '/status', '\Upress\EzCache\Rest\StatusController@show' );
21 $this->get( '/license', '\Upress\EzCache\Rest\LicenseController@show' );
22 $this->patch( '/license', '\Upress\EzCache\Rest\LicenseController@update' );
23 $this->delete( '/license', '\Upress\EzCache\Rest\LicenseController@destroy' );
24
25 // Performance endpoints
26 $this->get( '/performance', '\Upress\EzCache\Rest\PerformanceController@show' );
27 $this->post( '/performance', '\Upress\EzCache\Rest\PerformanceController@update' );
28 $this->post( '/performance/preload', '\Upress\EzCache\Rest\PerformanceController@runPreload' );
29 $this->delete( '/performance/preload', '\Upress\EzCache\Rest\PerformanceController@stopPreload' );
30 $this->post( '/performance/db-cleanup', '\Upress\EzCache\Rest\PerformanceController@runDbCleanup' );
31
32 $this->get( '/dev-mode', '\Upress\EzCache\Rest\SettingsController@devModeStatus' );
33 $this->post( '/dev-mode', '\Upress\EzCache\Rest\SettingsController@enableDevMode' );
34 $this->delete( '/dev-mode', '\Upress\EzCache\Rest\SettingsController@disableDevMode' );
35 // Diagnostic
36 $this->post( '/diagnose', '\Upress\EzCache\Rest\SettingsController@diagnose' );
37
38 // Redis
39 $this->get( '/redis-status', '\Upress\EzCache\Rest\RedisController@status' );
40 $this->post( '/redis-flush', '\Upress\EzCache\Rest\RedisController@flush' );
41 $this->post( '/redis-toggle', '\Upress\EzCache\Rest\RedisController@toggle' );
42
43 // Critical CSS
44 $this->post( '/performance/critical-css', '\Upress\EzCache\Rest\PerformanceController@generateCriticalCss' );
45
46 // Settings backup
47 $this->get( '/backups', '\Upress\EzCache\Rest\SettingsController@listBackups' );
48 $this->post( '/backups', '\Upress\EzCache\Rest\SettingsController@createBackup' );
49 $this->post( '/backups/restore', '\Upress\EzCache\Rest\SettingsController@restoreBackup' );
50
51 add_action( 'rest_api_init', [ $this, 'rest_api_init' ] );
52 }
53
54 function rest_api_init() {
55 foreach ( $this->rest_routes as $rest ) {
56 register_rest_route( "ezcache/v1", $rest['route'], $rest['params'] );
57 }
58 }
59
60 function get( $route, $handler, $capabilities = 'manage_options' ) { $this->register_route( 'GET', $route, $handler, $capabilities ); }
61 function post( $route, $handler, $capabilities = 'manage_options' ) { $this->register_route( 'POST', $route, $handler, $capabilities ); }
62 function put( $route, $handler, $capabilities = 'manage_options' ) { $this->register_route( 'PUT', $route, $handler, $capabilities ); }
63 function patch( $route, $handler, $capabilities = 'manage_options' ) { $this->register_route( 'PATCH', $route, $handler, $capabilities ); }
64 function delete( $route, $handler, $capabilities = 'manage_options' ) { $this->register_route( 'DELETE', $route, $handler, $capabilities ); }
65
66 /**
67 * Register a REST route
68 * @param string|string[] $http_method
69 * @param string $route
70 * @param callable|string $handler
71 * @param callable|string $capability
72 */
73 protected function register_route( $http_method, $route, $handler, $capability = 'manage_options' ) {
74 if ( is_callable( $handler ) || strpos( $handler, '::' ) !== false ) {
75 $callback = $handler;
76 } else {
77 list( $class, $method ) = explode( '@', $handler );
78 $class = new $class;
79 $callback = [ $class, $method ];
80 }
81
82 if ( is_callable( $capability ) ) {
83 $permission_callback = $capability;
84 } else {
85 $permission_callback = function () use ($capability) {
86 return current_user_can( $capability );
87 };
88 }
89
90 $this->rest_routes[] = [
91 'route' => $route,
92 'params' => [
93 'methods' => $http_method,
94 'callback' => function($request) use ($callback) {
95 try {
96 return call_user_func($callback, $request);
97 } catch (\Exception $e) {
98 return new \WP_Error('server_error', $e->getMessage(), ['status' => 500]);
99 }
100 },
101 'permission_callback' => $permission_callback,
102 ]
103 ];
104 }
105 }
106