| 1 |
<?php |
| 2 |
/** |
| 3 |
* Appointment shortcode registration and public assets. |
| 4 |
* |
| 5 |
* @package Booking Calendar |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Read a positive Appointment selector value from the non-JavaScript GET fallback. |
| 14 |
* |
| 15 |
* @param string $key Request key. |
| 16 |
* |
| 17 |
* @return int Selected ID or zero. |
| 18 |
*/ |
| 19 |
function wpbc_booking_appointment_get_fallback_selection( $key ) { |
| 20 |
if ( ! isset( $_GET[ $key ] ) || is_array( $_GET[ $key ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 21 |
return 0; |
| 22 |
} |
| 23 |
|
| 24 |
return absint( wp_unslash( $_GET[ $key ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Render the [booking_appointment] Service-first booking workflow. |
| 29 |
* |
| 30 |
* Supported attributes include service_id, provider_id, services, providers, |
| 31 |
* nummonths, startmonth, calendar_dates_start, calendar_dates_end, options, |
| 32 |
* form_type, auto_select_provider, catalog_layout, show_resource_filters, |
| 33 |
* show_resource_image, show_resource_title, show_resource_description, |
| 34 |
* catalog_item_width, catalog_item_max_width, catalog_grid_items_per_row, |
| 35 |
* catalog_list_items_per_row, show_resource_hierarchy, show_availability, |
| 36 |
* show_starting_price, show_progress, progress_item_1_title, |
| 37 |
* progress_item_1_number through progress_item_3_title/progress_item_3_number, |
| 38 |
* screen_1_title, screen_1_description, screen_2_title, screen_2_description, |
| 39 |
* and allow_past. Provider auto-selection is disabled unless |
| 40 |
* auto_select_provider is explicitly enabled. Past bookings are available to |
| 41 |
* public visitors only when allow_past is explicitly enabled by the shortcode. |
| 42 |
* |
| 43 |
* @param mixed $attributes Shortcode attributes. |
| 44 |
* |
| 45 |
* @return string Appointment selector or fixed native booking form. |
| 46 |
*/ |
| 47 |
function wpbc_booking_appointment_shortcode( $attributes = array() ) { |
| 48 |
$config = wpbc_booking_appointment_normalize_config( $attributes ); |
| 49 |
$config['return_url'] = wpbc_booking_appointment_get_fallback_url(); |
| 50 |
$service_id = wpbc_booking_appointment_get_fallback_selection( 'wpbc_appointment_service' ); |
| 51 |
$provider_id = wpbc_booking_appointment_get_fallback_selection( 'wpbc_appointment_provider' ); |
| 52 |
$result = wpbc_booking_appointment_resolve_stage( $config, $service_id, $provider_id ); |
| 53 |
|
| 54 |
if ( is_wp_error( $result ) ) { |
| 55 |
$stage_html = wpbc_booking_appointment_render_error_notice( $result ); |
| 56 |
$stage = 'error'; |
| 57 |
} else { |
| 58 |
$stage_html = $result['html']; |
| 59 |
$stage = $result['stage']; |
| 60 |
} |
| 61 |
|
| 62 |
static $instance_number = 0; |
| 63 |
++$instance_number; |
| 64 |
$instance_id = 'wpbc_booking_appointment_' . $instance_number; |
| 65 |
$html = '<div id="' . esc_attr( $instance_id ) . '" class="wpbc_booking_appointment" data-appointment-stage="' . esc_attr( $stage ) . '" data-config-token="' . esc_attr( wpbc_booking_appointment_encode_config( $config ) ) . '" aria-busy="false">'; |
| 66 |
$html .= '<div class="wpbc_booking_appointment__stage wpbc_booking_ui_theme_scope" aria-live="polite" aria-busy="false">' . $stage_html . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 67 |
$html .= '<div class="wpbc_booking_appointment__loading" role="status" aria-live="polite" hidden aria-hidden="true"><span class="wpbc_booking_appointment__spinner" aria-hidden="true"></span><span>' . esc_html__( 'Loading appointment…', 'booking' ) . '</span></div>'; |
| 68 |
$html .= '<div class="wpbc_booking_appointment__ajax_notice wpbc_booking_ui_theme_scope" role="alert" aria-live="assertive" tabindex="-1" hidden></div></div>'; |
| 69 |
|
| 70 |
return (string) apply_filters( 'wpbc_booking_appointment_shortcode_html', $html, $config, $result ); |
| 71 |
} |
| 72 |
add_shortcode( 'booking_appointment', 'wpbc_booking_appointment_shortcode' ); |
| 73 |
|
| 74 |
/** |
| 75 |
* Enqueue the Appointment selector controller on public Booking Calendar pages. |
| 76 |
* |
| 77 |
* @param string $where_to_load Booking Calendar asset context. |
| 78 |
* |
| 79 |
* @return void |
| 80 |
*/ |
| 81 |
function wpbc_booking_appointment_enqueue_js( $where_to_load ) { |
| 82 |
if ( ! in_array( $where_to_load, array( 'client', 'both' ), true ) ) { |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
$base_url = trailingslashit( plugins_url( '', __FILE__ ) ); |
| 87 |
wp_enqueue_script( 'wpbc-booking-appointment', $base_url . '_out/booking-appointment.js', array( 'jquery', 'wpbc_capacity' ), WP_BK_VERSION_NUM, array( 'in_footer' => WPBC_JS_IN_FOOTER ) ); |
| 88 |
wp_localize_script( |
| 89 |
'wpbc-booking-appointment', |
| 90 |
'wpbc_booking_appointment_config', |
| 91 |
array( |
| 92 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 93 |
'action' => 'WPBC_AJX_BOOKING_APPOINTMENT_RESOLVE', |
| 94 |
'validate_action' => 'WPBC_AJX_BOOKING_APPOINTMENT_VALIDATE_TIME', |
| 95 |
'nonce' => wp_create_nonce( 'wpbc_booking_appointment_ajax' ), |
| 96 |
'error' => __( 'The Appointment form could not be loaded. Please try again.', 'booking' ), |
| 97 |
'validation_error'=> __( 'This Appointment time could not be verified. Please try again.', 'booking' ), |
| 98 |
'initialization_error' => __( 'The Appointment form could not be initialized. Please start over and try again.', 'booking' ), |
| 99 |
'duplicate_provider' => __( 'This Provider already has an open Appointment form on this page. Complete or restart that Appointment before opening another form for the same Provider.', 'booking' ), |
| 100 |
'service_found' => __( 'Service found.', 'booking' ), |
| 101 |
'services_found' => __( 'Services found.', 'booking' ), |
| 102 |
'provider_found' => __( 'Provider found.', 'booking' ), |
| 103 |
'providers_found' => __( 'Providers found.', 'booking' ), |
| 104 |
) |
| 105 |
); |
| 106 |
} |
| 107 |
add_action( 'wpbc_enqueue_js_files', 'wpbc_booking_appointment_enqueue_js', 72 ); |
| 108 |
|
| 109 |
/** |
| 110 |
* Enqueue Appointment selector styling on public Booking Calendar pages. |
| 111 |
* |
| 112 |
* @param string $where_to_load Booking Calendar asset context. |
| 113 |
* |
| 114 |
* @return void |
| 115 |
*/ |
| 116 |
function wpbc_booking_appointment_enqueue_css( $where_to_load ) { |
| 117 |
$is_add_appointment_admin_page = 'admin' === $where_to_load |
| 118 |
&& function_exists( 'wpbc_add_appointment_page_is_active' ) |
| 119 |
&& wpbc_add_appointment_page_is_active(); |
| 120 |
|
| 121 |
if ( ! in_array( $where_to_load, array( 'client', 'both' ), true ) && ! $is_add_appointment_admin_page ) { |
| 122 |
return; |
| 123 |
} |
| 124 |
|
| 125 |
wp_enqueue_style( 'wpbc-booking-appointment', trailingslashit( plugins_url( '', __FILE__ ) ) . '_out/booking-appointment.css', array( 'wpbc-all-client' ), WP_BK_VERSION_NUM ); |
| 126 |
|
| 127 |
$theme_css = wpbc_booking_appointment_build_theme_css(); |
| 128 |
if ( '' !== $theme_css && class_exists( 'WPBC_FE_Assets' ) ) { |
| 129 |
WPBC_FE_Assets::add_inline_css_to_wp_style( 'wpbc-booking-appointment', $theme_css, 'booking-appointment-theme' ); |
| 130 |
} |
| 131 |
} |
| 132 |
add_action( 'wpbc_enqueue_css_files', 'wpbc_booking_appointment_enqueue_css', 72 ); |
| 133 |
|