| 1 |
<?php |
| 2 |
/** |
| 3 |
* Our API calls responsible for handling requests from admins to save new options. |
| 4 |
* |
| 5 |
* @package ForceRefresh |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace JordanLeven\Plugins\ForceRefresh; |
| 9 |
|
| 10 |
define( |
| 11 |
'MESSAGE_SUCCESS_SAVE_OPTIONS', |
| 12 |
"You've successfully saved your options" |
| 13 |
); |
| 14 |
|
| 15 |
/** |
| 16 |
* Function used to save options for showing the refresh button in the admin bar. |
| 17 |
* |
| 18 |
* @return void |
| 19 |
*/ |
| 20 |
function save_option_show_in_admin_bar() { |
| 21 |
// This function returns the post id after the nonce has already been validated. |
| 22 |
// phpcs:disable WordPress.Security.NonceVerification |
| 23 |
$refresh_option_show_in_admin_bar = isset( $_REQUEST['show_refresh_in_admin_bar'] ) |
| 24 |
? sanitize_text_field( wp_unslash( $_REQUEST['show_refresh_in_admin_bar'] ) ) |
| 25 |
: null; |
| 26 |
// phpcs:enable WordPress.Security.NonceVerification |
| 27 |
if ( null !== $refresh_option_show_in_admin_bar ) { |
| 28 |
update_option( |
| 29 |
WP_FORCE_REFRESH_OPTION_SHOW_IN_WP_ADMIN_BAR, |
| 30 |
$refresh_option_show_in_admin_bar |
| 31 |
); |
| 32 |
} |
| 33 |
}; |
| 34 |
|
| 35 |
/** |
| 36 |
* Function used to save options for the refresh interval. |
| 37 |
* |
| 38 |
* @return void |
| 39 |
*/ |
| 40 |
function save_option_refresh_interval() { |
| 41 |
// This function returns the post id after the nonce has already been validated. |
| 42 |
// phpcs:disable WordPress.Security.NonceVerification |
| 43 |
$refresh_option_refresh_interval = isset( $_REQUEST['refresh_interval'] ) |
| 44 |
? sanitize_text_field( wp_unslash( $_REQUEST['refresh_interval'] ) ) |
| 45 |
: null; |
| 46 |
// phpcs:enable WordPress.Security.NonceVerification |
| 47 |
|
| 48 |
if ( $refresh_option_refresh_interval ) { |
| 49 |
update_option( |
| 50 |
WP_FORCE_REFRESH_OPTION_REFRESH_INTERVAL_IN_SECONDS, |
| 51 |
$refresh_option_refresh_interval |
| 52 |
); |
| 53 |
} |
| 54 |
}; |
| 55 |
|
| 56 |
/** |
| 57 |
* Function used by administrators to request a refresh of the site. |
| 58 |
* |
| 59 |
* @return void |
| 60 |
*/ |
| 61 |
function request_save_admin_options() { |
| 62 |
if ( ! is_user_authorized_to_request_refresh() ) { |
| 63 |
return_api_response( |
| 64 |
401, |
| 65 |
MESSAGE_ERROR_UNAUTHORIZED |
| 66 |
); |
| 67 |
} else { |
| 68 |
save_option_show_in_admin_bar(); |
| 69 |
save_option_refresh_interval(); |
| 70 |
|
| 71 |
return_api_response( |
| 72 |
200, |
| 73 |
MESSAGE_SUCCESS_SAVE_OPTIONS |
| 74 |
); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
// Register the action used by administrators to refresh the site. |
| 79 |
add_action( |
| 80 |
'wp_ajax_force_refresh_update_site_options', |
| 81 |
__NAMESPACE__ . '\\request_save_admin_options' |
| 82 |
); |
| 83 |
|