Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions gp-inventory/gpi-populate-days.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
/**
* Gravity Perks // Inventory // Populate Days into Radio Field
* https://gravitywiz.com/documentation/gravity-forms-inventory/
*

Check failure on line 5 in gp-inventory/gpi-populate-days.php

View workflow job for this annotation

GitHub Actions / PHPCS (Files Changed)

Whitespace found at end of line
* Populate a Radio field with the next `n` days, and assign each choice to have `x` inventory.
*
* For example, you can populate a Radio field with the next 10 Thursdays, with each Thursday having an inventory of 25.
* This is useful if you offer a service on a specific day only, and need users to select which date they want.
*
* Additionally, you can set a cutoff day and time for showing a day this week. For example, you can only show the current
* week's Thursday if it is before 4pm on Tuesday. This is useful if you need to set a cut-off time for bookings this week.
*

Check failure on line 13 in gp-inventory/gpi-populate-days.php

View workflow job for this annotation

GitHub Actions / PHPCS (Files Changed)

Whitespace found at end of line
* Instructions:
*

Check failure on line 15 in gp-inventory/gpi-populate-days.php

View workflow job for this annotation

GitHub Actions / PHPCS (Files Changed)

Whitespace found at end of line
* 1. Install the snippet.
* https://gravitywiz.com/documentation/how-do-i-install-a-snippet/
*
* 2. Follow the inline instructions to configure the snippet for your form.
*/
// Change `123` to your form ID
add_filter( 'gform_pre_render_123', 'gw_populate_days_into_radio', 5, 1 );
add_filter( 'gform_pre_validation_123', 'gw_populate_days_into_radio', 5, 1 );
function gw_populate_days_into_radio( $form ) {

// Update `1` with your Radio Button field ID
$field_id = 1;

// Update `thursday` with the day you want to populate
$day = 'thursday';

// Update `2` to your cut-off day for booking this week. Monday is 1, Tuesday is 2 etc.
$cutoff_day = 2;

// Update `16` to your cut-off time for booking this week. The time is in 24 hour format, so 16 is 4pm.
$cutoff_time = 16;

// Update `10` to the number of days to populate
$number_of_days = 10;

// Update `25` to the inventory limit each day should have
$inventory = 25;

// Update `l, F j, Y` to the PHP date format you want the populated days to be shown in.
// More information about formats can be found here: https://www.php.net/manual/en/datetime.format.php
$format = 'l, F j, Y';

Check failure on line 47 in gp-inventory/gpi-populate-days.php

View workflow job for this annotation

GitHub Actions / PHPCS (Files Changed)

Whitespace found at end of line
// That's it, stop editing!

static $has_run = false;
if ( $has_run ) {
return $form;
}
$has_run = true;

foreach ( $form['fields'] as &$field ) {
if ( $field->id == $field_id && $field->type == 'radio' ) {

$choices = array();

Check warning on line 59 in gp-inventory/gpi-populate-days.php

View workflow job for this annotation

GitHub Actions / PHPCS (Files Changed)

Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space
$today = new DateTime();

Check warning on line 60 in gp-inventory/gpi-populate-days.php

View workflow job for this annotation

GitHub Actions / PHPCS (Files Changed)

Equals sign not aligned with surrounding assignments; expected 5 spaces but found 3 spaces
$start_day = new DateTime( 'this ' . $day );

// If it's past the cutoff, also skip this week's day
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Use the WordPress timezone to avoid server/WP offset drift.

Gravity Forms/WordPress operate in the site timezone; default DateTime() may use server TZ. Initialize with wp_timezone() for consistency.

-			$choices = array();
-			$today   = new DateTime();
-			$start_day = new DateTime( 'this ' . $day );
+			$choices   = array();
+			$tz        = function_exists( 'wp_timezone' ) ? wp_timezone() : null;
+			$today     = new DateTime( 'now', $tz );
+			$start_day = new DateTime( 'this ' . $day, $tz );
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
$choices = array();
$today = new DateTime();
$start_day = new DateTime( 'this ' . $day );
// If it's past the cutoff, also skip this week's day
$choices = array();
$tz = function_exists( 'wp_timezone' ) ? wp_timezone() : null;
$today = new DateTime( 'now', $tz );
$start_day = new DateTime( 'this ' . $day, $tz );
// If it's past the cutoff, also skip this week's day
🧰 Tools
🪛 GitHub Check: PHPCS (Files Changed)

[warning] 60-60:
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 3 spaces


[warning] 59-59:
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

🤖 Prompt for AI Agents
In gp-inventory/gpi-populate-days.php around lines 59 to 63, the DateTime
instances are created without specifying the WordPress timezone causing
potential server vs WP timezone drift; change the DateTime creation to use
wp_timezone() so both $today and $start_day are initialized with the site
timezone (i.e., construct them using the WordPress timezone object returned by
wp_timezone()) to ensure consistent date calculations.

if ( ( $today->format('N') == $cutoff_day && (int)$today->format('H') >= $cutoff_time ) || $today->format('N') > $cutoff_day && $today->format('N') <= $start_day->format('N') ) {

Check failure on line 64 in gp-inventory/gpi-populate-days.php

View workflow job for this annotation

GitHub Actions / PHPCS (Files Changed)

Expected 1 spaces after opening parenthesis; 0 found

Check failure on line 64 in gp-inventory/gpi-populate-days.php

View workflow job for this annotation

GitHub Actions / PHPCS (Files Changed)

Expected 1 spaces before closing parenthesis; 0 found

Check failure on line 64 in gp-inventory/gpi-populate-days.php

View workflow job for this annotation

GitHub Actions / PHPCS (Files Changed)

Expected 1 spaces after opening parenthesis; 0 found

Check failure on line 64 in gp-inventory/gpi-populate-days.php

View workflow job for this annotation

GitHub Actions / PHPCS (Files Changed)

Expected 1 space(s) after cast statement; 0 found

Check failure on line 64 in gp-inventory/gpi-populate-days.php

View workflow job for this annotation

GitHub Actions / PHPCS (Files Changed)

Expected 1 spaces before closing parenthesis; 0 found

Check failure on line 64 in gp-inventory/gpi-populate-days.php

View workflow job for this annotation

GitHub Actions / PHPCS (Files Changed)

Expected 1 spaces after opening parenthesis; 0 found
$start_day->modify('+1 week');
}

// Generate next n days
for ( $i = 0; $i < $number_of_days; $i++ ) {
$label = $start_day->format( $format );

Check warning on line 70 in gp-inventory/gpi-populate-days.php

View workflow job for this annotation

GitHub Actions / PHPCS (Files Changed)

Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space
$choices[] = array(
'text' => $label,

Check warning on line 72 in gp-inventory/gpi-populate-days.php

View workflow job for this annotation

GitHub Actions / PHPCS (Files Changed)

Array double arrow not aligned correctly; expected 12 space(s) between &quot;'text'&quot; and double arrow, but found 1.
'value' => $label,

Check warning on line 73 in gp-inventory/gpi-populate-days.php

View workflow job for this annotation

GitHub Actions / PHPCS (Files Changed)

Array double arrow not aligned correctly; expected 11 space(s) between &quot;'value'&quot; and double arrow, but found 1.
'inventory_limit' => $inventory,
);
$start_day->modify('+1 week');
}

$field->choices = $choices;
}
}

return $form;
}