-
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
Open
SebastianWiz
wants to merge
6
commits into
master
Choose a base branch
from
SebastianWiz-patch-3
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5ef5330
`gpb-daily-service-booking-limit.php`: Added new snippet
SebastianWiz 81915ec
`gpb-daily-service-booking-limit.php`: Added a new snippet
SebastianWiz 4a28114
`gpb-daily-service-booking-limit.php`: Added a new snippet.
SebastianWiz 1ea2f3f
`gpb-daily-service-booking-limit.php`: Added a new snippet.
SebastianWiz 11b6517
`gpb-daily-service-booking-limit.php`: Added new snippet
SebastianWiz 8fb788f
`gpb-daily-service-booking-limit.php`: Added new snippet
SebastianWiz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
<?php | ||
/** | ||
* Gravity Perks // GP Bookings // Daily Service Booking Limit | ||
* https://gravitywiz.com/documentation/gravity-forms-bookings/ | ||
* | ||
* Enforce a daily capacity for one or more GP Bookings services. When the selected services | ||
* meet the limit, new submissions are blocked and the booking time field displays a | ||
* "fully booked" message for that day. List multiple service IDs to share the cap between them. | ||
* | ||
* Instructions: | ||
* | ||
* 1. Install this snippet by following the steps here: | ||
* https://gravitywiz.com/documentation/how-do-i-install-a-snippet/ | ||
* | ||
* 2. Update the configuration at the bottom of the snippet: | ||
* - Set form_id to the Gravity Form that hosts your booking field (or leave false to run on every form). | ||
* - List the GP Bookings service IDs that should share the daily cap in service_ids. | ||
* - Adjust daily_limit to the maximum combined bookings allowed per day. | ||
* - Optionally customize capacity_message to change the validation text shown to users. | ||
*/ | ||
class GPB_Daily_Service_Limit { | ||
|
||
private $form_id; | ||
private $service_ids; | ||
private $daily_limit; | ||
private $capacity_message; | ||
|
||
public function __construct( array $args ) { | ||
$args = wp_parse_args( $args, array( | ||
'form_id' => false, | ||
'service_ids' => array(), | ||
'daily_limit' => 10, | ||
'capacity_message' => __( 'We are fully booked for that day. Please choose another date.', 'gp-bookings' ), | ||
)); | ||
|
||
$this->form_id = $args['form_id']; | ||
$this->service_ids = array_map( 'intval', (array) $args['service_ids'] ); | ||
$this->daily_limit = (int) $args['daily_limit']; | ||
$this->capacity_message = $args['capacity_message']; | ||
|
||
if ( empty( $this->service_ids ) ) { | ||
return; | ||
} | ||
|
||
add_action( 'gpb_before_booking_created', array( $this, 'guard_booking_creation' ), 10, 2 ); | ||
add_filter( 'gform_validation', array( $this, 'validate_submission' ) ); | ||
} | ||
|
||
public function guard_booking_creation( array $booking_data, $bookable ) { | ||
if ( ! $bookable instanceof \GP_Bookings\Service || ! in_array( $bookable->get_id(), $this->service_ids, true ) ) { | ||
return; | ||
} | ||
|
||
// Normalize to the start date in the site timezone so nightly/range bookings count correctly. | ||
$date = $this->normalize_booking_date( | ||
$booking_data['start_datetime'] ?? '', | ||
$booking_data['end_datetime'] ?? ( $booking_data['start_datetime'] ?? '' ), | ||
$bookable | ||
); | ||
if ( ! $date ) { | ||
return; | ||
} | ||
$quantity = isset( $booking_data['quantity'] ) ? max( 1, (int) $booking_data['quantity'] ) : 1; | ||
|
||
// Guard again at save time so last-second bookings can't slip past the form validation. | ||
if ( $this->get_total_for_date( $date ) + $quantity > $this->daily_limit ) { | ||
throw new \GP_Bookings\Exceptions\CapacityException( $this->capacity_message ); | ||
} | ||
} | ||
|
||
public function validate_submission( $result ) { | ||
$is_object = is_object( $result ); | ||
$form = $is_object ? $result->form : $result['form']; | ||
|
||
if ( $this->form_id && (int) $form['id'] !== (int) $this->form_id ) { | ||
return $result; | ||
} | ||
|
||
$is_valid = $is_object ? $result->is_valid : $result['is_valid']; | ||
|
||
// Track per-day totals so multiple booking fields in one submission don't exceed the cap. | ||
$daily_totals = []; | ||
|
||
foreach ( $form['fields'] as &$field ) { | ||
if ( ! isset( $field->inputType ) || $field->inputType !== 'gpb_booking' ) { | ||
continue; | ||
} | ||
|
||
$children = $field->get_child_fields( $form ); | ||
$service = $children['service'] ?? null; | ||
$time = $children['booking_time'] ?? null; | ||
|
||
if ( ! $service || ! $time ) { | ||
continue; | ||
} | ||
|
||
$service_id = isset( $service->gpbService ) ? (int) $service->gpbService : 0; | ||
if ( ! $service_id || ! in_array( $service_id, $this->service_ids, true ) ) { | ||
continue; | ||
} | ||
|
||
$service_model = \GP_Bookings\Service::get( $service_id ); | ||
if ( ! $service_model ) { | ||
continue; | ||
} | ||
|
||
$datetime = $this->get_posted_value( (int) $time->id ); | ||
if ( ! $datetime ) { | ||
continue; | ||
} | ||
|
||
$date = $this->normalize_booking_date( $datetime, $datetime, $service_model ); | ||
if ( ! $date ) { | ||
continue; | ||
} | ||
|
||
$quantity = rgpost( 'input_' . (int) $field->id . '_3' ); | ||
$quantity = $quantity === null || $quantity === '' ? 1 : max( 1, (int) $quantity ); | ||
|
||
// Reuse the current total for this date so we only hit the database once per day per submission. | ||
$current_total = $daily_totals[ $date ] ?? $this->get_total_for_date( $date ); | ||
|
||
if ( $current_total + $quantity > $this->daily_limit ) { | ||
$this->flag_field_error( $form, (int) $time->id ); | ||
$is_valid = false; | ||
continue; | ||
} | ||
|
||
$daily_totals[ $date ] = $current_total + $quantity; | ||
} | ||
|
||
unset( $field ); | ||
|
||
if ( ! $is_valid ) { | ||
$form['validation_message'] = $this->capacity_message; | ||
} | ||
|
||
if ( $is_object ) { | ||
$result->form = $form; | ||
$result->is_valid = $is_valid; | ||
return $result; | ||
} | ||
|
||
$result['form'] = $form; | ||
$result['is_valid'] = $is_valid; | ||
return $result; | ||
} | ||
|
||
private function get_total_for_date( string $date ): int { | ||
$start = $date . ' 00:00:00'; | ||
$end = $date . ' 23:59:59'; | ||
// Count both pending and confirmed bookings to reflect in-progress reservations. | ||
$bookings = \GP_Bookings\Queries\Booking_Query::get_bookings_in_range( | ||
$start, | ||
$end, | ||
array( | ||
'object_id' => $this->service_ids, | ||
'object_type' => 'service', | ||
'status' => array( 'pending', 'confirmed' ), | ||
'exclude_service_with_resource' => false, | ||
) | ||
); | ||
|
||
$total = 0; | ||
|
||
foreach ( $bookings as $booking ) { | ||
$total += (int) $booking->get_quantity(); | ||
} | ||
|
||
return $total; | ||
} | ||
|
||
private function normalize_booking_date( $start, $end, $bookable ): ?string { | ||
try { | ||
$normalized = \GP_Bookings\Booking::normalize_datetime_values( $start, $end, $bookable ); | ||
} catch ( \Throwable $e ) { | ||
return null; | ||
} | ||
|
||
return $normalized['start']->format( 'Y-m-d' ); | ||
} | ||
|
||
private function get_posted_value( int $field_id ) { | ||
$value = rgpost( 'input_' . $field_id ); | ||
|
||
if ( is_array( $value ) ) { | ||
$value = reset( $value ); | ||
} | ||
|
||
return $value === null || $value === '' ? null : $value; | ||
} | ||
|
||
private function flag_field_error( array &$form, int $field_id ): void { | ||
foreach ( $form['fields'] as &$field ) { | ||
if ( (int) $field->id === $field_id ) { | ||
$field->failed_validation = true; | ||
$field->validation_message = $this->capacity_message; | ||
break; | ||
} | ||
} | ||
|
||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
unset( $field ); | ||
} | ||
|
||
} | ||
|
||
# Configuration | ||
new GPB_Daily_Service_Limit( array( | ||
'form_id' => 123, | ||
'service_ids' => array( 45, 67 ), | ||
'daily_limit' => 10, | ||
// 'capacity_message' => '', | ||
) ); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.