| 1 |
<?php |
| 2 |
/** |
| 3 |
* Our API calls responsible for handling requests from admins requesting a refresh for the page. |
| 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\Options_Storage_Service; |
| 13 |
use JordanLeven\Plugins\ForceRefresh\Services\Refresh_Service; |
| 14 |
|
| 15 |
/** |
| 16 |
* Main class controller. |
| 17 |
*/ |
| 18 |
class Api_Handler_Admin_Refresh_Page extends Api_Handler_Admin implements Api_Handler_Admin_Interface { |
| 19 |
|
| 20 |
/** |
| 21 |
* The path for this endpoint. |
| 22 |
* |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
const ENDPOINT_PATH = '/page-version'; |
| 26 |
|
| 27 |
/** |
| 28 |
* The version for this endpoint. |
| 29 |
* |
| 30 |
* @var int |
| 31 |
*/ |
| 32 |
const ENDPOINT_VERSION = 1; |
| 33 |
|
| 34 |
/** |
| 35 |
* Method for registering the endpoints for this class. |
| 36 |
* |
| 37 |
* @return void |
| 38 |
*/ |
| 39 |
public function register_routes(): void { |
| 40 |
self::register_rest_endpoint( |
| 41 |
self::ENDPOINT_PATH, |
| 42 |
self::ENDPOINT_VERSION, |
| 43 |
array( |
| 44 |
'methods' => \WP_REST_Server::CREATABLE, |
| 45 |
'callback' => array( $this, 'refresh_page' ), |
| 46 |
'permission_callback' => $this->get_admin_permission_callback(), |
| 47 |
), |
| 48 |
); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Method for refreshing the page version. |
| 53 |
* |
| 54 |
* @param \WP_REST_Request $request The request object. |
| 55 |
* |
| 56 |
* @return \WP_REST_Response |
| 57 |
*/ |
| 58 |
public function refresh_page( \WP_REST_Request $request ): \WP_REST_Response { |
| 59 |
$page_id = $request->get_param( 'postId' ) ?? null; |
| 60 |
$page_version = Refresh_Service::update_version_page( $page_id ); |
| 61 |
|
| 62 |
return $this->return_api_response( |
| 63 |
\WP_Http::CREATED, |
| 64 |
'You\'ve successfully requested all browsers to refresh this page.', |
| 65 |
array( |
| 66 |
'new_page_version' => $page_version, |
| 67 |
'page_id' => $page_id, |
| 68 |
'refresh_interval' => Options_Storage_Service::get_refresh_interval(), |
| 69 |
) |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Method for getting the endpoint for this service. |
| 75 |
* |
| 76 |
* @return string The service endpoint. |
| 77 |
*/ |
| 78 |
public static function get_rest_endpoint(): string { |
| 79 |
return self::get_formatted_rest_endpoint( self::ENDPOINT_PATH, self::ENDPOINT_VERSION ); |
| 80 |
} |
| 81 |
} |
| 82 |
|