PluginProbe
Force Refresh / 2.16.2
Force Refresh v2.16.2
3.2.1 3.2.0 3.1.2 3.1.1 3.1.0 trunk 1.0.0 1.1.0 1.1.1 1.1.2 1.2.0 2.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.11.0 2.11.1 2.12.0 All 54 releases
force-refresh / includes / api / classes / class-api-handler-client.php

class-api-handler-client.php in Force Refresh 2.16.2, at includes/api/classes/class-api-handler-client.php

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