PluginProbe
xSpeed Cache: AI-Powered Performance Hub with MCP, Caching & CDN / 1.0.2
xSpeed Cache: AI-Powered Performance Hub with MCP, Caching & CDN v1.0.2
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.2, at includes/class-rest-api.php

173 lines 4.5 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 register_rest_route(
69 self::NAMESPACE_V1,
70 '/cache/benchmark',
71 array(
72 'methods' => 'GET',
73 'callback' => array( $this, 'benchmark' ),
74 'permission_callback' => array( $this, 'permissions' ),
75 )
76 );
77
78 register_rest_route(
79 self::NAMESPACE_V1,
80 '/audit/pro',
81 array(
82 'methods' => 'GET',
83 'callback' => array( $this, 'pro_audit' ),
84 'permission_callback' => array( $this, 'permissions' ),
85 )
86 );
87 }
88
89 /**
90 * Run the Pro audit — scans current settings + cache stats,
91 * returns a personalized list of Pro features that would help
92 * THIS site. See Pro_Audit::run() for the rule set.
93 */
94 public function pro_audit( $request ) {
95 unset( $request );
96 return rest_ensure_response( array( 'suggestions' => Pro_Audit::run() ) );
97 }
98
99 /**
100 * Cache before/after benchmark — fetches home_url() twice (with +
101 * without the bypass header) and returns side-by-side timings for
102 * the dashboard widget.
103 */
104 public function benchmark( $request ) {
105 unset( $request );
106 return rest_ensure_response( Cache_Benchmark::run() );
107 }
108
109 public function permissions() {
110 return current_user_can( 'manage_options' );
111 }
112
113 public function get_status() {
114 $opts = Settings::get();
115 $stats = Cache::get_stats();
116 return rest_ensure_response(
117 array(
118 'enabled' => (bool) $opts['cache_enabled'],
119 'stats' => $stats,
120 'server' => array(
121 'type' => Server::type(),
122 'gzip_mode' => Server::gzip_mode(),
123 'gzip_active' => Gzip::probe_active(),
124 'nginx_snippet' => Gzip::nginx_snippet(),
125 ),
126 )
127 );
128 }
129
130 public function get_settings() {
131 return rest_ensure_response( Settings::get() );
132 }
133
134 public function update_settings( \WP_REST_Request $request ) {
135 $params = $request->get_json_params();
136 if ( ! is_array( $params ) ) {
137 $params = $request->get_params();
138 }
139 // `cache_enabled` is the trigger for drop-in install / wp-config.php
140 // edit and must only flow through the dedicated /cache/toggle
141 // endpoint. Strip it here so generic settings updates can never
142 // implicitly write a drop-in or modify wp-config.php.
143 unset( $params['cache_enabled'] );
144 $updated = Settings::update( $params );
145 return rest_ensure_response( $updated );
146 }
147
148 public function purge() {
149 Cache::purge_all();
150 return rest_ensure_response( array( 'stats' => Cache::get_stats() ) );
151 }
152
153 public function toggle_cache( \WP_REST_Request $request ) {
154 $params = $request->get_json_params();
155 $enabled = isset( $params['enabled'] ) ? (bool) $params['enabled'] : false;
156
157 // User-explicit drop-in install / wp-config.php edit happens here.
158 // permission_callback above already enforced current_user_can(
159 // 'manage_options' ); the REST nonce is verified by core via the
160 // X-WP-Nonce header.
161 $state = Cache::toggle( $enabled );
162 $updated = Settings::update( array( 'cache_enabled' => $state['enabled'] ) );
163
164 return rest_ensure_response(
165 array(
166 'enabled' => $updated['cache_enabled'],
167 'stats' => Cache::get_stats(),
168 'install_state' => $state,
169 )
170 );
171 }
172 }
173