| 1 |
<?php |
| 2 |
/** |
| 3 |
* Neutral Booking Form publishing modal. |
| 4 |
* |
| 5 |
* @package Booking Calendar |
| 6 |
* @since 11.6.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Render and configure the shared Booking Form page-publishing interface. |
| 15 |
* |
| 16 |
* The modal is independent of the Resources catalog and Form Builder. Any |
| 17 |
* authorized administration screen can render it and open it through the |
| 18 |
* wpbc_publish_booking_form__open() JavaScript API. |
| 19 |
*/ |
| 20 |
final class WPBC_Booking_Form_Publish_Modal { |
| 21 |
|
| 22 |
const SCRIPT_HANDLE = 'wpbc-publish-booking-form'; |
| 23 |
const MODAL_DOM_ID = 'wpbc_publish_booking_form__modal'; |
| 24 |
|
| 25 |
/** |
| 26 |
* Whether the script and localized configuration were registered this request. |
| 27 |
* |
| 28 |
* @var bool |
| 29 |
*/ |
| 30 |
private static $assets_configured = false; |
| 31 |
|
| 32 |
/** |
| 33 |
* Register the route-scoped runtime asset. |
| 34 |
* |
| 35 |
* @return void |
| 36 |
*/ |
| 37 |
public static function init() { |
| 38 |
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_assets' ), 10, 0 ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Enqueue the neutral publishing client on the new Resources catalog only. |
| 43 |
* |
| 44 |
* Other consumers can enqueue the same handle explicitly before rendering |
| 45 |
* the modal. Keeping the automatic enqueue route-scoped prevents catalog |
| 46 |
* assets from leaking onto unrelated administration screens. |
| 47 |
* |
| 48 |
* @param bool $force Whether an explicit modal consumer is requesting assets. |
| 49 |
* |
| 50 |
* @return void |
| 51 |
*/ |
| 52 |
public static function enqueue_assets( $force = false ) { |
| 53 |
if ( self::$assets_configured ) { |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
if ( ! $force && ( ! function_exists( 'wpbc_catalog_booking_resources_is_page' ) || ! wpbc_catalog_booking_resources_is_page() ) ) { |
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
if ( function_exists( 'wpbc_catalog_booking_resources_get_manage_capability' ) && ! current_user_can( wpbc_catalog_booking_resources_get_manage_capability() ) ) { |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
$script_path = WPBC_PLUGIN_DIR . '/includes/publish/_out/wpbc-publish-booking-form.js'; |
| 66 |
$version = file_exists( $script_path ) ? (string) filemtime( $script_path ) : WP_BK_VERSION_NUM; |
| 67 |
|
| 68 |
wp_enqueue_script( |
| 69 |
self::SCRIPT_HANDLE, |
| 70 |
wpbc_plugin_url( '/includes/publish/_out/wpbc-publish-booking-form.js' ), |
| 71 |
array( 'jquery' ), |
| 72 |
$version, |
| 73 |
true |
| 74 |
); |
| 75 |
|
| 76 |
wp_localize_script( |
| 77 |
self::SCRIPT_HANDLE, |
| 78 |
'wpbc_publish_booking_form_vars', |
| 79 |
array( |
| 80 |
'ajax_url' => admin_url( 'admin-ajax.php', 'relative' ), |
| 81 |
'action' => WPBC_Booking_Form_Publish_Ajax::ACTION, |
| 82 |
'pages_action' => WPBC_Booking_Form_Publish_Ajax::LIST_PAGES_ACTION, |
| 83 |
'nonce' => wp_create_nonce( WPBC_Booking_Form_Publish_Ajax::NONCE_ACTION ), |
| 84 |
'modal_selector' => '#' . self::MODAL_DOM_ID, |
| 85 |
'is_demo' => WPBC_Booking_Form_Publisher::is_demo_restricted() ? 1 : 0, |
| 86 |
'i18n' => array( |
| 87 |
'loading' => __( 'Publishing booking form', 'booking' ), |
| 88 |
'loading_pages' => __( 'Loading pages', 'booking' ), |
| 89 |
'select_page' => __( 'Please select an existing page.', 'booking' ), |
| 90 |
'no_pages' => __( 'No editable pages are available.', 'booking' ), |
| 91 |
'enter_page_title' => __( 'Please enter a page title.', 'booking' ), |
| 92 |
'generic_error' => __( 'An unexpected error occurred while publishing the booking form.', 'booking' ), |
| 93 |
'demo_error' => __( 'In the demo versions this operation is not allowed.', 'booking' ), |
| 94 |
), |
| 95 |
) |
| 96 |
); |
| 97 |
|
| 98 |
self::$assets_configured = true; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Print the shared native Booking Calendar publishing modal. |
| 103 |
* |
| 104 |
* The caller is responsible for rendering this once on an authorized screen. |
| 105 |
* Page writes are always performed asynchronously through the neutral AJAX |
| 106 |
* controller and are re-authorized server-side. |
| 107 |
* |
| 108 |
* @return void |
| 109 |
*/ |
| 110 |
public static function render() { |
| 111 |
self::enqueue_assets( true ); |
| 112 |
|
| 113 |
$is_demo = WPBC_Booking_Form_Publisher::is_demo_restricted(); |
| 114 |
?> |
| 115 |
<span class="wpdevelop"> |
| 116 |
<div id="<?php echo esc_attr( self::MODAL_DOM_ID ); ?>" class="modal wpbc_popup_modal" tabindex="-1" role="dialog" aria-hidden="true" aria-labelledby="<?php echo esc_attr( self::MODAL_DOM_ID ); ?>_title"> |
| 117 |
<style type="text/css"> |
| 118 |
#<?php echo esc_attr( self::MODAL_DOM_ID ); ?> .modal-header .modal-title { font-weight: 600; } |
| 119 |
#<?php echo esc_attr( self::MODAL_DOM_ID ); ?> .wpbc_publish_booking_form__notice { margin: 0 0 15px; } |
| 120 |
#<?php echo esc_attr( self::MODAL_DOM_ID ); ?> .wpbc_publish_booking_form__chooser_text, |
| 121 |
#<?php echo esc_attr( self::MODAL_DOM_ID ); ?> .wpbc_publish_booking_form__panel_text { font-size: 16px; font-weight: 400; line-height: 1.75em; margin: 0 auto 15px; max-width: 32em; text-align: center; } |
| 122 |
#<?php echo esc_attr( self::MODAL_DOM_ID ); ?> .wpbc_publish_booking_form__actions, |
| 123 |
#<?php echo esc_attr( self::MODAL_DOM_ID ); ?> .wpbc_publish_booking_form__inputs, |
| 124 |
#<?php echo esc_attr( self::MODAL_DOM_ID ); ?> .wpbc_publish_booking_form__result_actions { align-items: center; display: flex; flex-flow: row wrap; gap: 10px; justify-content: center; margin: 10px 0; } |
| 125 |
#<?php echo esc_attr( self::MODAL_DOM_ID ); ?> .wpbc_publish_booking_form__inputs input[type="text"], |
| 126 |
#<?php echo esc_attr( self::MODAL_DOM_ID ); ?> .wpbc_publish_booking_form__inputs select { margin: 5px 10px 10px; max-width: min(100%, 28em); min-height: 34px; width: min(100%, 28em); } |
| 127 |
#<?php echo esc_attr( self::MODAL_DOM_ID ); ?> .wpbc_publish_booking_form__result_actions { display: none; margin-top: 15px; } |
| 128 |
</style> |
| 129 |
<div class="modal-dialog modal-lg0"> |
| 130 |
<div class="modal-content"> |
| 131 |
<div class="modal-header"> |
| 132 |
<button type="button" class="close" data-dismiss="modal" aria-label="<?php esc_attr_e( 'Close', 'booking' ); ?>"><span aria-hidden="true">×</span></button> |
| 133 |
<h4 id="<?php echo esc_attr( self::MODAL_DOM_ID ); ?>_title" class="modal-title"><?php esc_html_e( 'Publish Booking Form', 'booking' ); ?></h4> |
| 134 |
</div> |
| 135 |
<div class="modal-body"> |
| 136 |
<div class="wpbc_publish_booking_form__notice" role="status" aria-live="polite"></div> |
| 137 |
<input type="hidden" data-wpbc-publish-booking-form-resource-id value="" /> |
| 138 |
<input type="hidden" data-wpbc-publish-booking-form-form-name value="standard" /> |
| 139 |
<input type="hidden" data-wpbc-publish-booking-form-shortcode value="" /> |
| 140 |
|
| 141 |
<?php if ( $is_demo ) : ?> |
| 142 |
<div class="wpbc-settings-notice notice-warning" style="text-align:left;font-size:1rem;margin-top:0;"> |
| 143 |
<?php esc_html_e( 'In the demo versions this operation is not allowed.', 'booking' ); ?> |
| 144 |
</div> |
| 145 |
<?php else : ?> |
| 146 |
<div class="wpbc_publish_booking_form__chooser"> |
| 147 |
<div class="wpbc_publish_booking_form__chooser_text"><?php esc_html_e( 'Choose whether to embed your booking form in an existing page or create a new one.', 'booking' ); ?></div> |
| 148 |
<div class="wpbc_publish_booking_form__actions"> |
| 149 |
<button type="button" class="button button-secondary" data-wpbc-publish-booking-form-mode="edit"><?php esc_html_e( 'Embed in Existing Page', 'booking' ); ?></button> |
| 150 |
<button type="button" class="button button-secondary" data-wpbc-publish-booking-form-mode="create"><?php esc_html_e( 'Create New Page', 'booking' ); ?></button> |
| 151 |
</div> |
| 152 |
</div> |
| 153 |
|
| 154 |
<div class="wpbc_publish_booking_form__panel wpbc_publish_booking_form__panel--edit" style="display:none;"> |
| 155 |
<div class="wpbc_publish_booking_form__panel_text"><?php esc_html_e( 'Select the page where you want to embed your booking form.', 'booking' ); ?></div> |
| 156 |
<div class="wpbc_publish_booking_form__inputs"> |
| 157 |
<select id="wpbc_publish_booking_form_page_id" name="wpbc_publish_booking_form_page_id" data-wpbc-publish-booking-form-page-list disabled="disabled"> |
| 158 |
<option value="0"><?php esc_html_e( 'Select', 'booking' ); ?></option> |
| 159 |
</select> |
| 160 |
<button type="button" class="button button-primary" data-wpbc-publish-booking-form-submit="edit"><?php esc_html_e( 'Use This Page', 'booking' ); ?></button> |
| 161 |
</div> |
| 162 |
</div> |
| 163 |
|
| 164 |
<div class="wpbc_publish_booking_form__panel wpbc_publish_booking_form__panel--create" style="display:none;"> |
| 165 |
<div class="wpbc_publish_booking_form__panel_text"><?php esc_html_e( 'Provide a name for your new page.', 'booking' ); ?></div> |
| 166 |
<div class="wpbc_publish_booking_form__inputs"> |
| 167 |
<input id="wpbc_publish_booking_form_page_title" type="text" value="" placeholder="<?php echo esc_attr__( 'Enter Page Name', 'booking' ); ?>" /> |
| 168 |
<button type="button" class="button button-primary" data-wpbc-publish-booking-form-submit="create"><?php esc_html_e( 'Create Page', 'booking' ); ?></button> |
| 169 |
</div> |
| 170 |
</div> |
| 171 |
|
| 172 |
<div class="wpbc_publish_booking_form__result_actions"> |
| 173 |
<a href="#" class="button button-primary" target="_blank" rel="noopener noreferrer" data-wpbc-publish-booking-form-open-page style="display:none;"><?php esc_html_e( 'Open Page', 'booking' ); ?></a> |
| 174 |
<a href="#" class="button button-secondary" target="_blank" rel="noopener noreferrer" data-wpbc-publish-booking-form-edit-page style="display:none;"><?php esc_html_e( 'Edit Page', 'booking' ); ?></a> |
| 175 |
</div> |
| 176 |
<?php endif; ?> |
| 177 |
</div> |
| 178 |
<div class="modal-footer" style="display:none;"> |
| 179 |
<button type="button" class="button button-secondary" data-wpbc-publish-booking-form-back><i class="menu_icon icon-1x wpbc_icn_keyboard_arrow_left" aria-hidden="true"></i> <?php esc_html_e( 'Go Back', 'booking' ); ?></button> |
| 180 |
</div> |
| 181 |
</div> |
| 182 |
</div> |
| 183 |
</div> |
| 184 |
</span> |
| 185 |
<?php |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
WPBC_Booking_Form_Publish_Modal::init(); |
| 190 |
|