| 1 |
<?php /** |
| 2 |
* @version 1.0 |
| 3 |
* @package Booking Calendar |
| 4 |
* @category Reusable Add Booking component |
| 5 |
*/ |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly. |
| 8 |
|
| 9 |
/** |
| 10 |
* Reusable renderer for the Booking > Add Booking experience. |
| 11 |
* |
| 12 |
* The admin page calls this without args, so legacy $_GET driven behavior stays intact. |
| 13 |
* Future embedded contexts can pass explicit args, which are always preferred over $_GET. |
| 14 |
*/ |
| 15 |
class WPBC_Add_Booking_Component { |
| 16 |
|
| 17 |
/** |
| 18 |
* Check if current user can access the Add Booking workflow. |
| 19 |
* |
| 20 |
* @return bool |
| 21 |
*/ |
| 22 |
public static function current_user_can_add_booking() { |
| 23 |
|
| 24 |
$user_role = get_bk_option( 'booking_user_role_addbooking' ); |
| 25 |
$cap = 'read'; |
| 26 |
|
| 27 |
if ( class_exists( 'WPBC_Admin_Menus' ) && isset( WPBC_Admin_Menus::$capability[ $user_role ] ) ) { |
| 28 |
$cap = WPBC_Admin_Menus::$capability[ $user_role ]; |
| 29 |
} elseif ( class_exists( 'WPBC_Admin_Menus' ) && isset( WPBC_Admin_Menus::$capability['subscriber'] ) ) { |
| 30 |
$cap = WPBC_Admin_Menus::$capability['subscriber']; |
| 31 |
} |
| 32 |
|
| 33 |
return current_user_can( $cap ); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Render the component and echo by default. |
| 38 |
* |
| 39 |
* @param array $args Component options. |
| 40 |
* |
| 41 |
* @return string|void |
| 42 |
*/ |
| 43 |
public static function render( $args = array() ) { |
| 44 |
|
| 45 |
$args = self::normalize_args( $args ); |
| 46 |
$is_echo = ( ! empty( $args['is_echo'] ) ); |
| 47 |
|
| 48 |
ob_start(); |
| 49 |
|
| 50 |
?><span class="wpdevelop"><?php // BS UI CSS Class. |
| 51 |
|
| 52 |
if ( ! empty( $args['is_booking_page_js'] ) ) { |
| 53 |
wpbc_js_for_bookings_page(); // JavaScript functions. |
| 54 |
} |
| 55 |
|
| 56 |
if ( ! empty( $args['is_toolbar_visible'] ) ) { |
| 57 |
wpbc_add_new_booking_toolbar( |
| 58 |
array( |
| 59 |
'resource_id' => $args['_is_explicit_resource_id'] ? $args['resource_id'] : null, |
| 60 |
'booking_form' => $args['_is_explicit_booking_form'] ? $args['custom_booking_form'] : null, |
| 61 |
) |
| 62 |
); |
| 63 |
} |
| 64 |
|
| 65 |
?></span><!-- wpdevelop class --><?php |
| 66 |
|
| 67 |
if ( ! empty( $args['is_show_before_content_spacer'] ) ) { |
| 68 |
?><div class="clear" style="height:40px;"></div><?php |
| 69 |
} |
| 70 |
|
| 71 |
?><div class="<?php echo esc_attr( $args['content_css_class'] ); ?>" style="<?php echo esc_attr( $args['content_style'] ); ?>"><?php |
| 72 |
|
| 73 |
self::print_context_js( $args ); |
| 74 |
|
| 75 |
WPBC_FE_Render::render_booking_form( |
| 76 |
array( |
| 77 |
'resource_id' => $args['resource_id'], |
| 78 |
'cal_count' => $args['cal_count'], |
| 79 |
'is_echo' => 1, |
| 80 |
'custom_booking_form' => $args['custom_booking_form'], |
| 81 |
'selected_dates_without_calendar' => $args['selected_dates_without_calendar'], |
| 82 |
'start_month_calendar' => $args['start_month_calendar'], |
| 83 |
'shortcode_param__options' => $args['shortcode_param__options'], |
| 84 |
'calendar_dates_start' => $args['calendar_dates_start'], |
| 85 |
'calendar_dates_end' => $args['calendar_dates_end'], |
| 86 |
'booking_hash' => $args['booking_hash'], |
| 87 |
) |
| 88 |
); |
| 89 |
|
| 90 |
?></div><?php |
| 91 |
|
| 92 |
if ( ! empty( $args['is_show_footer_email_toggle'] ) ) { |
| 93 |
?><hr /><?php |
| 94 |
wpbc_toolbar_is_send_emails_btn_duplicated(); |
| 95 |
} |
| 96 |
|
| 97 |
if ( ! empty( $args['is_booking_page_popover'] ) ) { |
| 98 |
wpbc_bs_javascript_popover(); // JS Popover. |
| 99 |
} |
| 100 |
|
| 101 |
$html = ob_get_clean(); |
| 102 |
|
| 103 |
if ( $is_echo ) { |
| 104 |
echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
return $html; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
/** |
| 113 |
* Normalize component args. |
| 114 |
* |
| 115 |
* Explicit args are preferred first. Empty/non-provided args fall back to legacy $_GET behavior. |
| 116 |
* |
| 117 |
* @param array $args Component options. |
| 118 |
* |
| 119 |
* @return array |
| 120 |
*/ |
| 121 |
private static function normalize_args( $args ) { |
| 122 |
|
| 123 |
$defaults = array( |
| 124 |
'resource_id' => null, |
| 125 |
'booking_type' => null, |
| 126 |
'booking_form' => null, |
| 127 |
'custom_booking_form' => null, |
| 128 |
'calendar_options' => null, |
| 129 |
'cal_count' => null, |
| 130 |
'calendar_months_count' => null, |
| 131 |
'shortcode_param__options' => null, |
| 132 |
'selected_dates_without_calendar' => null, |
| 133 |
'start_month_calendar' => false, |
| 134 |
'calendar_dates_start' => '', |
| 135 |
'calendar_dates_end' => '', |
| 136 |
'booking_hash' => '', |
| 137 |
'selected_date' => '', |
| 138 |
'selected_time' => '', |
| 139 |
'is_toolbar_visible' => true, |
| 140 |
'is_booking_page_js' => true, |
| 141 |
'is_booking_page_popover' => true, |
| 142 |
'is_show_before_content_spacer' => true, |
| 143 |
'is_show_footer_email_toggle' => true, |
| 144 |
'content_css_class' => 'add_booking_page_content', |
| 145 |
'content_style' => 'width:100%;margin-bottom:100px;', |
| 146 |
'is_echo' => 1, |
| 147 |
); |
| 148 |
$args = wp_parse_args( $args, $defaults ); |
| 149 |
|
| 150 |
$args['_is_explicit_resource_id'] = ( ( null !== $args['resource_id'] ) && ( absint( $args['resource_id'] ) > 0 ) ) |
| 151 |
|| ( ( null !== $args['booking_type'] ) && ( absint( $args['booking_type'] ) > 0 ) ); |
| 152 |
$args['_is_explicit_booking_form'] = ( ( null !== $args['custom_booking_form'] ) && ( '' !== (string) $args['custom_booking_form'] ) ) |
| 153 |
|| ( ( null !== $args['booking_form'] ) && ( '' !== (string) $args['booking_form'] ) ); |
| 154 |
|
| 155 |
$resource_id = self::get_explicit_or_get_resource_id( $args ); |
| 156 |
$form_name = self::get_explicit_or_get_booking_form( $args ); |
| 157 |
|
| 158 |
$calendar_params = self::get_calendar_params( $args ); |
| 159 |
|
| 160 |
$args['resource_id'] = $resource_id; |
| 161 |
$args['custom_booking_form'] = $form_name; |
| 162 |
$args['cal_count'] = $calendar_params['months_number']; |
| 163 |
$args['shortcode_param__options'] = $calendar_params['shortcode_param__options']; |
| 164 |
|
| 165 |
$args['selected_dates_without_calendar'] = ( null !== $args['selected_dates_without_calendar'] ) ? (string) $args['selected_dates_without_calendar'] : ''; |
| 166 |
$args['calendar_dates_start'] = (string) $args['calendar_dates_start']; |
| 167 |
$args['calendar_dates_end'] = (string) $args['calendar_dates_end']; |
| 168 |
$args['booking_hash'] = sanitize_text_field( wp_unslash( (string) $args['booking_hash'] ) ); |
| 169 |
$args['selected_date'] = sanitize_text_field( wp_unslash( (string) $args['selected_date'] ) ); |
| 170 |
$args['selected_time'] = sanitize_text_field( wp_unslash( (string) $args['selected_time'] ) ); |
| 171 |
|
| 172 |
return $args; |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
/** |
| 177 |
* Print per-render context that legacy front-end JavaScript reads from the global _wpbc object. |
| 178 |
* |
| 179 |
* @param array $args Component options. |
| 180 |
* |
| 181 |
* @return void |
| 182 |
*/ |
| 183 |
private static function print_context_js( $args ) { |
| 184 |
|
| 185 |
$booking_hash = isset( $args['booking_hash'] ) ? (string) $args['booking_hash'] : ''; |
| 186 |
$context = array( |
| 187 |
'resource_id' => absint( $args['resource_id'] ), |
| 188 |
'selected_dates_without_calendar' => (string) $args['selected_dates_without_calendar'], |
| 189 |
'selected_date' => (string) $args['selected_date'], |
| 190 |
'selected_time' => (string) $args['selected_time'], |
| 191 |
); |
| 192 |
?> |
| 193 |
<script type="text/javascript"> |
| 194 |
window.wpbc_add_booking_component_context = <?php echo wp_json_encode( $context ); ?>; |
| 195 |
if ( 'undefined' !== typeof _wpbc ) { |
| 196 |
_wpbc.set_other_param( 'this_page_booking_hash', <?php echo wp_json_encode( $booking_hash ); ?> ); |
| 197 |
} |
| 198 |
</script> |
| 199 |
<?php |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
/** |
| 204 |
* Resolve booking resource id: explicit component args first, then legacy $_GET. |
| 205 |
* |
| 206 |
* @param array $args Component options. |
| 207 |
* |
| 208 |
* @return int |
| 209 |
*/ |
| 210 |
private static function get_explicit_or_get_resource_id( $args ) { |
| 211 |
|
| 212 |
if ( ( null !== $args['resource_id'] ) && ( absint( $args['resource_id'] ) > 0 ) ) { |
| 213 |
return absint( $args['resource_id'] ); |
| 214 |
} |
| 215 |
|
| 216 |
if ( ( null !== $args['booking_type'] ) && ( absint( $args['booking_type'] ) > 0 ) ) { |
| 217 |
return absint( $args['booking_type'] ); |
| 218 |
} |
| 219 |
|
| 220 |
if ( isset( $_GET['booking_type'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 221 |
return intval( $_GET['booking_type'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 222 |
} |
| 223 |
|
| 224 |
return 1; |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
/** |
| 229 |
* Resolve booking form: explicit component args first, then legacy $_GET. |
| 230 |
* |
| 231 |
* @param array $args Component options. |
| 232 |
* |
| 233 |
* @return string |
| 234 |
*/ |
| 235 |
private static function get_explicit_or_get_booking_form( $args ) { |
| 236 |
|
| 237 |
if ( ( null !== $args['custom_booking_form'] ) && ( '' !== (string) $args['custom_booking_form'] ) ) { |
| 238 |
return sanitize_text_field( wp_unslash( (string) $args['custom_booking_form'] ) ); |
| 239 |
} |
| 240 |
|
| 241 |
if ( ( null !== $args['booking_form'] ) && ( '' !== (string) $args['booking_form'] ) ) { |
| 242 |
return sanitize_text_field( wp_unslash( (string) $args['booking_form'] ) ); |
| 243 |
} |
| 244 |
|
| 245 |
return WPBC_GET_Request::has_non_empty_get( 'booking_form' ) ? WPBC_GET_Request::get_sanitized( 'booking_form' ) : 'standard'; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
/** |
| 250 |
* Resolve calendar settings for the component. |
| 251 |
* |
| 252 |
* @param array $args Component options. |
| 253 |
* |
| 254 |
* @return array |
| 255 |
*/ |
| 256 |
private static function get_calendar_params( $args ) { |
| 257 |
|
| 258 |
if ( null !== $args['calendar_options'] && is_array( $args['calendar_options'] ) ) { |
| 259 |
$calendar_options = self::format_calendar_options( $args['calendar_options'] ); |
| 260 |
} else { |
| 261 |
$calendar_options = self::get_saved_user_calendar_options(); |
| 262 |
} |
| 263 |
|
| 264 |
if ( null !== $args['calendar_months_count'] ) { |
| 265 |
$calendar_options['months_number'] = absint( $args['calendar_months_count'] ); |
| 266 |
} |
| 267 |
|
| 268 |
if ( null !== $args['cal_count'] ) { |
| 269 |
$calendar_options['months_number'] = absint( $args['cal_count'] ); |
| 270 |
} |
| 271 |
|
| 272 |
if ( empty( $calendar_options['months_number'] ) ) { |
| 273 |
$calendar_options['months_number'] = 1; |
| 274 |
} |
| 275 |
|
| 276 |
if ( null !== $args['shortcode_param__options'] ) { |
| 277 |
$shortcode_param__options = (string) $args['shortcode_param__options']; |
| 278 |
} else { |
| 279 |
$shortcode_param__options = '{calendar' . $calendar_options['options_param'] . '}'; |
| 280 |
} |
| 281 |
|
| 282 |
return array( |
| 283 |
'months_number' => absint( $calendar_options['months_number'] ), |
| 284 |
'shortcode_param__options' => $shortcode_param__options, |
| 285 |
); |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
/** |
| 290 |
* Get Calendar Options of specific User. |
| 291 |
* |
| 292 |
* @return array Number of months and calendar options parameter. |
| 293 |
*/ |
| 294 |
public static function get_saved_user_calendar_options() { |
| 295 |
|
| 296 |
$user_calendar_options = get_user_option( 'booking_custom_' . 'add_booking_calendar_options', wpbc_get_current_user_id() ); |
| 297 |
|
| 298 |
if ( false === $user_calendar_options ) { |
| 299 |
$user_calendar_options = array(); |
| 300 |
} else { |
| 301 |
$user_calendar_options = maybe_unserialize( $user_calendar_options ); |
| 302 |
} |
| 303 |
|
| 304 |
return self::format_calendar_options( $user_calendar_options ); |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
/** |
| 309 |
* Convert calendar option values to the shortcode option string used by the renderer. |
| 310 |
* |
| 311 |
* @param array $user_calendar_options Raw calendar options. |
| 312 |
* |
| 313 |
* @return array |
| 314 |
*/ |
| 315 |
private static function format_calendar_options( $user_calendar_options ) { |
| 316 |
|
| 317 |
$defaults = array( |
| 318 |
'calendar_months_count' => 1, |
| 319 |
'calendar_months_num_in_1_row' => 0, |
| 320 |
'calendar_width' => '', |
| 321 |
'calendar_widthunits' => 'px', |
| 322 |
'calendar_cell_height' => '', |
| 323 |
'calendar_cell_heightunits' => 'px', |
| 324 |
); |
| 325 |
|
| 326 |
$user_calendar_options = wp_parse_args( (array) $user_calendar_options, $defaults ); |
| 327 |
|
| 328 |
if ( ! empty( $user_calendar_options['calendar_months_count'] ) ) { |
| 329 |
$selected_calendar_months_count = intval( $user_calendar_options['calendar_months_count'] ); |
| 330 |
} else { |
| 331 |
$selected_calendar_months_count = 1; |
| 332 |
} |
| 333 |
|
| 334 |
if ( ! empty( $user_calendar_options['calendar_months_num_in_1_row'] ) ) { |
| 335 |
$option_months_num_in_row = ' months_num_in_row=' . intval( $user_calendar_options['calendar_months_num_in_1_row'] ); |
| 336 |
} else { |
| 337 |
$option_months_num_in_row = ''; |
| 338 |
} |
| 339 |
|
| 340 |
if ( ! empty( $user_calendar_options['calendar_width'] ) ) { |
| 341 |
$unit_value = ( esc_attr( $user_calendar_options['calendar_widthunits'] ) == 'percent' ) ? '%' : esc_attr( $user_calendar_options['calendar_widthunits'] ); |
| 342 |
$option_width = ' width=' . intval( $user_calendar_options['calendar_width'] ) . $unit_value; |
| 343 |
$option_width .= ' strong_width=' . intval( $user_calendar_options['calendar_width'] ) . $unit_value; // FixIn: 9.3.1.6. |
| 344 |
} else { |
| 345 |
$option_width = ''; |
| 346 |
} |
| 347 |
|
| 348 |
if ( ! empty( $user_calendar_options['calendar_cell_height'] ) ) { |
| 349 |
$unit_value = ( esc_attr( $user_calendar_options['calendar_cell_heightunits'] ) == 'percent' ) ? '%' : esc_attr( $user_calendar_options['calendar_cell_heightunits'] ); |
| 350 |
$option_cell_height = ' cell_height=' . intval( $user_calendar_options['calendar_cell_height'] ) . $unit_value; |
| 351 |
} else { |
| 352 |
$option_cell_height = ''; |
| 353 |
} |
| 354 |
|
| 355 |
return array( |
| 356 |
'months_number' => $selected_calendar_months_count, |
| 357 |
'options_param' => $option_months_num_in_row . $option_width . $option_cell_height, |
| 358 |
); |
| 359 |
} |
| 360 |
} |
| 361 |
|