Skip to content

Commit

Permalink
Fix selection on the Events page with non-GMT local time.
Browse files Browse the repository at this point in the history
  • Loading branch information
kagg-design committed Feb 16, 2025
1 parent 087c6aa commit a9fb1bf
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 56 deletions.
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,7 @@ Instructions for popular native integrations are below:
* Fixed processing wpDiscuz comment form with wpDiscuz custom ajax.
* Fixed adding hCaptcha internal fields to Avada from submission.
* Fixed ASC ordering by date on the Events page.
* Fixed selection of a time interval on the Events page when site local time was not GMT.

= 4.10.0 =
* Added support for wp_login_form() function and LoginOut block.
Expand Down
113 changes: 57 additions & 56 deletions src/php/Admin/Events/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,28 +312,17 @@ public static function create_table(): void {
public static function get_where_date_gmt( array $args ): string {
$dates = $args['dates'];

if ( $dates ) {
$dates[1] = $dates[1] ?? $dates[0];

$dates[0] .= ' 00:00:00';
$dates[1] .= ' 23:59:59';

foreach ( $dates as &$date ) {
$date = wp_date( 'Y-m-d H:i:s', strtotime( $date ) );
}

unset( $date );

$where_date = sprintf(
"date_gmt BETWEEN '%s' AND '%s'",
esc_sql( $dates[0] ),
esc_sql( $dates[1] )
);
} else {
$where_date = '1=1';
if ( ! $dates ) {
return '1=1';
}

return $where_date;
$dates = self::prepare_gmt_dates( $dates );

return sprintf(
"date_gmt BETWEEN '%s' AND '%s'",
esc_sql( $dates[0] ),
esc_sql( $dates[1] )
);
}

/**
Expand All @@ -348,46 +337,57 @@ public static function get_where_date_gmt_nested( array $args ): string {

$dates = $args['dates'];

if ( $dates ) {
$dates[1] = $dates[1] ?? $dates[0];
if ( ! $dates ) {
return '1=1';
}

$dates = self::prepare_gmt_dates( $dates );
$table_name = $wpdb->prefix . self::TABLE_NAME;
$order = $args['order'];
$offset = $args['offset'];
$compare = 'DESC' === $order ? '<=' : '>=';

return sprintf(
"date_gmt BETWEEN '%s' AND '%s'
AND date_gmt %s (
SELECT date_gmt
FROM %s
WHERE date_gmt BETWEEN '%s' AND '%s'
ORDER BY date_gmt %s
LIMIT %d, 1
)
",
esc_sql( $dates[0] ),
esc_sql( $dates[1] ),
$compare,
$table_name,
esc_sql( $dates[0] ),
esc_sql( $dates[1] ),
$order,
$offset
);
}

$dates[0] .= ' 00:00:00';
$dates[1] .= ' 23:59:59';
/**
* Prepare dates.
*
* @param array $dates Dates.
*
* @return array
*/
private static function prepare_gmt_dates( array $dates ): array {
$dates[1] = $dates[1] ?? $dates[0];

foreach ( $dates as &$date ) {
$date = wp_date( 'Y-m-d H:i:s', strtotime( $date ) );
}
$dates[0] .= ' 00:00:00';
$dates[1] .= ' 23:59:59';

unset( $date );

$table_name = $wpdb->prefix . self::TABLE_NAME;
$order = $args['order'];
$offset = $args['offset'];
$compare = 'DESC' === $order ? '<=' : '>=';
$where_date = sprintf(
"date_gmt BETWEEN '%s' AND '%s'
AND date_gmt %s (
SELECT date_gmt
FROM %s
WHERE date_gmt BETWEEN '%s' AND '%s'
ORDER BY date_gmt %s
LIMIT %d, 1
)
",
esc_sql( $dates[0] ),
esc_sql( $dates[1] ),
$compare,
$table_name,
esc_sql( $dates[0] ),
esc_sql( $dates[1] ),
$order,
$offset
);
} else {
$where_date = '1=1';
foreach ( $dates as &$date ) {
$date = get_gmt_from_date( $date );
}

return $where_date;
unset( $date );

return $dates;
}

/**
Expand Down Expand Up @@ -510,7 +510,8 @@ private static function prepare_args( array $args ): array {
$args['order'] = in_array( $order, [ 'ASC', 'DESC' ], true ) ? $order : 'ASC';
$orderby = strtolower( $args['orderby'] );
$args['orderby'] = in_array( $orderby, $args['columns'], true ) ? $orderby : '';
$args['dates'] = $args['dates'] ?: self::get_default_dates();
$dates = (array) $args['dates'];
$args['dates'] = $dates ?: self::get_default_dates();

return $args;
}
Expand Down

0 comments on commit a9fb1bf

Please sign in to comment.