From 9a27b0d51d8b78178a0ba95a7387eadbcb61bace Mon Sep 17 00:00:00 2001 From: Carsten Bach Date: Thu, 19 Sep 2024 05:10:41 +0200 Subject: [PATCH 01/11] Make event query respect ORDERBY, ORDER and 'inclusive running events' --- includes/core/classes/class-event-query.php | 103 +++++++++++++++----- 1 file changed, 78 insertions(+), 25 deletions(-) diff --git a/includes/core/classes/class-event-query.php b/includes/core/classes/class-event-query.php index f59092a5b..fe97aa0b9 100644 --- a/includes/core/classes/class-event-query.php +++ b/includes/core/classes/class-event-query.php @@ -53,7 +53,7 @@ protected function __construct() { */ protected function setup_hooks(): void { add_action( 'pre_get_posts', array( $this, 'prepare_event_query_before_execution' ) ); - add_filter( 'posts_clauses', array( $this, 'adjust_admin_event_sorting' ) ); + add_filter( 'posts_clauses', array( $this, 'adjust_admin_event_sorting' ), 10, 2 ); } /** @@ -231,10 +231,10 @@ static function () use ( $page_id ) { switch ( $events_query ) { case 'upcoming': remove_filter( 'posts_clauses', array( $this, 'adjust_sorting_for_past_events' ) ); - add_filter( 'posts_clauses', array( $this, 'adjust_sorting_for_upcoming_events' ) ); + add_filter( 'posts_clauses', array( $this, 'adjust_sorting_for_upcoming_events' ), 10, 2 ); break; case 'past': - add_filter( 'posts_clauses', array( $this, 'adjust_sorting_for_past_events' ) ); + add_filter( 'posts_clauses', array( $this, 'adjust_sorting_for_past_events' ), 10, 2 ); remove_filter( 'posts_clauses', array( $this, 'adjust_sorting_for_upcoming_events' ) ); break; default: @@ -249,13 +249,23 @@ static function () use ( $page_id ) { * This method modifies the SQL query pieces, including join, where, orderby, etc., to adjust the sorting criteria * for upcoming events in the query. It ensures that events are ordered by their start datetime in ascending order. * + * @see https://developer.wordpress.org/reference/hooks/posts_clauses/ + * * @since 1.0.0 * - * @param array $query_pieces An array containing pieces of the SQL query. + * @param array $query_pieces An array containing pieces of the SQL query. + * @param WP_Query $query The WP_Query instance (passed by reference). * @return array The modified SQL query pieces with adjusted sorting criteria for upcoming events. */ - public function adjust_sorting_for_upcoming_events( array $query_pieces ): array { - return $this->adjust_event_sql( $query_pieces, 'upcoming', 'ASC' ); + public function adjust_sorting_for_upcoming_events( array $query_pieces, WP_Query $query ): array { + + return $this->adjust_event_sql( + $query_pieces, + 'upcoming', + $query->get( 'order' ), + $query->get( 'orderby' ), + (bool) $query->get( 'include_unfinished' ) + ); } /** @@ -264,12 +274,18 @@ public function adjust_sorting_for_upcoming_events( array $query_pieces ): array * This method modifies the SQL query pieces, including join, where, orderby, etc., to adjust the sorting criteria * for past events in the query. It ensures that events are ordered by their start datetime in the desired order. * - * @param array $query_pieces An array containing pieces of the SQL query. - * + * @param array $query_pieces An array containing pieces of the SQL query. + * @param WP_Query $query The WP_Query instance (passed by reference). * @return array The modified SQL query pieces with adjusted sorting criteria for past events. */ - public function adjust_sorting_for_past_events( array $query_pieces ): array { - return $this->adjust_event_sql( $query_pieces, 'past' ); + public function adjust_sorting_for_past_events( array $query_pieces, WP_Query $query ): array { + return $this->adjust_event_sql( + $query_pieces, + 'past', + $query->get( 'order' ), + $query->get( 'orderby' ), + (bool) $query->get( 'include_unfinished' ) + ); } /** @@ -280,18 +296,17 @@ public function adjust_sorting_for_past_events( array $query_pieces ): array { * * @since 1.0.0 * - * @param array $query_pieces An array containing pieces of the SQL query. + * @param array $query_pieces An array containing pieces of the SQL query. + * @param WP_Query $query The WP_Query instance (passed by reference). * @return array The modified SQL query pieces with adjusted sorting criteria. */ - public function adjust_admin_event_sorting( array $query_pieces ): array { + public function adjust_admin_event_sorting( array $query_pieces, WP_Query $query ): array { if ( ! is_admin() ) { return $query_pieces; } - global $wp_query; - - if ( 'datetime' === $wp_query->get( 'orderby' ) ) { - $query_pieces = $this->adjust_event_sql( $query_pieces, 'all', $wp_query->get( 'order' ) ); + if ( 'datetime' === $query->get( 'orderby' ) ) { + $query_pieces = $this->adjust_event_sql( $query_pieces, 'all', $query->get( 'order' ) ); } return $query_pieces; @@ -304,14 +319,26 @@ public function adjust_admin_event_sorting( array $query_pieces ): array { * the `gatherpress_events` table in the database join. It allows querying events based on different * criteria such as upcoming or past events and specifying the event order (DESC or ASC). * + * @see https://developer.wordpress.org/reference/hooks/posts_join/ + * @see https://developer.wordpress.org/reference/hooks/posts_orderby/ + * @see https://developer.wordpress.org/reference/hooks/posts_where/ + * * @since 1.0.0 * - * @param array $pieces An array of query pieces, including join, where, orderby, and more. - * @param string $type The type of events to query (options: 'all', 'upcoming', 'past'). - * @param string $order The event order ('DESC' for descending or 'ASC' for ascending). + * @param array $pieces An array of query pieces, including join, where, orderby, and more. + * @param string $type The type of events to query (options: 'all', 'upcoming', 'past') (Default: 'all'). + * @param string $order The event order ('DESC' for descending or 'ASC' for ascending) (Default: 'DESC'). + * @param string[]|string $order_by List or singular string of ORDERBY statement(s) (Default: ['datetime']). + * @param bool $inclusive Whether to include currently running events in the query (Default: true). * @return array An array containing adjusted SQL clauses for the Event query. */ - public function adjust_event_sql( array $pieces, string $type = 'all', string $order = 'DESC' ): array { + public function adjust_event_sql( + array $pieces, + string $type = 'all', + string $order = 'DESC', + array|string $order_by = array( 'datetime' ), + bool $inclusive = true + ): array { global $wpdb; $defaults = array( @@ -324,13 +351,37 @@ public function adjust_event_sql( array $pieces, string $type = 'all', string $o 'limits' => '', ); $pieces = array_merge( $defaults, $pieces ); - $table = sprintf( Event::TABLE_FORMAT, $wpdb->prefix ); + $table = sprintf( Event::TABLE_FORMAT, $wpdb->prefix ); // Could also be (just) $wpdb->{gatherpress_events}. $pieces['join'] .= ' LEFT JOIN ' . esc_sql( $table ) . ' ON ' . esc_sql( $wpdb->posts ) . '.ID=' . esc_sql( $table ) . '.post_id'; $order = strtoupper( $order ); - if ( in_array( $order, array( 'DESC', 'ASC' ), true ) ) { - $pieces['orderby'] = sprintf( esc_sql( $table ) . '.datetime_start_gmt %s', esc_sql( $order ) ); + + // ORDERBY is an array, which allows to orderby multiple values. + // Currently it is only allowed to order events by ONE value. + $order_by = ( is_array( $order_by ) ) ? $order_by[0] : $order_by; + switch ( strtolower( $order_by ) ) { + case 'id': + $pieces['orderby'] = sprintf( esc_sql( $wpdb->posts ) . '.ID %s', esc_sql( $order ) ); + break; + + case 'title': + $pieces['orderby'] = sprintf( esc_sql( $wpdb->posts ) . '.post_name %s', esc_sql( $order ) ); + break; + + case 'modified': + $pieces['orderby'] = sprintf( esc_sql( $wpdb->posts ) . '.post_modified_gmt %s', esc_sql( $order ) ); + break; + + case 'rand': + $pieces['orderby'] = esc_sql( 'RAND()' ); + break; + + case 'datetime': + default: + $pieces['orderby'] = sprintf( esc_sql( $table ) . '.datetime_start_gmt %s', esc_sql( $order ) ); + break; + } } if ( 'all' === $type ) { @@ -339,10 +390,12 @@ public function adjust_event_sql( array $pieces, string $type = 'all', string $o $current = gmdate( Event::DATETIME_FORMAT, time() ); + $column = ( ( $inclusive && 'upcoming' === $type ) || ( ! $inclusive && 'past' === $type ) ) ? 'datetime_end_gmt' : 'datetime_start_gmt'; + if ( 'upcoming' === $type ) { - $pieces['where'] .= $wpdb->prepare( ' AND %i.datetime_end_gmt >= %s', $table, $current ); // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.UnsupportedIdentifierPlaceholder + $pieces['where'] .= $wpdb->prepare( ' AND %i.%i >= %s', $table, $column, $current ); // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.UnsupportedIdentifierPlaceholder } elseif ( 'past' === $type ) { - $pieces['where'] .= $wpdb->prepare( ' AND %i.datetime_end_gmt < %s', $table, $current ); // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.UnsupportedIdentifierPlaceholder + $pieces['where'] .= $wpdb->prepare( ' AND %i.%i < %s', $table, $column, $current ); // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.UnsupportedIdentifierPlaceholder } return $pieces; From 6d5631d9fc48e91889972064c31f66ab8a777d8a Mon Sep 17 00:00:00 2001 From: Carsten Bach Date: Thu, 19 Sep 2024 05:19:26 +0200 Subject: [PATCH 02/11] Remove the union return type declarations to become compatible with PHP 7.4 --- includes/core/classes/class-event-query.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/core/classes/class-event-query.php b/includes/core/classes/class-event-query.php index fe97aa0b9..e6c482cce 100644 --- a/includes/core/classes/class-event-query.php +++ b/includes/core/classes/class-event-query.php @@ -336,7 +336,7 @@ public function adjust_event_sql( array $pieces, string $type = 'all', string $order = 'DESC', - array|string $order_by = array( 'datetime' ), + $order_by = array( 'datetime' ), bool $inclusive = true ): array { global $wpdb; From 0209441bc8bc4e60564c53b5e7152403d9852702 Mon Sep 17 00:00:00 2001 From: Carsten Bach Date: Thu, 19 Sep 2024 14:36:48 +0200 Subject: [PATCH 03/11] Add nneded 2nd argument wp_query to func call --- .../includes/core/classes/class-test-event-query.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/unit/php/includes/core/classes/class-test-event-query.php b/test/unit/php/includes/core/classes/class-test-event-query.php index 56e82e1cc..3a03cd83c 100644 --- a/test/unit/php/includes/core/classes/class-test-event-query.php +++ b/test/unit/php/includes/core/classes/class-test-event-query.php @@ -200,19 +200,19 @@ public function test_get_events_list(): void { */ public function test_adjust_admin_event_sorting(): void { $instance = Event_Query::get_instance(); + global $wp_query; $this->mock->user( false, 'admin' ); - $response = $instance->adjust_admin_event_sorting( array() ); + $response = $instance->adjust_admin_event_sorting( array(), $wp_query ); $this->assertEmpty( $response, 'Failed to assert array is not empty' ); $this->mock->user( true, 'admin' ); // Set 'orderby' admin query to 'datetime'. - global $wp_query; $wp_query->set( 'orderby', 'datetime' ); // Run function with empty array passed as 'pieces' argument. - $response = $instance->adjust_admin_event_sorting( array() ); + $response = $instance->adjust_admin_event_sorting( array(), $wp_query ); // Assert that an array was generated from the adjustsql argument. todo: make this test more meaningful. $this->assertNotEmpty( $response, 'Failed to assert array is empty' ); @@ -239,11 +239,11 @@ public function test_adjust_event_sql(): void { $retval = $instance->adjust_event_sql( array(), 'past', 'desc' ); $this->assertStringContainsString( 'DESC', $retval['orderby'] ); - $this->assertStringContainsString( "AND `{$table}`.datetime_end_gmt <", $retval['where'] ); + $this->assertStringContainsString( "AND `{$table}`.`datetime_end_gmt` <", $retval['where'] ); $retval = $instance->adjust_event_sql( array(), 'upcoming', 'ASC' ); $this->assertStringContainsString( 'ASC', $retval['orderby'] ); - $this->assertStringContainsString( "AND `{$table}`.datetime_end_gmt >=", $retval['where'] ); + $this->assertStringContainsString( "AND `{$table}`.`datetime_end_gmt` >=", $retval['where'] ); } } From e46b8e1e8653ba3c2825c72e52d1e3f9bec2bbda Mon Sep 17 00:00:00 2001 From: Carsten Bach Date: Sat, 21 Sep 2024 01:20:15 +0200 Subject: [PATCH 04/11] NEW method get_datetime_comparison_column() --- includes/core/classes/class-event-query.php | 28 ++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/includes/core/classes/class-event-query.php b/includes/core/classes/class-event-query.php index e6c482cce..b0736d6d1 100644 --- a/includes/core/classes/class-event-query.php +++ b/includes/core/classes/class-event-query.php @@ -390,7 +390,7 @@ public function adjust_event_sql( $current = gmdate( Event::DATETIME_FORMAT, time() ); - $column = ( ( $inclusive && 'upcoming' === $type ) || ( ! $inclusive && 'past' === $type ) ) ? 'datetime_end_gmt' : 'datetime_start_gmt'; + $column = $this->get_datetime_comparison_column( $type, $inclusive ); if ( 'upcoming' === $type ) { $pieces['where'] .= $wpdb->prepare( ' AND %i.%i >= %s', $table, $column, $current ); // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.UnsupportedIdentifierPlaceholder @@ -400,4 +400,30 @@ public function adjust_event_sql( return $pieces; } + + /** + * Determine which db column to compare against, + * based on the type of event query (either upcoming or past) + * and if started but unfinished events should be included. + * + * @param string $type The type of events to query (options: 'all', 'upcoming', 'past') (Can not be 'all' anymore). + * @param bool $inclusive Whether to include currently running events in the query. + * + * @return string Name of the DB column, which content to compare against the current time. + */ + protected static function get_datetime_comparison_column( string $type, bool $inclusive ) : string { + if ( + // Upcoming events, including ones that are running. + ( $inclusive && 'upcoming' === $type ) || + // Past events, that are finished already. + ( ! $inclusive && 'past' === $type ) + ) { + return 'datetime_end_gmt'; + } + + // All others, means: + // - Upcoming events, without running events. + // - Past events, that are still running. + return 'datetime_start_gmt'; + } } From 401d35acb33ea518d9591ea5a4d3e19d9391fe56 Mon Sep 17 00:00:00 2001 From: Carsten Bach Date: Sat, 21 Sep 2024 01:36:57 +0200 Subject: [PATCH 05/11] Fix for CS --- includes/core/classes/class-event-query.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/includes/core/classes/class-event-query.php b/includes/core/classes/class-event-query.php index b0736d6d1..de5f2412e 100644 --- a/includes/core/classes/class-event-query.php +++ b/includes/core/classes/class-event-query.php @@ -389,8 +389,7 @@ public function adjust_event_sql( } $current = gmdate( Event::DATETIME_FORMAT, time() ); - - $column = $this->get_datetime_comparison_column( $type, $inclusive ); + $column = $this->get_datetime_comparison_column( $type, $inclusive ); if ( 'upcoming' === $type ) { $pieces['where'] .= $wpdb->prepare( ' AND %i.%i >= %s', $table, $column, $current ); // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.UnsupportedIdentifierPlaceholder @@ -411,7 +410,7 @@ public function adjust_event_sql( * * @return string Name of the DB column, which content to compare against the current time. */ - protected static function get_datetime_comparison_column( string $type, bool $inclusive ) : string { + protected static function get_datetime_comparison_column( string $type, bool $inclusive ): string { if ( // Upcoming events, including ones that are running. ( $inclusive && 'upcoming' === $type ) || From 6a9919f7474ed43dc95f56cd3dff0a67325d938d Mon Sep 17 00:00:00 2001 From: Carsten Bach Date: Sat, 21 Sep 2024 01:37:06 +0200 Subject: [PATCH 06/11] Fix unit test --- .../php/includes/core/classes/class-test-event-query.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/unit/php/includes/core/classes/class-test-event-query.php b/test/unit/php/includes/core/classes/class-test-event-query.php index 3a03cd83c..96bfeee6a 100644 --- a/test/unit/php/includes/core/classes/class-test-event-query.php +++ b/test/unit/php/includes/core/classes/class-test-event-query.php @@ -236,7 +236,12 @@ public function test_adjust_event_sql(): void { $this->assertStringContainsString( 'DESC', $retval['orderby'] ); $this->assertEmpty( $retval['where'] ); - $retval = $instance->adjust_event_sql( array(), 'past', 'desc' ); + $retval = $instance->adjust_event_sql( array(), 'past', 'desc' ); // inclusive will be TRUE by default + + $this->assertStringContainsString( 'DESC', $retval['orderby'] ); + $this->assertStringContainsString( "AND `{$table}`.`datetime_start_gmt` <", $retval['where'] ); + + $retval = $instance->adjust_event_sql( array(), 'past', 'desc', 'datetime', false ); $this->assertStringContainsString( 'DESC', $retval['orderby'] ); $this->assertStringContainsString( "AND `{$table}`.`datetime_end_gmt` <", $retval['where'] ); From 7c95790fcbc1a796f64d0f8b8c43b01544c17015 Mon Sep 17 00:00:00 2001 From: Carsten Bach Date: Sat, 21 Sep 2024 01:40:12 +0200 Subject: [PATCH 07/11] Fix for CS --- test/unit/php/includes/core/classes/class-test-event-query.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/php/includes/core/classes/class-test-event-query.php b/test/unit/php/includes/core/classes/class-test-event-query.php index 96bfeee6a..cb935c75e 100644 --- a/test/unit/php/includes/core/classes/class-test-event-query.php +++ b/test/unit/php/includes/core/classes/class-test-event-query.php @@ -236,7 +236,7 @@ public function test_adjust_event_sql(): void { $this->assertStringContainsString( 'DESC', $retval['orderby'] ); $this->assertEmpty( $retval['where'] ); - $retval = $instance->adjust_event_sql( array(), 'past', 'desc' ); // inclusive will be TRUE by default + $retval = $instance->adjust_event_sql( array(), 'past', 'desc' ); // inclusive will be TRUE by default. $this->assertStringContainsString( 'DESC', $retval['orderby'] ); $this->assertStringContainsString( "AND `{$table}`.`datetime_start_gmt` <", $retval['where'] ); From 9f674b13acdae8f55c63a0865ec598f92923ecba Mon Sep 17 00:00:00 2001 From: Carsten Bach Date: Tue, 1 Oct 2024 06:16:44 +0200 Subject: [PATCH 08/11] NEW test for get_datetime_comparison_column() method --- .../core/classes/class-test-event-query.php | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/test/unit/php/includes/core/classes/class-test-event-query.php b/test/unit/php/includes/core/classes/class-test-event-query.php index cb935c75e..a78f367f6 100644 --- a/test/unit/php/includes/core/classes/class-test-event-query.php +++ b/test/unit/php/includes/core/classes/class-test-event-query.php @@ -14,6 +14,7 @@ use GatherPress\Core\Topic; use GatherPress\Core\Venue; use PMC\Unit_Test\Base; +use PMC\Unit_Test\Utility; /** * Class Test_Event_Query. @@ -251,4 +252,38 @@ public function test_adjust_event_sql(): void { $this->assertStringContainsString( 'ASC', $retval['orderby'] ); $this->assertStringContainsString( "AND `{$table}`.`datetime_end_gmt` >=", $retval['where'] ); } + + /** + * Coverage for get_datetime_comparison_column method. + * + * @covers ::get_datetime_comparison_column + * + * @return void + */ + public function test_get_datetime_comparison_column(): void { + + $instance = Event_Query::get_instance(); + + $this->assertSame( + 'datetime_end_gmt', + Utility::invoke_hidden_method( $instance, 'get_datetime_comparison_column', array( 'upcoming', true ) ), + 'Failed to assert, that inclusive, upcoming events should be ordered by datetime_end_gmt.' + ); + $this->assertSame( + 'datetime_start_gmt', + Utility::invoke_hidden_method( $instance, 'get_datetime_comparison_column', array( 'upcoming', false ) ), + 'Failed to assert, that non-inclusive, upcoming events should be ordered by datetime_start_gmt.' + ); + + $this->assertSame( + 'datetime_start_gmt', + Utility::invoke_hidden_method( $instance, 'get_datetime_comparison_column', array( 'past', true ) ), + 'Failed to assert, that inclusive, past events should be ordered by datetime_start_gmt.' + ); + $this->assertSame( + 'datetime_end_gmt', + Utility::invoke_hidden_method( $instance, 'get_datetime_comparison_column', array( 'past', false ) ), + 'Failed to assert, that non-inclusive, past events should be ordered by datetime_end_gmt.' + ); + } } From d69a5bb15c7db238685a967ba58697e781738795 Mon Sep 17 00:00:00 2001 From: Carsten Bach Date: Tue, 1 Oct 2024 10:56:16 +0200 Subject: [PATCH 09/11] NEW assertions for adjust_event_sql() method --- .../core/classes/class-test-event-query.php | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/test/unit/php/includes/core/classes/class-test-event-query.php b/test/unit/php/includes/core/classes/class-test-event-query.php index a78f367f6..6953f661a 100644 --- a/test/unit/php/includes/core/classes/class-test-event-query.php +++ b/test/unit/php/includes/core/classes/class-test-event-query.php @@ -234,23 +234,39 @@ public function test_adjust_event_sql(): void { $table = sprintf( Event::TABLE_FORMAT, $wpdb->prefix, Event::POST_TYPE ); $retval = $instance->adjust_event_sql( array(), 'all', 'DESC' ); - $this->assertStringContainsString( 'DESC', $retval['orderby'] ); + $this->assertStringContainsString( '.datetime_start_gmt DESC', $retval['orderby'] ); $this->assertEmpty( $retval['where'] ); $retval = $instance->adjust_event_sql( array(), 'past', 'desc' ); // inclusive will be TRUE by default. - $this->assertStringContainsString( 'DESC', $retval['orderby'] ); + $this->assertStringContainsString( '.datetime_start_gmt DESC', $retval['orderby'] ); $this->assertStringContainsString( "AND `{$table}`.`datetime_start_gmt` <", $retval['where'] ); $retval = $instance->adjust_event_sql( array(), 'past', 'desc', 'datetime', false ); - $this->assertStringContainsString( 'DESC', $retval['orderby'] ); + $this->assertStringContainsString( '.datetime_start_gmt DESC', $retval['orderby'] ); $this->assertStringContainsString( "AND `{$table}`.`datetime_end_gmt` <", $retval['where'] ); $retval = $instance->adjust_event_sql( array(), 'upcoming', 'ASC' ); - $this->assertStringContainsString( 'ASC', $retval['orderby'] ); + $this->assertStringContainsString( '.datetime_start_gmt ASC', $retval['orderby'] ); $this->assertStringContainsString( "AND `{$table}`.`datetime_end_gmt` >=", $retval['where'] ); + + $retval = $instance->adjust_event_sql( array(), 'past', 'desc', 'id', false ); + + $this->assertStringContainsString( '.ID DESC', $retval['orderby'] ); + + $retval = $instance->adjust_event_sql( array(), 'past', 'desc', 'title', false ); + + $this->assertStringContainsString( '.post_name DESC', $retval['orderby'] ); + + $retval = $instance->adjust_event_sql( array(), 'past', 'desc', 'modified', false ); + + $this->assertStringContainsString( '.post_modified_gmt DESC', $retval['orderby'] ); + + $retval = $instance->adjust_event_sql( array(), 'upcoming', 'desc', 'rand', false ); + + $this->assertStringContainsString( 'RAND()', $retval['orderby'] ); } /** From fe87883ed461657a49dc4cd6270971038120cb1d Mon Sep 17 00:00:00 2001 From: Mike Auteri Date: Sat, 12 Oct 2024 09:34:33 -0400 Subject: [PATCH 10/11] Small cleanup. --- includes/core/classes/class-event-query.php | 9 ++------- .../php/includes/core/classes/class-test-event-query.php | 1 - 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/includes/core/classes/class-event-query.php b/includes/core/classes/class-event-query.php index 1c4dacd3f..7f6b09b9d 100644 --- a/includes/core/classes/class-event-query.php +++ b/includes/core/classes/class-event-query.php @@ -258,7 +258,6 @@ static function () use ( $page_id ) { * @return array The modified SQL query pieces with adjusted sorting criteria for upcoming events. */ public function adjust_sorting_for_upcoming_events( array $query_pieces, WP_Query $query ): array { - return $this->adjust_event_sql( $query_pieces, 'upcoming', @@ -358,25 +357,21 @@ public function adjust_event_sql( if ( in_array( $order, array( 'DESC', 'ASC' ), true ) ) { // ORDERBY is an array, which allows to orderby multiple values. - // Currently it is only allowed to order events by ONE value. + // Currently, it is only allowed to order events by ONE value. $order_by = ( is_array( $order_by ) ) ? $order_by[0] : $order_by; switch ( strtolower( $order_by ) ) { case 'id': $pieces['orderby'] = sprintf( esc_sql( $wpdb->posts ) . '.ID %s', esc_sql( $order ) ); break; - case 'title': $pieces['orderby'] = sprintf( esc_sql( $wpdb->posts ) . '.post_name %s', esc_sql( $order ) ); break; - case 'modified': $pieces['orderby'] = sprintf( esc_sql( $wpdb->posts ) . '.post_modified_gmt %s', esc_sql( $order ) ); break; - case 'rand': $pieces['orderby'] = esc_sql( 'RAND()' ); break; - case 'datetime': default: $pieces['orderby'] = sprintf( esc_sql( $table ) . '.datetime_start_gmt %s', esc_sql( $order ) ); @@ -405,7 +400,7 @@ public function adjust_event_sql( * based on the type of event query (either upcoming or past) * and if started but unfinished events should be included. * - * @param string $type The type of events to query (options: 'all', 'upcoming', 'past') (Can not be 'all' anymore). + * @param string $type The type of events to query (options: 'all', 'upcoming', 'past') (Cannot be 'all' anymore). * @param bool $inclusive Whether to include currently running events in the query. * * @return string Name of the DB column, which content to compare against the current time. diff --git a/test/unit/php/includes/core/classes/class-test-event-query.php b/test/unit/php/includes/core/classes/class-test-event-query.php index 6953f661a..b36be14cc 100644 --- a/test/unit/php/includes/core/classes/class-test-event-query.php +++ b/test/unit/php/includes/core/classes/class-test-event-query.php @@ -277,7 +277,6 @@ public function test_adjust_event_sql(): void { * @return void */ public function test_get_datetime_comparison_column(): void { - $instance = Event_Query::get_instance(); $this->assertSame( From da7a512c060ad403dfa1c8c012061045511778de Mon Sep 17 00:00:00 2001 From: Mike Auteri Date: Sat, 12 Oct 2024 09:36:55 -0400 Subject: [PATCH 11/11] Small cleanup. --- includes/core/classes/class-event-query.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/includes/core/classes/class-event-query.php b/includes/core/classes/class-event-query.php index 7f6b09b9d..e6e2e37c7 100644 --- a/includes/core/classes/class-event-query.php +++ b/includes/core/classes/class-event-query.php @@ -354,11 +354,12 @@ public function adjust_event_sql( $pieces['join'] .= ' LEFT JOIN ' . esc_sql( $table ) . ' ON ' . esc_sql( $wpdb->posts ) . '.ID=' . esc_sql( $table ) . '.post_id'; $order = strtoupper( $order ); - if ( in_array( $order, array( 'DESC', 'ASC' ), true ) ) { + if ( in_array( $order, array( 'DESC', 'ASC' ), true ) ) { // ORDERBY is an array, which allows to orderby multiple values. // Currently, it is only allowed to order events by ONE value. $order_by = ( is_array( $order_by ) ) ? $order_by[0] : $order_by; + switch ( strtolower( $order_by ) ) { case 'id': $pieces['orderby'] = sprintf( esc_sql( $wpdb->posts ) . '.ID %s', esc_sql( $order ) );