From 5405553c6a5773ee8d436d780da62c959d273e8c Mon Sep 17 00:00:00 2001 From: Tetrakern <26898880+Tetrakern@users.noreply.github.com> Date: Sun, 11 Jun 2023 03:03:08 +0200 Subject: [PATCH] Add function to append date query And consider the orderby argument because this makes sense to me. Published Last Week should only show what was published last week, not what was updated. --- chapters.php | 35 +-------------- collections.php | 35 +-------------- includes/functions/_utility.php | 77 +++++++++++++++++++++++++++++++++ recommendations.php | 35 +-------------- stories.php | 35 +-------------- 5 files changed, 85 insertions(+), 132 deletions(-) diff --git a/chapters.php b/chapters.php index 4f4fdcb6..e55970ab 100644 --- a/chapters.php +++ b/chapters.php @@ -38,39 +38,8 @@ $query_args = array ( 'meta_value' => '0' ); -// Date query? -if ( is_numeric( $ago ) && $ago > 0 ) { - $query_args['date_query'] = array( - 'relation' => 'OR', - array( - 'after'=> "{$ago} days ago", - 'inclusive' => true, - ), - array( - 'column' => 'post_modified', - 'after'=> "{$ago} days ago", - 'inclusive' => true, - ) - ); -} elseif ( ! empty( $ago ) ) { - $ago = array_intersect( [strtolower( $ago )], ['week', 'month', 'year'] ); - $ago = reset( $ago ) ?: 0; - - if ( ! empty( $ago ) ) { - $query_args['date_query'] = array( - 'relation' => 'OR', - array( - 'after'=> "1 {$ago} ago", - 'inclusive' => true, - ), - array( - 'column' => 'post_modified', - 'after'=> "1 {$ago} ago", - 'inclusive' => true, - ) - ); - } -} +// Append date query (if any) +$query_args = fictioneer_append_date_query( $query_args, $ago, $order, $orderby ); // Filter query arguments $query_args = apply_filters( 'fictioneer_filter_chapters_query_args', $query_args, get_the_ID() ); diff --git a/collections.php b/collections.php index e5cf06d1..fa67c248 100644 --- a/collections.php +++ b/collections.php @@ -36,39 +36,8 @@ $query_args = array ( 'posts_per_page' => get_option( 'posts_per_page' ) ?? 8 ); -// Date query? -if ( is_numeric( $ago ) && $ago > 0 ) { - $query_args['date_query'] = array( - 'relation' => 'OR', - array( - 'after'=> "{$ago} days ago", - 'inclusive' => true, - ), - array( - 'column' => 'post_modified', - 'after'=> "{$ago} days ago", - 'inclusive' => true, - ) - ); -} elseif ( ! empty( $ago ) ) { - $ago = array_intersect( [strtolower( $ago )], ['week', 'month', 'year'] ); - $ago = reset( $ago ) ?: 0; - - if ( ! empty( $ago ) ) { - $query_args['date_query'] = array( - 'relation' => 'OR', - array( - 'after'=> "1 {$ago} ago", - 'inclusive' => true, - ), - array( - 'column' => 'post_modified', - 'after'=> "1 {$ago} ago", - 'inclusive' => true, - ) - ); - } -} +// Append date query (if any) +$query_args = fictioneer_append_date_query( $query_args, $ago, $order, $orderby ); // Filter query arguments $query_args = apply_filters( 'fictioneer_filter_collections_query_args', $query_args, get_the_ID() ); diff --git a/includes/functions/_utility.php b/includes/functions/_utility.php index 52b9cfc9..c3a09840 100644 --- a/includes/functions/_utility.php +++ b/includes/functions/_utility.php @@ -1569,4 +1569,81 @@ if ( ! function_exists( 'fictioneer_minify_html' ) ) { } } +// ============================================================================= +// APPEND DATE QUERY +// ============================================================================= + +if ( ! function_exists( 'fictioneer_append_date_query' ) ) { + /** + * Append date query to query arguments + * + * @since 5.4.0 + * + * @param array $query_args The query arguments to modify. + * @param string|int $ago The time range. Default null. + * @param string $order The current order. Default null. + * @param string $orderby The current orderby. Default null. + * + * @return array The modified query arguments. + */ + + function fictioneer_append_date_query( $query_args, $ago = null, $order = null, $orderby = null ) { + // Ago? + if ( empty( $ago ) ) { + $ago = $_GET['ago'] ?? 0; + $ago = is_numeric( $ago ) ? absint( $ago ) : sanitize_text_field( $ago ); + } + + // Order? + if ( empty( $order ) ) { + $order = array_intersect( [strtolower( $_GET['order'] ?? 0 )], ['desc', 'asc'] ); + $order = reset( $order ) ?: 'desc'; + } + + // Orderby? + if ( empty( $orderby ) ) { + $orderby = array_intersect( [strtolower( $_GET['orderby'] ?? 0 )], ['modified', 'date', 'title', 'rand'] ); + $orderby = reset( $orderby ) ?: 'modified'; + } + + // Date queried? + if ( is_numeric( $ago ) && $ago > 0 ) { + $query_args['date_query'] = array( + array( + 'column' => $orderby === 'modified' ? 'post_modified' : 'post_date', + 'after'=> "{$ago} days ago", + 'inclusive' => true, + ) + ); + } elseif ( ! empty( $ago ) ) { + $ago = array_intersect( [strtolower( $ago )], ['week', 'month', 'year'] ); + $ago = reset( $ago ) ?: 0; + + if ( ! empty( $ago ) ) { + $query_args['date_query'] = array( + array( + 'column' => $orderby === 'modified' ? 'post_modified' : 'post_date', + 'after'=> "1 {$ago} ago", + 'inclusive' => true, + ) + ); + } + } + + // Non-date related order? + if ( isset( $query_args['date_query'] ) && in_array( $orderby, ['title', 'rand'] ) ) { + $modified_arg = $query_args['date_query'][0]; + $modified_arg['column'] = 'post_modified'; + + $query_args['date_query'] = array( + 'relation' => 'OR', + $query_args['date_query'][0], + $modified_arg + ); + } + + return $query_args; + } +} + ?> diff --git a/recommendations.php b/recommendations.php index 389a5e88..4760e875 100644 --- a/recommendations.php +++ b/recommendations.php @@ -36,39 +36,8 @@ $query_args = array ( 'posts_per_page' => get_option( 'posts_per_page', 8 ) ); -// Date query? -if ( is_numeric( $ago ) && $ago > 0 ) { - $query_args['date_query'] = array( - 'relation' => 'OR', - array( - 'after'=> "{$ago} days ago", - 'inclusive' => true, - ), - array( - 'column' => 'post_modified', - 'after'=> "{$ago} days ago", - 'inclusive' => true, - ) - ); -} elseif ( ! empty( $ago ) ) { - $ago = array_intersect( [strtolower( $ago )], ['week', 'month', 'year'] ); - $ago = reset( $ago ) ?: 0; - - if ( ! empty( $ago ) ) { - $query_args['date_query'] = array( - 'relation' => 'OR', - array( - 'after'=> "1 {$ago} ago", - 'inclusive' => true, - ), - array( - 'column' => 'post_modified', - 'after'=> "1 {$ago} ago", - 'inclusive' => true, - ) - ); - } -} +// Append date query (if any) +$query_args = fictioneer_append_date_query( $query_args, $ago, $order, $orderby ); // Filter query arguments $query_args = apply_filters( 'fictioneer_filter_recommendations_query_args', $query_args, get_the_ID() ); diff --git a/stories.php b/stories.php index 757f95bf..adec8c8c 100644 --- a/stories.php +++ b/stories.php @@ -48,39 +48,8 @@ $query_args = array ( ) ); -// Date query? -if ( is_numeric( $ago ) && $ago > 0 ) { - $query_args['date_query'] = array( - 'relation' => 'OR', - array( - 'after'=> "{$ago} days ago", - 'inclusive' => true, - ), - array( - 'column' => 'post_modified', - 'after'=> "{$ago} days ago", - 'inclusive' => true, - ) - ); -} elseif ( ! empty( $ago ) ) { - $ago = array_intersect( [strtolower( $ago )], ['week', 'month', 'year'] ); - $ago = reset( $ago ) ?: 0; - - if ( ! empty( $ago ) ) { - $query_args['date_query'] = array( - 'relation' => 'OR', - array( - 'after'=> "1 {$ago} ago", - 'inclusive' => true, - ), - array( - 'column' => 'post_modified', - 'after'=> "1 {$ago} ago", - 'inclusive' => true, - ) - ); - } -} +// Append date query (if any) +$query_args = fictioneer_append_date_query( $query_args, $ago, $order, $orderby ); // Filter query arguments $query_args = apply_filters( 'fictioneer_filter_stories_query_args', $query_args, get_the_ID() );