PluginProbe
Appointment Booking Plugin – LatePoint | Calendar & Scheduling for WordPress / 5.1.92
Appointment Booking Plugin – LatePoint | Calendar & Scheduling for WordPress v5.1.92
5.6.11 5.6.10 5.6.9 5.6.8 5.6.7 5.6.6 5.6.5 5.6.4 5.6.3 5.6.2 5.6.1 5.6.0 5.5.2 5.5.1 5.5.0 5.4.2 trunk 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 All 48 releases
latepoint / lib / controllers / steps_controller.php
steps_controller.php
307 lines 10.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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'], [ 'start_from_order_intent' ] );
17 $this->action_access['public'] = array_merge( $this->action_access['public'], [
18 'start',
19 'load_step',
20 'reload_booking_form_summary_panel',
21 'check_order_intent_bookable',
22 'load_datepicker_month'
23 ] );
24
25 $this->views_folder = LATEPOINT_VIEWS_ABSPATH . 'steps/';
26 $this->vars['page_header'] = __( 'Appointments', 'latepoint' );
27 $this->vars['breadcrumbs'][] = array(
28 'label' => __( 'Appointments', 'latepoint' ),
29 'link' => OsRouterHelper::build_link( [
30 'bookings',
31 'pending_approval'
32 ] )
33 );
34 }
35
36 public function load_datepicker_month() {
37 OsStepsHelper::set_required_objects( $this->params );
38
39 $target_date = new OsWpDateTime( $this->params['target_date_string'] );
40 $calendar_settings = [
41 'layout' => $this->params['calendar_layout'] ?? 'classic',
42 'timezone_name' => $this->params['timezone_name'] ?? false,
43 ];
44
45 $calendar_settings['earliest_possible_booking'] = OsSettingsHelper::get_settings_value( 'earliest_possible_booking', false );
46 $calendar_settings['latest_possible_booking'] = OsSettingsHelper::get_settings_value( 'latest_possible_booking', false );
47
48 $this->format_render( 'partials/_monthly_calendar_days', [
49 'target_date' => $target_date,
50 'calendar_settings' => $calendar_settings,
51 'booking_request' => \LatePoint\Misc\BookingRequest::create_from_booking_model( OsStepsHelper::$booking_object )
52 ] );
53 }
54
55
56 public function check_order_intent_bookable() {
57 OsStepsHelper::set_required_objects( $this->params );
58 if(OsStepsHelper::$cart_object->order_id){
59 // already converted, so we are good
60 $this->send_json( [
61 'status' => LATEPOINT_STATUS_SUCCESS,
62 'message' => __( 'Cart has already been converted to order', 'latepoint' )
63 ] );
64 }
65 $order_intent = OsOrderIntentHelper::create_or_update_order_intent( OsStepsHelper::$cart_object, OsStepsHelper::$restrictions, OsStepsHelper::$presets );
66 if ( $order_intent->is_bookable() ) {
67 $this->send_json( [
68 'status' => LATEPOINT_STATUS_SUCCESS,
69 'message' => __( 'Order intent can be converted to order', 'latepoint' )
70 ] );
71 } else {
72 $this->send_json( [
73 'status' => LATEPOINT_STATUS_ERROR,
74 'message' => __( 'Selected booking slot is not available anymore. Please pick a different time slot.', 'latepoint' )
75 ] );
76 }
77 }
78
79 function generate_timeslots_for_day(){
80 OsStepsHelper::set_required_objects( $this->params );
81
82
83
84 wp_send_json( [ 'status' => LATEPOINT_STATUS_SUCCESS, 'message' => '' ] );
85 }
86
87
88 function reload_booking_form_summary_panel() {
89 OsStepsHelper::set_required_objects( $this->params );
90 $this->vars['cart'] = OsStepsHelper::$cart_object;
91
92 if ( OsStepsHelper::is_ready_for_summary() ) {
93 $this->vars['booking'] = OsStepsHelper::$booking_object;
94 $this->vars['active_cart_item'] = OsStepsHelper::$active_cart_item;
95
96 if ( $this->get_return_format() == 'json' ) {
97 $response_html = $this->render( $this->views_folder . 'partials/_booking_form_summary_panel', 'none' );
98 wp_send_json( [ 'status' => LATEPOINT_STATUS_SUCCESS, 'message' => $response_html ] );
99 exit();
100 } else {
101 echo $this->render( $this->views_folder . 'partials/_booking_form_summary_panel', $this->get_layout() );
102 }
103 } else {
104 wp_send_json( [ 'status' => LATEPOINT_STATUS_SUCCESS, 'message' => '' ] );
105 }
106
107 }
108
109
110 public function start_from_order_intent() {
111 $order_intent = OsOrderIntentHelper::get_order_intent_by_intent_key($this->params['order_intent_key']);
112
113 if ( !$order_intent->is_new_record() ) {
114 $step_codes_to_preload = [];
115 $steps = OsStepsHelper::get_steps();
116 OsStepsHelper::set_required_objects($this->params);
117
118 if ( $order_intent->order_id ) {
119 // if order is created - load it
120 OsStepsHelper::load_order_object( $order_intent->order_id );
121 $current_step_code = 'confirmation';
122 $current_step = $steps[ $current_step_code ];
123
124 $this->vars['price_breakdown_rows'] = OsStepsHelper::$order_object->generate_price_breakdown_rows();
125
126 $steps = [ 'confirmation' => $steps['confirmation'] ];
127
128
129 } else {
130
131 OsStepsHelper::set_cart_object_from_order_intent( $order_intent );
132 $current_step_code = 'verify';
133 $current_step = $steps[ $current_step_code ];
134
135
136 foreach ( OsStepsHelper::$step_codes_in_order as $step_code ) {
137 if ( $step_code == $current_step_code ) {
138 break;
139 } else {
140 $step_codes_to_preload[] = $step_code;
141 }
142 }
143
144 $this->vars['price_breakdown_rows'] = OsStepsHelper::$cart_object->generate_price_breakdown_rows();
145 // order exists - only load confirmation step
146 $this->vars['step_codes_to_preload'] = $step_codes_to_preload;
147
148 }
149
150 $this->vars['cart'] = OsStepsHelper::$cart_object;
151 $this->vars['show_next_btn'] = OsStepsHelper::can_step_show_next_btn( $current_step_code );
152 $this->vars['show_prev_btn'] = OsStepsHelper::can_step_show_prev_btn( $current_step_code );
153 $this->vars['all_steps'] = OsStepsHelper::get_steps( true );
154 $this->vars['steps'] = $steps;
155 $this->vars['current_step'] = $current_step;
156
157 $this->vars['current_step_code'] = $current_step_code;
158 $this->vars['booking'] = OsStepsHelper::$booking_object;
159 $this->vars['active_cart_item'] = OsStepsHelper::$active_cart_item;
160 $this->vars['restrictions'] = OsStepsHelper::$restrictions;
161 $this->vars['presets'] = OsStepsHelper::$presets;
162 $this->vars['booking_element_type'] = 'lightbox';
163 $this->vars['booking_element_styles'] = [];
164
165
166
167
168
169 $this->set_layout( 'none' );
170
171 $this->format_render( 'start', array(), array( 'lightbox_class' => '' ) );
172 } else {
173 $this->send_json( array(
174 'status' => LATEPOINT_STATUS_ERROR,
175 'message' => __( 'Invalid order intent key', 'latepoint' )
176 ) );
177 }
178
179 }
180
181 public function start( array $custom_restrictions = [], array $custom_presets = [], array $booking_element_styles = [], bool $output = true, string $booking_element_type = 'lightbox' ) {
182 $merged_params = $this->params;
183
184 if ( ! empty( $custom_restrictions ) ) {
185 $merged_params['restrictions'] = $custom_restrictions;
186 }
187 if ( ! empty( $custom_presets ) ) {
188 $merged_params['presets'] = $custom_presets;
189 }
190 if( ! empty( $this->params['booking_element_type'] ) ) {
191 $booking_element_type = $this->params['booking_element_type'];
192 }
193
194
195 if(!empty($merged_params['booking_element_styles'])){
196 $booking_element_styles = array_merge( $booking_element_styles, $merged_params['booking_element_styles'] );
197 }
198
199 // set early to check if it's converted or should be emptied
200 OsStepsHelper::set_cart_object();
201 if(!empty(OsStepsHelper::$cart_object->order_id)){
202 OsCartsHelper::reset_cart();
203 OsStepsHelper::set_cart_object();
204 }
205 // clear cart if "shopping cart" feature is not enabled
206 if ( ! OsCartsHelper::can_checkout_multiple_items() ) OsStepsHelper::$cart_object->clear();
207
208
209
210
211 OsStepsHelper::set_required_objects( $merged_params );
212
213 $steps = OsStepsHelper::get_steps();
214
215 $current_step_code = OsStepsHelper::get_step_codes_in_order()[0];
216
217 if ( OsStepsHelper::should_step_be_skipped( $current_step_code ) ) {
218 $current_step_code = OsStepsHelper::get_next_step_code( $current_step_code );
219 }
220 // 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
221 $ready_to_add_to_cart = true;
222 if(OsStepsHelper::$active_cart_item->is_booking()){
223 foreach($steps as $step_code => $step_object) {
224 $step_main_parent_code = explode('__', $step_code);
225 if(!empty($step_main_parent_code[0]) && $step_main_parent_code[0] == 'booking'){
226 $ready_to_add_to_cart = false;
227 break;
228 }
229 }
230 }
231 // 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
232 if($ready_to_add_to_cart){
233 try{
234 OsStepsHelper::add_current_item_to_cart();
235 }catch(Exception $e){
236 $this->vars['booking'] = OsStepsHelper::$booking_object;
237 if ( $output ) {
238 $this->format_render( 'preset_slot_not_available' );
239 return false;
240 } else {
241 return $this->format_render_return( 'preset_slot_not_available' );
242 }
243 }
244 }
245 $current_step = $steps[ $current_step_code ];
246
247 $this->vars['cart'] = OsStepsHelper::$cart_object;
248 $this->vars['show_next_btn'] = OsStepsHelper::can_step_show_next_btn( $current_step->code );
249 $this->vars['show_prev_btn'] = OsStepsHelper::can_step_show_prev_btn( $current_step->code );
250 $this->vars['all_steps'] = OsStepsHelper::get_steps( true );
251 $this->vars['steps'] = $steps;
252 $this->vars['current_step'] = $current_step;
253
254 $this->vars['current_step_code'] = $current_step->code;
255 $this->vars['booking'] = OsStepsHelper::$booking_object;
256 $this->vars['active_cart_item'] = OsStepsHelper::$active_cart_item;
257 $this->vars['restrictions'] = OsStepsHelper::$restrictions;
258 $this->vars['presets'] = OsStepsHelper::$presets;
259 $this->vars['booking_element_type'] = $booking_element_type;
260 $this->vars['booking_element_styles'] = $booking_element_styles;
261 $this->vars['timezone_name'] = OsTimeHelper::get_timezone_name_from_session();
262
263 $this->set_layout( 'none' );
264
265
266
267 if ( $output ) {
268 $this->format_render( __FUNCTION__, [], [ 'step' => $current_step->code ] );
269 } else {
270 return $this->format_render_return( __FUNCTION__, [], [ 'step' => $current_step->code ] );
271 }
272 }
273
274
275 public function load_step() {
276 OsStepsHelper::set_required_objects( $this->params );
277
278 $current_step_code = OsStepsHelper::retrieve_step_code( $this->params['current_step_code'] );
279 if ( empty( $current_step_code ) ) {
280 return false;
281 }
282
283 $step_direction = $this->params['step_direction'] ?? 'next';
284 $step_code_to_load = false;
285 switch ( $step_direction ) {
286 case 'next':
287 do_action( 'latepoint_process_step', $current_step_code, OsStepsHelper::$booking_object );
288 $step_code_to_load = OsStepsHelper::get_next_step_code( $current_step_code );
289 break;
290 case 'prev':
291 $step_code_to_load = OsStepsHelper::get_prev_step_code( $current_step_code );
292 break;
293 case 'specific':
294 $step_code_to_load = OsStepsHelper::should_step_be_skipped( $current_step_code ) ? OsStepsHelper::get_next_step_code( $current_step_code ) : $current_step_code;
295 break;
296 }
297 if ( $step_code_to_load ) {
298 do_action( 'latepoint_load_step', $step_code_to_load, 'json', $this->params );
299 }
300 }
301
302
303 }
304
305
306 endif;
307