Skip to content

Commit

Permalink
Add caching back and fix PHPCS issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
mauteri committed Jan 28, 2024
1 parent c41df57 commit 88661a8
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions includes/core/classes/class-event.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@
* @since 1.0.0
*/
class Event {
/**
* Cache key format for storing and retrieving event datetimes.
*
* @since 1.0.0
* @var string $DATETIME_CACHE_KEY
*/
const DATETIME_CACHE_KEY = 'datetime_%d';

/**
* Date and time format used within GatherPress.
*
Expand Down Expand Up @@ -70,15 +78,15 @@ class Event {
* @since 1.0.0
* @var array
*/
protected array $datetimes = [];
protected array $datetimes = array();

/**
* Storing and retrieving various formats for event datetimes.
*
* @since 1.0.0
* @var array
*/
protected array $formatted_datetimes = [];
protected array $formatted_datetimes = array();

/**
* RSVP instance.
Expand Down Expand Up @@ -582,13 +590,15 @@ public function get_datetime(): array {
return $default;
}

$data = $this->datetimes;
$cache_key = sprintf( self::DATETIME_CACHE_KEY, $this->event->ID );
$data = wp_cache_get( $cache_key ) ?? $this->datetimes;

if ( empty( $data ) || ! is_array( $data ) ) {
$table = sprintf( static::TABLE_FORMAT, $wpdb->prefix );
$data = (array) $wpdb->get_results( $wpdb->prepare( 'SELECT datetime_start, datetime_start_gmt, datetime_end, datetime_end_gmt, timezone FROM ' . esc_sql( $table ) . ' WHERE post_id = %d LIMIT 1', $this->event->ID ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
$data = ( ! empty( $data ) ) ? (array) current( $data ) : array();

wp_cache_set( $cache_key, $data, 15 * MINUTE_IN_SECONDS );
$this->datetimes = $data;
}

Expand Down Expand Up @@ -933,6 +943,7 @@ function ( $key ) {
$fields,
array( 'post_id' => $fields['post_id'] )
);
wp_cache_delete( sprintf( self::DATETIME_CACHE_KEY, $fields['post_id'] ) );
} else {
$retval = $wpdb->insert( $table, $fields ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
}
Expand Down

0 comments on commit 88661a8

Please sign in to comment.