| 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; |
| 11 |
use JordanLeven\Plugins\ForceRefresh\Services\Versions_Storage_Service; |
| 12 |
|
| 13 |
/** |
| 14 |
* Main class controller. |
| 15 |
*/ |
| 16 |
class Api_Handler_Client extends Api_Handler { |
| 17 |
|
| 18 |
/** |
| 19 |
* The path for this endpoint. |
| 20 |
* |
| 21 |
* @var string |
| 22 |
*/ |
| 23 |
const ENDPOINT_PATH = '/current-version'; |
| 24 |
|
| 25 |
/** |
| 26 |
* The version for this endpoint. |
| 27 |
* |
| 28 |
* @var int |
| 29 |
*/ |
| 30 |
const ENDPOINT_VERSION = 1; |
| 31 |
|
| 32 |
/** |
| 33 |
* Method for registering the endpoints for this class. |
| 34 |
* |
| 35 |
* @return void |
| 36 |
*/ |
| 37 |
public function register_routes(): void { |
| 38 |
self::register_rest_endpoint( |
| 39 |
self::ENDPOINT_PATH, |
| 40 |
self::ENDPOINT_VERSION, |
| 41 |
array( |
| 42 |
'methods' => \WP_REST_Server::READABLE, |
| 43 |
'callback' => array( $this, 'get_version' ), |
| 44 |
'permission_callback' => '__return_true', |
| 45 |
), |
| 46 |
); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Method used by ajax requests to get the current site version. |
| 51 |
* |
| 52 |
* @param \WP_REST_Request $request The ReST Request object. |
| 53 |
* |
| 54 |
* @return \WP_REST_Response |
| 55 |
*/ |
| 56 |
public function get_version( \WP_REST_Request $request ): \WP_REST_Response { |
| 57 |
$post_id = $request->get_param( 'postId' ) ?? null; |
| 58 |
|
| 59 |
$response = array( |
| 60 |
'currentVersionSite' => $this->get_current_version_site(), |
| 61 |
); |
| 62 |
|
| 63 |
if ( $post_id ) { |
| 64 |
$response['currentVersionPage'] = $this->get_current_version_post( $post_id ); |
| 65 |
} |
| 66 |
|
| 67 |
return $this->return_api_response( |
| 68 |
\WP_Http::OK, |
| 69 |
'The current site version has been successfully retrieved.', |
| 70 |
$response, |
| 71 |
); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Method to get the current version of the site. |
| 76 |
* |
| 77 |
* @return string The version of the site |
| 78 |
*/ |
| 79 |
private function get_current_version_site(): string { |
| 80 |
$current_site_version = get_option( 'force_refresh_current_site_version' ); |
| 81 |
return (bool) $current_site_version ? $current_site_version : '0'; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Method to get the current version for a specific post. |
| 86 |
* |
| 87 |
* @param int $post_id The post ID to check. |
| 88 |
* |
| 89 |
* @return string The version of the provided post |
| 90 |
*/ |
| 91 |
private static function get_current_version_post( int $post_id ): string { |
| 92 |
return Versions_Storage_Service::get_page_version( $post_id ); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Method for getting the endpoint for this service. |
| 97 |
* |
| 98 |
* @return string The service endpoint. |
| 99 |
*/ |
| 100 |
public static function get_rest_endpoint(): string { |
| 101 |
return self::get_formatted_rest_endpoint( self::ENDPOINT_PATH, self::ENDPOINT_VERSION ); |
| 102 |
} |
| 103 |
} |
| 104 |
|