Files
wrapartamenty.pl/PLAN-click-to-block-calendar.md

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_custom WP option (array of rules with room_type_id, room_id, date_from, date_to, restrictions, comment)
  • Rendering: Already handled — setupBlocks() reads rules, renderPseudoCell() applies .mphb-date-blocked CSS class
  • AJAX pattern: Extend AbstractAjaxApiAction, register in AjaxApiHandler
  • Nonces: Automatically generated for all registered actions by AjaxApiHandler::getAjaxActionWPNonces(), passed to JS via MPHBAdmin

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:
    1. Check current_user_can('edit_posts')
    2. Read get_option('mphb_booking_rules_custom', [])
    3. Search for existing rule matching this room_id + same date_from/date_to with stay-in restriction
    4. If found → remove it (unblock), return { blocked: false }
    5. 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 }
    6. update_option('mphb_booking_rules_custom', $rules)

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 td cells in the date table
  • On click:
    1. If click target is inside .mphb-link-to-booking or .mphb-silent-link-to-booking → skip (booking popup handles it)
    2. If cell is inside a locked booking (has .mphb-date-booked or .mphb-date-pending) → skip
    3. Get data-date from <td>, room-id from parent <tr>, data-room-type-id from <tr>
    4. AJAX POST → mphb_toggle_block_date
    5. On success → location.reload() (simplest, avoids complex two-column DOM manipulation)

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

  1. Navigate to WP Admin → Hotel Booking → Booking Calendar
  2. Click on a free (white) cell → page reloads, cell is now red (blocked)
  3. Click on a blocked (red) cell → page reloads, cell is now white (free)
  4. Verify: Booking Rules → Custom Rules shows new rule
  5. Verify: clicking on a booked cell still opens booking popup (unchanged)
  6. Verify: blocked date is unavailable on frontend search