| 1 |
<?php |
| 2 |
/** |
| 3 |
* The route to get the current Cache Delivery state. |
| 4 |
* |
| 5 |
* @package SolidWP\Performance |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace SolidWP\Performance\API\Routes\Page_Cache; |
| 11 |
|
| 12 |
use SolidWP\Performance\Admin\Settings_Page; |
| 13 |
use SolidWP\Performance\API\Base_Route; |
| 14 |
use SolidWP\Performance\Cache_Delivery\Cache_Delivery_Type; |
| 15 |
use SolidWP\Performance\Cache_Delivery\Exceptions\CacheDeliveryReadException; |
| 16 |
use SolidWP\Performance\Cache_Delivery\Manager_Collection; |
| 17 |
use SolidWP\Performance\StellarWP\Arrays\Arr; |
| 18 |
use WP_Error; |
| 19 |
use WP_REST_Request; |
| 20 |
use WP_REST_Response; |
| 21 |
use WP_REST_Server; |
| 22 |
|
| 23 |
if ( ! defined( 'ABSPATH' ) ) { |
| 24 |
exit; |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* The route to get the current Cache Delivery state. |
| 29 |
* |
| 30 |
* @package SolidWP\Performance |
| 31 |
*/ |
| 32 |
final class Cache_Delivery extends Base_Route { |
| 33 |
|
| 34 |
public const CODE_FAILED = 'solid_performance_cache_delivery_failed'; |
| 35 |
|
| 36 |
public const PARAM_NGINX_RULES = 'nginx_rules'; |
| 37 |
|
| 38 |
/** |
| 39 |
* @var Manager_Collection |
| 40 |
*/ |
| 41 |
private Manager_Collection $manager_collection; |
| 42 |
|
| 43 |
/** |
| 44 |
* @param Manager_Collection $manager_collection The cache delivery manager collection. |
| 45 |
*/ |
| 46 |
public function __construct( Manager_Collection $manager_collection ) { |
| 47 |
$this->manager_collection = $manager_collection; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* {@inheritdoc} |
| 52 |
*/ |
| 53 |
public function get_path(): string { |
| 54 |
return '/page/cache-delivery'; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* {@inheritdoc} |
| 59 |
*/ |
| 60 |
public function get_methods() { |
| 61 |
return WP_REST_Server::READABLE; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* {@inheritdoc} |
| 66 |
*/ |
| 67 |
public function get_arguments(): array { |
| 68 |
return [ |
| 69 |
WP_REST_Server::READABLE => [ |
| 70 |
self::PARAM_NGINX_RULES => [ |
| 71 |
'type' => 'boolean', |
| 72 |
'description' => esc_html__( 'Whether to include the current rules from the swpsp-nginx.conf file in the response.', 'solid-performance' ), |
| 73 |
'required' => false, |
| 74 |
], |
| 75 |
], |
| 76 |
]; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* {@inheritdoc} |
| 81 |
*/ |
| 82 |
public function callback( WP_REST_Request $request ) { |
| 83 |
$htaccess = $this->manager_collection->get( Cache_Delivery_Type::HTACCESS ); |
| 84 |
$nginx = $this->manager_collection->get( Cache_Delivery_Type::NGINX ); |
| 85 |
|
| 86 |
$response = [ |
| 87 |
'cacheDelivery' => [ |
| 88 |
'htaccess' => [ |
| 89 |
'supported' => $htaccess->supported(), |
| 90 |
'hasRules' => $htaccess->has_rules(), |
| 91 |
], |
| 92 |
'nginx' => [ |
| 93 |
'supported' => $nginx->supported(), |
| 94 |
'hasRules' => $nginx->has_rules(), |
| 95 |
], |
| 96 |
], |
| 97 |
]; |
| 98 |
|
| 99 |
if ( $request->get_param( self::PARAM_NGINX_RULES ) ) { |
| 100 |
try { |
| 101 |
$response = Arr::set( $response, 'cacheDelivery.nginx.rules', $nginx->get() ); |
| 102 |
} catch ( CacheDeliveryReadException $e ) { |
| 103 |
return new WP_Error( |
| 104 |
self::CODE_FAILED, |
| 105 |
$e->getMessage(), |
| 106 |
); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
return new WP_REST_Response( $response ); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* {@inheritdoc} |
| 115 |
* |
| 116 |
* @see Settings_Page::admin_head() |
| 117 |
*/ |
| 118 |
public function schema_callback(): array { |
| 119 |
return [ |
| 120 |
'$schema' => 'http://json-schema.org/draft-04/schema#', |
| 121 |
'title' => 'cache_delivery_status', |
| 122 |
'type' => 'object', |
| 123 |
'properties' => [ |
| 124 |
'cacheDelivery' => [ |
| 125 |
'description' => esc_html__( 'The cache delivery status for each method.', 'solid-performance' ), |
| 126 |
'type' => 'object', |
| 127 |
'properties' => [ |
| 128 |
'htaccess' => [ |
| 129 |
'type' => 'object', |
| 130 |
'properties' => [ |
| 131 |
'supported' => [ |
| 132 |
'description' => esc_html__( 'Whether .htaccess delivery is supported.', 'solid-performance' ), |
| 133 |
'type' => 'boolean', |
| 134 |
'readonly' => true, |
| 135 |
], |
| 136 |
'hasRules' => [ |
| 137 |
'description' => esc_html__( 'Whether .htaccess rules are present.', 'solid-performance' ), |
| 138 |
'type' => 'boolean', |
| 139 |
'readonly' => true, |
| 140 |
], |
| 141 |
'path' => [ |
| 142 |
'description' => esc_html__( 'The optional path to the configuration file.', 'solid-performance' ), |
| 143 |
'type' => 'string', |
| 144 |
'readonly' => true, |
| 145 |
'required' => false, |
| 146 |
], |
| 147 |
], |
| 148 |
], |
| 149 |
'nginx' => [ |
| 150 |
'type' => 'object', |
| 151 |
'properties' => [ |
| 152 |
'supported' => [ |
| 153 |
'description' => esc_html__( 'Whether Nginx delivery is supported.', 'solid-performance' ), |
| 154 |
'type' => 'boolean', |
| 155 |
'readonly' => true, |
| 156 |
], |
| 157 |
'hasRules' => [ |
| 158 |
'description' => esc_html__( 'Whether Nginx rules are present.', 'solid-performance' ), |
| 159 |
'type' => 'boolean', |
| 160 |
'readonly' => true, |
| 161 |
], |
| 162 |
'rules' => [ |
| 163 |
'description' => esc_html__( 'The contents of the swpsp-nginx.conf.', 'solid-performance' ), |
| 164 |
'type' => 'string', |
| 165 |
'readonly' => true, |
| 166 |
'required' => false, |
| 167 |
], |
| 168 |
], |
| 169 |
], |
| 170 |
], |
| 171 |
], |
| 172 |
], |
| 173 |
]; |
| 174 |
} |
| 175 |
} |
| 176 |
|