-
Notifications
You must be signed in to change notification settings - Fork 92
gpb-daily-service-booking-limit.php
: Added new snippet.
#1172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
WalkthroughAdds class Changes
Sequence Diagram(s)sequenceDiagram
autonumber
actor Client
participant REST as REST Client
participant API as WP REST /availability
participant Filter as GPB_Daily_Service_Limit.filter_rest_availability
participant DB as Booking Store
note over Filter,DB #f0f8ff: When querying availability for tracked services
REST->>API: Request availability for service/date range
API->>Filter: rest_post_dispatch -> filter_rest_availability(response, server, request)
Filter->>DB: get_daily_totals(dates) for tracked services
DB-->>Filter: totals per date
alt date total >= daily_limit
Filter->>API: override response -> mark date unavailable
else
Filter->>API: leave response unchanged
end
sequenceDiagram
autonumber
actor System
participant Creator as Booking Creator
participant Guard as GPB_Daily_Service_Limit.guard_booking_creation
participant DB as Booking Store
note over Guard,DB #f0fff0: On booking creation attempt
Creator->>Guard: gpb_before_booking_created(booking_data, bookable)
Guard->>Guard: normalize_booking_date(start,end,bookable)
Guard->>DB: get_totals_for_range(start_datetime,end_datetime, exclude_booking_id?)
DB-->>Guard: existing totals
alt existing + incoming_quantity > daily_limit
Guard-->>Creator: throw CapacityException (prevent creation)
else
Guard-->>Creator: allow creation to proceed
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Pre-merge checks and finishing touches❌ Failed checks (2 warnings)
✅ Passed checks (1 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🧰 Additional context used🪛 GitHub Actions: PHP Lint (PR)gp-bookings/gpb-daily-service-booking-limit.php[error] 197-197: PHPCompatibility error: Nullable return types are not supported in PHP 7.0 or earlier. (source: PHPCompatibility.FunctionDeclarations.NewNullableTypes.returnTypeFound). Command/Step: Run thenabeel/action-phpcs@v8. 🪛 GitHub Check: PHPCS (Files Changed)gp-bookings/gpb-daily-service-booking-limit.php[failure] 197-197: 🪛 PHPMD (2.15.0)gp-bookings/gpb-daily-service-booking-limit.php64-64: Avoid unused parameters such as '$server'. (undefined) (UnusedFormalParameter) 🔇 Additional comments (6)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 3
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
gp-bookings/gpb-daily-service-booking-limit.php
(1 hunks)
🧰 Additional context used
🪛 GitHub Check: PHPCS (Files Changed)
gp-bookings/gpb-daily-service-booking-limit.php
[warning] 209-209:
Array double arrow not aligned correctly; expected 5 space(s) between "'form_id'" and double arrow, but found 1.
[warning] 151-151:
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 6 spaces
[warning] 150-150:
Equals sign not aligned with surrounding assignments; expected 1 space but found 4 spaces
[failure] 33-33:
Tabs must be used to indent lines; spaces are not allowed
[failure] 32-32:
Tabs must be used to indent lines; spaces are not allowed
[failure] 31-31:
Tabs must be used to indent lines; spaces are not allowed
[failure] 30-30:
Tabs must be used to indent lines; spaces are not allowed
[failure] 29-29:
Tabs must be used to indent lines; spaces are not allowed
[failure] 28-28:
Tabs must be used to indent lines; spaces are not allowed
[failure] 26-26:
Tabs must be used to indent lines; spaces are not allowed
[failure] 25-25:
Tabs must be used to indent lines; spaces are not allowed
[failure] 24-24:
Tabs must be used to indent lines; spaces are not allowed
[failure] 23-23:
Tabs must be used to indent lines; spaces are not allowed
🪛 GitHub Actions: PHP Lint (PR)
gp-bookings/gpb-daily-service-booking-limit.php
[error] 23-23: Tabs must be used to indent lines; spaces are not allowed (Generic.WhiteSpace.DisallowSpaceIndent.SpacesUsed). PHPCS check failed.
🔇 Additional comments (2)
gp-bookings/gpb-daily-service-booking-limit.php (2)
54-69
: Good concurrency guard before booking creation.Validates against the cap with normalized dates and throws a CapacityException to prevent races. Solid safeguard.
112-116
: Confirm expected behavior for multi‑day bookings.Currently, the date normalized from start/end is reduced to the start day only. If multi‑day bookings should decrement capacity for each day in the range, this needs a loop over all dates in the span.
Do you intend to count only the start day, or every day in a multi‑day booking’s range?
gpb-daily-service-booking-limit.php
: Added new snippetgpb-daily-service-booking-limit.php
: Added new snippet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
gp-bookings/gpb-daily-service-booking-limit.php
(1 hunks)
🧰 Additional context used
🪛 GitHub Check: PHPCS (Files Changed)
gp-bookings/gpb-daily-service-booking-limit.php
[warning] 209-209:
Array double arrow not aligned correctly; expected 5 space(s) between "'form_id'" and double arrow, but found 1.
[failure] 193-193:
void return type is not present in PHP version 7.0 or earlier
[failure] 173-173:
Nullable return types are not supported in PHP 7.0 or earlier.
[failure] 82-82:
Short array syntax is not allowed
🪛 GitHub Actions: PHP Lint (PR)
gp-bookings/gpb-daily-service-booking-limit.php
[error] 82-82: PHPCS: Short array syntax is not allowed (Generic.Arrays.DisallowShortArraySyntax.Found)
🔇 Additional comments (2)
gp-bookings/gpb-daily-service-booking-limit.php (2)
97-105
: Don't skip Select Service bookings when enforcing the capWhen the booking field uses “Select Service,”
gpbService
is empty, so$service_id
stays 0 and we skip validation entirely, effectively disabling the limit for those forms. Please fall back to the posted child value before bailing out.- $service_id = isset( $service->gpbService ) ? (int) $service->gpbService : 0; - if ( ! $service_id || ! in_array( $service_id, $this->service_ids, true ) ) { + $service_id = isset( $service->gpbService ) ? (int) $service->gpbService : 0; + if ( ! $service_id ) { + $posted_service = $this->get_posted_value( (int) $service->id ); + $service_id = $posted_service ? (int) $posted_service : 0; + } + if ( ! $service_id || ! in_array( $service_id, $this->service_ids, true ) ) {
117-129
: Quantity lookup must resolve the actual child inputHard-coding
_3
for the quantity input breaks as soon as the field is reordered or uses the Quantity child. That causes us to undercount bookings and miss the cap. Resolve the quantity child and fall back to the legacy index only if needed.- $quantity = rgpost( 'input_' . (int) $field->id . '_3' ); - $quantity = $quantity === null || $quantity === '' ? 1 : max( 1, (int) $quantity ); + $quantity_field = $children['quantity'] ?? null; + $quantity_value = $quantity_field ? $this->get_posted_value( (int) $quantity_field->id ) : null; + if ( $quantity_value === null ) { + $quantity_value = rgpost( 'input_' . (int) $field->id . '_3' ); + } + $quantity = $quantity_value === null || $quantity_value === '' ? 1 : max( 1, (int) $quantity_value );
@SebastianWiz Strong! How hard/possible would it be to also prevent the day from being selected at all once the limit is reached? I wouldn't sink too much more time into this, just curious if it's an option? |
@spivurno Thank you! I've been exploring this and I think we could hook into the REST API endpoint that provides calendar availability to mark days as unavailable when they hit the limit. That way we could prevent the days from being selectable in the first place rather than showing a validation error after submission. That would definitely be better UX for sure. Should hopefully have an update to the snippet soon! |
Update: I rewrote the snippet to hook straight into the availability REST endpoint so days gets blocked from the calendar once they hit the daily limit |
Context
⛑️ Ticket(s): https://secure.helpscout.net/conversation/3084918923/89679?viewId=8172236
Summary
This PR adds a snippet that lets you set a daily booking limit for one or multiple services. The snippet hooks into Gravity Forms validation to block submissions once the combined quantity for that day reaches the limit and shows the “fully booked” validation message on the booking-time field. It also hooks
gpb_before_booking_created
so the same check runs during booking creation, preventing overbookings if multiple requests land at the same time.Loom demo: https://www.loom.com/share/6cae41c053a647e48deec68868e23ef4