Skip to content

Commit 9fa1b14

Browse files
authored
Merge pull request #1020 from GatherPress/revert-889
Revert 889 as it breaks Event Lists block. This code will need to be …
2 parents 637dbb3 + bc7ecd9 commit 9fa1b14

File tree

2 files changed

+36
-162
lines changed

2 files changed

+36
-162
lines changed

includes/core/classes/class-event-query.php

+26-98
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* upcoming and past events, applying filters, and ordering events. It also handles adjustments
77
* for event pages and admin queries.
88
*
9+
* @todo Reverted this PR, but needs to be investigated again to work with Query block https://github.com/GatherPress/gatherpress/pull/889
10+
*
911
* @package GatherPress\Core
1012
* @since 1.0.0
1113
*/
@@ -53,7 +55,7 @@ protected function __construct() {
5355
*/
5456
protected function setup_hooks(): void {
5557
add_action( 'pre_get_posts', array( $this, 'prepare_event_query_before_execution' ) );
56-
add_filter( 'posts_clauses', array( $this, 'adjust_admin_event_sorting' ), 10, 2 );
58+
add_filter( 'posts_clauses', array( $this, 'adjust_admin_event_sorting' ) );
5759
}
5860

5961
/**
@@ -231,10 +233,10 @@ static function () use ( $page_id ) {
231233
switch ( $events_query ) {
232234
case 'upcoming':
233235
remove_filter( 'posts_clauses', array( $this, 'adjust_sorting_for_past_events' ) );
234-
add_filter( 'posts_clauses', array( $this, 'adjust_sorting_for_upcoming_events' ), 10, 2 );
236+
add_filter( 'posts_clauses', array( $this, 'adjust_sorting_for_upcoming_events' ) );
235237
break;
236238
case 'past':
237-
add_filter( 'posts_clauses', array( $this, 'adjust_sorting_for_past_events' ), 10, 2 );
239+
add_filter( 'posts_clauses', array( $this, 'adjust_sorting_for_past_events' ) );
238240
remove_filter( 'posts_clauses', array( $this, 'adjust_sorting_for_upcoming_events' ) );
239241
break;
240242
default:
@@ -249,22 +251,13 @@ static function () use ( $page_id ) {
249251
* This method modifies the SQL query pieces, including join, where, orderby, etc., to adjust the sorting criteria
250252
* for upcoming events in the query. It ensures that events are ordered by their start datetime in ascending order.
251253
*
252-
* @see https://developer.wordpress.org/reference/hooks/posts_clauses/
253-
*
254254
* @since 1.0.0
255255
*
256-
* @param array $query_pieces An array containing pieces of the SQL query.
257-
* @param WP_Query $query The WP_Query instance (passed by reference).
256+
* @param array $query_pieces An array containing pieces of the SQL query.
258257
* @return array The modified SQL query pieces with adjusted sorting criteria for upcoming events.
259258
*/
260-
public function adjust_sorting_for_upcoming_events( array $query_pieces, WP_Query $query ): array {
261-
return $this->adjust_event_sql(
262-
$query_pieces,
263-
'upcoming',
264-
$query->get( 'order' ),
265-
$query->get( 'orderby' ),
266-
(bool) $query->get( 'include_unfinished' )
267-
);
259+
public function adjust_sorting_for_upcoming_events( array $query_pieces ): array {
260+
return $this->adjust_event_sql( $query_pieces, 'upcoming', 'ASC' );
268261
}
269262

270263
/**
@@ -273,18 +266,11 @@ public function adjust_sorting_for_upcoming_events( array $query_pieces, WP_Quer
273266
* This method modifies the SQL query pieces, including join, where, orderby, etc., to adjust the sorting criteria
274267
* for past events in the query. It ensures that events are ordered by their start datetime in the desired order.
275268
*
276-
* @param array $query_pieces An array containing pieces of the SQL query.
277-
* @param WP_Query $query The WP_Query instance (passed by reference).
269+
* @param array $query_pieces An array containing pieces of the SQL query.
278270
* @return array The modified SQL query pieces with adjusted sorting criteria for past events.
279271
*/
280-
public function adjust_sorting_for_past_events( array $query_pieces, WP_Query $query ): array {
281-
return $this->adjust_event_sql(
282-
$query_pieces,
283-
'past',
284-
$query->get( 'order' ),
285-
$query->get( 'orderby' ),
286-
(bool) $query->get( 'include_unfinished' )
287-
);
272+
public function adjust_sorting_for_past_events( array $query_pieces ): array {
273+
return $this->adjust_event_sql( $query_pieces, 'past' );
288274
}
289275

290276
/**
@@ -295,17 +281,18 @@ public function adjust_sorting_for_past_events( array $query_pieces, WP_Query $q
295281
*
296282
* @since 1.0.0
297283
*
298-
* @param array $query_pieces An array containing pieces of the SQL query.
299-
* @param WP_Query $query The WP_Query instance (passed by reference).
284+
* @param array $query_pieces An array containing pieces of the SQL query.
300285
* @return array The modified SQL query pieces with adjusted sorting criteria.
301286
*/
302-
public function adjust_admin_event_sorting( array $query_pieces, WP_Query $query ): array {
287+
public function adjust_admin_event_sorting( array $query_pieces ): array {
303288
if ( ! is_admin() ) {
304289
return $query_pieces;
305290
}
306291

307-
if ( 'datetime' === $query->get( 'orderby' ) ) {
308-
$query_pieces = $this->adjust_event_sql( $query_pieces, 'all', $query->get( 'order' ) );
292+
global $wp_query;
293+
294+
if ( 'datetime' === $wp_query->get( 'orderby' ) ) {
295+
$query_pieces = $this->adjust_event_sql( $query_pieces, 'all', $wp_query->get( 'order' ) );
309296
}
310297

311298
return $query_pieces;
@@ -318,26 +305,15 @@ public function adjust_admin_event_sorting( array $query_pieces, WP_Query $query
318305
* the `gatherpress_events` table in the database join. It allows querying events based on different
319306
* criteria such as upcoming or past events and specifying the event order (DESC or ASC).
320307
*
321-
* @see https://developer.wordpress.org/reference/hooks/posts_join/
322-
* @see https://developer.wordpress.org/reference/hooks/posts_orderby/
323-
* @see https://developer.wordpress.org/reference/hooks/posts_where/
324-
*
325308
* @since 1.0.0
326309
*
327-
* @param array $pieces An array of query pieces, including join, where, orderby, and more.
328-
* @param string $type The type of events to query (options: 'all', 'upcoming', 'past') (Default: 'all').
329-
* @param string $order The event order ('DESC' for descending or 'ASC' for ascending) (Default: 'DESC').
330-
* @param string[]|string $order_by List or singular string of ORDERBY statement(s) (Default: ['datetime']).
331-
* @param bool $inclusive Whether to include currently running events in the query (Default: true).
310+
* @param array $pieces An array of query pieces, including join, where, orderby, and more.
311+
* @param string $type The type of events to query (options: 'all', 'upcoming', 'past').
312+
* @param string $order The event order ('DESC' for descending or 'ASC' for ascending).
313+
*
332314
* @return array An array containing adjusted SQL clauses for the Event query.
333315
*/
334-
public function adjust_event_sql(
335-
array $pieces,
336-
string $type = 'all',
337-
string $order = 'DESC',
338-
$order_by = array( 'datetime' ),
339-
bool $inclusive = true
340-
): array {
316+
public function adjust_event_sql( array $pieces, string $type = 'all', string $order = 'DESC' ): array {
341317
global $wpdb;
342318

343319
$defaults = array(
@@ -350,75 +326,27 @@ public function adjust_event_sql(
350326
'limits' => '',
351327
);
352328
$pieces = array_merge( $defaults, $pieces );
353-
$table = sprintf( Event::TABLE_FORMAT, $wpdb->prefix ); // Could also be (just) $wpdb->{gatherpress_events}.
329+
$table = sprintf( Event::TABLE_FORMAT, $wpdb->prefix );
354330
$pieces['join'] .= ' LEFT JOIN ' . esc_sql( $table ) . ' ON ' . esc_sql( $wpdb->posts ) . '.ID='
355331
. esc_sql( $table ) . '.post_id';
356332
$order = strtoupper( $order );
357333

358334
if ( in_array( $order, array( 'DESC', 'ASC' ), true ) ) {
359-
// ORDERBY is an array, which allows to orderby multiple values.
360-
// Currently, it is only allowed to order events by ONE value.
361-
$order_by = ( is_array( $order_by ) ) ? $order_by[0] : $order_by;
362-
363-
switch ( strtolower( $order_by ) ) {
364-
case 'id':
365-
$pieces['orderby'] = sprintf( esc_sql( $wpdb->posts ) . '.ID %s', esc_sql( $order ) );
366-
break;
367-
case 'title':
368-
$pieces['orderby'] = sprintf( esc_sql( $wpdb->posts ) . '.post_name %s', esc_sql( $order ) );
369-
break;
370-
case 'modified':
371-
$pieces['orderby'] = sprintf( esc_sql( $wpdb->posts ) . '.post_modified_gmt %s', esc_sql( $order ) );
372-
break;
373-
case 'rand':
374-
$pieces['orderby'] = esc_sql( 'RAND()' );
375-
break;
376-
case 'datetime':
377-
default:
378-
$pieces['orderby'] = sprintf( esc_sql( $table ) . '.datetime_start_gmt %s', esc_sql( $order ) );
379-
break;
380-
}
335+
$pieces['orderby'] = sprintf( esc_sql( $table ) . '.datetime_start_gmt %s', esc_sql( $order ) );
381336
}
382337

383338
if ( 'all' === $type ) {
384339
return $pieces;
385340
}
386341

387342
$current = gmdate( Event::DATETIME_FORMAT, time() );
388-
$column = $this->get_datetime_comparison_column( $type, $inclusive );
389343

390344
if ( 'upcoming' === $type ) {
391-
$pieces['where'] .= $wpdb->prepare( ' AND %i.%i >= %s', $table, $column, $current );
345+
$pieces['where'] .= $wpdb->prepare( ' AND %i.datetime_end_gmt >= %s', $table, $current );
392346
} elseif ( 'past' === $type ) {
393-
$pieces['where'] .= $wpdb->prepare( ' AND %i.%i < %s', $table, $column, $current );
347+
$pieces['where'] .= $wpdb->prepare( ' AND %i.datetime_end_gmt < %s', $table, $current );
394348
}
395349

396350
return $pieces;
397351
}
398-
399-
/**
400-
* Determine which db column to compare against,
401-
* based on the type of event query (either upcoming or past)
402-
* and if started but unfinished events should be included.
403-
*
404-
* @param string $type The type of events to query (options: 'all', 'upcoming', 'past') (Cannot be 'all' anymore).
405-
* @param bool $inclusive Whether to include currently running events in the query.
406-
*
407-
* @return string Name of the DB column, which content to compare against the current time.
408-
*/
409-
protected static function get_datetime_comparison_column( string $type, bool $inclusive ): string {
410-
if (
411-
// Upcoming events, including ones that are running.
412-
( $inclusive && 'upcoming' === $type ) ||
413-
// Past events, that are finished already.
414-
( ! $inclusive && 'past' === $type )
415-
) {
416-
return 'datetime_end_gmt';
417-
}
418-
419-
// All others, means:
420-
// - Upcoming events, without running events.
421-
// - Past events, that are still running.
422-
return 'datetime_start_gmt';
423-
}
424352
}

test/unit/php/includes/tests/core/classes/class-test-event-query.php

+10-64
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use GatherPress\Core\Topic;
1515
use GatherPress\Core\Venue;
1616
use GatherPress\Tests\Base;
17-
use PMC\Unit_Test\Utility;
1817
use WP_Query;
1918

2019
/**
@@ -374,11 +373,12 @@ public function test_prepare_query_with_no_event_type(): void {
374373
* @return void
375374
*/
376375
public function test_adjust_admin_event_sorting(): void {
377-
$instance = Event_Query::get_instance();
378376
global $wp_query;
379377

378+
$instance = Event_Query::get_instance();
379+
380380
$this->mock->user( false, 'admin' );
381-
$response = $instance->adjust_admin_event_sorting( array(), $wp_query );
381+
$response = $instance->adjust_admin_event_sorting( array() );
382382
$this->assertEmpty( $response, 'Failed to assert array is not empty' );
383383

384384
$this->mock->user( true, 'admin' );
@@ -387,7 +387,7 @@ public function test_adjust_admin_event_sorting(): void {
387387
$wp_query->set( 'orderby', 'datetime' );
388388

389389
// Run function with empty array passed as 'pieces' argument.
390-
$response = $instance->adjust_admin_event_sorting( array(), $wp_query );
390+
$response = $instance->adjust_admin_event_sorting( array() );
391391

392392
// Assert that an array was generated from the adjustsql argument. todo: make this test more meaningful.
393393
$this->assertNotEmpty( $response, 'Failed to assert array is empty' );
@@ -408,71 +408,17 @@ public function test_adjust_event_sql(): void {
408408
$table = sprintf( Event::TABLE_FORMAT, $wpdb->prefix );
409409
$retval = $instance->adjust_event_sql( array(), 'all', 'DESC' );
410410

411-
$this->assertStringContainsString( '.datetime_start_gmt DESC', $retval['orderby'] );
411+
$this->assertStringContainsString( 'DESC', $retval['orderby'] );
412412
$this->assertEmpty( $retval['where'] );
413413

414-
$retval = $instance->adjust_event_sql( array(), 'past', 'desc' ); // inclusive will be TRUE by default.
415-
416-
$this->assertStringContainsString( '.datetime_start_gmt DESC', $retval['orderby'] );
417-
$this->assertStringContainsString( "AND `{$table}`.`datetime_start_gmt` <", $retval['where'] );
414+
$retval = $instance->adjust_event_sql( array(), 'past', 'desc' );
418415

419-
$retval = $instance->adjust_event_sql( array(), 'past', 'desc', 'datetime', false );
420-
421-
$this->assertStringContainsString( '.datetime_start_gmt DESC', $retval['orderby'] );
422-
$this->assertStringContainsString( "AND `{$table}`.`datetime_end_gmt` <", $retval['where'] );
416+
$this->assertStringContainsString( 'DESC', $retval['orderby'] );
417+
$this->assertStringContainsString( "AND `{$table}`.datetime_end_gmt <", $retval['where'] );
423418

424419
$retval = $instance->adjust_event_sql( array(), 'upcoming', 'ASC' );
425420

426-
$this->assertStringContainsString( '.datetime_start_gmt ASC', $retval['orderby'] );
427-
$this->assertStringContainsString( "AND `{$table}`.`datetime_end_gmt` >=", $retval['where'] );
428-
429-
$retval = $instance->adjust_event_sql( array(), 'past', 'desc', 'id', false );
430-
431-
$this->assertStringContainsString( '.ID DESC', $retval['orderby'] );
432-
433-
$retval = $instance->adjust_event_sql( array(), 'past', 'desc', 'title', false );
434-
435-
$this->assertStringContainsString( '.post_name DESC', $retval['orderby'] );
436-
437-
$retval = $instance->adjust_event_sql( array(), 'past', 'desc', 'modified', false );
438-
439-
$this->assertStringContainsString( '.post_modified_gmt DESC', $retval['orderby'] );
440-
441-
$retval = $instance->adjust_event_sql( array(), 'upcoming', 'desc', 'rand', false );
442-
443-
$this->assertStringContainsString( 'RAND()', $retval['orderby'] );
444-
}
445-
446-
/**
447-
* Coverage for get_datetime_comparison_column method.
448-
*
449-
* @covers ::get_datetime_comparison_column
450-
*
451-
* @return void
452-
*/
453-
public function test_get_datetime_comparison_column(): void {
454-
$instance = Event_Query::get_instance();
455-
456-
$this->assertSame(
457-
'datetime_end_gmt',
458-
Utility::invoke_hidden_method( $instance, 'get_datetime_comparison_column', array( 'upcoming', true ) ),
459-
'Failed to assert, that inclusive, upcoming events should be ordered by datetime_end_gmt.'
460-
);
461-
$this->assertSame(
462-
'datetime_start_gmt',
463-
Utility::invoke_hidden_method( $instance, 'get_datetime_comparison_column', array( 'upcoming', false ) ),
464-
'Failed to assert, that non-inclusive, upcoming events should be ordered by datetime_start_gmt.'
465-
);
466-
467-
$this->assertSame(
468-
'datetime_start_gmt',
469-
Utility::invoke_hidden_method( $instance, 'get_datetime_comparison_column', array( 'past', true ) ),
470-
'Failed to assert, that inclusive, past events should be ordered by datetime_start_gmt.'
471-
);
472-
$this->assertSame(
473-
'datetime_end_gmt',
474-
Utility::invoke_hidden_method( $instance, 'get_datetime_comparison_column', array( 'past', false ) ),
475-
'Failed to assert, that non-inclusive, past events should be ordered by datetime_end_gmt.'
476-
);
421+
$this->assertStringContainsString( 'ASC', $retval['orderby'] );
422+
$this->assertStringContainsString( "AND `{$table}`.datetime_end_gmt >=", $retval['where'] );
477423
}
478424
}

0 commit comments

Comments
 (0)