activities_controller.php
1 month ago
auth_controller.php
3 months ago
booking_form_settings_controller.php
3 months ago
bookings_controller.php
12 hours ago
calendars_controller.php
3 months ago
carts_controller.php
12 hours ago
controller.php
3 months ago
customer_cabinet_controller.php
2 months ago
customers_controller.php
12 hours ago
dashboard_controller.php
2 months ago
default_agent_controller.php
3 months ago
events_controller.php
3 months ago
form_fields_controller.php
1 week ago
integrations_controller.php
3 months ago
invoices_controller.php
12 hours ago
manage_booking_by_key_controller.php
3 months ago
manage_order_by_key_controller.php
3 months ago
notifications_controller.php
3 months ago
orders_controller.php
12 hours ago
pro_controller.php
2 weeks ago
process_jobs_controller.php
3 months ago
processes_controller.php
1 month ago
razorpay_connect_controller.php
1 week ago
search_controller.php
3 months ago
services_controller.php
3 months ago
settings_controller.php
2 months ago
steps_controller.php
2 weeks ago
stripe_connect_controller.php
1 week ago
support_topics_controller.php
3 months ago
todos_controller.php
3 months ago
transactions_controller.php
12 hours ago
wizard_controller.php
1 week ago
steps_controller.php
394 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; // Exit if accessed directly. |
| 4 | } |
| 5 | |
| 6 | |
| 7 | if ( ! class_exists( 'OsStepsController' ) ) : |
| 8 | |
| 9 | |
| 10 | class OsStepsController extends OsController { |
| 11 | |
| 12 | private $booking; |
| 13 | |
| 14 | function __construct() { |
| 15 | parent::__construct(); |
| 16 | $this->action_access['customer'] = array_merge( $this->action_access['customer'], [] ); |
| 17 | $this->action_access['public'] = array_merge( |
| 18 | $this->action_access['public'], |
| 19 | [ |
| 20 | 'start', |
| 21 | 'start_instant', |
| 22 | 'load_step', |
| 23 | 'reload_booking_form_summary_panel', |
| 24 | 'check_order_intent_bookable', |
| 25 | 'load_datepicker_month', |
| 26 | 'start_from_order_intent', |
| 27 | ] |
| 28 | ); |
| 29 | |
| 30 | $this->views_folder = LATEPOINT_VIEWS_ABSPATH . 'steps/'; |
| 31 | $this->vars['page_header'] = __( 'Appointments', 'latepoint' ); |
| 32 | $this->vars['breadcrumbs'][] = array( |
| 33 | 'label' => __( 'Appointments', 'latepoint' ), |
| 34 | 'link' => OsRouterHelper::build_link( |
| 35 | [ |
| 36 | 'bookings', |
| 37 | 'pending_approval', |
| 38 | ] |
| 39 | ), |
| 40 | ); |
| 41 | } |
| 42 | |
| 43 | |
| 44 | public function start_instant() { |
| 45 | $atts = []; |
| 46 | if ( ! empty( $this->params['selected_agent'] ) ) { |
| 47 | $atts['selected_agent'] = sanitize_text_field( $this->params['selected_agent'] ); |
| 48 | } |
| 49 | if ( ! empty( $this->params['selected_service'] ) ) { |
| 50 | $atts['selected_service'] = sanitize_text_field( $this->params['selected_service'] ); |
| 51 | } |
| 52 | if ( ! empty( $this->params['selected_location'] ) ) { |
| 53 | $atts['selected_location'] = sanitize_text_field( $this->params['selected_location'] ); |
| 54 | } |
| 55 | |
| 56 | if ( ! empty( $this->params['hide_side_panel'] ) && $this->params['hide_side_panel'] == 'yes' ) { |
| 57 | $atts['hide_side_panel'] = 'yes'; |
| 58 | } |
| 59 | if ( ! empty( $this->params['hide_summary'] ) && $this->params['hide_summary'] == 'yes' ) { |
| 60 | $atts['hide_summary'] = 'yes'; |
| 61 | } |
| 62 | if ( ! empty( $this->params['background_pattern'] ) ) { |
| 63 | $this->vars['background_pattern'] = sanitize_text_field( $this->params['background_pattern'] ); |
| 64 | } |
| 65 | |
| 66 | $this->vars['atts'] = $atts; |
| 67 | $this->set_layout( 'clean' ); |
| 68 | $this->format_render( __FUNCTION__ ); |
| 69 | } |
| 70 | |
| 71 | public function load_datepicker_month() { |
| 72 | OsStepsHelper::set_required_objects( $this->params ); |
| 73 | |
| 74 | $target_date = new OsWpDateTime( $this->params['target_date_string'] ); |
| 75 | $calendar_settings = [ |
| 76 | 'layout' => $this->params['calendar_layout'] ?? 'classic', |
| 77 | 'timezone_name' => $this->params['timezone_name'] ?? false, |
| 78 | ]; |
| 79 | |
| 80 | $calendar_settings['earliest_possible_booking'] = OsSettingsHelper::get_earliest_possible_booking_restriction( OsStepsHelper::$booking_object->service_id ?? false ); |
| 81 | $calendar_settings['latest_possible_booking'] = OsSettingsHelper::get_latest_possible_booking_restriction( OsStepsHelper::$booking_object->service_id ?? false ); |
| 82 | $calendar_settings['consider_cart_items'] = true; |
| 83 | |
| 84 | $this->format_render( |
| 85 | 'partials/_monthly_calendar_days', |
| 86 | [ |
| 87 | 'target_date' => $target_date, |
| 88 | 'calendar_settings' => $calendar_settings, |
| 89 | 'booking_request' => \LatePoint\Misc\BookingRequest::create_from_booking_model( OsStepsHelper::$booking_object ), |
| 90 | ] |
| 91 | ); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | public function check_order_intent_bookable() { |
| 96 | OsStepsHelper::set_required_objects( $this->params ); |
| 97 | if ( OsStepsHelper::$cart_object->order_id ) { |
| 98 | // already converted, so we are good |
| 99 | $this->send_json( |
| 100 | [ |
| 101 | 'status' => LATEPOINT_STATUS_SUCCESS, |
| 102 | 'message' => __( 'Cart has already been converted to order', 'latepoint' ), |
| 103 | ] |
| 104 | ); |
| 105 | } |
| 106 | $order_intent = OsOrderIntentHelper::create_or_update_order_intent( OsStepsHelper::$cart_object, OsStepsHelper::$restrictions, OsStepsHelper::$presets, '', OsStepsHelper::get_customer_object_id() ); |
| 107 | if ( $order_intent->is_bookable() ) { |
| 108 | $this->send_json( |
| 109 | [ |
| 110 | 'status' => LATEPOINT_STATUS_SUCCESS, |
| 111 | 'message' => __( 'Order intent can be converted to order', 'latepoint' ), |
| 112 | ] |
| 113 | ); |
| 114 | } else { |
| 115 | $this->send_json( |
| 116 | [ |
| 117 | 'status' => LATEPOINT_STATUS_ERROR, |
| 118 | 'message' => __( 'Selected booking slot is not available anymore. Please pick a different time slot.', 'latepoint' ), |
| 119 | ] |
| 120 | ); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | function generate_timeslots_for_day() { |
| 125 | OsStepsHelper::set_required_objects( $this->params ); |
| 126 | |
| 127 | |
| 128 | |
| 129 | wp_send_json( |
| 130 | [ |
| 131 | 'status' => LATEPOINT_STATUS_SUCCESS, |
| 132 | 'message' => '', |
| 133 | ] |
| 134 | ); |
| 135 | } |
| 136 | |
| 137 | |
| 138 | function reload_booking_form_summary_panel() { |
| 139 | OsStepsHelper::set_required_objects( $this->params ); |
| 140 | $this->vars['cart'] = OsStepsHelper::$cart_object; |
| 141 | |
| 142 | if ( OsStepsHelper::is_ready_for_summary() ) { |
| 143 | $this->vars['current_step_code'] = $this->params['current_step_code'] ?? ''; |
| 144 | $this->vars['booking'] = OsStepsHelper::$booking_object; |
| 145 | $this->vars['customer'] = OsStepsHelper::get_customer_object(); |
| 146 | $this->vars['active_cart_item'] = OsStepsHelper::$active_cart_item; |
| 147 | |
| 148 | if ( $this->get_return_format() == 'json' ) { |
| 149 | $response_html = $this->render( $this->views_folder . 'partials/_booking_form_summary_panel', 'none' ); |
| 150 | wp_send_json( |
| 151 | [ |
| 152 | 'status' => LATEPOINT_STATUS_SUCCESS, |
| 153 | 'message' => $response_html, |
| 154 | ] |
| 155 | ); |
| 156 | exit(); |
| 157 | } else { |
| 158 | echo $this->render( $this->views_folder . 'partials/_booking_form_summary_panel', $this->get_layout() ); |
| 159 | } |
| 160 | } else { |
| 161 | wp_send_json( |
| 162 | [ |
| 163 | 'status' => LATEPOINT_STATUS_SUCCESS, |
| 164 | 'message' => '', |
| 165 | ] |
| 166 | ); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | |
| 171 | public function start_from_order_intent() { |
| 172 | $order_intent = OsOrderIntentHelper::get_order_intent_by_intent_key( $this->params['order_intent_key'] ); |
| 173 | |
| 174 | if ( ! $order_intent->is_new_record() ) { |
| 175 | $step_codes_to_preload = []; |
| 176 | $steps = OsStepsHelper::get_steps(); |
| 177 | OsStepsHelper::set_required_objects( $this->params ); |
| 178 | |
| 179 | if ( $order_intent->order_id ) { |
| 180 | // if order is created - load it |
| 181 | OsStepsHelper::load_order_object( $order_intent->order_id ); |
| 182 | $current_step_code = 'confirmation'; |
| 183 | $current_step = $steps[ $current_step_code ]; |
| 184 | |
| 185 | $this->vars['price_breakdown_rows'] = OsStepsHelper::$order_object->generate_price_breakdown_rows(); |
| 186 | |
| 187 | $steps = [ 'confirmation' => $steps['confirmation'] ]; |
| 188 | |
| 189 | |
| 190 | } else { |
| 191 | |
| 192 | OsStepsHelper::set_cart_object_from_order_intent( $order_intent ); |
| 193 | $current_step_code = 'verify'; |
| 194 | $current_step = $steps[ $current_step_code ]; |
| 195 | |
| 196 | |
| 197 | foreach ( OsStepsHelper::$step_codes_in_order as $step_code ) { |
| 198 | if ( $step_code == $current_step_code ) { |
| 199 | break; |
| 200 | } else { |
| 201 | $step_codes_to_preload[] = $step_code; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | $this->vars['price_breakdown_rows'] = OsStepsHelper::$cart_object->generate_price_breakdown_rows(); |
| 206 | // order exists - only load confirmation step |
| 207 | $this->vars['step_codes_to_preload'] = $step_codes_to_preload; |
| 208 | |
| 209 | } |
| 210 | |
| 211 | $this->vars['cart'] = OsStepsHelper::$cart_object; |
| 212 | $this->vars['show_next_btn'] = OsStepsHelper::can_step_show_next_btn( $current_step_code ); |
| 213 | $this->vars['show_prev_btn'] = OsStepsHelper::can_step_show_prev_btn( $current_step_code ); |
| 214 | $this->vars['all_steps'] = OsStepsHelper::get_steps( true ); |
| 215 | $this->vars['steps'] = $steps; |
| 216 | $this->vars['current_step'] = $current_step; |
| 217 | |
| 218 | $this->vars['current_step_code'] = $current_step_code; |
| 219 | $this->vars['booking'] = OsStepsHelper::$booking_object; |
| 220 | $this->vars['customer'] = OsStepsHelper::get_customer_object(); |
| 221 | $this->vars['active_cart_item'] = OsStepsHelper::$active_cart_item; |
| 222 | $this->vars['restrictions'] = OsStepsHelper::$restrictions; |
| 223 | $this->vars['presets'] = OsStepsHelper::$presets; |
| 224 | $this->vars['booking_element_type'] = 'lightbox'; |
| 225 | $this->vars['booking_element_styles'] = []; |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | $this->set_layout( 'none' ); |
| 232 | |
| 233 | $this->format_render( 'start', array(), array( 'lightbox_class' => '' ) ); |
| 234 | } else { |
| 235 | $this->send_json( |
| 236 | array( |
| 237 | 'status' => LATEPOINT_STATUS_ERROR, |
| 238 | 'message' => __( 'Invalid order intent key', 'latepoint' ), |
| 239 | ) |
| 240 | ); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | public function start( array $custom_restrictions = [], array $custom_presets = [], array $booking_element_styles = [], bool $output = true, string $booking_element_type = 'lightbox' ) { |
| 245 | $merged_params = $this->params; |
| 246 | |
| 247 | if ( ! empty( $custom_restrictions ) ) { |
| 248 | $merged_params['restrictions'] = $custom_restrictions; |
| 249 | } |
| 250 | if ( ! empty( $custom_presets ) ) { |
| 251 | $merged_params['presets'] = $custom_presets; |
| 252 | } |
| 253 | if ( ! empty( $this->params['booking_element_type'] ) ) { |
| 254 | $booking_element_type = $this->params['booking_element_type']; |
| 255 | } |
| 256 | |
| 257 | |
| 258 | if ( ! empty( $merged_params['booking_element_styles'] ) ) { |
| 259 | $booking_element_styles = array_merge( $booking_element_styles, $merged_params['booking_element_styles'] ); |
| 260 | } |
| 261 | |
| 262 | // set early to check if it's converted or should be emptied |
| 263 | OsStepsHelper::set_cart_object(); |
| 264 | if ( ! empty( OsStepsHelper::$cart_object->order_id ) ) { |
| 265 | OsCartsHelper::reset_cart(); |
| 266 | OsStepsHelper::set_cart_object(); |
| 267 | } |
| 268 | // clear cart if "shopping cart" feature is not enabled |
| 269 | if ( ! OsCartsHelper::can_checkout_multiple_items() ) { |
| 270 | OsStepsHelper::$cart_object->clear(); |
| 271 | } |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | OsStepsHelper::set_required_objects( $merged_params ); |
| 277 | |
| 278 | $steps = OsStepsHelper::get_steps(); |
| 279 | |
| 280 | $current_step_code = OsStepsHelper::get_step_codes_in_order()[0]; |
| 281 | |
| 282 | if ( OsStepsHelper::should_step_be_skipped( $current_step_code ) ) { |
| 283 | $current_step_code = OsStepsHelper::get_next_step_code( $current_step_code ); |
| 284 | } |
| 285 | // check if all booking steps have to be skipped, if so - it means the booking object is ready and we can add it to the cart |
| 286 | $ready_to_add_to_cart = true; |
| 287 | if ( OsStepsHelper::$active_cart_item->is_booking() ) { |
| 288 | foreach ( $steps as $step_code => $step_object ) { |
| 289 | $step_main_parent_code = explode( '__', $step_code ); |
| 290 | if ( ! empty( $step_main_parent_code[0] ) && $step_main_parent_code[0] == 'booking' ) { |
| 291 | $ready_to_add_to_cart = false; |
| 292 | break; |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | // looks like item is ready to be added to cart (because all necessary steps/presets where applied in a trigger element), add it to cart |
| 297 | if ( $ready_to_add_to_cart ) { |
| 298 | try { |
| 299 | OsStepsHelper::add_current_item_to_cart(); |
| 300 | } catch ( Exception $e ) { |
| 301 | $this->vars['booking'] = OsStepsHelper::$booking_object; |
| 302 | if ( $output ) { |
| 303 | $this->format_render( 'preset_slot_not_available' ); |
| 304 | return false; |
| 305 | } else { |
| 306 | return $this->format_render_return( 'preset_slot_not_available' ); |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | $current_step = $steps[ $current_step_code ]; |
| 311 | |
| 312 | $this->vars['cart'] = OsStepsHelper::$cart_object; |
| 313 | $this->vars['show_next_btn'] = OsStepsHelper::can_step_show_next_btn( $current_step->code ); |
| 314 | $this->vars['show_prev_btn'] = OsStepsHelper::can_step_show_prev_btn( $current_step->code ); |
| 315 | $this->vars['all_steps'] = OsStepsHelper::get_steps( true ); |
| 316 | $this->vars['steps'] = $steps; |
| 317 | $this->vars['current_step'] = $current_step; |
| 318 | |
| 319 | $this->vars['current_step_code'] = $current_step->code; |
| 320 | $this->vars['booking'] = OsStepsHelper::$booking_object; |
| 321 | $this->vars['customer'] = OsStepsHelper::get_customer_object(); |
| 322 | $this->vars['active_cart_item'] = OsStepsHelper::$active_cart_item; |
| 323 | $this->vars['restrictions'] = OsStepsHelper::$restrictions; |
| 324 | $this->vars['presets'] = OsStepsHelper::$presets; |
| 325 | $this->vars['booking_element_type'] = $booking_element_type; |
| 326 | $this->vars['booking_element_styles'] = $booking_element_styles; |
| 327 | $this->vars['timezone_name'] = OsTimeHelper::get_timezone_name_from_session(); |
| 328 | |
| 329 | $this->set_layout( 'none' ); |
| 330 | |
| 331 | |
| 332 | |
| 333 | if ( $output ) { |
| 334 | $this->format_render( __FUNCTION__, [], [ 'step' => $current_step->code ] ); |
| 335 | } else { |
| 336 | return $this->format_render_return( __FUNCTION__, [], [ 'step' => $current_step->code ] ); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | |
| 341 | public function load_step() { |
| 342 | OsStepsHelper::set_required_objects( $this->params ); |
| 343 | |
| 344 | $current_step_code = OsStepsHelper::retrieve_step_code( $this->params['current_step_code'] ); |
| 345 | if ( empty( $current_step_code ) ) { |
| 346 | return false; |
| 347 | } |
| 348 | |
| 349 | $step_direction = $this->params['step_direction'] ?? 'next'; |
| 350 | $step_code_to_load = false; |
| 351 | switch ( $step_direction ) { |
| 352 | case 'next': |
| 353 | /** |
| 354 | * Process step by code |
| 355 | * |
| 356 | * @param {string} $step_code step code that will be processed |
| 357 | * @param {OsBookingModel} $booking booking object |
| 358 | * @param {array} $params array of params |
| 359 | * |
| 360 | * @since 5.0.0 |
| 361 | * @hook latepoint_process_step |
| 362 | * |
| 363 | */ |
| 364 | do_action( 'latepoint_process_step', $current_step_code, OsStepsHelper::$booking_object, $this->params ); |
| 365 | $step_code_to_load = OsStepsHelper::get_next_step_code( $current_step_code ); |
| 366 | break; |
| 367 | case 'prev': |
| 368 | $step_code_to_load = OsStepsHelper::get_prev_step_code( $current_step_code ); |
| 369 | break; |
| 370 | case 'specific': |
| 371 | $step_code_to_load = OsStepsHelper::should_step_be_skipped( $current_step_code ) ? OsStepsHelper::get_next_step_code( $current_step_code ) : $current_step_code; |
| 372 | break; |
| 373 | } |
| 374 | if ( $step_code_to_load ) { |
| 375 | |
| 376 | /** |
| 377 | * Load step by code |
| 378 | * |
| 379 | * @param {string} $step_code step code to load |
| 380 | * @param {string} $type type of return (json) |
| 381 | * @param {array} $params array of params |
| 382 | * |
| 383 | * @since 5.0.0 |
| 384 | * @hook latepoint_load_step |
| 385 | * |
| 386 | */ |
| 387 | do_action( 'latepoint_load_step', $step_code_to_load, 'json', $this->params ); |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | |
| 393 | endif; |
| 394 |