Skip to content

Commit 938ea3c

Browse files
authored
Fix PHP warning when using Date Range filter with single date (#2420)
When entering only a start or end date in a Date Range filter, the search widget attempted to access array element [0] on an associative array, causing: "PHP Warning: Undefined array key 0 in class-search-widget.php on line 829" Changed array access from $value[0] to reset($value) to properly handle both numeric and associative arrays. Fixes issue where Date Range filters were ignored when only one date was entered in DataTables layout Views. Resolves GravityKit/DataTables#268 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Date Range filters now correctly handle cases when only a start or only an end date is entered. * Resolved an issue affecting the DataTables layout. * **Documentation** * Updated changelog to reflect the Date Range and layout fixes. * **Tests** * Added unit coverage verifying Date Range filter behavior for associative input (start/end) to prevent warnings and ensure correct filtering. <!-- end of auto-generated comment: release notes by coderabbit.ai --> 💾 [Build file](https://www.dropbox.com/scl/fi/1aho7s9v5tpdfox8kri1l/gravityview-2.43.3-e2ea39417.zip?rlkey=o403p1il6uyhowvo6tdxdrsdy&dl=1) (e2ea394).
2 parents 7b16efd + e2ea394 commit 938ea3c

File tree

3 files changed

+137
-3
lines changed

3 files changed

+137
-3
lines changed

includes/widgets/search-widget/class-search-widget.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,7 @@ public function filter_entries( $search_criteria, $form_id = null, $args = [], $
833833
gv_empty( $value, false, false )
834834
|| (
835835
is_array( $value ) && 1 === count( $value )
836-
&& gv_empty( $value[0], false, false )
836+
&& gv_empty( reset( $value ), false, false )
837837
)
838838
) {
839839
/**

readme.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,10 @@ Beautifully display your Gravity Forms entries. Learn more on [gravitykit.com](h
2424
= develop =
2525

2626
#### 🐛 Fixed
27-
27+
* Date Range filters now work correctly when only a start or end date is entered. Also fixes the issue when using the DataTables layout.
2828
* Some Search Field icons were displaying too large.
2929

3030
#### ✨ Improved
31-
3231
* Detection of BuddyBoss and BuddyPress pages on groups pages as well as user profile pages.
3332
* Performance of [Magic Links](https://www.gravitykit.com/products/magic-links/) validation.
3433

tests/unit-tests/GravityView_Widget_Search_Test.php

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2045,4 +2045,139 @@ public function test_search_everything_limited_to_visible_fields(): void {
20452045
remove_filter( 'gk/gravityview/widget/search/visible_fields_only', '__return_false' );
20462046
unset( $_GET['gv_search'] );
20472047
}
2048+
2049+
/**
2050+
* Test that Date Range filter with single date (associative array) doesn't cause PHP warning.
2051+
* Regression test for fix in commit ac90184c1.
2052+
*
2053+
* @covers GravityView_Widget_Search::filter_entries()
2054+
* @group GravityView_Widget_Search
2055+
* @since 2.43.3
2056+
*/
2057+
public function test_date_range_filter_with_associative_array() {
2058+
// Test that filter_entries() handles associative arrays properly
2059+
// This addresses the fix where reset($value) is used instead of $value[0]
2060+
// to avoid "Undefined array key 0" warning with associative arrays
2061+
2062+
$form = $this->factory->form->import_and_get( 'complete.json' );
2063+
$view = $this->factory->view->create_and_get( array(
2064+
'form_id' => $form['id'],
2065+
'template_id' => 'table',
2066+
'fields' => array(
2067+
'directory_table-columns' => array(
2068+
wp_generate_password( 4, false ) => array(
2069+
'id' => '3',
2070+
'label' => 'Date',
2071+
),
2072+
),
2073+
),
2074+
'widgets' => array(
2075+
'header_top' => array(
2076+
wp_generate_password( 4, false ) => array(
2077+
'id' => 'search_bar',
2078+
'search_fields' => '[{"field":"3","input":"date_range"}]',
2079+
),
2080+
),
2081+
),
2082+
) );
2083+
2084+
// Create test entries
2085+
$this->factory->entry->create_and_get( array(
2086+
'form_id' => $form['id'],
2087+
'status' => 'active',
2088+
'3' => '2025-08-01',
2089+
) );
2090+
2091+
$this->factory->entry->create_and_get( array(
2092+
'form_id' => $form['id'],
2093+
'status' => 'active',
2094+
'3' => '2025-08-05',
2095+
) );
2096+
2097+
$this->factory->entry->create_and_get( array(
2098+
'form_id' => $form['id'],
2099+
'status' => 'active',
2100+
'3' => '2025-08-10',
2101+
) );
2102+
2103+
// Test with associative array containing only 'start' key
2104+
// This simulates the DataTables date range filter with only start date
2105+
$_GET = array(
2106+
'filter_3' => array( 'start' => '2025-08-01' )
2107+
);
2108+
2109+
add_filter( 'gravityview/widgets/search/datepicker/format', function() { return 'ymd_dash'; } );
2110+
2111+
// Before the fix, this would generate a PHP warning:
2112+
// "Undefined array key 0 in class-search-widget.php on line 836"
2113+
// After the fix, it should work without warnings and properly filter entries
2114+
$search_criteria = $this->widget->filter_entries( array(), null, array( 'id' => $view->ID ), true );
2115+
2116+
// Verify the search criteria was built correctly
2117+
$this->assertArrayHasKey( 'field_filters', $search_criteria );
2118+
$this->assertIsArray( $search_criteria['field_filters'] );
2119+
2120+
// Find the date filter in the field_filters array
2121+
$date_filter_found = false;
2122+
foreach ( $search_criteria['field_filters'] as $filter ) {
2123+
if ( is_array( $filter ) && isset( $filter['key'] ) && $filter['key'] === '3' ) {
2124+
$date_filter_found = true;
2125+
// Verify the filter was created with the start date
2126+
$this->assertEquals( '>=', $filter['operator'] );
2127+
$this->assertEquals( '2025-08-01', $filter['value'] );
2128+
break;
2129+
}
2130+
}
2131+
$this->assertTrue( $date_filter_found, 'Date filter should be created from associative array with start key' );
2132+
2133+
// Test with associative array containing only 'end' key
2134+
$_GET = array(
2135+
'filter_3' => array( 'end' => '2025-08-10' )
2136+
);
2137+
2138+
$search_criteria = $this->widget->filter_entries( array(), null, array( 'id' => $view->ID ), true );
2139+
2140+
// Find the date filter in the field_filters array
2141+
$date_filter_found = false;
2142+
foreach ( $search_criteria['field_filters'] as $filter ) {
2143+
if ( is_array( $filter ) && isset( $filter['key'] ) && $filter['key'] === '3' ) {
2144+
$date_filter_found = true;
2145+
// Verify the filter was created with the end date
2146+
$this->assertEquals( '<=', $filter['operator'] );
2147+
$this->assertEquals( '2025-08-10', $filter['value'] );
2148+
break;
2149+
}
2150+
}
2151+
$this->assertTrue( $date_filter_found, 'Date filter should be created from associative array with end key' );
2152+
2153+
// Test with both start and end dates (full date range)
2154+
$_GET = array(
2155+
'filter_3' => array( 'start' => '2025-08-01', 'end' => '2025-08-10' )
2156+
);
2157+
2158+
$search_criteria = $this->widget->filter_entries( array(), null, array( 'id' => $view->ID ), true );
2159+
2160+
// Count the date filters - should have two (one for start, one for end)
2161+
$date_filter_count = 0;
2162+
$has_start_filter = false;
2163+
$has_end_filter = false;
2164+
foreach ( $search_criteria['field_filters'] as $filter ) {
2165+
if ( is_array( $filter ) && isset( $filter['key'] ) && $filter['key'] === '3' ) {
2166+
$date_filter_count++;
2167+
if ( $filter['operator'] === '>=' && $filter['value'] === '2025-08-01' ) {
2168+
$has_start_filter = true;
2169+
}
2170+
if ( $filter['operator'] === '<=' && $filter['value'] === '2025-08-10' ) {
2171+
$has_end_filter = true;
2172+
}
2173+
}
2174+
}
2175+
$this->assertEquals( 2, $date_filter_count, 'Should have two date filters for full date range' );
2176+
$this->assertTrue( $has_start_filter, 'Should have start date filter' );
2177+
$this->assertTrue( $has_end_filter, 'Should have end date filter' );
2178+
2179+
// Clean up
2180+
remove_all_filters( 'gravityview/widgets/search/datepicker/format' );
2181+
$_GET = array();
2182+
}
20482183
}

0 commit comments

Comments
 (0)