Skip to content
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

Date Controls: Add before and after current date controls. #87

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ DNU
composer.lock
.phpunit.*
artifacts
.vscode
114 changes: 78 additions & 36 deletions includes/Traits/Date_Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,57 +20,99 @@ public function process_date_query(): void {
// Ranges and Relationships can't co-exist.
$range = $date_query['range'] ?? false;


if ( $date_query && $range && ! empty( $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;
$date_primary = $date_query['date_primary'] ?? null;
if ( $date_query && $date_relationship && $date_primary ) {
$date_is_inclusive = $date_query['inclusive'] ?? false;
$date_secondary = $date_query['date_secondary'] ?? null;

// Date format: 2022-12-27T11:14:21.
$primary_year = substr( $date_primary, 0, 4 );
$primary_month = substr( $date_primary, 5, 2 );
$primary_day = substr( $date_primary, 8, 2 );

if ( 'between' === $date_relationship && $date_secondary ) {
$secondary_year = substr( $date_secondary, 0, 4 );
$secondary_month = substr( $date_secondary, 5, 2 );
$secondary_day = substr( $date_secondary, 8, 2 );

$date_queries = array(
'after' => array(
'year' => $primary_year,
'month' => $primary_month,
'day' => $primary_day,
),
'before' => array(
'year' => $secondary_year,
'month' => $secondary_month,
'day' => $secondary_day,
),
);
} else {
$date_queries = array(
$date_relationship => array(
'year' => $primary_year,
'month' => $primary_month,
'day' => $primary_day,
),
);

if ( $date_query && $date_relationship ) {

if ( 'before-current' === $date_relationship || 'after-current' === $date_relationship ) {
switch ( $date_relationship ) {
case 'before-current':
$date_queries = $this->show_before_current_date();
break;
case 'after-current':
$date_queries = $this->show_after_current_date();
break;
}
} elseif ( $date_primary ) {
$date_is_inclusive = $date_query['inclusive'] ?? false;
$date_secondary = $date_query['date_secondary'] ?? null;

// Date format: 2022-12-27T11:14:21.
$primary_year = substr( $date_primary, 0, 4 );
$primary_month = substr( $date_primary, 5, 2 );
$primary_day = substr( $date_primary, 8, 2 );

if ( 'between' === $date_relationship && $date_secondary ) {
$secondary_year = substr( $date_secondary, 0, 4 );
$secondary_month = substr( $date_secondary, 5, 2 );
$secondary_day = substr( $date_secondary, 8, 2 );

$date_queries = array(
'after' => array(
'year' => $primary_year,
'month' => $primary_month,
'day' => $primary_day,
),
'before' => array(
'year' => $secondary_year,
'month' => $secondary_month,
'day' => $secondary_day,
),
);
} else {
$date_queries = array(
$date_relationship => array(
'year' => $primary_year,
'month' => $primary_month,
'day' => $primary_day,
),
);
}
$date_queries['inclusive'] = $date_is_inclusive;
}
$date_queries['inclusive'] = $date_is_inclusive;
}
}

// Return the date queries.
$this->custom_args['date_query'] = array_filter( $date_queries );
}

/**
* Generate the query to only show content before the current date
*/
public function show_before_current_date() {
$today = strtotime( 'today' );
// Return the date query.
return array(
'before' => array(
'year' => gmdate( 'Y', $today ),
'month' => gmdate( 'm', $today ),
'day' => gmdate( 'd', $today ),
),
);
}

/**
* Generate the query to only show content after the current date
*/
public function show_after_current_date() {
$today = strtotime( 'today' );
// Return the date query.
return array(
'after' => array(
'year' => gmdate( 'Y', $today ),
'month' => gmdate( 'm', $today ),
'day' => gmdate( 'd', $today ),
),
);
}

/**
* Generate the date ranges data
*
Expand Down
158 changes: 93 additions & 65 deletions src/components/post-date-query-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export const PostDateQueryControls = ( { attributes, setAttributes } ) => {
} = {},
} = {},
} = attributes;

return (
<>
<h2>{ __( 'Post Date Query', 'advanced-query-loop' ) }</h2>
Expand Down Expand Up @@ -97,7 +96,7 @@ export const PostDateQueryControls = ( { attributes, setAttributes } ) => {
<SelectControl
label={ __( 'Date Relationship', 'advanced-query-loop' ) }
help={ __(
'Show posts before, after, or between the selected date(s).',
'Show posts before or after the current date, or before, after, or between specific dates.',
'advanced-query-loop'
) }
value={ relationFromQuery }
Expand All @@ -108,15 +107,38 @@ export const PostDateQueryControls = ( { attributes, setAttributes } ) => {
value: '',
},
{
label: __( 'Before', 'advanced-query-loop' ),
label: __(
'Before current date',
'advanced-query-loop'
),
value: 'before-current',
},
{
label: __(
'After current date',
'advanced-query-loop'
),
value: 'after-current',
},
{
label: __(
'Before specific date',
'advanced-query-loop'
),
value: 'before',
},
{
label: __( 'After', 'advanced-query-loop' ),
label: __(
'After specific date',
'advanced-query-loop'
),
value: 'after',
},
{
label: __( 'Between', 'advanced-query-loop' ),
label: __(
'Between specific dates',
'advanced-query-loop'
),
value: 'between',
},
] }
Expand All @@ -135,71 +157,77 @@ export const PostDateQueryControls = ( { attributes, setAttributes } ) => {
} );
} }
/>
{ relationFromQuery !== '' && (
<>
{ relationFromQuery === 'between' && (
<h4>{ __( 'Start date', 'advanced-query-loop' ) }</h4>
) }
<DatePicker
currentDate={ datePrimary }
onChange={ ( newDate ) => {
setAttributes( {
query: {
...attributes.query,
date_query: {
...attributes.query.date_query,
date_primary: newDate,
{ relationFromQuery !== '' &&
! relationFromQuery.includes( 'current' ) && (
<>
{ relationFromQuery === 'between' && (
<h4>
{ __( 'Start date', 'advanced-query-loop' ) }
</h4>
) }
<DatePicker
currentDate={ datePrimary }
onChange={ ( newDate ) => {
setAttributes( {
query: {
...attributes.query,
date_query: {
...attributes.query.date_query,
date_primary: newDate,
},
},
},
} );
} }
/>
} );
} }
/>

{ relationFromQuery === 'between' && (
<>
<h4>{ __( 'End date', 'advanced-query-loop' ) }</h4>
<DatePicker
currentDate={ dateSecondary }
onChange={ ( newDate ) => {
setAttributes( {
query: {
...attributes.query,
date_query: {
...attributes.query.date_query,
date_secondary: newDate,
{ relationFromQuery === 'between' && (
<>
<h4>
{ __( 'End date', 'advanced-query-loop' ) }
</h4>
<DatePicker
currentDate={ dateSecondary }
onChange={ ( newDate ) => {
setAttributes( {
query: {
...attributes.query,
date_query: {
...attributes.query
.date_query,
date_secondary: newDate,
},
},
},
} );
} }
/>
</>
) }

<br />
<CheckboxControl
label={ __(
'Include selected date(s)',
'advanced-query-loop'
) }
help={ __(
'Should the selected date(s) be included in your query?',
'advanced-query-loop'
} );
} }
/>
</>
) }
checked={ isInclusive }
onChange={ ( newIsInclusive ) => {
setAttributes( {
query: {
...attributes.query,
date_query: {
...attributes.query.date_query,
inclusive: newIsInclusive,

<br />
<CheckboxControl
label={ __(
'Include selected date(s)',
'advanced-query-loop'
) }
help={ __(
'Should the selected date(s) be included in your query?',
'advanced-query-loop'
) }
checked={ isInclusive }
onChange={ ( newIsInclusive ) => {
setAttributes( {
query: {
...attributes.query,
date_query: {
...attributes.query.date_query,
inclusive: newIsInclusive,
},
},
},
} );
} }
/>
</>
) }
} );
} }
/>
</>
) }
</>
);
};
Loading