| 1 |
<?php |
| 2 |
/** |
| 3 |
* Our API calls responsible for handling requests from website visitors. |
| 4 |
* |
| 5 |
* @package ForceRefresh |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Function to get the current version of the site. |
| 10 |
* |
| 11 |
* @return string The version of the site |
| 12 |
*/ |
| 13 |
function get_current_version_site() { |
| 14 |
$current_site_version = get_option( 'force_refresh_current_site_version' ); |
| 15 |
return ! ! $current_site_version ? $current_site_version : '0'; |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Function to get the current version for a specific post. |
| 20 |
* |
| 21 |
* @param int $post_id The post ID to check. |
| 22 |
* |
| 23 |
* @return string The version of the provided post |
| 24 |
*/ |
| 25 |
function get_current_version_post( int $post_id ) { |
| 26 |
$current_page_version = get_post_meta( $post_id, 'force_refresh_current_page_version', true ); |
| 27 |
return ! ! $current_page_version ? $current_page_version : '0'; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Function used by ajax requests to get the current site version. |
| 32 |
* |
| 33 |
* @return void |
| 34 |
*/ |
| 35 |
function get_version() { |
| 36 |
// Since this is a client-side request, we don't need to validate the nonce |
| 37 |
// since there is no concept of a user session for end-users. |
| 38 |
// phpcs:disable WordPress.Security.NonceVerification |
| 39 |
|
| 40 |
// Get the post ID. |
| 41 |
$post_id = isset( $_REQUEST['postId'] ) |
| 42 |
? sanitize_text_field( wp_unslash( $_REQUEST['postId'] ) ) |
| 43 |
: null; |
| 44 |
return_api_response( |
| 45 |
200, |
| 46 |
'The current site version has been successfully retrieved.', |
| 47 |
array( |
| 48 |
'currentVersionSite' => get_current_version_site(), |
| 49 |
'currentVersionPage' => get_current_version_post( $post_id ), |
| 50 |
) |
| 51 |
); |
| 52 |
|
| 53 |
// phpcs:enable WordPress.Security.NonceVerification |
| 54 |
} |
| 55 |
|
| 56 |
// The call used for users requesting the current site version from non-admins. |
| 57 |
add_action( |
| 58 |
'wp_ajax_nopriv_force_refresh_get_version', |
| 59 |
__NAMESPACE__ . '\\get_version' |
| 60 |
); |
| 61 |
|
| 62 |
// The call used for users requesting the current site version from admins. |
| 63 |
add_action( |
| 64 |
'wp_ajax_force_refresh_get_version', |
| 65 |
__NAMESPACE__ . '\\get_version' |
| 66 |
); |
| 67 |
|