3.9 KiB
Plan: Click-to-Block Dates on Admin Calendar
Context
User wants to block/unblock dates directly from the Booking Calendar view (admin.php?page=mphb_calendar&period=month) by clicking on date cells. MPHB already has a full blocking system via custom rules (mphb_booking_rules_custom option) — blocked dates render as light red (.mphb-date-blocked). Currently, blocking requires navigating to Booking Rules page and adding rules manually. This change adds a click shortcut on the calendar grid.
Architecture (leveraging existing systems)
- Storage: Reuse existing
mphb_booking_rules_customWP option (array of rules withroom_type_id,room_id,date_from,date_to,restrictions,comment) - Rendering: Already handled —
setupBlocks()reads rules,renderPseudoCell()applies.mphb-date-blockedCSS class - AJAX pattern: Extend
AbstractAjaxApiAction, register inAjaxApiHandler - Nonces: Automatically generated for all registered actions by
AjaxApiHandler::getAjaxActionWPNonces(), passed to JS viaMPHBAdmin
Changes
1. Add data-date to calendar cells
File: includes/bookings-calendar.php (~line 1098-1109)
Add data-date="Y-m-d" attribute to both <td> elements in renderPseudoCell(). The <tr> already has room-id attribute.
2. Add data-room-type-id to calendar <tr>
File: includes/bookings-calendar.php (~line 863)
Change <tr room-id="..."> to also include data-room-type-id="...". Use \MPHB\Entities\RoomHelper::getRoomTypeId($roomPost->ID).
3. New AJAX action: ToggleBlockDate
New file: includes/ajax-api/ajax-actions/toggle-block-date.php
Class ToggleBlockDate extends AbstractAjaxApiAction:
- Action name:
toggle_block_date - Guest access:
false - Request params:
room_id(int),date(Y-m-d string),room_type_id(int) - Logic:
- Check
current_user_can('edit_posts') - Read
get_option('mphb_booking_rules_custom', []) - Search for existing rule matching this
room_id+ samedate_from/date_towithstay-inrestriction - If found → remove it (unblock), return
{ blocked: false } - If not found → append new rule
{ room_type_id, room_id, date_from: date, date_to: date, restrictions: ['check-in','check-out','stay-in'], comment: 'Blocked' }, return{ blocked: true } update_option('mphb_booking_rules_custom', $rules)
- Check
4. Register AJAX action
File: includes/ajax-api/ajax-api-handler.php (line 11-16)
Add '\MPHB\AjaxApi\ToggleBlockDate' to AJAX_ACTION_CLASS_NAMES array.
5. JS click handler
File: assets/js/admin/admin.js
Add to MPHBAdmin.BookingsCalendar control:
- New click handler on
tdcells in the date table - On click:
- If click target is inside
.mphb-link-to-bookingor.mphb-silent-link-to-booking→ skip (booking popup handles it) - If cell is inside a locked booking (has
.mphb-date-bookedor.mphb-date-pending) → skip - Get
data-datefrom<td>,room-idfrom parent<tr>,data-room-type-idfrom<tr> - AJAX POST →
mphb_toggle_block_date - On success →
location.reload()(simplest, avoids complex two-column DOM manipulation)
- If click target is inside
Files Summary
| # | File | Action |
|---|---|---|
| 1 | includes/bookings-calendar.php |
Add data-date on <td>, data-room-type-id on <tr> |
| 2 | includes/ajax-api/ajax-api-handler.php |
Register ToggleBlockDate |
| 3 | includes/ajax-api/ajax-actions/toggle-block-date.php |
NEW — AJAX handler |
| 4 | assets/js/admin/admin.js |
Click handler + AJAX call + reload |
Verification
- Navigate to
WP Admin → Hotel Booking → Booking Calendar - Click on a free (white) cell → page reloads, cell is now red (blocked)
- Click on a blocked (red) cell → page reloads, cell is now white (free)
- Verify:
Booking Rules → Custom Rulesshows new rule - Verify: clicking on a booked cell still opens booking popup (unchanged)
- Verify: blocked date is unavailable on frontend search