PluginProbe
Booking Calendar / 11.3
Booking Calendar v11.3
11.8.3 11.8.2 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 All 203 releases
booking / includes / _front_end / class-fe-render.php

class-fe-render.php in Booking Calendar 11.3, at includes/_front_end/class-fe-render.php

349 lines 15.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Front-End Render (Booking form, Calendar)
4 *
5 * File: /wp-content/plugins/booking/includes/_front_end/class-fe-render.php
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit;
10 }
11
12 class WPBC_FE_Render {
13
14 /**
15 * Echo or return helper (to preserve legacy API behavior).
16 *
17 * @param string $html - already escaped/sanitizing HTML content.
18 * @param bool $is_echo - do we need to echo it?.
19 *
20 * @return string|void
21 */
22 private static function echo_or_return( $html, $is_echo ) {
23
24 if ( $is_echo ) {
25
26 echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
27
28 return;
29 }
30
31 return $html;
32 }
33
34 // -----------------------------------------------------------------------------------------------------------------
35
36 /**
37 * Render Booking Form (Calendar + Form).
38 *
39 * @param array $params_arr
40 *
41 * @return string|void
42 */
43 public static function render_booking_form( $params_arr = array() ) {
44
45 // Default parameters (keep identical to legacy).
46 $default_params = array(
47 'resource_id' => 1,
48 'cal_count' => 1,
49 'is_echo' => 1,
50 'custom_booking_form' => 'standard',
51 'selected_dates_without_calendar' => '',
52 'start_month_calendar' => false,
53 'shortcode_param__options' => '',
54 'calendar_dates_start' => '',
55 'calendar_dates_end' => '',
56 'booking_hash' => '',
57 'form_status' => 'published',
58 'calendar_request_overrides' => array(),
59 );
60 $params_arr = wp_parse_args( $params_arr, $default_params );
61 $is_echo = ( ! empty( $params_arr['is_echo'] ) );
62
63 // ---------------------------------------------------------------------
64 // Hash resolution, e.g. $_GET['booking_hash'] for Booking Form rendering.
65 // ---------------------------------------------------------------------
66 $hash_res = WPBC_FE_Attr_Postprocessor::resolve_booking_hash_and_maybe_get_parent_resource_id( $params_arr, 'form' );
67 if ( ! $hash_res['ok'] ) {
68 return self::echo_or_return( $hash_res['error_html'], $is_echo );
69 }
70 $params_arr = $hash_res['params_arr'];
71
72 // ---------------------------------------------------------------------
73 // Normalize aggregate resource IDs. - "5;3;9" -> [ 'resource_id' => 5, 'aggregate_resource_id_arr' => [5,3,9] ].
74 // ---------------------------------------------------------------------
75 $aggregate_resource_id_arr = array();
76 $split = WPBC_FE_Attr_Postprocessor::split_aggregate_resource_id( $params_arr['resource_id'] );
77 $params_arr['resource_id'] = $split['resource_id'];
78 $aggregate_resource_id_arr = $split['aggregate_resource_id_arr'];
79
80 // ---------------------------------------------------------------------
81 // Validate resource_id parameter and check if booking resource exists.
82 // ---------------------------------------------------------------------
83 $resource_validation = WPBC_FE_Attr_Postprocessor::validate_resource_id( $params_arr['resource_id'] );
84 if ( true !== $resource_validation ) {
85 return self::echo_or_return( $resource_validation, $is_echo );
86 }
87
88 // ---------------------------------------------------------------------
89 // == MU ==
90 // ---------------------------------------------------------------------
91 make_bk_action( 'check_multiuser_params_for_client_side', $params_arr['resource_id'] );
92
93 // ---------------------------------------------------------------------
94 // Normalize calendar_dates_start/end.
95 // ---------------------------------------------------------------------
96 $calendar_dates_range = WPBC_FE_Attr_Postprocessor::normalize_calendar_dates_range( $params_arr['calendar_dates_start'], $params_arr['calendar_dates_end'] );
97
98 // ---------------------------------------------------------------------
99 // Calendar load params (keep identical keys).
100 // ---------------------------------------------------------------------
101 $calendar_load_params = array(
102 'resource_id' => $params_arr['resource_id'],
103 'aggregate_resource_id_arr' => $aggregate_resource_id_arr,
104 'selected_dates_without_calendar' => $params_arr['selected_dates_without_calendar'],
105 'calendar_number_of_months' => $params_arr['cal_count'],
106 'start_month_calendar' => $params_arr['start_month_calendar'],
107 'shortcode_options' => $params_arr['shortcode_param__options'],
108 'custom_form' => $params_arr['custom_booking_form'],
109 'calendar_dates_start' => $calendar_dates_range['start'],
110 'calendar_dates_end' => $calendar_dates_range['end'],
111 'calendar_request_overrides' => $params_arr['calendar_request_overrides'],
112 );
113
114 $start_script_code = wpbc__calendar__load( $calendar_load_params );
115
116 // Disable booked time slots when selected date is predefined (legacy behavior).
117 if ( '' !== $params_arr['selected_dates_without_calendar'] ) {
118 $assets_key = 'wpbc:disable_time_fields_in_booking_form:' . intval( $params_arr['resource_id'] );
119
120 // Fire on all booking dates loaded. // FixIn: 10.14.17.2.
121 $local_js_code = " jQuery( 'body' ).on( 'wpbc_calendar_ajx__loaded_data', function ( event, loaded_resource_id ){ ";
122 $local_js_code .= ' if ( loaded_resource_id == ' . intval( $params_arr['resource_id'] ) . ' ){ ';
123 $local_js_code .= ' wpbc_disable_time_fields_in_booking_form(' . intval( $params_arr['resource_id'] ) . ')';
124 $local_js_code .= ' }';
125 $local_js_code .= '} );';
126
127 $added = WPBC_FE_Assets::add_jq_ready_js_to_wp_script( 'wpbc_all', $local_js_code, $assets_key );
128 if ( ! $added ) {
129 $start_script_code .= WPBC_FE_Assets::add_inline_js( $local_js_code, $assets_key ); // Fallback to direct inline JS, if previosly not edded script.
130 }
131 }
132
133 // ---------------------------------------------------------------------
134 // For Phase #1 we still use legacy HTML builder for the form body. (Later phases will move it fully.)
135 // ---------------------------------------------------------------------
136 $legacy = wpbc_get_legacy_booking_instance();
137 if ( ! $legacy ) {
138 $err = '<div class="wpbc_after_booking_thank_you_section"><div class="wpbc_ty__container"><div class="wpbc_ty__header"><strong>' . esc_html__( 'Oops!', 'booking' ) . '</strong> ' . esc_html__( 'Booking Calendar front-end renderer is not available. Please contact support.', 'booking' ) . '</div></div></div>';
139 return self::echo_or_return( $err, $is_echo );
140 }
141
142 $form_html = WPBC_FE_Form_Body_Renderer::render(
143 array(
144 'resource_id' => $params_arr['resource_id'],
145 'custom_booking_form' => $params_arr['custom_booking_form'],
146 'form_status' => $params_arr['form_status'],
147 'selected_dates_without_calendar' => $params_arr['selected_dates_without_calendar'],
148 'cal_count' => $params_arr['cal_count'],
149 'shortcode_param__options' => $params_arr['shortcode_param__options'],
150 'booking_hash' => $params_arr['booking_hash'],
151 'legacy_instance' => $legacy,
152 )
153 );
154
155 $my_result = ' ' . $form_html . ' ' . $start_script_code;
156
157
158 // Add DIV structure, where to show payment form.
159 $my_result = apply_filters( 'wpdev_booking_form', $my_result, $params_arr['resource_id'] ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
160
161 // == MU ==
162 make_bk_action( 'finish_check_multiuser_params_for_client_side', $params_arr['resource_id'] );
163
164 return self::echo_or_return( $my_result, $is_echo );
165 }
166
167
168 /**
169 * Render Availability Calendar Only.
170 *
171 * @param array $params_arr
172 *
173 * @return string|void
174 */
175 public static function render_calendar_only( $params_arr = array() ) {
176
177 $default_params = array(
178 'resource_id' => 1,
179 'cal_count' => 1,
180 'is_echo' => 1,
181 'start_month_calendar' => false,
182 'shortcode_param__options' => '',
183 'calendar_dates_start' => '',
184 'calendar_dates_end' => '',
185 'booking_hash' => '',
186 'calendar_request_overrides' => array(),
187 );
188 $params_arr = wp_parse_args( $params_arr, $default_params );
189 $is_echo = ( ! empty( $params_arr['is_echo'] ) );
190
191 // ---------------------------------------------------------------------
192 // Hash resolution, e.g. $_GET['booking_hash'] for Booking Form rendering.
193 // ---------------------------------------------------------------------
194 $hash_res = WPBC_FE_Attr_Postprocessor::resolve_booking_hash_and_maybe_get_parent_resource_id( $params_arr, 'calendar' );
195 if ( ! $hash_res['ok'] ) {
196 return self::echo_or_return( $hash_res['error_html'], $is_echo );
197 }
198 $params_arr = $hash_res['params_arr'];
199
200 // ---------------------------------------------------------------------
201 // Normalize aggregate resource IDs. - "5;3;9" -> [ 'resource_id' => 5, 'aggregate_resource_id_arr' => [5,3,9] ].
202 // ---------------------------------------------------------------------
203 $aggregate_resource_id_arr = array();
204 $split = WPBC_FE_Attr_Postprocessor::split_aggregate_resource_id( $params_arr['resource_id'] );
205 $params_arr['resource_id'] = $split['resource_id'];
206 $aggregate_resource_id_arr = $split['aggregate_resource_id_arr'];
207
208
209 // ---------------------------------------------------------------------
210 // Validate resource_id parameter and check if booking resource exists.
211 // ---------------------------------------------------------------------
212 $resource_validation = WPBC_FE_Attr_Postprocessor::validate_resource_id( $params_arr['resource_id'] );
213 if ( true !== $resource_validation ) {
214 return self::echo_or_return( $resource_validation, $is_echo );
215 }
216
217 // ---------------------------------------------------------------------
218 // == MU ==
219 // ---------------------------------------------------------------------
220 make_bk_action( 'check_multiuser_params_for_client_side', $params_arr['resource_id'] );
221
222 // ---------------------------------------------------------------------
223 // Normalize calendar_dates_start/end.
224 // ---------------------------------------------------------------------
225 $calendar_dates_range = WPBC_FE_Attr_Postprocessor::normalize_calendar_dates_range( $params_arr['calendar_dates_start'], $params_arr['calendar_dates_end'] );
226
227 $calendar_load_params = array(
228 'resource_id' => $params_arr['resource_id'],
229 'aggregate_resource_id_arr' => $aggregate_resource_id_arr,
230 'selected_dates_without_calendar' => '',
231 'calendar_number_of_months' => $params_arr['cal_count'],
232 'start_month_calendar' => $params_arr['start_month_calendar'],
233 'shortcode_options' => $params_arr['shortcode_param__options'],
234 'custom_form' => 'standard', // Because we show only 'AVAILABILITY CALENDAR' without the form, at all.
235 'calendar_dates_start' => $calendar_dates_range['start'],
236 'calendar_dates_end' => $calendar_dates_range['end'],
237 'calendar_request_overrides' => $params_arr['calendar_request_overrides'],
238 );
239 $start_script_code = wpbc__calendar__load( $calendar_load_params );
240
241
242 $my_result = '<div style="clear:both;height:10px;"></div>';
243 $my_result .= wpbc_pre_get_calendar_html( $params_arr['resource_id'], $params_arr['cal_count'], $params_arr['shortcode_param__options'] );
244 $my_result .= ' ' . $start_script_code;
245
246 // Keep legacy filter.
247 $my_result = apply_filters( 'wpdev_booking_calendar', $my_result, $params_arr['resource_id'] ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
248
249 $booking_form_is_using_bs_css = get_bk_option( 'booking_form_is_using_bs_css' );
250 $my_result = '<span ' . ( ( 'On' === $booking_form_is_using_bs_css ) ? 'class="wpdevelop"' : '' ) . '>' . $my_result . '</span>';
251
252 // == MU ==
253 make_bk_action( 'finish_check_multiuser_params_for_client_side', $params_arr['resource_id'] );
254
255 return self::echo_or_return( $my_result, $is_echo );
256 }
257
258 }
259
260
261 /**
262 * Try to get legacy wpdev_booking instance (for Phase #1 fallback calls).
263 *
264 * @return wpdev_booking|null
265 */
266 function wpbc_get_legacy_booking_instance() {
267
268 if ( ! function_exists( 'WPBC' ) ) {
269 return null;
270 }
271
272 $wpbc_instance = WPBC();
273 if ( ! $wpbc_instance ) {
274 return null;
275 }
276
277 if ( empty( $wpbc_instance->booking_obj ) && class_exists( 'wpdev_booking' ) ) {
278 // Lazy boot as a last resort (avoid doing this if you don’t want any boot during true WPBC ajax).
279 $wpbc_instance->booking_obj = new wpdev_booking();
280 }
281
282 return ( $wpbc_instance->booking_obj instanceof wpdev_booking ) ? $wpbc_instance->booking_obj : null;
283 }
284
285
286
287 //TODO: replace evrywhere wpbc_get_rendered_booking_form_html() to some simpler call.
288
289 /**
290 * Get HTML of Rendered Booking From - Legacy type of calling render.
291 *
292 * @param $resource_id
293 * @param $cal_count
294 * @param $is_echo
295 * @param $my_booking_form
296 * @param $my_selected_dates_without_calendar
297 * @param $start_month_calendar
298 * @param $shortcode_param__options
299 *
300 * @return string|null
301 */
302 function wpbc_get_rendered_booking_form_html( $resource_id = 1, $cal_count = 1, $is_echo = 1, $my_booking_form = 'standard', $my_selected_dates_without_calendar = '', $start_month_calendar = false, $shortcode_param__options = array() ) {
303
304 return WPBC_FE_Render::render_booking_form(
305 array(
306 'resource_id' => $resource_id,
307 'cal_count' => $cal_count,
308 'is_echo' => $is_echo,
309 'custom_booking_form' => $my_booking_form,
310 'selected_dates_without_calendar' => $my_selected_dates_without_calendar,
311 'start_month_calendar' => $start_month_calendar,
312 'shortcode_param__options' => $shortcode_param__options,
313 )
314 );
315 }
316
317 /**
318 * Get booking form rendered HTML content without echo.
319 *
320 * @param $resource_id
321 * @param $cal_count
322 * @param $my_booking_form
323 * @param $my_selected_dates_without_calendar
324 * @param $start_month_calendar
325 * @param $shortcode_param__options
326 *
327 * @return string|null
328 */
329 function wpbc_get_rendered_booking_form_html__noecho( $resource_id = 1, $cal_count = 1, $my_booking_form = 'standard', $my_selected_dates_without_calendar = '', $start_month_calendar = false, $shortcode_param__options = array() ) {
330
331 $my_result = WPBC_FE_Render::render_booking_form(
332 array(
333 'resource_id' => $resource_id,
334 'cal_count' => $cal_count,
335 'is_echo' => 0,
336 'custom_booking_form' => $my_booking_form,
337 'selected_dates_without_calendar' => $my_selected_dates_without_calendar,
338 'start_month_calendar' => $start_month_calendar,
339 'shortcode_param__options' => $shortcode_param__options,
340 )
341 );
342 return $my_result;
343 }
344
345 /**
346 * This hook exists in personal.php file for the [bookingselect ..]
347 */
348 add_bk_filter( 'wpdevbk_get_booking_form', 'wpbc_get_rendered_booking_form_html__noecho' );
349