| 1 |
<?php |
| 2 |
/** |
| 3 |
* @version 1.0 |
| 4 |
* @package Booking Manager |
| 5 |
* @subpackage Ajax Responder |
| 6 |
* @category Items |
| 7 |
* |
| 8 |
* @author wpdevelop |
| 9 |
* @link https://oplugins.com/ |
| 10 |
* @email info@oplugins.com |
| 11 |
* |
| 12 |
* @modified 2014.05.26 |
| 13 |
*/ |
| 14 |
|
| 15 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 16 |
|
| 17 |
//////////////////////////////////////////////////////////////////////////////// |
| 18 |
// S u p p o r t f u n c t i o n s f o r A j a x /////////////// |
| 19 |
//////////////////////////////////////////////////////////////////////////////// |
| 20 |
|
| 21 |
// Verify the nonce. |
| 22 |
function wpbm_check_nonce_in_admin_panel( $action_check = 'wpbm_ajax_admin_nonce' ){ |
| 23 |
|
| 24 |
$nonce = ( isset( $_REQUEST[ 'wpbm_nonce' ] ) ) ? $_REQUEST[ 'wpbm_nonce' ] : ''; |
| 25 |
|
| 26 |
if ( '' === $nonce ) return false; // Its was request from some other plugin |
| 27 |
|
| 28 |
if ( ! wp_verify_nonce( $nonce, $action_check ) ) { // This nonce is not valid. |
| 29 |
?> |
| 30 |
<script type="text/javascript"> |
| 31 |
jQuery("#ajax_respond").after( "<div class='wpdevelop'><div class='alert alert-warning alert-danger'><?php |
| 32 |
printf(__('%sError!%s Request do not pass security check! Please refresh the page and try one more time.' , 'booking-manager'),'<strong>','</strong>'); |
| 33 |
?></div></div>" ); |
| 34 |
if ( jQuery("#ajax_message").length ) |
| 35 |
jQuery("#ajax_message").slideUp(); |
| 36 |
</script> |
| 37 |
<?php |
| 38 |
die; |
| 39 |
} |
| 40 |
return true; //FixIn: 7.2.1.10 |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
// Check and (re)Load specific Locale for the Ajax request - based on "admin_init" hook |
| 45 |
function wpbm_check_locale_for_ajax() { |
| 46 |
|
| 47 |
add_wpbm_filter( 'wpbm_check_for_active_language', 'wpbm_check_for_active_language' ); // Add Hook for ability to check the content for active lanaguges |
| 48 |
|
| 49 |
if ( isset( $_POST[ 'wpbm_active_locale' ] ) ) { // Reload locale according request parameter |
| 50 |
global $l10n; |
| 51 |
if ( isset( $l10n[ 'booking-manager' ] ) ) |
| 52 |
unset( $l10n[ 'booking-manager' ] ); |
| 53 |
|
| 54 |
if ( ! defined( 'WPBM_RELOAD' ) ) |
| 55 |
define( 'WPBM_RELOAD', esc_js( $_POST[ 'wpbm_active_locale' ] ) ); |
| 56 |
|
| 57 |
// Reload locale settings, its required for the correct dates format |
| 58 |
if ( isset( $l10n[ 'default' ] ) ) |
| 59 |
unset( $l10n[ 'default' ] ); // Unload locale |
| 60 |
add_filter( 'locale', 'wpbm_get_locale', 999 ); // Set filter to load the locale of the Booking Manager |
| 61 |
load_default_textdomain(); // Load default locale |
| 62 |
global $wp_locale; |
| 63 |
$wp_locale = new WP_Locale(); // Reload class |
| 64 |
|
| 65 |
wpbm_load_locale( WPBM_RELOAD ); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
//////////////////////////////////////////////////////////////////////////////// |
| 71 |
// A j a x H o o k s f o r s p e c i f i c A c t i o n s ///// |
| 72 |
//////////////////////////////////////////////////////////////////////////////// |
| 73 |
|
| 74 |
|
| 75 |
function wpbm_ajax_WPBM_USER_SAVE_WINDOW_STATE() { |
| 76 |
if ( ! wpbm_check_nonce_in_admin_panel() ) { |
| 77 |
return false; |
| 78 |
} |
| 79 |
|
| 80 |
$requested_user_id = isset( $_POST['user_id'] ) && is_scalar( $_POST['user_id'] ) |
| 81 |
? absint( wp_unslash( (string) $_POST['user_id'] ) ) |
| 82 |
: 0; |
| 83 |
$authorized_user_id = wpbm_get_authorized_user_option_target_id( $requested_user_id ); |
| 84 |
if ( 0 === $authorized_user_id ) { |
| 85 |
status_header( 403 ); |
| 86 |
wp_send_json_error( array( 'message' => __( 'You can update only your own interface preferences.', 'booking-manager' ) ) ); |
| 87 |
} |
| 88 |
|
| 89 |
$window_name = isset( $_POST['window'] ) && is_scalar( $_POST['window'] ) |
| 90 |
? sanitize_text_field( wp_unslash( (string) $_POST['window'] ) ) |
| 91 |
: ''; |
| 92 |
if ( '' === $window_name ) { |
| 93 |
status_header( 400 ); |
| 94 |
wp_send_json_error( array( 'message' => __( 'The interface preference is not valid.', 'booking-manager' ) ) ); |
| 95 |
} |
| 96 |
|
| 97 |
$is_closed = isset( $_POST['is_closed'] ) && is_scalar( $_POST['is_closed'] ) |
| 98 |
? (int) wp_unslash( (string) $_POST['is_closed'] ) |
| 99 |
: 0; |
| 100 |
update_user_option( $authorized_user_id, 'wpbm_win_' . $window_name, $is_closed ); |
| 101 |
wp_send_json_success(); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
/** Save Custom User Data */ |
| 106 |
function wpbm_ajax_WPBM_USER_SAVE_CUSTOM_DATA() { |
| 107 |
if ( ! wpbm_check_nonce_in_admin_panel() ) { |
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
$requested_user_id = isset( $_POST['user_id'] ) && is_scalar( $_POST['user_id'] ) |
| 112 |
? absint( wp_unslash( (string) $_POST['user_id'] ) ) |
| 113 |
: 0; |
| 114 |
$authorized_user_id = wpbm_get_authorized_user_option_target_id( $requested_user_id ); |
| 115 |
if ( 0 === $authorized_user_id ) { |
| 116 |
status_header( 403 ); |
| 117 |
wp_send_json_error( array( 'message' => __( 'You can update only your own interface preferences.', 'booking-manager' ) ) ); |
| 118 |
} |
| 119 |
|
| 120 |
$data_name = isset( $_POST['data_name'] ) && is_scalar( $_POST['data_name'] ) |
| 121 |
? sanitize_text_field( wp_unslash( (string) $_POST['data_name'] ) ) |
| 122 |
: ''; |
| 123 |
$data_value = isset( $_POST['data_value'] ) && is_scalar( $_POST['data_value'] ) |
| 124 |
? wp_unslash( (string) $_POST['data_value'] ) |
| 125 |
: ''; |
| 126 |
if ( '' === $data_name ) { |
| 127 |
status_header( 400 ); |
| 128 |
wp_send_json_error( array( 'message' => __( 'The interface preference is not valid.', 'booking-manager' ) ) ); |
| 129 |
} |
| 130 |
$is_reload = ! empty( $_POST['is_reload'] ); |
| 131 |
|
| 132 |
/* Exmaple of $_POST: |
| 133 |
[data_name] => add_wpbm_calendar_options |
| 134 |
[data_value] => calendar_months_count=1&calendar_months_num_in_1_row=1&calendar_width=500px&calendar_cell_height |
| 135 |
*/ |
| 136 |
$post_param = explode( '&', $data_value ); // "&" was set by jQuery.param( data_params ) in client side. |
| 137 |
$data_to_save = array(); |
| 138 |
foreach ( $post_param as $param ) { |
| 139 |
$param_data = explode( '=', $param ); |
| 140 |
|
| 141 |
$data_to_save[ $param_data[0] ] = ( isset( $param_data[1] ) ) ? esc_attr( $param_data[1] ) : ''; |
| 142 |
} |
| 143 |
/* Exmaple: |
| 144 |
Array |
| 145 |
( |
| 146 |
[calendar_months_count] => 1 |
| 147 |
[calendar_months_num_in_1_row] => 1 |
| 148 |
[calendar_width] => 500px |
| 149 |
[calendar_cell_height] => |
| 150 |
) |
| 151 |
*/ |
| 152 |
|
| 153 |
// Save Custom User Data |
| 154 |
update_user_option( $authorized_user_id, 'wpbm_custom_' . $data_name, serialize( $data_to_save ) ); |
| 155 |
|
| 156 |
?> <script type="text/javascript"> |
| 157 |
var my_message = '<?php echo html_entity_decode( esc_js( __('Saved' , 'booking-manager') ),ENT_QUOTES) ; ?>'; |
| 158 |
wpbm_admin_show_message( my_message, 'success', 1000 ); |
| 159 |
<?php if ( $is_reload ) { ?> |
| 160 |
setTimeout(function ( ) {location.reload(true);} ,1500); |
| 161 |
<?php } ?> |
| 162 |
</script> <?php |
| 163 |
die(); |
| 164 |
|
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
//////////////////////////////////////////////////////////////////////////////// |
| 169 |
// R u n A j a x ////////////////////////////////// |
| 170 |
//////////////////////////////////////////////////////////////////////////////// |
| 171 |
if ( is_admin() && ( defined( 'DOING_AJAX' ) ) && ( DOING_AJAX ) ) { |
| 172 |
|
| 173 |
// Reload Locale if its required |
| 174 |
add_action( 'admin_init', 'wpbm_check_locale_for_ajax' ); |
| 175 |
|
| 176 |
// Hooks list |
| 177 |
$actions_list = array( |
| 178 |
'WPBM_USER_SAVE_WINDOW_STATE' => 'admin' |
| 179 |
,'WPBM_USER_SAVE_CUSTOM_DATA' => 'admin' |
| 180 |
|
| 181 |
, 'WPBM_LISTING_ICS_URL' => 'admin' |
| 182 |
); |
| 183 |
|
| 184 |
foreach ($actions_list as $action_name => $action_where) { |
| 185 |
|
| 186 |
if ( ( isset($_POST['action']) ) && ( $_POST['action'] == $action_name ) ){ |
| 187 |
|
| 188 |
if ( ( $action_where == 'admin' ) || ( $action_where == 'both' ) ) |
| 189 |
add_action( 'wp_ajax_' . $action_name, 'wpbm_ajax_' . $action_name); // Admin & Client (logged in usres) |
| 190 |
|
| 191 |
if ( ( $action_where == 'both' ) || ( $action_where == 'client' ) ) |
| 192 |
add_action( 'wp_ajax_nopriv_' . $action_name, 'wpbm_ajax_' . $action_name); // Client (not logged in) |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
|