| 1 |
/** |
| 2 |
* First-install Setup Wizard and starter-pages prompt adapter. |
| 3 |
* |
| 4 |
* @package Booking Calendar |
| 5 |
* @since 11.8.1 |
| 6 |
*/ |
| 7 |
( function( window, document, $ ) { |
| 8 |
'use strict'; |
| 9 |
|
| 10 |
var config = window.wpbc_setup_wizard_first_run_popup_vars || {}; |
| 11 |
var setup_prompt = config.setup_prompt || {}; |
| 12 |
var booking_pages_prompt = config.booking_pages_prompt || {}; |
| 13 |
var setup_request = null; |
| 14 |
var booking_pages_request = null; |
| 15 |
var booking_pages_dialog_started = false; |
| 16 |
var redirect_started = false; |
| 17 |
var previous_focus = null; |
| 18 |
|
| 19 |
/** |
| 20 |
* Persist the one-time Setup prompt choice without delaying dismissal. |
| 21 |
* |
| 22 |
* @param {string} choice Choice identifier: start or dismiss. |
| 23 |
* @return {jqXHR|null} Active persistence request, or null when unavailable. |
| 24 |
*/ |
| 25 |
function persist_setup_choice( choice ) { |
| 26 |
if ( setup_request ) { |
| 27 |
return setup_request; |
| 28 |
} |
| 29 |
|
| 30 |
if ( ! config.ajax_url || ! setup_prompt.action || ! setup_prompt.nonce ) { |
| 31 |
return null; |
| 32 |
} |
| 33 |
|
| 34 |
setup_request = $.post( config.ajax_url, { |
| 35 |
action: setup_prompt.action, |
| 36 |
nonce: setup_prompt.nonce, |
| 37 |
choice: choice |
| 38 |
} ); |
| 39 |
|
| 40 |
return setup_request; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Persist permanent dismissal of the starter booking-pages prompt. |
| 45 |
* |
| 46 |
* @return {jqXHR|null} Active persistence request, or null when unavailable. |
| 47 |
*/ |
| 48 |
function persist_booking_pages_dismissal() { |
| 49 |
if ( booking_pages_request ) { |
| 50 |
return booking_pages_request; |
| 51 |
} |
| 52 |
|
| 53 |
if ( ! config.ajax_url || ! booking_pages_prompt.action || ! booking_pages_prompt.nonce ) { |
| 54 |
return null; |
| 55 |
} |
| 56 |
|
| 57 |
booking_pages_request = $.post( config.ajax_url, { |
| 58 |
action: booking_pages_prompt.action, |
| 59 |
nonce: booking_pages_prompt.nonce |
| 60 |
} ); |
| 61 |
|
| 62 |
return booking_pages_request; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Restore useful page focus after the automatic dialog closes. |
| 67 |
* |
| 68 |
* @return {void} |
| 69 |
*/ |
| 70 |
function restore_focus() { |
| 71 |
var fallback_focus; |
| 72 |
|
| 73 |
if ( previous_focus && document.body !== previous_focus && document.contains( previous_focus ) ) { |
| 74 |
previous_focus.focus(); |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
fallback_focus = document.querySelector( '#wpbc_booking_listing_add_booking_button, .wpbc_ui_el__top_nav a, #wpbody-content h1' ); |
| 79 |
if ( fallback_focus && 'function' === typeof fallback_focus.focus ) { |
| 80 |
fallback_focus.focus(); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Confirm that an AJAX response represents a saved WordPress choice. |
| 86 |
* |
| 87 |
* @param {Object} response WordPress AJAX response. |
| 88 |
* @return {boolean} True when the server saved the requested state. |
| 89 |
*/ |
| 90 |
function is_success_response( response ) { |
| 91 |
return !! ( response && true === response.success ); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Check whether the starter-pages dialog has usable preloaded data. |
| 96 |
* |
| 97 |
* @return {boolean} True when the dialog can be presented. |
| 98 |
*/ |
| 99 |
function can_show_booking_pages_dialog() { |
| 100 |
return !! ( |
| 101 |
! booking_pages_dialog_started && |
| 102 |
booking_pages_prompt.available && |
| 103 |
Array.isArray( booking_pages_prompt.booking_pages ) && |
| 104 |
booking_pages_prompt.booking_pages.length && |
| 105 |
document.getElementById( 'tmpl-wpbc-setup-wizard-booking-pages-popup' ) |
| 106 |
); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Create and show the WordPress-templated starter booking-pages dialog. |
| 111 |
* |
| 112 |
* @param {boolean} should_consume_prompt Whether closing consumes automatic display. |
| 113 |
* @return {void} |
| 114 |
*/ |
| 115 |
function show_booking_pages_dialog( should_consume_prompt ) { |
| 116 |
var template; |
| 117 |
var $modal; |
| 118 |
var dismissal_started = false; |
| 119 |
|
| 120 |
if ( |
| 121 |
! can_show_booking_pages_dialog() || |
| 122 |
! window.wp || 'function' !== typeof window.wp.template || |
| 123 |
'function' !== typeof $.fn.wpbc_my_modal |
| 124 |
) { |
| 125 |
restore_focus(); |
| 126 |
return; |
| 127 |
} |
| 128 |
|
| 129 |
booking_pages_dialog_started = true; |
| 130 |
template = window.wp.template( 'wpbc-setup-wizard-booking-pages-popup' ); |
| 131 |
$( document.body ).append( template( { booking_pages: booking_pages_prompt.booking_pages } ) ); |
| 132 |
$modal = $( '#wpbc_setup_wizard_booking_pages_popup' ); |
| 133 |
$modal.find( '.wpbc_setup_wizard_booking_pages_popup__image img' ).on( 'error.wpbcSetupBookingPages', function() { |
| 134 |
$( this ).closest( '.wpbc_setup_wizard_booking_pages_popup__image' ).remove(); |
| 135 |
} ); |
| 136 |
|
| 137 |
$modal.on( 'shown.wpbc.modal.wpbcSetupBookingPages', function() { |
| 138 |
$modal.attr( 'aria-hidden', 'false' ); |
| 139 |
$( '.modal-backdrop' ).last().addClass( 'wpbc_setup_wizard_first_run_popup__backdrop' ); |
| 140 |
$modal.find( '[data-wpbc-booking-pages-open]' ).first().trigger( 'focus' ); |
| 141 |
} ); |
| 142 |
|
| 143 |
$modal.on( 'hide.wpbc.modal.wpbcSetupBookingPages', function() { |
| 144 |
if ( should_consume_prompt && ! dismissal_started ) { |
| 145 |
dismissal_started = true; |
| 146 |
persist_booking_pages_dismissal(); |
| 147 |
} |
| 148 |
} ); |
| 149 |
|
| 150 |
$modal.on( 'hidden.wpbc.modal.wpbcSetupBookingPages', function() { |
| 151 |
$modal.attr( 'aria-hidden', 'true' ).remove(); |
| 152 |
booking_pages_dialog_started = false; |
| 153 |
restore_focus(); |
| 154 |
} ); |
| 155 |
|
| 156 |
$modal.on( 'click.wpbcSetupBookingPages', '[data-wpbc-booking-pages-dismiss]', function( event ) { |
| 157 |
event.preventDefault(); |
| 158 |
$modal.wpbc_my_modal( 'hide' ); |
| 159 |
} ); |
| 160 |
|
| 161 |
$modal.wpbc_my_modal( { |
| 162 |
backdrop: 'static', |
| 163 |
keyboard: true, |
| 164 |
show: true |
| 165 |
} ); |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Create and show the WordPress-templated first-run Setup dialog. |
| 170 |
* |
| 171 |
* @return {void} |
| 172 |
*/ |
| 173 |
function show_setup_dialog() { |
| 174 |
var template; |
| 175 |
var $modal; |
| 176 |
var choice_started = false; |
| 177 |
var selected_choice = ''; |
| 178 |
var dismissal_request_finished = false; |
| 179 |
var dismissal_saved = false; |
| 180 |
var setup_dialog_hidden = false; |
| 181 |
|
| 182 |
/** |
| 183 |
* Continue only after both AJAX persistence and modal removal finish. |
| 184 |
* |
| 185 |
* @return {void} |
| 186 |
*/ |
| 187 |
function finish_setup_dismissal() { |
| 188 |
if ( ! dismissal_request_finished || ! setup_dialog_hidden ) { |
| 189 |
return; |
| 190 |
} |
| 191 |
|
| 192 |
if ( dismissal_saved && can_show_booking_pages_dialog() ) { |
| 193 |
show_booking_pages_dialog( true ); |
| 194 |
return; |
| 195 |
} |
| 196 |
|
| 197 |
restore_focus(); |
| 198 |
} |
| 199 |
|
| 200 |
if ( |
| 201 |
! config.ajax_url || ! setup_prompt.action || ! setup_prompt.nonce || ! setup_prompt.setup_url || |
| 202 |
! window.wp || 'function' !== typeof window.wp.template || 'function' !== typeof $.fn.wpbc_my_modal || |
| 203 |
! document.getElementById( 'tmpl-wpbc-setup-wizard-first-run-popup' ) |
| 204 |
) { |
| 205 |
return; |
| 206 |
} |
| 207 |
|
| 208 |
template = window.wp.template( 'wpbc-setup-wizard-first-run-popup' ); |
| 209 |
$( document.body ).append( template( {} ) ); |
| 210 |
$modal = $( '#wpbc_setup_wizard_first_run_popup' ); |
| 211 |
|
| 212 |
$modal.on( 'shown.wpbc.modal.wpbcSetupFirstRun', function() { |
| 213 |
$modal.attr( 'aria-hidden', 'false' ); |
| 214 |
$( '.modal-backdrop' ).last().addClass( 'wpbc_setup_wizard_first_run_popup__backdrop' ); |
| 215 |
$modal.find( '[data-wpbc-setup-first-run-start]' ).trigger( 'focus' ); |
| 216 |
} ); |
| 217 |
|
| 218 |
$modal.on( 'hide.wpbc.modal.wpbcSetupFirstRun', function() { |
| 219 |
var request; |
| 220 |
|
| 221 |
if ( ! choice_started ) { |
| 222 |
choice_started = true; |
| 223 |
selected_choice = 'dismiss'; |
| 224 |
request = persist_setup_choice( 'dismiss' ); |
| 225 |
if ( request ) { |
| 226 |
request.done( function( response ) { |
| 227 |
dismissal_saved = is_success_response( response ); |
| 228 |
} ); |
| 229 |
request.always( function() { |
| 230 |
dismissal_request_finished = true; |
| 231 |
finish_setup_dismissal(); |
| 232 |
} ); |
| 233 |
} else { |
| 234 |
dismissal_request_finished = true; |
| 235 |
} |
| 236 |
} |
| 237 |
} ); |
| 238 |
|
| 239 |
$modal.on( 'hidden.wpbc.modal.wpbcSetupFirstRun', function() { |
| 240 |
$modal.attr( 'aria-hidden', 'true' ).remove(); |
| 241 |
setup_dialog_hidden = true; |
| 242 |
restore_focus(); |
| 243 |
if ( 'dismiss' === selected_choice ) { |
| 244 |
finish_setup_dismissal(); |
| 245 |
} |
| 246 |
} ); |
| 247 |
|
| 248 |
$modal.on( 'click.wpbcSetupFirstRun', '[data-wpbc-setup-first-run-dismiss]', function( event ) { |
| 249 |
event.preventDefault(); |
| 250 |
$modal.wpbc_my_modal( 'hide' ); |
| 251 |
} ); |
| 252 |
|
| 253 |
$modal.on( 'click.wpbcSetupFirstRun', '[data-wpbc-setup-first-run-start]', function( event ) { |
| 254 |
var request; |
| 255 |
|
| 256 |
event.preventDefault(); |
| 257 |
choice_started = true; |
| 258 |
selected_choice = 'start'; |
| 259 |
request = persist_setup_choice( 'start' ); |
| 260 |
if ( request ) { |
| 261 |
request.always( function() { |
| 262 |
if ( ! redirect_started ) { |
| 263 |
redirect_started = true; |
| 264 |
window.location.assign( setup_prompt.setup_url ); |
| 265 |
} |
| 266 |
} ); |
| 267 |
} else if ( ! redirect_started ) { |
| 268 |
redirect_started = true; |
| 269 |
window.location.assign( setup_prompt.setup_url ); |
| 270 |
} |
| 271 |
$modal.wpbc_my_modal( 'hide' ); |
| 272 |
} ); |
| 273 |
|
| 274 |
$modal.wpbc_my_modal( { |
| 275 |
backdrop: 'static', |
| 276 |
keyboard: true, |
| 277 |
show: true |
| 278 |
} ); |
| 279 |
} |
| 280 |
|
| 281 |
/** |
| 282 |
* Start the first eligible dialog without allowing both to overlap. |
| 283 |
* |
| 284 |
* @return {void} |
| 285 |
*/ |
| 286 |
function initialize_prompts() { |
| 287 |
previous_focus = document.activeElement; |
| 288 |
|
| 289 |
if ( setup_prompt.show ) { |
| 290 |
show_setup_dialog(); |
| 291 |
return; |
| 292 |
} |
| 293 |
|
| 294 |
if ( booking_pages_prompt.show ) { |
| 295 |
show_booking_pages_dialog( true ); |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
/** |
| 300 |
* Reopen the starter-pages dialog from an explicit Booking Listing control. |
| 301 |
* |
| 302 |
* Manual opening never re-enables or consumes automatic first-run display. |
| 303 |
* Focus returns to the invoking button after the dialog closes. |
| 304 |
* |
| 305 |
* @param {Event} event Browser click event. |
| 306 |
* @return {void} |
| 307 |
*/ |
| 308 |
function open_booking_pages_dialog_manually( event ) { |
| 309 |
event.preventDefault(); |
| 310 |
previous_focus = event.currentTarget; |
| 311 |
show_booking_pages_dialog( false ); |
| 312 |
} |
| 313 |
|
| 314 |
$( document ).on( 'click.wpbcSetupBookingPagesLauncher', '[data-wpbc-booking-pages-open-dialog]', open_booking_pages_dialog_manually ); |
| 315 |
$( initialize_prompts ); |
| 316 |
}( window, document, window.jQuery ) ); |
| 317 |
|