PluginProbe
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution / 1.3.0
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution v1.3.0
2.0.1 trunk 1.0.0 1.1.0 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.5.0 1.6.0 1.6.1 1.7.0 1.7.1 1.8.0 1.9.0 2.0.0
solid-performance / src / Performance / API / Routes / Page_Cache / Debug.php

Debug.php in Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution 1.3.0, at src/Performance/API/Routes/Page_Cache/Debug.php

184 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The route for clearing the page cache.
4 *
5 * @since 0.1.0
6 *
7 * @package SolidWP\Performance
8 */
9
10 namespace SolidWP\Performance\API\Routes\Page_Cache;
11
12 use SolidWP\Performance\API\Base_Route;
13 use SolidWP\Performance\Page_Cache;
14 use WP_Error;
15 use WP_REST_Request;
16 use WP_REST_Response;
17 use WP_REST_Server;
18
19 if ( ! defined( 'ABSPATH' ) ) {
20 exit;
21 }
22
23 /**
24 * The class that handles the clear cache route.
25 *
26 * @since 0.1.0
27 *
28 * @package SolidWP|Performance
29 */
30 class Debug extends Base_Route {
31
32 /**
33 * @var Page_Cache
34 */
35 private Page_Cache $page_cache;
36
37 /**
38 * @param Page_Cache $page_cache The page cache object.
39 */
40 public function __construct( Page_Cache $page_cache ) {
41 $this->page_cache = $page_cache;
42 }
43
44 /**
45 * {@inheritdoc}
46 */
47 public function get_path(): string {
48 return '/page/debug';
49 }
50
51 /**
52 * {@inheritdoc}
53 */
54 public function get_methods() {
55 return [
56 WP_REST_Server::READABLE,
57 WP_REST_Server::CREATABLE,
58 ];
59 }
60
61 /**
62 * {@inheritdoc}
63 */
64 public function callback( WP_REST_Request $request ) {
65
66 // Return the current status for a GET request.
67 if ( $request->get_method() === WP_REST_Server::READABLE ) {
68 return $this->status_response();
69 }
70
71 $state = $request->get_param( 'state' ) === 'on';
72
73 return $this->update_state( $state );
74 }
75
76 /**
77 * {@inheritdoc}
78 */
79 public function get_arguments(): array {
80 return [
81 'state' => [
82 'type' => 'string',
83 'description' => 'The state of the page cache debug mode.',
84 'enum' => [
85 'on',
86 'off',
87 ],
88 ],
89 ];
90 }
91
92 /**
93 * {@inheritdoc}
94 */
95 public function schema_callback(): array {
96 return [
97 '$schema' => 'http://json-schema.org/draft-04/schema#',
98 'title' => 'setting',
99 'type' => 'object',
100 'properties' => [
101 'enabled' => [
102 'description' => esc_html__( 'The status of the page cache.', 'solid-performance' ),
103 'type' => 'boolean',
104 'readonly' => true,
105 ],
106 'code' => [
107 'description' => esc_html__( 'The identification code of the setting state.', 'solid-performance' ),
108 'type' => 'string',
109 'enum' => [
110 'solid_performance_page_cache_debug_mode_on',
111 'solid_performance_page_cache_debug_mode_off',
112 ],
113 'readonly' => true,
114 ],
115 'message' => [
116 'description' => esc_html__( 'The formatted message of the setting state.', 'solid-performance' ),
117 'type' => 'string',
118 'readonly' => true,
119 ],
120 ],
121 ];
122 }
123
124 /**
125 * Returns the status of page cache debug mode.
126 *
127 * @since 0.1.0
128 *
129 * @return WP_REST_Response
130 */
131 protected function status_response(): WP_REST_Response {
132 $status = $this->page_cache->is_debug_on();
133
134 if ( $status ) {
135 return new WP_REST_Response(
136 [
137 'enabled' => true,
138 'code' => 'solid_performance_page_cache_debug_mode_on',
139 'message' => 'Page cache debug mode is enabled',
140 ]
141 );
142 }
143
144 return new WP_REST_Response(
145 [
146 'enabled' => false,
147 'code' => 'solid_performance_page_cache_debug_mode_off',
148 'message' => 'Page cache debug mode is disabled',
149 ]
150 );
151 }
152
153 /**
154 * Changes the state of debug mode and returns a response.
155 *
156 * @since 0.1.0
157 *
158 * @param bool $state The state to change debug mode to.
159 *
160 * @return WP_REST_Response|WP_Error
161 */
162 protected function update_state( bool $state ) {
163 $result = $this->page_cache->debug( $state );
164
165 if ( $result ) {
166 return new WP_REST_Response(
167 [
168 'enabled' => true,
169 'code' => 'solid_performance_page_cache_debug_mode_on',
170 'message' => 'Page cache debug mode is enabled',
171 ]
172 );
173 }
174
175 return new WP_REST_Response(
176 [
177 'enabled' => false,
178 'code' => 'solid_performance_page_cache_debug_mode_off',
179 'message' => 'Page cache debug mode is disabled',
180 ]
181 );
182 }
183 }
184