PluginProbe
xSpeed Cache: AI-Powered Performance Hub with MCP, Caching & CDN / 1.0.0
xSpeed Cache: AI-Powered Performance Hub with MCP, Caching & CDN v1.0.0
1.3.3 1.3.2 1.3.1 1.3.0 1.2.4 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 All 29 releases
xspeed / includes / class-rest-api.php

class-rest-api.php in xSpeed Cache: AI-Powered Performance Hub with MCP, Caching & CDN 1.0.0, at includes/class-rest-api.php

133 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * REST API endpoints.
4 *
5 * @package XSpeed
6 */
7
8 namespace XSpeed;
9
10 defined( 'ABSPATH' ) || exit;
11
12 class Rest_Api {
13
14 const NAMESPACE_V1 = 'xspeed/v1';
15
16 public function __construct() {
17 add_action( 'rest_api_init', array( $this, 'register' ) );
18 }
19
20 public function register() {
21 register_rest_route(
22 self::NAMESPACE_V1,
23 '/status',
24 array(
25 'methods' => 'GET',
26 'callback' => array( $this, 'get_status' ),
27 'permission_callback' => array( $this, 'permissions' ),
28 )
29 );
30
31 register_rest_route(
32 self::NAMESPACE_V1,
33 '/settings',
34 array(
35 array(
36 'methods' => 'GET',
37 'callback' => array( $this, 'get_settings' ),
38 'permission_callback' => array( $this, 'permissions' ),
39 ),
40 array(
41 'methods' => 'POST',
42 'callback' => array( $this, 'update_settings' ),
43 'permission_callback' => array( $this, 'permissions' ),
44 ),
45 )
46 );
47
48 register_rest_route(
49 self::NAMESPACE_V1,
50 '/cache/purge',
51 array(
52 'methods' => 'POST',
53 'callback' => array( $this, 'purge' ),
54 'permission_callback' => array( $this, 'permissions' ),
55 )
56 );
57
58 register_rest_route(
59 self::NAMESPACE_V1,
60 '/cache/toggle',
61 array(
62 'methods' => 'POST',
63 'callback' => array( $this, 'toggle_cache' ),
64 'permission_callback' => array( $this, 'permissions' ),
65 )
66 );
67 }
68
69 public function permissions() {
70 return current_user_can( 'manage_options' );
71 }
72
73 public function get_status() {
74 $opts = Settings::get();
75 $stats = Cache::get_stats();
76 return rest_ensure_response(
77 array(
78 'enabled' => (bool) $opts['cache_enabled'],
79 'stats' => $stats,
80 'server' => array(
81 'type' => Server::type(),
82 'gzip_mode' => Server::gzip_mode(),
83 'gzip_active' => Gzip::probe_active(),
84 'nginx_snippet' => Gzip::nginx_snippet(),
85 ),
86 )
87 );
88 }
89
90 public function get_settings() {
91 return rest_ensure_response( Settings::get() );
92 }
93
94 public function update_settings( \WP_REST_Request $request ) {
95 $params = $request->get_json_params();
96 if ( ! is_array( $params ) ) {
97 $params = $request->get_params();
98 }
99 // `cache_enabled` is the trigger for drop-in install / wp-config.php
100 // edit and must only flow through the dedicated /cache/toggle
101 // endpoint. Strip it here so generic settings updates can never
102 // implicitly write a drop-in or modify wp-config.php.
103 unset( $params['cache_enabled'] );
104 $updated = Settings::update( $params );
105 return rest_ensure_response( $updated );
106 }
107
108 public function purge() {
109 Cache::purge_all();
110 return rest_ensure_response( array( 'stats' => Cache::get_stats() ) );
111 }
112
113 public function toggle_cache( \WP_REST_Request $request ) {
114 $params = $request->get_json_params();
115 $enabled = isset( $params['enabled'] ) ? (bool) $params['enabled'] : false;
116
117 // User-explicit drop-in install / wp-config.php edit happens here.
118 // permission_callback above already enforced current_user_can(
119 // 'manage_options' ); the REST nonce is verified by core via the
120 // X-WP-Nonce header.
121 $state = Cache::toggle( $enabled );
122 $updated = Settings::update( array( 'cache_enabled' => $state['enabled'] ) );
123
124 return rest_ensure_response(
125 array(
126 'enabled' => $updated['cache_enabled'],
127 'stats' => Cache::get_stats(),
128 'install_state' => $state,
129 )
130 );
131 }
132 }
133