Skip to content

Commit

Permalink
Merge pull request #508 from deshabhishek007/main
Browse files Browse the repository at this point in the history
Fix for Issue #507: Optimize get_formatted_datetime Performance
  • Loading branch information
mauteri authored Jan 25, 2024
2 parents 35fbf64 + 5dc82b1 commit 5b46b60
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions includes/core/classes/class-event.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public static function get_post_type_registration_args(): array {
public static function get_post_meta_registration_args(): array {
return array(
'_online_event_link' => array(
'auth_callback' => function() {
'auth_callback' => function () {
return current_user_can( 'edit_posts' );
},
'sanitize_callback' => 'sanitize_url',
Expand Down Expand Up @@ -399,6 +399,13 @@ protected function get_formatted_datetime(
string $which = 'start',
bool $local = true
): string {
$cache_key = 'formatted_datetime_' . md5( $format . $which . ( $local ? 'local' : 'gmt' ) );

$cached_date = get_transient( $cache_key );
if ( false !== $cached_date ) {
return $cached_date;
}

$dt = $this->get_datetime();
$date = $dt[ sprintf( 'datetime_%s_gmt', $which ) ];
$dt['timezone'] = static::maybe_convert_offset( $dt['timezone'] );
Expand All @@ -419,6 +426,8 @@ protected function get_formatted_datetime(
$date = wp_date( $format, $ts, $tz );
}

set_transient( $cache_key, $date, HOUR_IN_SECONDS * 12 );

return (string) $date;
}

Expand Down Expand Up @@ -868,7 +877,7 @@ public function save_datetimes( array $params ): bool {
$retval = false;
$fields = array_filter(
$params,
function( $key ) {
function ( $key ) {
return in_array(
$key,
array(
Expand Down

0 comments on commit 5b46b60

Please sign in to comment.