| 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 |
public const PARAM_STATE = 'state'; |
| 33 |
|
| 34 |
/** |
| 35 |
* @var Page_Cache |
| 36 |
*/ |
| 37 |
private Page_Cache $page_cache; |
| 38 |
|
| 39 |
/** |
| 40 |
* @param Page_Cache $page_cache The page cache object. |
| 41 |
*/ |
| 42 |
public function __construct( Page_Cache $page_cache ) { |
| 43 |
$this->page_cache = $page_cache; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* {@inheritdoc} |
| 48 |
*/ |
| 49 |
public function get_path(): string { |
| 50 |
return '/page/debug'; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* {@inheritdoc} |
| 55 |
*/ |
| 56 |
public function get_methods() { |
| 57 |
return [ |
| 58 |
WP_REST_Server::READABLE, |
| 59 |
WP_REST_Server::CREATABLE, |
| 60 |
]; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* {@inheritdoc} |
| 65 |
*/ |
| 66 |
public function callback( WP_REST_Request $request ) { |
| 67 |
|
| 68 |
// Return the current status for a GET request. |
| 69 |
if ( $request->get_method() === WP_REST_Server::READABLE ) { |
| 70 |
return $this->status_response(); |
| 71 |
} |
| 72 |
|
| 73 |
$state = $request->get_param( self::PARAM_STATE ) === 'on'; |
| 74 |
|
| 75 |
return $this->update_state( $state ); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* {@inheritdoc} |
| 80 |
*/ |
| 81 |
public function get_arguments(): array { |
| 82 |
return [ |
| 83 |
WP_REST_Server::CREATABLE => [ |
| 84 |
self::PARAM_STATE => [ |
| 85 |
'type' => 'string', |
| 86 |
'description' => esc_html__( 'The state of the page cache debug mode.', 'solid-performance' ), |
| 87 |
'enum' => [ |
| 88 |
'on', |
| 89 |
'off', |
| 90 |
], |
| 91 |
], |
| 92 |
], |
| 93 |
]; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* {@inheritdoc} |
| 98 |
*/ |
| 99 |
public function schema_callback(): array { |
| 100 |
return [ |
| 101 |
'$schema' => 'http://json-schema.org/draft-04/schema#', |
| 102 |
'title' => 'setting', |
| 103 |
'type' => 'object', |
| 104 |
'properties' => [ |
| 105 |
'enabled' => [ |
| 106 |
'description' => esc_html__( 'The status of the page cache.', 'solid-performance' ), |
| 107 |
'type' => 'boolean', |
| 108 |
'readonly' => true, |
| 109 |
], |
| 110 |
'code' => [ |
| 111 |
'description' => esc_html__( 'The identification code of the setting state.', 'solid-performance' ), |
| 112 |
'type' => 'string', |
| 113 |
'enum' => [ |
| 114 |
'solid_performance_page_cache_debug_mode_on', |
| 115 |
'solid_performance_page_cache_debug_mode_off', |
| 116 |
], |
| 117 |
'readonly' => true, |
| 118 |
], |
| 119 |
'message' => [ |
| 120 |
'description' => esc_html__( 'The formatted message of the setting state.', 'solid-performance' ), |
| 121 |
'type' => 'string', |
| 122 |
'readonly' => true, |
| 123 |
], |
| 124 |
], |
| 125 |
]; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Returns the status of page cache debug mode. |
| 130 |
* |
| 131 |
* @since 0.1.0 |
| 132 |
* |
| 133 |
* @return WP_REST_Response |
| 134 |
*/ |
| 135 |
protected function status_response(): WP_REST_Response { |
| 136 |
$status = $this->page_cache->is_debug_on(); |
| 137 |
|
| 138 |
if ( $status ) { |
| 139 |
return new WP_REST_Response( |
| 140 |
[ |
| 141 |
'enabled' => true, |
| 142 |
'code' => 'solid_performance_page_cache_debug_mode_on', |
| 143 |
'message' => __( 'Page cache debug mode is enabled', 'solid-performance' ), |
| 144 |
] |
| 145 |
); |
| 146 |
} |
| 147 |
|
| 148 |
return new WP_REST_Response( |
| 149 |
[ |
| 150 |
'enabled' => false, |
| 151 |
'code' => 'solid_performance_page_cache_debug_mode_off', |
| 152 |
'message' => __( 'Page cache debug mode is disabled', 'solid-performance' ), |
| 153 |
] |
| 154 |
); |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Changes the state of debug mode and returns a response. |
| 159 |
* |
| 160 |
* @since 0.1.0 |
| 161 |
* |
| 162 |
* @param bool $state The state to change debug mode to. |
| 163 |
* |
| 164 |
* @return WP_REST_Response|WP_Error |
| 165 |
*/ |
| 166 |
protected function update_state( bool $state ) { |
| 167 |
$result = $this->page_cache->debug( $state ); |
| 168 |
|
| 169 |
if ( $result ) { |
| 170 |
return new WP_REST_Response( |
| 171 |
[ |
| 172 |
'enabled' => true, |
| 173 |
'code' => 'solid_performance_page_cache_debug_mode_on', |
| 174 |
'message' => __( 'Page cache debug mode is enabled', 'solid-performance' ), |
| 175 |
] |
| 176 |
); |
| 177 |
} |
| 178 |
|
| 179 |
return new WP_REST_Response( |
| 180 |
[ |
| 181 |
'enabled' => false, |
| 182 |
'code' => 'solid_performance_page_cache_debug_mode_off', |
| 183 |
'message' => __( 'Page cache debug mode is disabled', 'solid-performance' ), |
| 184 |
] |
| 185 |
); |
| 186 |
} |
| 187 |
} |
| 188 |
|