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

[pull] trunk from ryanwelcher:trunk #17

Merged
merged 10 commits into from
Aug 8, 2024
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;
}
}
2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: Advanced Query Loop
* Description: Query loop block variations to create custom queries.
* Plugin URI: https://github.com/ryanwelcher/advanced-query-loop/
* Version: 3.1.0
* Version: 3.1.1
* Requires at least: 6.2
* Requires PHP: 7.4
* Author: Ryan Welcher
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "advanced-query-loop",
"version": "3.1.0",
"version": "3.1.1",
"description": "Query loop block variations to create custom queries.",
"main": "index.js",
"scripts": {
Expand Down
7 changes: 6 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Contributors: welcher
Tags: Query Loop, Custom Queries
Requires at least: 6.2
Tested up to: 6.5.2
Stable tag: 3.1.0
Stable tag: 3.1.1
Requires PHP: 7.4
License: GPL v2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Expand Down Expand Up @@ -64,6 +64,11 @@ Sort in ascending or descending order by:
3. Query posts before a date, after a date or between two dates.

== Changelog ==
= 3.1.1=
* Add better SVG that works in all usages
* Change ranges to allow to not include the current date
* Trim whitespace from title.rendered

= 3.1.0 =
* Add dynamic date ranges to see posts from the last 1, 3, 6 and 12 months.
* Insert a new instance by typing "AQL" or "aql" and pressing enter.
Expand Down
37 changes: 22 additions & 15 deletions src/components/icons.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
/**
* WordPress dependencies
*/
import { SVG, Path } from '@wordpress/primitives';

function AQLIcon() {
return (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 221.3 111.6">
<text
fill="#e76f51"
fontFamily="Montserrat-Bold, Montserrat"
fontSize="100"
transform="translate(.9 85)"
>
<tspan x="0" y="0" letterSpacing="0em">
A
</tspan>
<tspan x="75.6" y="0" letterSpacing="0em">
QL
</tspan>
</text>
</svg>
<SVG
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
fill="none"
viewBox="0 0 24 24"
>
<Path fill="#2D2B55" d="M0 0h24v24H0V0z"></Path>
<Path
fill="#fff"
d="M17.936 8.438h1.217v6.125h2.867v1.093h-4.084V8.437zM10.364 12.047c0 .515.11.97.33 1.361.22.392.519.701.897.928.385.22.815.33 1.289.33.481 0 .907-.106 1.279-.32.371-.22.663-.522.876-.907.213-.385.32-.825.32-1.32 0-.495-.107-.942-.32-1.34a2.455 2.455 0 00-.886-.95c-.372-.233-.812-.35-1.32-.35-.475 0-.898.11-1.269.33-.371.22-.663.526-.877.918-.213.385-.32.825-.32 1.32zm-1.29 0c0-.543.094-1.042.28-1.495.192-.454.456-.846.793-1.176a3.685 3.685 0 011.197-.784c.46-.185.955-.278 1.485-.278.543 0 1.041.093 1.495.278a3.57 3.57 0 011.186.784c.337.33.598.722.784 1.176.185.453.278.952.278 1.495 0 .536-.093 1.034-.278 1.495-.186.46-.447.86-.784 1.196-.33.337-.722.602-1.176.795a3.941 3.941 0 01-1.505.278 3.964 3.964 0 01-1.496-.278 3.676 3.676 0 01-1.196-.795 3.545 3.545 0 01-.784-1.196 3.964 3.964 0 01-.278-1.495zm3.425.515H13.9l3.248 3.146h-1.433l-3.217-3.146z"
></Path>
<Path
fill="#E76F51"
d="M3.546 13.8l.227-1.031h3.351l.237 1.031H3.546zm1.887-3.32l-1.134 2.65-.021.268-1 2.258H1.906l3.527-7.62 3.527 7.62H7.588l-.98-2.196-.03-.3-1.145-2.68z"
></Path>
</SVG>
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/components/multiple-post-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const MultiplePostSelect = ( { attributes, setAttributes } ) => {

const postTypes = useSelect( ( select ) =>
select( coreStore )
.getPostTypes({ per_page: 50 })
.getPostTypes( { per_page: 50 } )
?.filter( ( { viewable } ) => viewable )
?.map( ( { slug } ) => slug )
);
Expand Down
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
2 changes: 1 addition & 1 deletion src/components/post-include-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export const PostIncludeControls = ( { attributes, setAttributes } ) => {
const getPostId = ( postTitle ) => {
const foundPost =
includePosts.find( ( post ) => post.title === postTitle ) ||
posts.find( ( post ) => post.title.rendered === postTitle );
posts.find( ( post ) => post.title.rendered.trim() === postTitle );

return foundPost.title.rendered
? { id: foundPost.id, title: foundPost.title.rendered }
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
Loading