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