| 1 |
<?php |
| 2 |
/** |
| 3 |
* BFB Admin Templates: Page Controls + Confirm Delete Page modal. |
| 4 |
* |
| 5 |
* Prints Underscore templates consumed by bfb-ui.js via wp.template(): |
| 6 |
* - tmpl-wpbc-bfb-tpl-page-remove |
| 7 |
* - tmpl-wpbc-bfb-tpl-modal-page-delete |
| 8 |
* |
| 9 |
* @package Booking Calendar |
| 10 |
* @subpackage Booking Form Builder |
| 11 |
* @since 11.0.0 |
| 12 |
* @file ../includes/page-form-builder/admin-page-tpl/tpl-page-delete.php |
| 13 |
*/ |
| 14 |
|
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Templates for "Delete page" action. |
| 21 |
*/ |
| 22 |
class WPBC_BFB_Tpl__Page_Delete { |
| 23 |
|
| 24 |
/** |
| 25 |
* Action key (also used by JS as data-wpbc-bfb-action). |
| 26 |
* |
| 27 |
* @var string |
| 28 |
*/ |
| 29 |
const ACTION = 'bfb_action__page_delete'; |
| 30 |
|
| 31 |
/** |
| 32 |
* WP template ids (without the "tmpl-" prefix). |
| 33 |
* |
| 34 |
* @var string |
| 35 |
*/ |
| 36 |
const TPL_PAGE_CONTROLS = 'wpbc-bfb-tpl-page-remove'; |
| 37 |
const TPL_MODAL_DELETE = 'wpbc-bfb-tpl-modal-page-delete'; |
| 38 |
|
| 39 |
|
| 40 |
/** |
| 41 |
* Register templates output. |
| 42 |
* |
| 43 |
* @return void |
| 44 |
*/ |
| 45 |
public static function register() { |
| 46 |
add_action( 'wpbc_hook_bfb_template__hidden_templates', array( __CLASS__, 'print_templates' ) ); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Check capability for this action. |
| 51 |
* |
| 52 |
* @return bool |
| 53 |
*/ |
| 54 |
public static function user_can() { |
| 55 |
return true; // (bool) wpbc_is_user_can( self::ACTION, wpbc_get_current_user_id() ); //. |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Print all templates (called by the hidden templates hook). |
| 60 |
* |
| 61 |
* @return void |
| 62 |
*/ |
| 63 |
public static function print_templates() { |
| 64 |
self::template_page_controls(); |
| 65 |
self::template_modal(); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
/** |
| 70 |
* Template: page header controls (contains "Page X" + delete button + optional dropdown). |
| 71 |
* |
| 72 |
* JS usage: |
| 73 |
* wp.template('wpbc-bfb-tpl-page-remove')({ page_number: 1 }) |
| 74 |
* |
| 75 |
* @return void |
| 76 |
*/ |
| 77 |
public static function template_page_controls() { |
| 78 |
?> |
| 79 |
<script type="text/html" id="tmpl-<?php echo esc_attr( self::TPL_PAGE_CONTROLS ); ?>"> |
| 80 |
<div class="wpbc_bfb__controls"> |
| 81 |
<h3 class="wpbc_bfb__page_number"> |
| 82 |
<?php echo esc_html__( 'Page', 'booking' ); ?> {{ data.page_number }} |
| 83 |
|
| 84 |
<?php if ( self::user_can() ) : ?> |
| 85 |
<button type="button" |
| 86 |
class="wpbc_bfb__page_delete_btn wpbc_bfb__field-remove-btn" |
| 87 |
data-wpbc-bfb-action="<?php echo esc_attr( self::ACTION ); ?>" |
| 88 |
aria-label="<?php echo esc_attr__( 'Remove page', 'booking' ); ?>" |
| 89 |
title="<?php echo esc_attr__( 'Remove page', 'booking' ); ?>"> |
| 90 |
<i class="menu_icon icon-1x wpbc_icn_close"></i> |
| 91 |
</button> |
| 92 |
<?php endif; ?> |
| 93 |
</h3> |
| 94 |
</div> |
| 95 |
</script> |
| 96 |
<?php |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Template: confirm "Delete page" modal. |
| 101 |
* |
| 102 |
* JS usage: |
| 103 |
* wp.template('wpbc-bfb-tpl-modal-page-delete')({ page_number: 1 }) |
| 104 |
* |
| 105 |
* @return void |
| 106 |
*/ |
| 107 |
public static function template_modal() { |
| 108 |
|
| 109 |
if ( ! self::user_can() ) { |
| 110 |
return; |
| 111 |
} |
| 112 |
?> |
| 113 |
<script type="text/html" id="tmpl-<?php echo esc_attr( self::TPL_MODAL_DELETE ); ?>"> |
| 114 |
<span class="wpdevelop"> |
| 115 |
<div id="wpbc_bfb_modal__page_delete" class="modal wpbc_popup_modal wpbc_modal_in_listing" tabindex="-1" role="dialog" aria-hidden="true"> |
| 116 |
<div class="modal-dialog"> |
| 117 |
<div class="modal-content"> |
| 118 |
<div class="modal-header"> |
| 119 |
<button type="button" class="close" data-dismiss="modal" aria-label="<?php echo esc_attr__( 'Close', 'booking' ); ?>"> |
| 120 |
<span aria-hidden="true">×</span> |
| 121 |
</button> |
| 122 |
<h4 class="modal-title"> |
| 123 |
<?php echo esc_html__( 'Delete page', 'booking' ); ?> |
| 124 |
<sup class="wpbc_bfb__modal_page_number">{{ data.page_number }}</sup> |
| 125 |
</h4> |
| 126 |
</div> |
| 127 |
|
| 128 |
<div class="modal-body"> |
| 129 |
<p class="help-block"> |
| 130 |
<?php echo esc_html__( 'This will remove the entire page and all its sections and fields.', 'booking' ); ?> |
| 131 |
</p> |
| 132 |
<p class="help-block"> |
| 133 |
<?php echo esc_html__( 'This action cannot be undone.', 'booking' ); ?> |
| 134 |
</p> |
| 135 |
</div> |
| 136 |
|
| 137 |
<div class="modal-footer"> |
| 138 |
<a href="javascript:void(0)" |
| 139 |
class="button button-primary wpbc_bfb__modal_confirm" |
| 140 |
data-wpbc-bfb-action="<?php echo esc_attr( self::ACTION ); ?>" |
| 141 |
data-wpbc-bfb-confirm="1"> |
| 142 |
<?php echo esc_html__( 'Delete', 'booking' ); ?> |
| 143 |
</a> |
| 144 |
<a href="javascript:void(0)" class="button button-secondary" data-dismiss="modal"> |
| 145 |
<?php echo esc_html__( 'Cancel', 'booking' ); ?> |
| 146 |
</a> |
| 147 |
</div> |
| 148 |
</div><!-- /.modal-content --> |
| 149 |
</div><!-- /.modal-dialog --> |
| 150 |
</div><!-- /.modal --> |
| 151 |
</span> |
| 152 |
</script> |
| 153 |
<?php |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
// Register templates. |
| 158 |
WPBC_BFB_Tpl__Page_Delete::register(); |
| 159 |
|
| 160 |
/** |
| 161 |
* How it works: |
| 162 |
* |
| 163 |
* ┌──────────────────────────────────────────────────────────────────────────────┐ |
| 164 |
* │ A) Page header controls exist (template rendered somewhere in Builder UI) │ |
| 165 |
* └──────────────────────────────────────────────────────────────────────────────┘ |
| 166 |
* PHP template: tmpl-wpbc-bfb-tpl-page-remove |
| 167 |
* - shows "Page X" |
| 168 |
* - contains button: |
| 169 |
* data-wpbc-bfb-action="bfb_action__page_delete" |
| 170 |
* |
| 171 |
* User clicks delete button |
| 172 |
* | |
| 173 |
* v |
| 174 |
* ┌──────────────────────────────────────────────────────────────────────────────┐ |
| 175 |
* │ B) Page Delete module catches click (event delegation) │ |
| 176 |
* └──────────────────────────────────────────────────────────────────────────────┘ |
| 177 |
* UI.WPBC_BFB_Page_Delete_Confirm._on_trigger_click (capturing=true) |
| 178 |
* - finds closest [data-wpbc-bfb-action="bfb_action__page_delete"] |
| 179 |
* - finds closest page container: .wpbc_bfb__panel--preview |
| 180 |
* - calls _open_modal_for_page(page_el, ...) |
| 181 |
* |
| 182 |
* | |
| 183 |
* v |
| 184 |
* ┌──────────────────────────────────────────────────────────────────────────────┐ |
| 185 |
* │ C) Ensure modal exists in DOM (lazy insert from wp.template) │ |
| 186 |
* └──────────────────────────────────────────────────────────────────────────────┘ |
| 187 |
* _open_modal_for_page(): |
| 188 |
* page_number = page_el.getAttribute('data-page') |
| 189 |
* |
| 190 |
* ensure = UI.Templates.ensure_dom_ref_from_wp_template |
| 191 |
* ref = ensure( |
| 192 |
* tpl_modal_id = "wpbc-bfb-tpl-modal-page-delete", |
| 193 |
* dom_id = "wpbc_bfb_modal__page_delete", |
| 194 |
* data = { page_number } |
| 195 |
* ) |
| 196 |
* |
| 197 |
* If modal already exists in DOM: |
| 198 |
* - ensure() returns existing modal node (no duplicates) |
| 199 |
* Else: |
| 200 |
* - render wp.template() HTML |
| 201 |
* - insert first root element into DOM |
| 202 |
* - return the actual element + id |
| 203 |
* |
| 204 |
* modal_el.__wpbc_bfb_page_el = page_el (store the page node being deleted) |
| 205 |
* |
| 206 |
* UI.Modals.show(modal_id) |
| 207 |
* - prefer jQuery wpbc_my_modal('show') |
| 208 |
* - else fallback: modal_el.style.display = 'block' |
| 209 |
* |
| 210 |
* | |
| 211 |
* v |
| 212 |
* ┌──────────────────────────────────────────────────────────────────────────────┐ |
| 213 |
* │ D) Confirm click inside modal │ |
| 214 |
* └──────────────────────────────────────────────────────────────────────────────┘ |
| 215 |
* Document click handler (capturing=true) |
| 216 |
* - looks for: |
| 217 |
* #wpbc_bfb_modal__page_delete |
| 218 |
* [data-wpbc-bfb-confirm="1"] |
| 219 |
* [data-wpbc-bfb-action="bfb_action__page_delete"] |
| 220 |
* |
| 221 |
* If confirm clicked: |
| 222 |
* - modal_el = document.getElementById('wpbc_bfb_modal__page_delete') |
| 223 |
* - page_el = modal_el.__wpbc_bfb_page_el |
| 224 |
* - clear modal_el.__wpbc_bfb_page_el |
| 225 |
* |
| 226 |
* - _remove_page_el(page_el) |
| 227 |
* - UI.Modals.hide(modal_el) |
| 228 |
* jQuery wpbc_my_modal('hide') OR display='none' |
| 229 |
* |
| 230 |
* | |
| 231 |
* v |
| 232 |
* ┌──────────────────────────────────────────────────────────────────────────────┐ |
| 233 |
* │ E) Removing page and syncing Builder UI │ |
| 234 |
* └──────────────────────────────────────────────────────────────────────────────┘ |
| 235 |
* _remove_page_el(page_el): |
| 236 |
* - if current selection is inside this page: |
| 237 |
* neighbor = find a field on another page to select afterwards |
| 238 |
* |
| 239 |
* - page_el.remove() |
| 240 |
* - builder.usage.update_palette_ui() |
| 241 |
* |
| 242 |
* - emit STRUCTURE_CHANGE: |
| 243 |
* builder.bus.emit(WPBC_BFB_Events.STRUCTURE_CHANGE, { |
| 244 |
* source: 'page-remove', |
| 245 |
* structure: builder.get_structure(), |
| 246 |
* page_el |
| 247 |
* }) |
| 248 |
* |
| 249 |
* - builder.select_field(neighbor || null) |
| 250 |
* |
| 251 |
*/ |