From 4628e867ac4ef356643d8b23328748373facac88 Mon Sep 17 00:00:00 2001 From: Ryan Welcher Date: Wed, 7 Aug 2024 14:55:05 -0400 Subject: [PATCH 1/3] Add include current date control --- src/components/post-date-query-controls.js | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/components/post-date-query-controls.js b/src/components/post-date-query-controls.js index a6ee39e..12c5316 100644 --- a/src/components/post-date-query-controls.js +++ b/src/components/post-date-query-controls.js @@ -17,6 +17,7 @@ export const PostDateQueryControls = ( { attributes, setAttributes } ) => { date_secondary: dateSecondary = new Date(), inclusive: isInclusive = false, range = '', + current_date_in_range: currentDateInRange = false, } = {}, } = {}, } = attributes; @@ -66,6 +67,33 @@ export const PostDateQueryControls = ( { attributes, setAttributes } ) => { } ); } } /> + { range !== '' && ( + { + setAttributes( { + query: { + ...attributes.query, + date_query: { + ...attributes.query.date_query, + current_date_in_range: + newCurrentDateInRange, + }, + }, + } ); + } } + /> + ) } + Date: Wed, 7 Aug 2024 14:55:40 -0400 Subject: [PATCH 2/3] Clean up the process range command and have it support inclusive ranges. --- includes/Traits/Date_Query.php | 59 ++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 21 deletions(-) diff --git a/includes/Traits/Date_Query.php b/includes/Traits/Date_Query.php index 196d502..83ba827 100644 --- a/includes/Traits/Date_Query.php +++ b/includes/Traits/Date_Query.php @@ -19,8 +19,11 @@ public function process_date_query(): void { // Ranges and Relationships can't co-exist. $range = $date_query['range'] ?? false; + + if ( $date_query && $range && ! empty( $range ) ) { - $date_queries = $this->process_date_range( $range ); + $inclusive_range = isset( $date_query['current_date_in_range'] ) ? ( true === $date_query['current_date_in_range'] || 'true' === $date_query['current_date_in_range'] ) : false; + $date_queries = $this->process_date_range( $range, $inclusive_range ); } else { $date_queries = array(); $date_relationship = $date_query['relation'] ?? null; @@ -71,33 +74,47 @@ public function process_date_query(): void { /** * Generate the date ranges data * - * @param string $range The range as provided by the UI. + * @param string $range The range as provided by the UI. + * @param bool $inclusive_range Does the range end at the current date. */ - public function process_date_range( string $range ) { + public function process_date_range( string $range, bool $inclusive_range = false ) { switch ( $range ) { case 'last-month': - return array( - 'before' => 'today', - 'after' => 'first day of -1 months', - ); + $months_offset = '-1'; + break; case 'three-months': - return array( - 'before' => 'today', - 'after' => 'first day of -3 months', - ); - + $months_offset = '-3'; + break; case 'six-months': - return array( - 'before' => 'today', - 'after' => 'first day of -6 months', - ); - + $months_offset = '-6'; + break; case 'twelve-months': - return array( - 'before' => 'today', - 'after' => 'first day of -12 months', - ); + $months_offset = '-12'; + break; } + // Get the dates for the first and last day of the month offset. + $today = strtotime( 'today' ); + $after = strtotime( "first day of {$months_offset} months" ); + $before = strtotime( 'last day of last month' ); + + // Are we add the current date? + $range_to_use = $inclusive_range ? $today : $before; + + // Return the date query. + $date_query = array( + 'before' => array( + 'year' => gmdate( 'Y', $range_to_use ), + 'month' => gmdate( 'm', $range_to_use ), + 'day' => gmdate( 'd', $range_to_use ), + ), + 'after' => array( + 'year' => gmdate( 'Y', $after ), + 'month' => gmdate( 'm', $after ), + 'day' => gmdate( 'd', $after ), + ), + ); + + return $date_query; } } From bf6cfe024414ef376c9c2ca73ddc6ed6d7ff4ab2 Mon Sep 17 00:00:00 2001 From: Ryan Welcher Date: Wed, 7 Aug 2024 15:17:35 -0400 Subject: [PATCH 3/3] Update unit tests. --- tests/unit/Date_Query_Tests.php | 66 ++++++++++++++++++++++++++++----- 1 file changed, 56 insertions(+), 10 deletions(-) diff --git a/tests/unit/Date_Query_Tests.php b/tests/unit/Date_Query_Tests.php index fe5b692..583f765 100644 --- a/tests/unit/Date_Query_Tests.php +++ b/tests/unit/Date_Query_Tests.php @@ -26,8 +26,16 @@ public function data_range_and_relationship_are_discreet() { ], [ 'date_query' => [ - 'before' => 'today', - 'after' => 'first day of -1 months', + 'before' => [ + 'year' => gmdate( 'Y', strtotime( 'last day of last month' ) ), + 'month' => gmdate( 'm', strtotime( 'last day of last month' ) ), + 'day' => gmdate( 'd', strtotime( 'last day of last month' ) ), + ], + 'after' => [ + 'year' => gmdate( 'Y', strtotime( 'first day of -1 months' ) ), + 'month' => gmdate( 'm', strtotime( 'first day of -1 months' ) ), + 'day' => gmdate( 'd', strtotime( 'first day of -1 months' ) ), + ], ], ], ], @@ -92,6 +100,12 @@ public function test_range_and_relationship_are_discreet( $custom_data, $expecte * Data provider */ public function data_all_ranges_return_expected() { + + $today = strtotime( 'today' ); + $last_month = strtotime( 'first day of -1 months' ); + + $after = strtotime( "first day of {$months_offset} months" ); + $before = strtotime( 'last day of last month' ); return [ // Month [ @@ -102,8 +116,16 @@ public function data_all_ranges_return_expected() { ], [ 'date_query' => [ - 'before' => 'today', - 'after' => 'first day of -1 months', + 'before' => [ + 'year' => gmdate( 'Y', strtotime( 'last day of last month' ) ), + 'month' => gmdate( 'm', strtotime( 'last day of last month' ) ), + 'day' => gmdate( 'd', strtotime( 'last day of last month' ) ), + ], + 'after' => [ + 'year' => gmdate( 'Y', strtotime( 'first day of -1 months' ) ), + 'month' => gmdate( 'm', strtotime( 'first day of -1 months' ) ), + 'day' => gmdate( 'd', strtotime( 'first day of -1 months' ) ), + ], ], ], ], @@ -115,8 +137,16 @@ public function data_all_ranges_return_expected() { ], [ 'date_query' => [ - 'before' => 'today', - 'after' => 'first day of -3 months', + 'before' => [ + 'year' => gmdate( 'Y', strtotime( 'last day of last month' ) ), + 'month' => gmdate( 'm', strtotime( 'last day of last month' ) ), + 'day' => gmdate( 'd', strtotime( 'last day of last month' ) ), + ], + 'after' => [ + 'year' => gmdate( 'Y', strtotime( 'first day of -3 months' ) ), + 'month' => gmdate( 'm', strtotime( 'first day of -3 months' ) ), + 'day' => gmdate( 'd', strtotime( 'first day of -3 months' ) ), + ], ], ], ], @@ -128,8 +158,16 @@ public function data_all_ranges_return_expected() { ], [ 'date_query' => [ - 'before' => 'today', - 'after' => 'first day of -6 months', + 'before' => [ + 'year' => gmdate( 'Y', strtotime( 'last day of last month' ) ), + 'month' => gmdate( 'm', strtotime( 'last day of last month' ) ), + 'day' => gmdate( 'd', strtotime( 'last day of last month' ) ), + ], + 'after' => [ + 'year' => gmdate( 'Y', strtotime( 'first day of -6 months' ) ), + 'month' => gmdate( 'm', strtotime( 'first day of -6 months' ) ), + 'day' => gmdate( 'd', strtotime( 'first day of -6 months' ) ), + ], ], ], ], @@ -141,8 +179,16 @@ public function data_all_ranges_return_expected() { ], [ 'date_query' => [ - 'before' => 'today', - 'after' => 'first day of -12 months', + 'before' => [ + 'year' => gmdate( 'Y', strtotime( 'last day of last month' ) ), + 'month' => gmdate( 'm', strtotime( 'last day of last month' ) ), + 'day' => gmdate( 'd', strtotime( 'last day of last month' ) ), + ], + 'after' => [ + 'year' => gmdate( 'Y', strtotime( 'first day of -12 months' ) ), + 'month' => gmdate( 'm', strtotime( 'first day of -12 months' ) ), + 'day' => gmdate( 'd', strtotime( 'first day of -12 months' ) ), + ], ], ], ],