| 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 Off 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/off'; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* {@inheritdoc} |
| 53 |
*/ |
| 54 |
public function get_methods() { |
| 55 |
return WP_REST_Server::CREATABLE; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* {@inheritdoc} |
| 60 |
*/ |
| 61 |
public function callback( WP_REST_Request $request ) { |
| 62 |
$result = $this->page_cache->off(); |
| 63 |
|
| 64 |
if ( ! $result ) { |
| 65 |
return new WP_Error( |
| 66 |
'solid_performance_page_cache_not_disabled', |
| 67 |
'Page cache could not be disabled', |
| 68 |
); |
| 69 |
} |
| 70 |
|
| 71 |
return new WP_REST_Response( |
| 72 |
[ |
| 73 |
'enabled' => false, |
| 74 |
'code' => 'solid_performance_page_cache_off', |
| 75 |
'message' => 'Page cache disabled successfully', |
| 76 |
] |
| 77 |
); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* {@inheritdoc} |
| 82 |
*/ |
| 83 |
public function schema_callback(): array { |
| 84 |
return [ |
| 85 |
'$schema' => 'http://json-schema.org/draft-04/schema#', |
| 86 |
'title' => 'setting', |
| 87 |
'type' => 'object', |
| 88 |
'properties' => [ |
| 89 |
'enabled' => [ |
| 90 |
'description' => esc_html__( 'The current state of page caching.', 'solid-performance' ), |
| 91 |
'type' => 'boolean', |
| 92 |
'readonly' => true, |
| 93 |
], |
| 94 |
'code' => [ |
| 95 |
'description' => esc_html__( 'The identification code of the setting.', 'solid-performance' ), |
| 96 |
'type' => 'string', |
| 97 |
'enum' => [ |
| 98 |
'solid_performance_page_cache_not_disabled', |
| 99 |
'solid_performance_page_cache_off', |
| 100 |
], |
| 101 |
'readonly' => true, |
| 102 |
], |
| 103 |
'message' => [ |
| 104 |
'description' => esc_html__( 'The formatted message of the setting change.', 'solid-performance' ), |
| 105 |
'type' => 'string', |
| 106 |
'readonly' => true, |
| 107 |
], |
| 108 |
], |
| 109 |
]; |
| 110 |
} |
| 111 |
} |
| 112 |
|