| 1 |
<?php |
| 2 |
/** |
| 3 |
* The provider responsible for registering API classes with the container & WordPress. |
| 4 |
* |
| 5 |
* @since 0.1.0 |
| 6 |
* |
| 7 |
* @package SolidWP\Performance |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace SolidWP\Performance\API; |
| 11 |
|
| 12 |
use SolidWP\Performance\API\Routes\Page_Cache\Clear; |
| 13 |
use SolidWP\Performance\API\Routes\Page_Cache\Debug; |
| 14 |
use SolidWP\Performance\API\Routes\Page_Cache\Off; |
| 15 |
use SolidWP\Performance\API\Routes\Page_Cache\On; |
| 16 |
use SolidWP\Performance\API\Routes\Page_Cache\Status; |
| 17 |
use SolidWP\Performance\API\Routes\Page_Cache\Regenerate; |
| 18 |
use SolidWP\Performance\Contracts\Service_Provider; |
| 19 |
|
| 20 |
if ( ! defined( 'ABSPATH' ) ) { |
| 21 |
exit; |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* The provider responsible for registering API classes with the container & WordPress. |
| 26 |
* |
| 27 |
* @since 0.1.0 |
| 28 |
* |
| 29 |
* @package SolidWP\Performance |
| 30 |
*/ |
| 31 |
class Provider extends Service_Provider { |
| 32 |
|
| 33 |
/** |
| 34 |
* The namespace of the plugin's REST API routes. |
| 35 |
* |
| 36 |
* @since 0.1.0 |
| 37 |
* |
| 38 |
* @var string |
| 39 |
*/ |
| 40 |
private string $namespace = 'solid-performance/v1'; |
| 41 |
|
| 42 |
/** |
| 43 |
* All the endpoints that should be registered with the REST API. |
| 44 |
* |
| 45 |
* @since 0.1.0 |
| 46 |
* |
| 47 |
* @var array<int,string> |
| 48 |
*/ |
| 49 |
private array $endpoints = [ |
| 50 |
Clear::class, |
| 51 |
Debug::class, |
| 52 |
Off::class, |
| 53 |
On::class, |
| 54 |
Regenerate::class, |
| 55 |
Status::class, |
| 56 |
]; |
| 57 |
|
| 58 |
/** |
| 59 |
* {@inheritdoc} |
| 60 |
*/ |
| 61 |
public function register(): void { |
| 62 |
add_action( 'rest_api_init', [ $this, 'register_all_endpoints' ] ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Registers all endpoints of the REST API. |
| 67 |
* |
| 68 |
* @since 0.1.0 |
| 69 |
* |
| 70 |
* @return void |
| 71 |
*/ |
| 72 |
public function register_all_endpoints(): void { |
| 73 |
foreach ( $this->endpoints as $class ) { |
| 74 |
/** @var Route */ |
| 75 |
$endpoint = $this->container->get( $class ); |
| 76 |
register_rest_route( |
| 77 |
$this->namespace, |
| 78 |
$endpoint->get_path(), |
| 79 |
[ |
| 80 |
[ |
| 81 |
'methods' => $endpoint->get_methods(), |
| 82 |
'callback' => [ $endpoint, 'callback' ], |
| 83 |
'args' => $endpoint->get_arguments(), |
| 84 |
'permission_callback' => [ $endpoint, 'permission_callback' ], |
| 85 |
], |
| 86 |
'schema' => [ $endpoint, 'schema_callback' ], |
| 87 |
], |
| 88 |
); |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|