| 1 |
<?php |
| 2 |
|
| 3 |
// The call used for admins requesting a site be refreshed |
| 4 |
add_action( 'wp_ajax_force_refresh_update_site_version', function() { |
| 5 |
// Get the data |
| 6 |
$data = $_REQUEST; |
| 7 |
// Declare our return |
| 8 |
$return_array = array(); |
| 9 |
// Get the nonce |
| 10 |
$nonce = $data['nonce']; |
| 11 |
// Whether or not the call was successful |
| 12 |
$success = false; |
| 13 |
// The HTTP status code |
| 14 |
$status_code = null; |
| 15 |
// The status text |
| 16 |
$status_text = null; |
| 17 |
// The return data |
| 18 |
$return_data = array(); |
| 19 |
// Check the nonce |
| 20 |
if (wp_verify_nonce( $nonce, WP_FORCE_REFRESH_ACTION )){ |
| 21 |
// If the user is authenticated, get the current site version by making a hash of the current date |
| 22 |
$time = current_time( 'timestamp' ); |
| 23 |
// Create the site version |
| 24 |
$site_version_hash = wp_hash( $time ); |
| 25 |
// Get the first eight characters (the chance of having a duplicate hash from the first 8 characters is low) |
| 26 |
$site_version = substr( $site_version_hash, 0, 8 ); |
| 27 |
// Remove the old option |
| 28 |
delete_option( 'force_refresh_current_site_version' ); |
| 29 |
// Add the new option |
| 30 |
add_option('force_refresh_current_site_version', $site_version, null, 'no' ); |
| 31 |
// The call was successful |
| 32 |
$success = true; |
| 33 |
// Redeclare the status code as 200 |
| 34 |
$status_code = 200; |
| 35 |
// Redeclare the status text |
| 36 |
$status_text = 'You\'ve successfully requested all browsers to refresh (version <code>'. $site_version .'</code>).'; |
| 37 |
// Redeclare the status text |
| 38 |
$return_data['new_site_version'] = $site_version; |
| 39 |
} |
| 40 |
else { |
| 41 |
// Redeclare the status code as 401 (unauthorized) |
| 42 |
$status_code = 401; |
| 43 |
$status_text = 'There was an issue verifying your nonce ($nonce).'; |
| 44 |
} |
| 45 |
// Set the HTTP code |
| 46 |
status_header($status_code); |
| 47 |
// Return the success |
| 48 |
$return_array['success'] = $success; |
| 49 |
// Return the status code |
| 50 |
$return_array['status_code'] = $status_code; |
| 51 |
// Return the status text |
| 52 |
$return_array['status_text'] = $status_text; |
| 53 |
// Return the return data |
| 54 |
$return_array['return_data'] = $return_data; |
| 55 |
print json_encode($return_array); |
| 56 |
wp_die(); |
| 57 |
}); |
| 58 |
|
| 59 |
// The call used for admins requesting a site be refreshed |
| 60 |
add_action( 'wp_ajax_force_refresh_update_page_version', function(){ |
| 61 |
// Get the data |
| 62 |
$data = $_REQUEST; |
| 63 |
// Declare our return |
| 64 |
$return_array = array(); |
| 65 |
// Get the nonce |
| 66 |
$nonce = $data['nonce']; |
| 67 |
// Get the page id |
| 68 |
$page_id = $data['page_id']; |
| 69 |
// Whether or not the call was successful |
| 70 |
$success = false; |
| 71 |
// The HTTP status code |
| 72 |
$status_code = null; |
| 73 |
// The status text |
| 74 |
$status_text = null; |
| 75 |
// The return data |
| 76 |
$return_data = array(); |
| 77 |
// Check the nonce |
| 78 |
if (wp_verify_nonce($nonce, WP_FORCE_REFRESH_ACTION)){ |
| 79 |
// If the user is authenticated, get the current site version by making a hash of the current date |
| 80 |
$time = current_time('timestamp'); |
| 81 |
// Get the hash |
| 82 |
$page_version_hash = wp_hash($time); |
| 83 |
// Get the first eight characters (the chance of having a duplicate hash from the first 8 characters is low) |
| 84 |
$page_version = substr($page_version_hash, 0, 8); |
| 85 |
// Remove the old |
| 86 |
delete_post_meta( $page_id, 'force_refresh_current_page_version' ); |
| 87 |
// Add the new |
| 88 |
update_post_meta( $page_id, 'force_refresh_current_page_version', $page_version, null, 'no' ); |
| 89 |
// The call was successful |
| 90 |
$success = true; |
| 91 |
// Redeclare the status code as 200 |
| 92 |
$status_code = 200; |
| 93 |
// Redeclare the status text |
| 94 |
$status_text = 'You\'ve successfully requested all browsers to refresh this page (version <code>' . $page_version . '</code>).'; |
| 95 |
// Redeclare the status text |
| 96 |
$return_data['new_page_version'] = $page_version; |
| 97 |
$return_data['refresh_interval'] = (int) get_option(WP_FORCE_REFRESH_OPTION_REFRESH_INTERVAL_IN_SECONDS, WP_FORCE_REFRESH_OPTION_REFRESH_INTERVAL_IN_SECONDS_DEFAULT); |
| 98 |
|
| 99 |
} |
| 100 |
else { |
| 101 |
// Redeclare the status code as 401 (unauthorized) |
| 102 |
$status_code = 401; |
| 103 |
$status_text = 'There was an issue verifying your nonce ($nonce).'; |
| 104 |
} |
| 105 |
// Set the HTTP code |
| 106 |
status_header($status_code); |
| 107 |
// Return the success |
| 108 |
$return_array['success'] = $success; |
| 109 |
// Return the status code |
| 110 |
$return_array['status_code'] = $status_code; |
| 111 |
// Return the status text |
| 112 |
$return_array['status_text'] = $status_text; |
| 113 |
// Return the return data |
| 114 |
$return_array['return_data'] = $return_data; |
| 115 |
print json_encode($return_array); |
| 116 |
wp_die(); |
| 117 |
}); |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
?> |