Skip to content

Commit

Permalink
Merge pull request #79 from ryanwelcher/feature/add-inclusive-option-…
Browse files Browse the repository at this point in the history
…for-date-ranges

Change ranges to allow to not include the current date.
  • Loading branch information
ryanwelcher authored Aug 7, 2024
2 parents 7cefb2b + bf6cfe0 commit 54b654d
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 31 deletions.
59 changes: 38 additions & 21 deletions includes/Traits/Date_Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
28 changes: 28 additions & 0 deletions src/components/post-date-query-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,6 +67,33 @@ export const PostDateQueryControls = ( { attributes, setAttributes } ) => {
} );
} }
/>
{ range !== '' && (
<CheckboxControl
label={ __(
'Include up to current date',
'advanced-query-loop'
) }
help={ __(
'Should the dynamic range include up to the current date?',
'advanced-query-loop'
) }
disabled={ range === '' }
checked={ currentDateInRange }
onChange={ ( newCurrentDateInRange ) => {
setAttributes( {
query: {
...attributes.query,
date_query: {
...attributes.query.date_query,
current_date_in_range:
newCurrentDateInRange,
},
},
} );
} }
/>
) }

<SelectControl
label={ __( 'Date Relationship', 'advanced-query-loop' ) }
help={ __(
Expand Down
66 changes: 56 additions & 10 deletions tests/unit/Date_Query_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) ),
],
],
],
],
Expand Down Expand Up @@ -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
[
Expand All @@ -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' ) ),
],
],
],
],
Expand All @@ -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' ) ),
],
],
],
],
Expand All @@ -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' ) ),
],
],
],
],
Expand All @@ -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' ) ),
],
],
],
],
Expand Down

0 comments on commit 54b654d

Please sign in to comment.