| 1 |
<?php |
| 2 |
/** |
| 3 |
* Booking Resource selector shortcode registration and public assets. |
| 4 |
* |
| 5 |
* @package Booking Calendar |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Read one resource selection from the non-JavaScript GET fallback. |
| 14 |
* |
| 15 |
* @return int Selected Booking Resource ID or zero. |
| 16 |
*/ |
| 17 |
function wpbc_booking_resource_selector_get_fallback_selection() { |
| 18 |
if ( ! isset( $_GET['wpbc_resource_selector_resource'] ) || is_array( $_GET['wpbc_resource_selector_resource'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 19 |
return 0; |
| 20 |
} |
| 21 |
|
| 22 |
return absint( wp_unslash( $_GET['wpbc_resource_selector_resource'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Detect an explicit non-JavaScript request to return to Resource selection. |
| 27 |
* |
| 28 |
* @return bool True when the current URL requests the first selector stage. |
| 29 |
*/ |
| 30 |
function wpbc_booking_resource_selector_is_fallback_start_over() { |
| 31 |
if ( ! isset( $_GET['wpbc_resource_selector_start_over'] ) || is_array( $_GET['wpbc_resource_selector_start_over'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 32 |
return false; |
| 33 |
} |
| 34 |
|
| 35 |
return '1' === sanitize_text_field( wp_unslash( $_GET['wpbc_resource_selector_start_over'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Render the [booking_resource_selector] resource-first booking workflow. |
| 40 |
* |
| 41 |
* @param mixed $attributes Shortcode attributes. |
| 42 |
* |
| 43 |
* @return string Resource selector or selected native Booking Form. |
| 44 |
*/ |
| 45 |
function wpbc_booking_resource_selector_shortcode( $attributes = array() ) { |
| 46 |
$config = wpbc_booking_resource_selector_normalize_config( $attributes ); |
| 47 |
$config['return_url'] = wpbc_booking_resource_selector_get_fallback_url(); |
| 48 |
$resource_id = wpbc_booking_resource_selector_get_fallback_selection(); |
| 49 |
$result = wpbc_booking_resource_selector_resolve_stage( $config, $resource_id, ! wpbc_booking_resource_selector_is_fallback_start_over() ); |
| 50 |
|
| 51 |
if ( is_wp_error( $result ) ) { |
| 52 |
$stage_html = wpbc_booking_resource_selector_render_error_notice( $result ); |
| 53 |
$stage = 'error'; |
| 54 |
} else { |
| 55 |
$stage_html = $result['html']; |
| 56 |
$stage = $result['stage']; |
| 57 |
} |
| 58 |
|
| 59 |
static $instance_number = 0; |
| 60 |
++$instance_number; |
| 61 |
$instance_id = 'wpbc_booking_resource_selector_' . $instance_number; |
| 62 |
$html = '<div id="' . esc_attr( $instance_id ) . '" class="wpbc_booking_resource_selector" data-resource-selector-stage="' . esc_attr( $stage ) . '" data-config-token="' . esc_attr( wpbc_booking_resource_selector_encode_config( $config ) ) . '" data-selected-resource-id="' . wpbc_booking_resource_selector_get_default_resource_id( $config ) . '" aria-busy="false">'; |
| 63 |
$html .= '<div class="wpbc_booking_resource_selector__stage wpbc_booking_ui_theme_scope" aria-live="polite" aria-busy="false">' . $stage_html . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 64 |
$html .= '<div class="wpbc_booking_resource_selector__loading" role="status" aria-live="polite" hidden aria-hidden="true"><span class="wpbc_booking_resource_selector__spinner" aria-hidden="true"></span><span>' . esc_html__( 'Loading booking form...', 'booking' ) . '</span></div>'; |
| 65 |
$html .= '<div class="wpbc_booking_resource_selector__ajax_notice wpbc_booking_ui_theme_scope" role="alert" aria-live="assertive" tabindex="-1" hidden></div></div>'; |
| 66 |
|
| 67 |
return (string) apply_filters( 'wpbc_booking_resource_selector_shortcode_html', $html, $config, $result ); |
| 68 |
} |
| 69 |
add_shortcode( 'booking_resource_selector', 'wpbc_booking_resource_selector_shortcode' ); |
| 70 |
|
| 71 |
/** |
| 72 |
* Enqueue the Booking Resource selector controller on public pages. |
| 73 |
* |
| 74 |
* @param string $where_to_load Booking Calendar asset context. |
| 75 |
* |
| 76 |
* @return void |
| 77 |
*/ |
| 78 |
function wpbc_booking_resource_selector_enqueue_js( $where_to_load ) { |
| 79 |
if ( ! in_array( $where_to_load, array( 'client', 'both' ), true ) ) { |
| 80 |
return; |
| 81 |
} |
| 82 |
|
| 83 |
$base_url = trailingslashit( plugins_url( '', __FILE__ ) ); |
| 84 |
wp_enqueue_script( 'wpbc-booking-resource-selector', $base_url . '_out/booking-resource-selector.js', array( 'jquery', 'wpbc_capacity' ), WP_BK_VERSION_NUM, array( 'in_footer' => WPBC_JS_IN_FOOTER ) ); |
| 85 |
wp_localize_script( |
| 86 |
'wpbc-booking-resource-selector', |
| 87 |
'wpbc_booking_resource_selector_config', |
| 88 |
array( |
| 89 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 90 |
'action' => 'WPBC_AJX_BOOKING_RESOURCE_SELECTOR_RESOLVE', |
| 91 |
'nonce' => wp_create_nonce( 'wpbc_booking_resource_selector_ajax' ), |
| 92 |
'error' => __( 'The booking form could not be loaded. Please try again.', 'booking' ), |
| 93 |
'initialization_error' => __( 'The booking form could not be initialized. Please start over and try again.', 'booking' ), |
| 94 |
'duplicate_resource' => __( 'This Booking Resource already has an open booking form on this page. Complete or restart that booking before opening another form for the same resource.', 'booking' ), |
| 95 |
'resource_found' => __( 'Booking Resource found.', 'booking' ), |
| 96 |
'resources_found' => __( 'Booking Resources found.', 'booking' ), |
| 97 |
) |
| 98 |
); |
| 99 |
} |
| 100 |
add_action( 'wpbc_enqueue_js_files', 'wpbc_booking_resource_selector_enqueue_js', 73 ); |
| 101 |
|
| 102 |
/** |
| 103 |
* Enqueue Booking Resource selector styling on public pages. |
| 104 |
* |
| 105 |
* @param string $where_to_load Booking Calendar asset context. |
| 106 |
* |
| 107 |
* @return void |
| 108 |
*/ |
| 109 |
function wpbc_booking_resource_selector_enqueue_css( $where_to_load ) { |
| 110 |
if ( ! in_array( $where_to_load, array( 'client', 'both' ), true ) ) { |
| 111 |
return; |
| 112 |
} |
| 113 |
|
| 114 |
wp_enqueue_style( 'wpbc-booking-resource-selector', trailingslashit( plugins_url( '', __FILE__ ) ) . '_out/booking-resource-selector.css', array( 'wpbc-all-client' ), WP_BK_VERSION_NUM ); |
| 115 |
|
| 116 |
$theme_css = wpbc_booking_resource_selector_build_theme_css(); |
| 117 |
if ( '' !== $theme_css && class_exists( 'WPBC_FE_Assets' ) ) { |
| 118 |
WPBC_FE_Assets::add_inline_css_to_wp_style( 'wpbc-booking-resource-selector', $theme_css, 'booking-resource-selector-theme' ); |
| 119 |
} |
| 120 |
} |
| 121 |
add_action( 'wpbc_enqueue_css_files', 'wpbc_booking_resource_selector_enqueue_css', 73 ); |
| 122 |
|