| 1 |
<?php |
| 2 |
/** |
| 3 |
* BFB Admin Templates: "Publish" modal. |
| 4 |
* |
| 5 |
* Prints the modal window for embedding the current booking form shortcode |
| 6 |
* into an existing page or creating a new page via AJAX. |
| 7 |
* |
| 8 |
* Responsibilities: |
| 9 |
* - Enqueue bfb-publish.js on the Form Builder page. |
| 10 |
* - Localize AJAX config and UI strings. |
| 11 |
* - Print the modal HTML in the admin footer. |
| 12 |
* |
| 13 |
* @package Booking Calendar |
| 14 |
* @subpackage Booking Form Builder |
| 15 |
* @since 11.0.0 |
| 16 |
* @file ../includes/page-form-builder/publish/tpl-modal__publish.php |
| 17 |
*/ |
| 18 |
|
| 19 |
if ( ! defined( 'ABSPATH' ) ) { |
| 20 |
exit; |
| 21 |
} |
| 22 |
|
| 23 |
class WPBC_BFB_Tpl__Publish { |
| 24 |
|
| 25 |
const SCRIPT_HANDLE = 'wpbc-bfb-publish'; |
| 26 |
const MODAL_DOM_ID = 'wpbc_bfb_modal__publish'; |
| 27 |
|
| 28 |
/** |
| 29 |
* Init hooks. |
| 30 |
* |
| 31 |
* @return void |
| 32 |
*/ |
| 33 |
public static function init() { |
| 34 |
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_assets' ) ); |
| 35 |
add_action( 'wpbc_hook_settings_page_footer', array( __CLASS__, 'print_modal' ), 10, 1 ); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Check whether current admin page is the Form Builder page. |
| 40 |
* |
| 41 |
* @param string $page_name Optional page name from custom footer hook. |
| 42 |
* |
| 43 |
* @return bool |
| 44 |
*/ |
| 45 |
private static function is_builder_page( $page_name = '' ) { |
| 46 |
|
| 47 |
if ( function_exists( 'wpbc_is_builder_booking_form_page' ) && wpbc_is_builder_booking_form_page() ) { |
| 48 |
return true; |
| 49 |
} |
| 50 |
|
| 51 |
if ( ! empty( $page_name ) ) { |
| 52 |
$page_name = (string) $page_name; |
| 53 |
|
| 54 |
if ( |
| 55 |
( false !== strpos( $page_name, 'form-builder' ) ) || |
| 56 |
( false !== strpos( $page_name, 'form_builder' ) ) || |
| 57 |
( false !== strpos( $page_name, 'booking_form' ) ) |
| 58 |
) { |
| 59 |
return true; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
return false; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Get JS file URL. |
| 68 |
* |
| 69 |
* @return string |
| 70 |
*/ |
| 71 |
private static function get_script_url() { |
| 72 |
return plugins_url( '_out/bfb-publish.js', __FILE__ ); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Get JS file version. |
| 77 |
* |
| 78 |
* @return string |
| 79 |
*/ |
| 80 |
private static function get_script_version() { |
| 81 |
|
| 82 |
$script_path = dirname( __FILE__ ) . '/_out/bfb-publish.js'; |
| 83 |
|
| 84 |
if ( file_exists( $script_path ) ) { |
| 85 |
return (string) filemtime( $script_path ); |
| 86 |
} |
| 87 |
|
| 88 |
return '1.0.0'; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Get AJAX action name. |
| 93 |
* |
| 94 |
* @return string |
| 95 |
*/ |
| 96 |
private static function get_ajax_action_name() { |
| 97 |
|
| 98 |
if ( class_exists( 'WPBC_BFB_Publish_Ajax' ) ) { |
| 99 |
return WPBC_BFB_Publish_Ajax::ACTION; |
| 100 |
} |
| 101 |
|
| 102 |
return 'WPBC_AJX_BFB_PUBLISH_FORM'; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Get AJAX nonce action name. |
| 107 |
* |
| 108 |
* @return string |
| 109 |
*/ |
| 110 |
private static function get_ajax_nonce_action_name() { |
| 111 |
|
| 112 |
if ( class_exists( 'WPBC_BFB_Publish_Ajax' ) ) { |
| 113 |
return WPBC_BFB_Publish_Ajax::NONCE_ACTION; |
| 114 |
} |
| 115 |
|
| 116 |
return 'wpbc_bfb_publish_form'; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Get default resource ID. |
| 121 |
* |
| 122 |
* @return int |
| 123 |
*/ |
| 124 |
private static function get_default_resource_id() { |
| 125 |
|
| 126 |
if ( function_exists( 'wpbc_get_default_resource' ) ) { |
| 127 |
return absint( wpbc_get_default_resource() ); |
| 128 |
} |
| 129 |
|
| 130 |
return 1; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Get default form name. |
| 135 |
* |
| 136 |
* @return string |
| 137 |
*/ |
| 138 |
private static function get_default_form_name() { |
| 139 |
|
| 140 |
$form_name = 'standard'; |
| 141 |
|
| 142 |
/** |
| 143 |
* Filter default BFB publish form name. |
| 144 |
* |
| 145 |
* @param string $form_name Default form name. |
| 146 |
*/ |
| 147 |
$form_name = apply_filters( 'wpbc_bfb_publish_default_form_name', $form_name ); |
| 148 |
|
| 149 |
$form_name = sanitize_key( $form_name ); |
| 150 |
|
| 151 |
if ( empty( $form_name ) ) { |
| 152 |
$form_name = 'standard'; |
| 153 |
} |
| 154 |
|
| 155 |
return $form_name; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Get default raw shortcode for publishing. |
| 160 |
* |
| 161 |
* Important: |
| 162 |
* - This must be RAW shortcode only, without block comments. |
| 163 |
* - The AJAX controller wraps it into Gutenberg shortcode block comments. |
| 164 |
* |
| 165 |
* @param int $resource_id Booking resource ID. |
| 166 |
* @param string $form_name Form name. |
| 167 |
* |
| 168 |
* @return string |
| 169 |
*/ |
| 170 |
private static function get_default_shortcode_raw( $resource_id, $form_name ) { |
| 171 |
|
| 172 |
$resource_id = absint( $resource_id ); |
| 173 |
$form_name = sanitize_key( $form_name ); |
| 174 |
|
| 175 |
if ( empty( $form_name ) ) { |
| 176 |
$form_name = 'standard'; |
| 177 |
} |
| 178 |
|
| 179 |
$shortcode_raw = "[booking resource_id={$resource_id} form_type='{$form_name}']"; |
| 180 |
|
| 181 |
/** |
| 182 |
* Filter default raw shortcode used by BFB Publish modal. |
| 183 |
* |
| 184 |
* @param string $shortcode_raw Raw shortcode. |
| 185 |
* @param int $resource_id Booking resource ID. |
| 186 |
* @param string $form_name Form name. |
| 187 |
*/ |
| 188 |
$shortcode_raw = apply_filters( 'wpbc_bfb_publish_default_shortcode_raw', $shortcode_raw, $resource_id, $form_name ); |
| 189 |
|
| 190 |
return trim( $shortcode_raw ); |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Enqueue modal JS and pass AJAX config. |
| 195 |
* |
| 196 |
* @return void |
| 197 |
*/ |
| 198 |
public static function enqueue_assets() { |
| 199 |
|
| 200 |
if ( ! self::is_builder_page() ) { |
| 201 |
return; |
| 202 |
} |
| 203 |
|
| 204 |
$default_resource_id = self::get_default_resource_id(); |
| 205 |
$default_form_name = self::get_default_form_name(); |
| 206 |
$default_shortcode_raw = self::get_default_shortcode_raw( $default_resource_id, $default_form_name ); |
| 207 |
$is_demo = ( function_exists( 'wpbc_is_this_demo' ) && wpbc_is_this_demo() ); |
| 208 |
|
| 209 |
wp_enqueue_script( |
| 210 |
self::SCRIPT_HANDLE, |
| 211 |
self::get_script_url(), |
| 212 |
array( 'jquery' ), |
| 213 |
self::get_script_version(), |
| 214 |
true |
| 215 |
); |
| 216 |
|
| 217 |
wp_localize_script( |
| 218 |
self::SCRIPT_HANDLE, |
| 219 |
'wpbc_bfb_publish_vars', |
| 220 |
array( |
| 221 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 222 |
'action' => self::get_ajax_action_name(), |
| 223 |
'nonce' => wp_create_nonce( self::get_ajax_nonce_action_name() ), |
| 224 |
'modal_selector' => '#' . self::MODAL_DOM_ID, |
| 225 |
'default_resource_id' => $default_resource_id, |
| 226 |
'default_form_name' => $default_form_name, |
| 227 |
'default_shortcode_raw' => $default_shortcode_raw, |
| 228 |
'is_demo' => $is_demo ? 1 : 0, |
| 229 |
'i18n' => array( |
| 230 |
'loading' => __( 'Publishing booking form...', 'booking' ), |
| 231 |
'select_page' => __( 'Please select an existing page.', 'booking' ), |
| 232 |
'enter_page_title' => __( 'Please enter a page title.', 'booking' ), |
| 233 |
'generic_error' => __( 'An unexpected error occurred while publishing the booking form.', 'booking' ), |
| 234 |
'demo_error' => __( 'This operation is restricted in the demo version.', 'booking' ), |
| 235 |
'view_page' => __( 'Open Page', 'booking' ), |
| 236 |
'edit_page' => __( 'Edit Page', 'booking' ), |
| 237 |
'save_step_title' => __( 'Would you like to save the current booking form configuration before publishing?', 'booking' ), |
| 238 |
'save_and_continue' => __( 'Save and Continue', 'booking' ), |
| 239 |
'skip_save' => __( 'Skip', 'booking' ), |
| 240 |
'saving_form' => __( 'Saving booking form...', 'booking' ), |
| 241 |
'save_success' => __( 'Booking form has been saved. Continue with publishing.', 'booking' ), |
| 242 |
'save_timeout' => __( 'Saving is taking longer than expected. You can wait a little longer or use Skip to continue without waiting.', 'booking' ), |
| 243 |
'save_failed' => __( 'Unable to confirm that the booking form was saved.', 'booking' ), |
| 244 |
'save_fn_missing' => __( 'Save function is not available. You can use Skip to continue without saving.', 'booking' ), |
| 245 |
'chooser_title' => __( 'Choose whether to embed your booking form in an existing page or create a new one.', 'booking' ), |
| 246 |
'embed_existing' => __( 'Embed in Existing Page', 'booking' ), |
| 247 |
'create_new' => __( 'Create New Page', 'booking' ), |
| 248 |
), |
| 249 |
) |
| 250 |
); |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* Print Publish modal in admin footer. |
| 255 |
* |
| 256 |
* @param string $page_name Current page name from plugin footer hook. |
| 257 |
* |
| 258 |
* @return void |
| 259 |
*/ |
| 260 |
public static function print_modal( $page_name ) { |
| 261 |
|
| 262 |
if ( ! self::is_builder_page( $page_name ) ) { |
| 263 |
return; |
| 264 |
} |
| 265 |
|
| 266 |
$is_demo = ( function_exists( 'wpbc_is_this_demo' ) && wpbc_is_this_demo() ); |
| 267 |
?> |
| 268 |
<span class="wpdevelop"> |
| 269 |
<div id="<?php echo esc_attr( self::MODAL_DOM_ID ); ?>" class="modal wpbc_popup_modal" tabindex="-1" role="dialog" aria-hidden="true"> |
| 270 |
<style type="text/css"> |
| 271 |
#<?php echo esc_attr( self::MODAL_DOM_ID ); ?> .modal-header .modal-title { |
| 272 |
font-weight: 600; |
| 273 |
} |
| 274 |
#<?php echo esc_attr( self::MODAL_DOM_ID ); ?> .wpbc_bfb_publish__notice { |
| 275 |
margin: 0 0 15px; |
| 276 |
} |
| 277 |
#<?php echo esc_attr( self::MODAL_DOM_ID ); ?> .wpbc_bfb_publish__save_step, |
| 278 |
#<?php echo esc_attr( self::MODAL_DOM_ID ); ?> .wpbc_bfb_publish__chooser, |
| 279 |
#<?php echo esc_attr( self::MODAL_DOM_ID ); ?> .wpbc_bfb_publish__panel { |
| 280 |
margin: 0; |
| 281 |
} |
| 282 |
#<?php echo esc_attr( self::MODAL_DOM_ID ); ?> .wpbc_bfb_publish__save_step_text, |
| 283 |
#<?php echo esc_attr( self::MODAL_DOM_ID ); ?> .wpbc_bfb_publish__chooser_text, |
| 284 |
#<?php echo esc_attr( self::MODAL_DOM_ID ); ?> .wpbc_bfb_publish__panel_text { |
| 285 |
line-height: 1.75em; |
| 286 |
text-align: center; |
| 287 |
font-size: 16px; |
| 288 |
font-weight: 400; |
| 289 |
max-width: 32em; |
| 290 |
margin: 0 auto 15px; |
| 291 |
} |
| 292 |
#<?php echo esc_attr( self::MODAL_DOM_ID ); ?> .wpbc_bfb_publish__actions, |
| 293 |
#<?php echo esc_attr( self::MODAL_DOM_ID ); ?> .wpbc_bfb_publish__inputs, |
| 294 |
#<?php echo esc_attr( self::MODAL_DOM_ID ); ?> .wpbc_bfb_publish__result_actions { |
| 295 |
display: flex; |
| 296 |
flex-flow: row wrap; |
| 297 |
justify-content: center; |
| 298 |
align-items: center; |
| 299 |
gap: 10px; |
| 300 |
margin: 10px 0; |
| 301 |
} |
| 302 |
#<?php echo esc_attr( self::MODAL_DOM_ID ); ?> .wpbc_bfb_publish__inputs input[type="text"], |
| 303 |
#<?php echo esc_attr( self::MODAL_DOM_ID ); ?> .wpbc_bfb_publish__inputs select { |
| 304 |
width: min(100%, 28em); |
| 305 |
max-width: min(100%, 28em); |
| 306 |
margin: 5px 10px 10px; |
| 307 |
min-height: 34px; |
| 308 |
} |
| 309 |
#<?php echo esc_attr( self::MODAL_DOM_ID ); ?> .wpbc_bfb_publish__result_actions { |
| 310 |
display: none; |
| 311 |
margin-top: 15px; |
| 312 |
} |
| 313 |
#<?php echo esc_attr( self::MODAL_DOM_ID ); ?> code.wpbc_inserted_shortcode_view { |
| 314 |
background: #d5d5d5; |
| 315 |
color: #444; |
| 316 |
line-height: 2; |
| 317 |
padding: 3px 10px; |
| 318 |
} |
| 319 |
</style> |
| 320 |
<div class="modal-dialog modal-lg0"> |
| 321 |
<div class="modal-content"> |
| 322 |
<div class="modal-header"> |
| 323 |
<button type="button" class="close" data-dismiss="modal" aria-label="<?php esc_attr_e( 'Close', 'booking' ); ?>"> |
| 324 |
<span aria-hidden="true">×</span> |
| 325 |
</button> |
| 326 |
<h4 class="modal-title"><?php esc_html_e( 'Publish Booking Form', 'booking' ); ?></h4> |
| 327 |
</div> |
| 328 |
|
| 329 |
<div class="modal-body"> |
| 330 |
<div class="wpbc_bfb_publish__notice"></div> |
| 331 |
|
| 332 |
<input type="hidden" id="wpbc_bfb_publish__resource_id" value="" /> |
| 333 |
<input type="hidden" id="wpbc_bfb_publish__form_name" value="" /> |
| 334 |
<input type="hidden" id="wpbc_bfb_publish__shortcode_raw" value="" /> |
| 335 |
|
| 336 |
<?php if ( $is_demo ) { ?> |
| 337 |
<div class="wpbc-settings-notice notice-warning" style="text-align:left;font-size:1rem;margin-top:0;"> |
| 338 |
<?php esc_html_e( 'In the demo versions this operation is not allowed.', 'booking' ); ?> |
| 339 |
</div> |
| 340 |
<?php } else { ?> |
| 341 |
|
| 342 |
<div class="wpbc_bfb_publish__save_step"> |
| 343 |
<div class="wpbc_bfb_publish__save_step_text"> |
| 344 |
<?php esc_html_e( 'Would you like to save the current booking form configuration before publishing?', 'booking' ); ?> |
| 345 |
</div> |
| 346 |
|
| 347 |
<div class="wpbc_bfb_publish__actions"> |
| 348 |
<a |
| 349 |
href="#" |
| 350 |
class="button button-primary" |
| 351 |
data-wpbc-bfb-publish-save-step="save" |
| 352 |
data-wpbc-u-busy-text="<?php esc_attr_e( 'Saving booking form', 'booking' ); ?>..." |
| 353 |
> |
| 354 |
<?php esc_html_e( 'Save and Continue', 'booking' ); ?> |
| 355 |
</a> |
| 356 |
|
| 357 |
<a |
| 358 |
href="#" |
| 359 |
class="button button-secondary" |
| 360 |
data-wpbc-bfb-publish-save-step="skip" |
| 361 |
> |
| 362 |
<?php esc_html_e( 'Skip', 'booking' ); ?> |
| 363 |
</a> |
| 364 |
</div> |
| 365 |
</div> |
| 366 |
|
| 367 |
<div class="wpbc_bfb_publish__chooser" style="display:none;"> |
| 368 |
<div class="wpbc_bfb_publish__chooser_text"> |
| 369 |
<?php esc_html_e( 'Choose whether to embed your booking form in an existing page or create a new one.', 'booking' ); ?> |
| 370 |
</div> |
| 371 |
|
| 372 |
<div class="wpbc_bfb_publish__actions"> |
| 373 |
<a href="#" class="button button-secondary" data-wpbc-bfb-publish-mode="edit"> |
| 374 |
<?php esc_html_e( 'Embed in Existing Page', 'booking' ); ?> |
| 375 |
</a> |
| 376 |
|
| 377 |
<a href="#" class="button button-secondary" data-wpbc-bfb-publish-mode="create"> |
| 378 |
<?php esc_html_e( 'Create New Page', 'booking' ); ?> |
| 379 |
</a> |
| 380 |
</div> |
| 381 |
</div> |
| 382 |
|
| 383 |
<div class="wpbc_bfb_publish__panel wpbc_bfb_publish__panel--edit" style="display:none;"> |
| 384 |
<div class="wpbc_bfb_publish__panel_text"> |
| 385 |
<?php esc_html_e( 'Select the page where you want to embed your booking form.', 'booking' ); ?> |
| 386 |
</div> |
| 387 |
|
| 388 |
<div class="wpbc_bfb_publish__inputs"> |
| 389 |
<?php |
| 390 |
wp_dropdown_pages( |
| 391 |
array( |
| 392 |
'name' => 'wpbc_bfb_publish_page_id', |
| 393 |
'id' => 'wpbc_bfb_publish_page_id', |
| 394 |
'show_option_none' => '— ' . esc_html__( 'Select', 'booking' ) . ' —', |
| 395 |
'option_none_value' => '0', |
| 396 |
'selected' => 0, |
| 397 |
'post_type' => 'page', |
| 398 |
'post_status' => array( 'draft', 'publish', 'private' ), |
| 399 |
) |
| 400 |
); |
| 401 |
?> |
| 402 |
|
| 403 |
<a href="#" class="button button-primary" data-wpbc-bfb-publish-submit="edit"> |
| 404 |
<?php esc_html_e( 'Use This Page', 'booking' ); ?> |
| 405 |
</a> |
| 406 |
</div> |
| 407 |
</div> |
| 408 |
|
| 409 |
<div class="wpbc_bfb_publish__panel wpbc_bfb_publish__panel--create" style="display:none;"> |
| 410 |
<div class="wpbc_bfb_publish__panel_text"> |
| 411 |
<?php esc_html_e( 'Provide a name for your new page.', 'booking' ); ?> |
| 412 |
</div> |
| 413 |
|
| 414 |
<div class="wpbc_bfb_publish__inputs"> |
| 415 |
<input |
| 416 |
id="wpbc_bfb_publish_page_title" |
| 417 |
type="text" |
| 418 |
value="" |
| 419 |
placeholder="<?php echo esc_attr__( 'Enter Page Name', 'booking' ); ?>" |
| 420 |
/> |
| 421 |
|
| 422 |
<a href="#" class="button button-primary" data-wpbc-bfb-publish-submit="create"> |
| 423 |
<?php esc_html_e( 'Create Page', 'booking' ); ?> |
| 424 |
</a> |
| 425 |
</div> |
| 426 |
</div> |
| 427 |
|
| 428 |
<div class="wpbc_bfb_publish__result_actions"> |
| 429 |
<a href="#" class="button button-primary" target="_blank" rel="noopener noreferrer" data-wpbc-bfb-publish-open-page="1" style="display:none;"> |
| 430 |
<?php esc_html_e( 'Open Page', 'booking' ); ?> |
| 431 |
</a> |
| 432 |
|
| 433 |
<a href="#" class="button button-secondary" target="_blank" rel="noopener noreferrer" data-wpbc-bfb-publish-edit-page="1" style="display:none;"> |
| 434 |
<?php esc_html_e( 'Edit Page', 'booking' ); ?> |
| 435 |
</a> |
| 436 |
</div> |
| 437 |
|
| 438 |
<?php } ?> |
| 439 |
</div> |
| 440 |
|
| 441 |
<div class="modal-footer" style="display:none;"> |
| 442 |
<a href="#" class="button button-secondary" data-wpbc-bfb-publish-back="1"> |
| 443 |
<i class="menu_icon icon-1x wpbc_icn_keyboard_arrow_left"></i> |
| 444 |
<?php esc_html_e( 'Go Back', 'booking' ); ?> |
| 445 |
</a> |
| 446 |
</div> |
| 447 |
</div> |
| 448 |
</div> |
| 449 |
</div> |
| 450 |
</span> |
| 451 |
<?php |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
WPBC_BFB_Tpl__Publish::init(); |