| 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; |
| 9 |
|
| 10 |
/** |
| 11 |
* Function used to get the post ID of the post that is requesting a refresh. |
| 12 |
* |
| 13 |
* @return mixed The post ID of the requested post or null |
| 14 |
*/ |
| 15 |
function get_refresh_request_post_id() { |
| 16 |
// This function returns the post id after the nonce has already been validated. |
| 17 |
// phpcs:disable WordPress.Security.NonceVerification |
| 18 |
$post_id = isset( $_REQUEST['post_id'] ) |
| 19 |
? sanitize_text_field( wp_unslash( $_REQUEST['post_id'] ) ) |
| 20 |
: null; |
| 21 |
// phpcs:enable WordPress.Security.NonceVerification |
| 22 |
return $post_id; |
| 23 |
}; |
| 24 |
|
| 25 |
/** |
| 26 |
* Function used by administrators to request a refresh of a specific page. |
| 27 |
* |
| 28 |
* @return void |
| 29 |
*/ |
| 30 |
function request_refresh_page() { |
| 31 |
if ( ! is_user_authorized_to_request_refresh() ) { |
| 32 |
return_api_response( |
| 33 |
401, |
| 34 |
MESSAGE_ERROR_UNAUTHORIZED |
| 35 |
); |
| 36 |
} else { |
| 37 |
$page_version = get_new_version(); |
| 38 |
// Get the page id. |
| 39 |
$page_id = get_refresh_request_post_id(); |
| 40 |
// Remove the old. |
| 41 |
delete_post_meta( $page_id, 'force_refresh_current_page_version' ); |
| 42 |
// Add the new. |
| 43 |
update_post_meta( |
| 44 |
$page_id, |
| 45 |
'force_refresh_current_page_version', |
| 46 |
$page_version, |
| 47 |
null, |
| 48 |
'no' |
| 49 |
); |
| 50 |
return_api_response( |
| 51 |
200, |
| 52 |
MESSAGE_SUCCESS_PAGE, |
| 53 |
array( |
| 54 |
'new_page_version' => $page_version, |
| 55 |
'refresh_interval' => (int) get_option( |
| 56 |
WP_FORCE_REFRESH_OPTION_REFRESH_INTERVAL_IN_SECONDS, |
| 57 |
WP_FORCE_REFRESH_OPTION_REFRESH_INTERVAL_IN_SECONDS_DEFAULT |
| 58 |
), |
| 59 |
) |
| 60 |
); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
// Register the action used by administrators to refresh a specific page. |
| 65 |
add_action( |
| 66 |
'wp_ajax_force_refresh_update_page_version', |
| 67 |
__NAMESPACE__ . '\\request_refresh_page' |
| 68 |
); |
| 69 |
|