| 1 |
<?php |
| 2 |
/** |
| 3 |
* Our API calls responsible for handling requests from admins requesting a refresh for the site. |
| 4 |
* |
| 5 |
* @package ForceRefresh |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace JordanLeven\Plugins\ForceRefresh\Api; |
| 9 |
|
| 10 |
use JordanLeven\Plugins\ForceRefresh\Api\Api_Handler_Admin; |
| 11 |
use JordanLeven\Plugins\ForceRefresh\Api\Interfaces\Api_Handler_Admin_Interface; |
| 12 |
use JordanLeven\Plugins\ForceRefresh\Services\Versions_Storage_Service; |
| 13 |
|
| 14 |
/** |
| 15 |
* Main class controller. |
| 16 |
*/ |
| 17 |
class Api_Handler_Admin_Refresh_Site extends Api_Handler_Admin implements Api_Handler_Admin_Interface { |
| 18 |
|
| 19 |
/** |
| 20 |
* The path for this endpoint. |
| 21 |
* |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
const ENDPOINT_PATH = '/site-version'; |
| 25 |
|
| 26 |
/** |
| 27 |
* The version for this endpoint. |
| 28 |
* |
| 29 |
* @var int |
| 30 |
*/ |
| 31 |
const ENDPOINT_VERSION = 1; |
| 32 |
|
| 33 |
/** |
| 34 |
* Method for registering the endpoints for this class. |
| 35 |
* |
| 36 |
* @return void |
| 37 |
*/ |
| 38 |
public function register_routes(): void { |
| 39 |
self::register_rest_endpoint( |
| 40 |
self::ENDPOINT_PATH, |
| 41 |
self::ENDPOINT_VERSION, |
| 42 |
array( |
| 43 |
'methods' => \WP_REST_Server::EDITABLE, |
| 44 |
'callback' => array( $this, 'refresh_site' ), |
| 45 |
'permission_callback' => array( $this, 'user_is_able_to_admin_force_refresh' ), |
| 46 |
), |
| 47 |
); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Method for refreshing the site version. |
| 52 |
* |
| 53 |
* @return void |
| 54 |
*/ |
| 55 |
public function refresh_site(): void { |
| 56 |
$site_version = Versions_Storage_Service::get_new_version(); |
| 57 |
|
| 58 |
Versions_Storage_Service::set_site_version( $site_version ); |
| 59 |
|
| 60 |
$this->return_api_response( |
| 61 |
201, |
| 62 |
'You\'ve successfully requested all browsers to refresh', |
| 63 |
array( |
| 64 |
'new_site_version' => $site_version, |
| 65 |
) |
| 66 |
); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Method for getting the endpoint for this service. |
| 71 |
* |
| 72 |
* @return string The service endpoint. |
| 73 |
*/ |
| 74 |
public static function get_rest_endpoint(): string { |
| 75 |
return self::get_formatted_rest_endpoint( self::ENDPOINT_PATH, self::ENDPOINT_VERSION ); |
| 76 |
} |
| 77 |
} |
| 78 |
|