Files

104 lines
2.8 KiB
PHP

<?php
/**
* Our API calls responsible for handling requests from admins requesting a refresh for the site.
*
* @package ForceRefresh
*/
namespace JordanLeven\Plugins\ForceRefresh\Api;
use JordanLeven\Plugins\ForceRefresh\Api\Api_Handler;
use JordanLeven\Plugins\ForceRefresh\Services\Versions_Storage_Service;
/**
* Main class controller.
*/
class Api_Handler_Client extends Api_Handler {
/**
* The path for this endpoint.
*
* @var string
*/
const ENDPOINT_PATH = '/current-version';
/**
* The version for this endpoint.
*
* @var int
*/
const ENDPOINT_VERSION = 1;
/**
* Method for registering the endpoints for this class.
*
* @return void
*/
public function register_routes(): void {
self::register_rest_endpoint(
self::ENDPOINT_PATH,
self::ENDPOINT_VERSION,
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_version' ),
'permission_callback' => '__return_true',
),
);
}
/**
* Method used by ajax requests to get the current site version.
*
* @param \WP_REST_Request $request The ReST Request object.
*
* @return \WP_REST_Response
*/
public function get_version( \WP_REST_Request $request ): \WP_REST_Response {
$post_id = $request->get_param( 'postId' ) ?? null;
$response = array(
'currentVersionSite' => $this->get_current_version_site(),
);
if ( $post_id ) {
$response['currentVersionPage'] = $this->get_current_version_post( $post_id );
}
return $this->return_api_response(
\WP_Http::OK,
'The current site version has been successfully retrieved.',
$response,
);
}
/**
* Method to get the current version of the site.
*
* @return string The version of the site
*/
private function get_current_version_site(): string {
$current_site_version = get_option( 'force_refresh_current_site_version' );
return (bool) $current_site_version ? $current_site_version : '0';
}
/**
* Method to get the current version for a specific post.
*
* @param int $post_id The post ID to check.
*
* @return string The version of the provided post
*/
private static function get_current_version_post( int $post_id ): string {
return Versions_Storage_Service::get_page_version( $post_id );
}
/**
* Method for getting the endpoint for this service.
*
* @return string The service endpoint.
*/
public static function get_rest_endpoint(): string {
return self::get_formatted_rest_endpoint( self::ENDPOINT_PATH, self::ENDPOINT_VERSION );
}
}