| 1 |
<?php |
| 2 |
/** |
| 3 |
* The route to manage generating and removing our htaccess rules. |
| 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\API\Base_Route; |
| 13 |
use SolidWP\Performance\Cache_Delivery\Htaccess\Manager; |
| 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 route to manage generating and removing our htaccess rules. |
| 25 |
* |
| 26 |
* @package SolidWP\Performance |
| 27 |
*/ |
| 28 |
final class Htaccess extends Base_Route { |
| 29 |
|
| 30 |
public const CODE_SUCCESS = 'solid_performance_htaccess_success'; |
| 31 |
public const CODE_FAILED = 'solid_performance_htaccess_failed'; |
| 32 |
|
| 33 |
/** |
| 34 |
* @var Manager |
| 35 |
*/ |
| 36 |
private Manager $manager; |
| 37 |
|
| 38 |
/** |
| 39 |
* @param Manager $manager The htaccess manager. |
| 40 |
*/ |
| 41 |
public function __construct( Manager $manager ) { |
| 42 |
$this->manager = $manager; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* {@inheritdoc} |
| 47 |
*/ |
| 48 |
public function get_path(): string { |
| 49 |
return '/page/htaccess'; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* {@inheritdoc} |
| 54 |
*/ |
| 55 |
public function get_methods() { |
| 56 |
return [ |
| 57 |
WP_REST_Server::READABLE, |
| 58 |
WP_REST_Server::CREATABLE, |
| 59 |
WP_REST_Server::DELETABLE, |
| 60 |
]; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* {@inheritdoc} |
| 65 |
*/ |
| 66 |
public function callback( WP_REST_Request $request ) { |
| 67 |
if ( $request->get_method() === WP_REST_Server::DELETABLE ) { |
| 68 |
$result = $this->manager->remove_rules(); |
| 69 |
|
| 70 |
if ( ! $result ) { |
| 71 |
return new WP_Error( |
| 72 |
self::CODE_FAILED, |
| 73 |
__( 'Failed to remove htaccess rules', 'solid-performance' ), |
| 74 |
); |
| 75 |
} |
| 76 |
|
| 77 |
return new WP_REST_Response( |
| 78 |
[ |
| 79 |
'code' => self::CODE_SUCCESS, |
| 80 |
'message' => __( 'Htaccess rules removed', 'solid-performance' ), |
| 81 |
] |
| 82 |
); |
| 83 |
} elseif ( $request->get_method() === WP_REST_Server::CREATABLE ) { |
| 84 |
$result = $this->manager->add_rules( true ); |
| 85 |
|
| 86 |
if ( ! $result ) { |
| 87 |
return new WP_Error( |
| 88 |
self::CODE_FAILED, |
| 89 |
__( 'Failed to regenerate htaccess rules', 'solid-performance' ), |
| 90 |
); |
| 91 |
} |
| 92 |
|
| 93 |
return new WP_REST_Response( |
| 94 |
[ |
| 95 |
'code' => self::CODE_SUCCESS, |
| 96 |
'message' => __( 'Htaccess rules regenerated', 'solid-performance' ), |
| 97 |
] |
| 98 |
); |
| 99 |
} |
| 100 |
|
| 101 |
return new WP_REST_Response( |
| 102 |
[ |
| 103 |
'code' => self::CODE_SUCCESS, |
| 104 |
'has_rules' => $this->manager->has_rules(), |
| 105 |
] |
| 106 |
); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* {@inheritdoc} |
| 111 |
*/ |
| 112 |
public function schema_callback(): array { |
| 113 |
return [ |
| 114 |
'$schema' => 'http://json-schema.org/draft-04/schema#', |
| 115 |
'title' => 'setting', |
| 116 |
'type' => 'object', |
| 117 |
'properties' => [ |
| 118 |
'code' => [ |
| 119 |
'description' => esc_html__( 'The identification code of the htaccess action.', 'solid-performance' ), |
| 120 |
'type' => 'string', |
| 121 |
'enum' => [ |
| 122 |
self::CODE_SUCCESS, |
| 123 |
self::CODE_FAILED, |
| 124 |
], |
| 125 |
'readonly' => true, |
| 126 |
], |
| 127 |
'message' => [ |
| 128 |
'description' => esc_html__( 'The formatted message of the htaccess action.', 'solid-performance' ), |
| 129 |
'type' => 'string', |
| 130 |
'readonly' => true, |
| 131 |
], |
| 132 |
'has_rules' => [ |
| 133 |
'description' => esc_html__( 'Whether our rules are present in the .htaccess file.', 'solid-performance' ), |
| 134 |
'type' => 'boolean', |
| 135 |
'readonly' => true, |
| 136 |
], |
| 137 |
], |
| 138 |
]; |
| 139 |
} |
| 140 |
} |
| 141 |
|