diff --git a/includes/core/classes/class-event-query.php b/includes/core/classes/class-event-query.php index c5517656a..cb8878fa9 100644 --- a/includes/core/classes/class-event-query.php +++ b/includes/core/classes/class-event-query.php @@ -6,6 +6,8 @@ * upcoming and past events, applying filters, and ordering events. It also handles adjustments * for event pages and admin queries. * + * @todo Reverted this PR, but needs to be investigated again to work with Query block https://github.com/GatherPress/gatherpress/pull/889 + * * @package GatherPress\Core * @since 1.0.0 */ @@ -53,7 +55,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' ), 10, 2 ); + add_filter( 'posts_clauses', array( $this, 'adjust_admin_event_sorting' ) ); } /** @@ -231,10 +233,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' ), 10, 2 ); + add_filter( 'posts_clauses', array( $this, 'adjust_sorting_for_upcoming_events' ) ); break; case 'past': - add_filter( 'posts_clauses', array( $this, 'adjust_sorting_for_past_events' ), 10, 2 ); + add_filter( 'posts_clauses', array( $this, 'adjust_sorting_for_past_events' ) ); remove_filter( 'posts_clauses', array( $this, 'adjust_sorting_for_upcoming_events' ) ); break; default: @@ -249,22 +251,13 @@ 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 WP_Query $query The WP_Query instance (passed by reference). + * @param array $query_pieces An array containing pieces of the SQL query. * @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', - $query->get( 'order' ), - $query->get( 'orderby' ), - (bool) $query->get( 'include_unfinished' ) - ); + public function adjust_sorting_for_upcoming_events( array $query_pieces ): array { + return $this->adjust_event_sql( $query_pieces, 'upcoming', 'ASC' ); } /** @@ -273,18 +266,11 @@ public function adjust_sorting_for_upcoming_events( array $query_pieces, WP_Quer * 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 WP_Query $query The WP_Query instance (passed by reference). + * @param array $query_pieces An array containing pieces of the SQL query. * @return array The modified SQL query pieces with adjusted sorting criteria for past events. */ - 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' ) - ); + public function adjust_sorting_for_past_events( array $query_pieces ): array { + return $this->adjust_event_sql( $query_pieces, 'past' ); } /** @@ -295,17 +281,18 @@ public function adjust_sorting_for_past_events( array $query_pieces, WP_Query $q * * @since 1.0.0 * - * @param array $query_pieces An array containing pieces of the SQL query. - * @param WP_Query $query The WP_Query instance (passed by reference). + * @param array $query_pieces An array containing pieces of the SQL query. * @return array The modified SQL query pieces with adjusted sorting criteria. */ - public function adjust_admin_event_sorting( array $query_pieces, WP_Query $query ): array { + public function adjust_admin_event_sorting( array $query_pieces ): array { if ( ! is_admin() ) { return $query_pieces; } - if ( 'datetime' === $query->get( 'orderby' ) ) { - $query_pieces = $this->adjust_event_sql( $query_pieces, 'all', $query->get( 'order' ) ); + global $wp_query; + + if ( 'datetime' === $wp_query->get( 'orderby' ) ) { + $query_pieces = $this->adjust_event_sql( $query_pieces, 'all', $wp_query->get( 'order' ) ); } return $query_pieces; @@ -318,26 +305,15 @@ public function adjust_admin_event_sorting( array $query_pieces, WP_Query $query * 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') (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). + * @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). + * * @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', - $order_by = array( 'datetime' ), - bool $inclusive = true - ): array { + public function adjust_event_sql( array $pieces, string $type = 'all', string $order = 'DESC' ): array { global $wpdb; $defaults = array( @@ -350,34 +326,13 @@ public function adjust_event_sql( 'limits' => '', ); $pieces = array_merge( $defaults, $pieces ); - $table = sprintf( Event::TABLE_FORMAT, $wpdb->prefix ); // Could also be (just) $wpdb->{gatherpress_events}. + $table = sprintf( Event::TABLE_FORMAT, $wpdb->prefix ); $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 ) ) { - // 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; - } + $pieces['orderby'] = sprintf( esc_sql( $table ) . '.datetime_start_gmt %s', esc_sql( $order ) ); } if ( 'all' === $type ) { @@ -385,40 +340,13 @@ public function adjust_event_sql( } $current = gmdate( Event::DATETIME_FORMAT, time() ); - $column = $this->get_datetime_comparison_column( $type, $inclusive ); if ( 'upcoming' === $type ) { - $pieces['where'] .= $wpdb->prepare( ' AND %i.%i >= %s', $table, $column, $current ); + $pieces['where'] .= $wpdb->prepare( ' AND %i.datetime_end_gmt >= %s', $table, $current ); } elseif ( 'past' === $type ) { - $pieces['where'] .= $wpdb->prepare( ' AND %i.%i < %s', $table, $column, $current ); + $pieces['where'] .= $wpdb->prepare( ' AND %i.datetime_end_gmt < %s', $table, $current ); } 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') (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. - */ - 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'; - } } diff --git a/test/unit/php/includes/tests/core/classes/class-test-event-query.php b/test/unit/php/includes/tests/core/classes/class-test-event-query.php index 84050e003..9d147db34 100644 --- a/test/unit/php/includes/tests/core/classes/class-test-event-query.php +++ b/test/unit/php/includes/tests/core/classes/class-test-event-query.php @@ -14,7 +14,6 @@ use GatherPress\Core\Topic; use GatherPress\Core\Venue; use GatherPress\Tests\Base; -use PMC\Unit_Test\Utility; use WP_Query; /** @@ -374,11 +373,12 @@ public function test_prepare_query_with_no_event_type(): void { * @return void */ public function test_adjust_admin_event_sorting(): void { - $instance = Event_Query::get_instance(); global $wp_query; + $instance = Event_Query::get_instance(); + $this->mock->user( false, 'admin' ); - $response = $instance->adjust_admin_event_sorting( array(), $wp_query ); + $response = $instance->adjust_admin_event_sorting( array() ); $this->assertEmpty( $response, 'Failed to assert array is not empty' ); $this->mock->user( true, 'admin' ); @@ -387,7 +387,7 @@ public function test_adjust_admin_event_sorting(): void { $wp_query->set( 'orderby', 'datetime' ); // Run function with empty array passed as 'pieces' argument. - $response = $instance->adjust_admin_event_sorting( array(), $wp_query ); + $response = $instance->adjust_admin_event_sorting( array() ); // 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' ); @@ -408,71 +408,17 @@ public function test_adjust_event_sql(): void { $table = sprintf( Event::TABLE_FORMAT, $wpdb->prefix ); $retval = $instance->adjust_event_sql( array(), 'all', 'DESC' ); - $this->assertStringContainsString( '.datetime_start_gmt DESC', $retval['orderby'] ); + $this->assertStringContainsString( 'DESC', $retval['orderby'] ); $this->assertEmpty( $retval['where'] ); - $retval = $instance->adjust_event_sql( array(), 'past', 'desc' ); // inclusive will be TRUE by default. - - $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' ); - $retval = $instance->adjust_event_sql( array(), 'past', 'desc', 'datetime', false ); - - $this->assertStringContainsString( '.datetime_start_gmt DESC', $retval['orderby'] ); - $this->assertStringContainsString( "AND `{$table}`.`datetime_end_gmt` <", $retval['where'] ); + $this->assertStringContainsString( 'DESC', $retval['orderby'] ); + $this->assertStringContainsString( "AND `{$table}`.datetime_end_gmt <", $retval['where'] ); $retval = $instance->adjust_event_sql( array(), 'upcoming', 'ASC' ); - $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'] ); - } - - /** - * 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.' - ); + $this->assertStringContainsString( 'ASC', $retval['orderby'] ); + $this->assertStringContainsString( "AND `{$table}`.datetime_end_gmt >=", $retval['where'] ); } }