6
6
* upcoming and past events, applying filters, and ordering events. It also handles adjustments
7
7
* for event pages and admin queries.
8
8
*
9
+ * @todo Reverted this PR, but needs to be investigated again to work with Query block https://github.com/GatherPress/gatherpress/pull/889
10
+ *
9
11
* @package GatherPress\Core
10
12
* @since 1.0.0
11
13
*/
@@ -53,7 +55,7 @@ protected function __construct() {
53
55
*/
54
56
protected function setup_hooks (): void {
55
57
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 ' ) );
57
59
}
58
60
59
61
/**
@@ -231,10 +233,10 @@ static function () use ( $page_id ) {
231
233
switch ( $ events_query ) {
232
234
case 'upcoming ' :
233
235
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 ' ) );
235
237
break ;
236
238
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 ' ) );
238
240
remove_filter ( 'posts_clauses ' , array ( $ this , 'adjust_sorting_for_upcoming_events ' ) );
239
241
break ;
240
242
default :
@@ -249,22 +251,13 @@ static function () use ( $page_id ) {
249
251
* This method modifies the SQL query pieces, including join, where, orderby, etc., to adjust the sorting criteria
250
252
* for upcoming events in the query. It ensures that events are ordered by their start datetime in ascending order.
251
253
*
252
- * @see https://developer.wordpress.org/reference/hooks/posts_clauses/
253
- *
254
254
* @since 1.0.0
255
255
*
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.
258
257
* @return array The modified SQL query pieces with adjusted sorting criteria for upcoming events.
259
258
*/
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 ' );
268
261
}
269
262
270
263
/**
@@ -273,18 +266,11 @@ public function adjust_sorting_for_upcoming_events( array $query_pieces, WP_Quer
273
266
* This method modifies the SQL query pieces, including join, where, orderby, etc., to adjust the sorting criteria
274
267
* for past events in the query. It ensures that events are ordered by their start datetime in the desired order.
275
268
*
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.
278
270
* @return array The modified SQL query pieces with adjusted sorting criteria for past events.
279
271
*/
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 ' );
288
274
}
289
275
290
276
/**
@@ -295,17 +281,18 @@ public function adjust_sorting_for_past_events( array $query_pieces, WP_Query $q
295
281
*
296
282
* @since 1.0.0
297
283
*
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.
300
285
* @return array The modified SQL query pieces with adjusted sorting criteria.
301
286
*/
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 {
303
288
if ( ! is_admin () ) {
304
289
return $ query_pieces ;
305
290
}
306
291
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 ' ) );
309
296
}
310
297
311
298
return $ query_pieces ;
@@ -318,26 +305,15 @@ public function adjust_admin_event_sorting( array $query_pieces, WP_Query $query
318
305
* the `gatherpress_events` table in the database join. It allows querying events based on different
319
306
* criteria such as upcoming or past events and specifying the event order (DESC or ASC).
320
307
*
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
- *
325
308
* @since 1.0.0
326
309
*
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
+ *
332
314
* @return array An array containing adjusted SQL clauses for the Event query.
333
315
*/
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 {
341
317
global $ wpdb ;
342
318
343
319
$ defaults = array (
@@ -350,75 +326,27 @@ public function adjust_event_sql(
350
326
'limits ' => '' ,
351
327
);
352
328
$ 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 );
354
330
$ pieces ['join ' ] .= ' LEFT JOIN ' . esc_sql ( $ table ) . ' ON ' . esc_sql ( $ wpdb ->posts ) . '.ID= '
355
331
. esc_sql ( $ table ) . '.post_id ' ;
356
332
$ order = strtoupper ( $ order );
357
333
358
334
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 ) );
381
336
}
382
337
383
338
if ( 'all ' === $ type ) {
384
339
return $ pieces ;
385
340
}
386
341
387
342
$ current = gmdate ( Event::DATETIME_FORMAT , time () );
388
- $ column = $ this ->get_datetime_comparison_column ( $ type , $ inclusive );
389
343
390
344
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 );
392
346
} 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 );
394
348
}
395
349
396
350
return $ pieces ;
397
351
}
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
- }
424
352
}
0 commit comments