PluginProbe
Booking Calendar / 11.7
Booking Calendar v11.7
11.8.1 11.8 11.7 11.6.1 11.6 11.5 11.4.3 11.4.2 11.4.1 11.4 11.3 11.2.1 11.2 11.1 11.0 10.15.7 10.15.6 10.1.3 10.10 10.10.1 10.10.2 10.11 10.11.2 10.11.3 10.11.4 All 201 releases
booking / includes / page-setup / setup_ajax.php

setup_ajax.php in Booking Calendar 11.7, at includes/page-setup/setup_ajax.php

455 lines 23.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php /**
2 * @version 1.0
3 * @description Ajax and Requests Structure for WPBC_AJX__Setup__Ajax_Request
4 * @category Setup Class
5 * @author wpdevelop
6 *
7 * @web-site http://oplugins.com/
8 * @email info@oplugins.com
9 *
10 * @modified 2023-06-23
11 */
12
13 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
14
15 // =====================================================================================================================
16 // == Get RULES STRUCTURE ==
17 // =====================================================================================================================
18
19 /**
20 * Get params names for escaping and/or default value of such params
21 *
22 * @return array array ( 'resource_id' => array( 'validate' => 'digit_or_csd', 'default' => array( '1' ) )
23 * , ... )
24 */
25 function wpbc_setup_wizard_page__request_rules_structure() {
26
27 return array(
28 'do_action' => array(
29 'validate' => array(
30 'none',
31 'save_and_continue',
32 'make_reset', 'skip_wizard',
33
34 'save_and_continue__welcome',
35 'save_and_continue__general_info',
36 'save_and_continue__date_time_formats',
37 'save_and_continue__bookings_types',
38 'redirect_to_setup_step'
39 ),
40 'default' => 'none'
41 ),
42 'current_step' => array( 'validate' => 's', 'default' => '' ),
43 'resource_id' => array( 'validate' => 'd', 'default' => wpbc_get_default_resource() ),
44 'ui_clicked_element_id' => array( 'validate' => 's', 'default' => '' )
45 // 'calendar__booking_start_day_weeek' => array( 'validate' => array( '0', '1', '2', '3', '4', '5', '6' ), 'default' => get_bk_option( 'booking_start_day_weeek' ) )
46 );
47 }
48
49
50 /**
51 * Get default params
52 *
53 * @return array array ( 'ui_wh_modification_date_radio' => 0
54 * , ... )
55 */
56 function wpbc_setup_wizard_page__get__request_values__default(){
57
58 $request_rules_structure = wpbc_setup_wizard_page__request_rules_structure();
59
60 $default_params_arr = array();
61
62 $structure_type = 'default';
63
64 foreach ( $request_rules_structure as $key => $value ) {
65 $default_params_arr[ $key ] = $value[ $structure_type ];
66 }
67
68 return $default_params_arr;
69 }
70
71
72 // =====================================================================================================================
73 // == Get sanitised Request parameters for Ajax ==
74 // =====================================================================================================================
75
76 /**
77 * Get sanitised request parameters. | 01. -> Firstly check if user saved request params in user_meta DB.
78 * | 02. -> Otherwise check $_REQUEST.
79 * | 03. -> Otherwise Get default.
80 *
81 * @return array|false
82 */
83 function wpbc_setup_wizard_page__get_cleaned_params__saved_request_default(){
84
85 // User Specific Experience with Setup -> saved to user meta_table.
86 // E.g. next time user open the page with saved own settings
87 $user_request = new WPBC_AJX__REQUEST( array(
88 'db_option_name' => 'booking_setup_wizard_page_request_params',
89 'user_id' => wpbc_get_current_user_id(),
90 'request_rules_structure' => wpbc_setup_wizard_page__request_rules_structure()
91 )
92 );
93
94 // -----------------------------------------------------------------------------------------------------------------
95 // Get saved from DB
96 // -----------------------------------------------------------------------------------------------------------------
97 $escaped_request_params_arr = $user_request->get_sanitized__saved__user_request_params();
98
99
100 // -----------------------------------------------------------------------------------------------------------------
101 // Get $_REQUEST or Default :: This request was not saved before, then get sanitized direct parameters , such as: $_REQUEST['resource_id']
102 // -----------------------------------------------------------------------------------------------------------------
103 if ( false === $escaped_request_params_arr ) {
104 $request_prefix = false;
105 $escaped_request_params_arr = $user_request->get_sanitized__in_request__value_or_default( $request_prefix );
106 }
107
108 // -----------------------------------------------------------------------------------------------------------------
109 // == O V E R R I D E - DB params by the params from REQUEST! ==
110 // -----------------------------------------------------------------------------------------------------------------
111 $request_key = 'current_step';
112 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
113 if ( isset( $_REQUEST[ $request_key ] ) ) {
114
115 // Get SANITIZED REQUEST parameters together with default values
116 $request_prefix = false;
117 $url_request_params_arr = $user_request->get_sanitized__in_request__value_or_default( $request_prefix ); // Direct: $_REQUEST['resource_id']
118
119 // Now get only SANITIZED values that exist in REQUEST
120 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
121 $url_request_params_only_arr = array_intersect_key( $url_request_params_arr, $_REQUEST );
122
123 // And now override our DB $escaped_request_params_arr by SANITIZED $_REQUEST values
124 $escaped_request_params_arr = wp_parse_args( $url_request_params_only_arr, $escaped_request_params_arr );
125 }
126 // ---------------------------------------------------------------------------------------------------------
127
128 // //MU
129 // if ( class_exists( 'wpdev_bk_multiuser' ) ) {
130 //
131 // // Check if this MU user activated or super-admin, otherwise show warning
132 // if ( ! wpbc_is_mu_user_can_be_here('activated_user') )
133 // return false;
134 //
135 // // Check if this MU user owner of this resource or super-admin, otherwise show warning
136 // if ( ! wpbc_is_mu_user_can_be_here( 'resource_owner', $escaped_request_params_arr['resource_id'] ) ) {
137 // $default_values = $user_request->get_request_rules__default();
138 // $escaped_request_params_arr['resource_id'] = $default_values['resource_id'];
139 // }
140 // }
141
142 return $escaped_request_params_arr;
143 }
144
145
146 // =====================================================================================================================
147 // == A J A X ==
148 // =====================================================================================================================
149
150 class WPBC_AJX__Setup_Wizard__Ajax_Request {
151
152
153 /**
154 * Define HOOKs for start loading Ajax
155 */
156 public function define_ajax_hook(){
157
158 // Ajax Handlers. Note. "locale_for_ajax" rechecked in wpbc-ajax.php
159 add_action( 'wp_ajax_' . 'WPBC_AJX_SETUP_WIZARD_PAGE', array( $this, 'ajax_' . 'WPBC_AJX_SETUP_WIZARD_PAGE' ) ); // Admin & Client (logged in usres)
160 add_action( 'wp_ajax_' . 'WPBC_AJX_SETUP_WIZARD_MARK_STEP_SAVED', array( $this, 'ajax_' . 'WPBC_AJX_SETUP_WIZARD_MARK_STEP_SAVED' ) );
161
162 // Ajax Handlers for actions
163 // add_action( 'wp_ajax_nopriv_' . 'WPBC_AJX_BOOKING_LISTING', array( $this, 'ajax_' . 'WPBC_AJX_BOOKING_LISTING' ) ); // Client (not logged in)
164 }
165
166 /**
167 * Ajax - mark an external setup step as saved after an existing settings page saves successfully.
168 *
169 * @return void
170 */
171 public function ajax_WPBC_AJX_SETUP_WIZARD_MARK_STEP_SAVED() {
172
173 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing
174 if ( ! wp_verify_nonce( $nonce, 'wpbc_setup_wizard_mark_step_saved' ) ) {
175 wp_send_json_error( array( 'message' => __( 'Security check failed.', 'booking' ) ) );
176 }
177
178 $step_name = isset( $_POST['wpbc_setup_step'] ) ? sanitize_key( wp_unslash( $_POST['wpbc_setup_step'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing
179 $setup_steps = new WPBC_SETUP_WIZARD_STEPS();
180 $steps_arr = $setup_steps->get_steps_arr();
181
182 if ( empty( $step_name ) && function_exists( 'wpbc_setup_wizard__detect_step_from_admin_url' ) ) {
183 $referer_step = wpbc_setup_wizard__detect_step_from_admin_url( wp_get_referer() );
184 if ( ! empty( $referer_step ) && isset( $steps_arr[ $referer_step ] ) ) {
185 $step_name = $referer_step;
186 }
187 }
188
189 if ( empty( $step_name ) || ! isset( $steps_arr[ $step_name ] ) ) {
190 wp_send_json_error( array( 'message' => __( 'Unknown setup step.', 'booking' ) ) );
191 }
192
193 $setup_steps->db__set_step_as_saved( $step_name, true );
194 $setup_steps->db__save_current_step_name( $step_name );
195
196 wp_send_json_success( array( 'step' => $step_name ) );
197 }
198
199
200 /**
201 * Ajax - Get Listing Data and Response to JS script
202 */
203 public function ajax_WPBC_AJX_SETUP_WIZARD_PAGE() {
204
205 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
206 if ( ! isset( $_POST['all_ajx_params'] ) || empty( $_POST['all_ajx_params'] ) ) { exit; }
207
208 // -------------------------------------------------------------------------------------------------------------
209 // == Security == -> in Ajax Post: 'nonce': wpbc_ajx_booking_listing.get_secure_param( 'nonce' )
210 // -------------------------------------------------------------------------------------------------------------
211 $action_name = 'wpbc_setup_wizard_page_ajx' . '_wpbcnonce';
212 $nonce_post_key = 'nonce';
213 $result_check = check_ajax_referer( $action_name, $nonce_post_key );
214
215 $user_id = ( isset( $_REQUEST['wpbc_ajx_user_id'] ) ) ? intval( $_REQUEST['wpbc_ajx_user_id'] ) : wpbc_get_current_user_id(); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
216
217 // -------------------------------------------------------------------------------------------------------------
218 // == Request == -> $_REQUEST['all_ajx_params']['page_num'], $_REQUEST['all_ajx_params']['page_items_count'], ...
219 // -------------------------------------------------------------------------------------------------------------
220 $user_request = new WPBC_AJX__REQUEST( array(
221 'db_option_name' => 'booking_setup_wizard_page_request_params',
222 'user_id' => $user_id,
223 'request_rules_structure' => wpbc_setup_wizard_page__request_rules_structure()
224 )
225 );
226 //--------------------------------------------------------------------------------------------------------------
227 // If in Ajax: all_ajx_params: _wpbc_settings.get_all_..() -> Use prefix "all_ajx_params" THEN Sanitize required REQUEST params
228 //--------------------------------------------------------------------------------------------------------------
229 $request_prefix = 'all_ajx_params';
230 $cleaned_request_params = $user_request->get_sanitized__in_request__value_or_default( $request_prefix ); // NOT Direct: $_REQUEST['all_ajx_params']['resource_id']
231 //--------------------------------------------------------------------------------------------------------------
232
233 $cleaned_data = array();
234 $setup_steps = new WPBC_SETUP_WIZARD_STEPS();
235
236 $data_arr = array();
237 $data_arr['ajx_after_action_message'] = '';
238 $data_arr['ajx_after_action_result'] = 1; // Message Type: ? '1' => 'success' : 'error'
239 //--------------------------------------------------------------------------------------------------------------
240 // Steps
241 //--------------------------------------------------------------------------------------------------------------
242 $data_arr['current_step'] = ( ! empty( $cleaned_request_params['current_step'] )
243 ? $cleaned_request_params['current_step']
244 : $setup_steps->get_active_step_name() ); // e.g. 'general_info' or 'date_availability'
245 $data_arr['steps'] = $setup_steps->get_steps_arr();
246 if ( ! isset( $data_arr['steps'][ $data_arr['current_step'] ] ) ) {
247 $data_arr['current_step'] = $setup_steps->get_active_step_name();
248 }
249
250 // -------------------------------------------------------------------------------------------------------------
251 // Get Wizard history
252 // -------------------------------------------------------------------------------------------------------------
253 $booking_wizard_data_arr = get_bk_option( 'booking_wizard_data' );
254 $booking_wizard_data_arr = ( empty( $booking_wizard_data_arr ) ) ? array() : $booking_wizard_data_arr;
255
256
257 // =============================================================================================================
258 // == Do Action ==
259 // =============================================================================================================
260 switch ( $cleaned_request_params['do_action'] ) {
261
262 // ---------------------------------------------------------------------------------------------------------
263 // == RESET ==
264 // ---------------------------------------------------------------------------------------------------------
265 case 'make_reset':
266
267 $is_reseted = $user_request->user_request_params__db_delete(); // Delete from DB
268
269 wpbc_setup_wizard__set_full_screen_mode_for_current_user( true );
270
271 $cleaned_request_params['do_action'] = $is_reseted ? 'reset_done' : 'reset_error';
272
273 $cleaned_request_params = wpbc_setup_wizard_page__get__request_values__default();
274
275 $data_arr['ajx_after_action_message'] = __( 'Start Setup from Beginning', 'booking' );
276
277 $data_arr['current_step'] = 'welcome';
278
279 update_bk_option( 'booking_wizard_data', array() );
280
281 $setup_steps->db__set_all_steps_as( false ); // Clear All Steps Mark as Undone
282 break;
283
284 case 'skip_wizard':
285
286 $data_arr['current_step'] = 'welcome';
287 // $data_arr['redirect_url'] = wpbc_get_settings_url();
288 $data_arr['redirect_url'] = wpbc_get_bookings_url();
289
290 wpbc_setup_wizard__set_full_screen_mode_for_current_user( false );
291 $setup_steps->db__set_all_steps_as( true ); // Mark All Steps as Done
292
293 break;
294
295 case 'save_and_continue__welcome':
296
297 $setup_steps->db__set_step_as_completed( 'welcome' );
298 break;
299
300 case 'save_and_continue__general_info':
301
302 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
303 if ( isset( $_POST['all_ajx_params']['step_data'] ) && ( ! empty( $_POST['all_ajx_params']['step_data'] ) ) ) {
304 $cleaned_data = wpbc_template__general_info__action_validate_data( $_POST['all_ajx_params']['step_data'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotValidated, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
305
306 if ( 'On' === $cleaned_data['wpbc_swp_accept_send'] ) {
307 //wpbc_setup_feedback__send_email( $cleaned_data ); // FixIn: 10.7.1.3.
308 update_bk_option( 'booking_feedback__send_email', $cleaned_data );
309 } else {
310 delete_bk_option( 'booking_feedback__send_email' );
311 }
312
313 wpbc_setup__update__general_info( $cleaned_data );
314 }
315
316 $setup_steps->db__set_step_as_completed( 'general_info' );
317 break;
318
319 case 'save_and_continue__date_time_formats':
320
321 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
322 if ( isset( $_POST['all_ajx_params']['step_data'] ) && ( ! empty( $_POST['all_ajx_params']['step_data'] ) ) ) {
323 $cleaned_data = wpbc_template__date_time_formats__action_validate_data( $_POST['all_ajx_params']['step_data'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotValidated, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
324 wpbc_setup__update__date_time_formats( $cleaned_data );
325 }
326
327 $setup_steps->db__set_step_as_completed( 'date_time_formats' );
328 break;
329
330 case 'save_and_continue__bookings_types':
331
332 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
333 if ( isset( $_POST['all_ajx_params']['step_data'] ) && ( ! empty( $_POST['all_ajx_params']['step_data'] ) ) ) {
334 $cleaned_data = wpbc_template__bookings_types__action_validate_data( $_POST['all_ajx_params']['step_data'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotValidated, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
335
336 // Legacy update booking form and dates selection, as some other options (recurrent time)....
337 wpbc_setup__update__bookings_types( $cleaned_data );
338 $booking_wizard_data_arr['save_and_continue__bookings_types'] = $cleaned_data;
339 update_bk_option( 'booking_wizard_data', $booking_wizard_data_arr );
340
341 /**
342 * Fires after the validated Booking Type configuration is applied and saved.
343 *
344 * Optional modules can synchronize presentation state from the selected
345 * workflow without entering the Setup Wizard validation or mutation engine.
346 *
347 * @param array $cleaned_data Validated Setup Wizard booking-type data.
348 */
349 do_action( 'wpbc_setup_wizard_booking_type_saved', $cleaned_data );
350 $setup_steps = new WPBC_SETUP_WIZARD_STEPS();
351
352 // FixIn: 10.7.1.3. // FixIn: 10.15.1.1.
353 $cleaned_data_booking_feedback_arr = get_bk_option( 'booking_feedback__send_email' );
354 if (! empty($cleaned_data_booking_feedback_arr)){
355 if ( 'On' === $cleaned_data_booking_feedback_arr['wpbc_swp_accept_send'] ) {
356 $cleaned_data_booking_feedback_arr = array_merge( $cleaned_data_booking_feedback_arr , array( 'type' => 'Type: ' . $cleaned_data ['wpbc_swp_booking_types'] ) );
357 if ( 'time_slots_appointments' === $cleaned_data ['wpbc_swp_booking_types'] ) {
358 $cleaned_data_booking_feedback_arr = array_merge( $cleaned_data_booking_feedback_arr, array( 'appointments_type' => 'Appointment: ' . $cleaned_data ['wpbc_swp_booking_appointments_type'] ) );
359 }
360 if ( ! WPBC_IS_PLAYGROUND ) {
361 wpbc_setup_feedback__send_email( $cleaned_data_booking_feedback_arr );
362 }
363 update_bk_option( 'booking_feedback__after_send', $cleaned_data_booking_feedback_arr );
364 delete_bk_option( 'booking_feedback__send_email' );
365 }
366 }
367
368 }
369
370 $setup_steps->db__set_step_as_completed( 'bookings_types' );
371 if ( ! empty( $cleaned_data ) ) {
372 wpbc_setup_wizard__set_full_screen_mode_for_current_user( true );
373 $data_arr['current_step'] = wpbc_setup_wizard__get_first_profile_step();
374 $current_steps_arr = $setup_steps->get_steps_arr();
375 $setup_steps->db__reset_steps_from( $data_arr['current_step'] );
376 if (
377 isset( $current_steps_arr[ $data_arr['current_step'] ] )
378 && 'manual_save_required' === $setup_steps->get_step_save_behavior( $data_arr['current_step'] )
379 ) {
380 $setup_steps->db__set_step_as_saved( $data_arr['current_step'], false );
381 }
382 $data_arr['redirect_url'] = $setup_steps->get_step_target_url( $data_arr['current_step'] );
383 $data_arr['steps'] = $current_steps_arr;
384 $cleaned_request_params['do_action'] = 'redirect_to_setup_step';
385 }
386 break;
387
388 default:
389 // Default
390 }
391
392 //--------------------------------------------------------------------------------------------------------------
393 // Other
394 //--------------------------------------------------------------------------------------------------------------
395 $data_arr['steps_is_done'] = $setup_steps->db__get_steps_is_done();
396 $data_arr['left_navigation'] = wpbc_setup_wizard_page__get_left_navigation_menu_arr();
397 $data_arr['plugin_menu__setup_progress'] = $setup_steps->get_plugin_menu_title__setup_progress( $data_arr['current_step'] );
398
399 // External setup steps are now configured on their existing admin pages, so the setup-page AJAX response does
400 // not need to embed their duplicated calendars or forms.
401 $data_arr['ui'] = array();
402 $data_arr['calendar_force_load'] = '';
403
404
405 // -------------------------------------------------------------------------------------------------------------
406 // Save Wizard history
407 // -------------------------------------------------------------------------------------------------------------
408 // Save to DB
409 $booking_wizard_data_arr[ $cleaned_request_params['do_action'] ] = $cleaned_data;
410 update_bk_option( 'booking_wizard_data', $booking_wizard_data_arr );
411 // Ajax Transfer
412 $data_arr['booking_wizard_data'] = $booking_wizard_data_arr;
413 // -------------------------------------------------------------------------------------------------------------
414
415 // -------------------------------------------------------------------------------------------------------------
416 // Save Status of Wizard for specific user
417 // -------------------------------------------------------------------------------------------------------------
418 if ( 'make_reset' !== $cleaned_request_params['do_action'] ) {
419
420 $cleaned_request_params['current_step'] = $data_arr['current_step'];
421 $request_params_to_save = $cleaned_request_params;
422
423 // Do not safe such elements
424 unset( $request_params_to_save['ui_clicked_element_id'] );
425 unset( $request_params_to_save['do_action'] );
426 unset( $request_params_to_save['calendar_force_load'] );
427 unset( $request_params_to_save['plugin_menu__setup_progress'] );
428
429 $is_success_update = $user_request->user_request_params__db_save( $request_params_to_save ); // Save to DB // - $cleaned_request_params - serialized here automatically
430 }
431
432 if ( ! empty( $data_arr['calendar_force_load'] ) ) {
433 $data_arr['calendar_force_load'] = wpbc_clean_calendar_loading_scripts( $data_arr['calendar_force_load'] );
434 }
435 // -------------------------------------------------------------------------------------------------------------
436 // Send JSON. It will make "wp_json_encode" - so pass only array, and This function call wp_die( '', '', array( 'response' => null, ) ) Pass JS OBJ: response_data in "jQuery.post( " function on success.
437 // -------------------------------------------------------------------------------------------------------------
438 wp_send_json( array(
439 'ajx_data' => $data_arr,
440 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
441 'ajx_all_ajx_params' => $_REQUEST[ $request_prefix ], // $_REQUEST[ 'all_ajx_params' ]
442 'ajx_cleaned_params' => $cleaned_request_params
443 ) );
444 }
445
446 }
447
448 /**
449 * Just for loading CSS and JavaScript files
450 */
451 if ( true ) {
452 $wpbc_setup_wizard_page_loading = new WPBC_AJX__Setup_Wizard__Ajax_Request;
453 $wpbc_setup_wizard_page_loading->define_ajax_hook();
454 }
455