PluginProbe
Booking Calendar / 11.5
Booking Calendar v11.5
11.8.4 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 All 204 releases
booking / includes / booking-appointment / booking-appointment__shortcode.php

booking-appointment__shortcode.php in Booking Calendar 11.5, at includes/booking-appointment/booking-appointment__shortcode.php

125 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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, show_progress, progress_item_1_title,
33 * progress_item_1_number through progress_item_3_title/progress_item_3_number,
34 * screen_1_title, screen_1_description, screen_2_title, screen_2_description,
35 * and allow_past. Provider auto-selection is disabled unless
36 * auto_select_provider is explicitly enabled. Past bookings are available to
37 * public visitors only when allow_past is explicitly enabled by the shortcode.
38 *
39 * @param mixed $attributes Shortcode attributes.
40 *
41 * @return string Appointment selector or fixed native booking form.
42 */
43 function wpbc_booking_appointment_shortcode( $attributes = array() ) {
44 $config = wpbc_booking_appointment_normalize_config( $attributes );
45 $config['return_url'] = wpbc_booking_appointment_get_fallback_url();
46 $service_id = wpbc_booking_appointment_get_fallback_selection( 'wpbc_appointment_service' );
47 $provider_id = wpbc_booking_appointment_get_fallback_selection( 'wpbc_appointment_provider' );
48 $result = wpbc_booking_appointment_resolve_stage( $config, $service_id, $provider_id );
49
50 if ( is_wp_error( $result ) ) {
51 $stage_html = wpbc_booking_appointment_render_error_notice( $result );
52 $stage = 'error';
53 } else {
54 $stage_html = $result['html'];
55 $stage = $result['stage'];
56 }
57
58 static $instance_number = 0;
59 ++$instance_number;
60 $instance_id = 'wpbc_booking_appointment_' . $instance_number;
61 $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">';
62 $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
63 $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>';
64 $html .= '<div class="wpbc_booking_appointment__ajax_notice wpbc_booking_ui_theme_scope" role="alert" aria-live="assertive" tabindex="-1" hidden></div></div>';
65
66 return (string) apply_filters( 'wpbc_booking_appointment_shortcode_html', $html, $config, $result );
67 }
68 add_shortcode( 'booking_appointment', 'wpbc_booking_appointment_shortcode' );
69
70 /**
71 * Enqueue the Appointment selector controller on public Booking Calendar pages.
72 *
73 * @param string $where_to_load Booking Calendar asset context.
74 *
75 * @return void
76 */
77 function wpbc_booking_appointment_enqueue_js( $where_to_load ) {
78 if ( ! in_array( $where_to_load, array( 'client', 'both' ), true ) ) {
79 return;
80 }
81
82 $base_url = trailingslashit( plugins_url( '', __FILE__ ) );
83 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 ) );
84 wp_localize_script(
85 'wpbc-booking-appointment',
86 'wpbc_booking_appointment_config',
87 array(
88 'ajax_url' => admin_url( 'admin-ajax.php' ),
89 'action' => 'WPBC_AJX_BOOKING_APPOINTMENT_RESOLVE',
90 'validate_action' => 'WPBC_AJX_BOOKING_APPOINTMENT_VALIDATE_TIME',
91 'nonce' => wp_create_nonce( 'wpbc_booking_appointment_ajax' ),
92 'error' => __( 'The Appointment form could not be loaded. Please try again.', 'booking' ),
93 'validation_error'=> __( 'This Appointment time could not be verified. Please try again.', 'booking' ),
94 'initialization_error' => __( 'The Appointment form could not be initialized. Please start over and try again.', 'booking' ),
95 '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' ),
96 )
97 );
98 }
99 add_action( 'wpbc_enqueue_js_files', 'wpbc_booking_appointment_enqueue_js', 72 );
100
101 /**
102 * Enqueue Appointment selector styling on public Booking Calendar pages.
103 *
104 * @param string $where_to_load Booking Calendar asset context.
105 *
106 * @return void
107 */
108 function wpbc_booking_appointment_enqueue_css( $where_to_load ) {
109 $is_add_appointment_admin_page = 'admin' === $where_to_load
110 && function_exists( 'wpbc_add_appointment_page_is_active' )
111 && wpbc_add_appointment_page_is_active();
112
113 if ( ! in_array( $where_to_load, array( 'client', 'both' ), true ) && ! $is_add_appointment_admin_page ) {
114 return;
115 }
116
117 wp_enqueue_style( 'wpbc-booking-appointment', trailingslashit( plugins_url( '', __FILE__ ) ) . '_out/booking-appointment.css', array( 'wpbc-all-client' ), WP_BK_VERSION_NUM );
118
119 $theme_css = wpbc_booking_appointment_build_theme_css();
120 if ( '' !== $theme_css && class_exists( 'WPBC_FE_Assets' ) ) {
121 WPBC_FE_Assets::add_inline_css_to_wp_style( 'wpbc-booking-appointment', $theme_css, 'booking-appointment-theme' );
122 }
123 }
124 add_action( 'wpbc_enqueue_css_files', 'wpbc_booking_appointment_enqueue_css', 72 );
125