Skip to content

Commit 5b46b60

Browse files
authored
Merge pull request #508 from deshabhishek007/main
Fix for Issue #507: Optimize get_formatted_datetime Performance
2 parents 35fbf64 + 5dc82b1 commit 5b46b60

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

includes/core/classes/class-event.php

+11-2
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public static function get_post_type_registration_args(): array {
176176
public static function get_post_meta_registration_args(): array {
177177
return array(
178178
'_online_event_link' => array(
179-
'auth_callback' => function() {
179+
'auth_callback' => function () {
180180
return current_user_can( 'edit_posts' );
181181
},
182182
'sanitize_callback' => 'sanitize_url',
@@ -399,6 +399,13 @@ protected function get_formatted_datetime(
399399
string $which = 'start',
400400
bool $local = true
401401
): string {
402+
$cache_key = 'formatted_datetime_' . md5( $format . $which . ( $local ? 'local' : 'gmt' ) );
403+
404+
$cached_date = get_transient( $cache_key );
405+
if ( false !== $cached_date ) {
406+
return $cached_date;
407+
}
408+
402409
$dt = $this->get_datetime();
403410
$date = $dt[ sprintf( 'datetime_%s_gmt', $which ) ];
404411
$dt['timezone'] = static::maybe_convert_offset( $dt['timezone'] );
@@ -419,6 +426,8 @@ protected function get_formatted_datetime(
419426
$date = wp_date( $format, $ts, $tz );
420427
}
421428

429+
set_transient( $cache_key, $date, HOUR_IN_SECONDS * 12 );
430+
422431
return (string) $date;
423432
}
424433

@@ -868,7 +877,7 @@ public function save_datetimes( array $params ): bool {
868877
$retval = false;
869878
$fields = array_filter(
870879
$params,
871-
function( $key ) {
880+
function ( $key ) {
872881
return in_array(
873882
$key,
874883
array(

0 commit comments

Comments
 (0)