From b633e5b82d876b598fc0416b296cd8a6883dc26c Mon Sep 17 00:00:00 2001 From: deshabhishek007 <428211+deshabhishek007@users.noreply.github.com> Date: Wed, 24 Jan 2024 20:53:09 +0530 Subject: [PATCH 1/2] Adding Caching for get_formatted_datetime() Transient time set to 12 Hours for now. --- includes/core/classes/class-event.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/includes/core/classes/class-event.php b/includes/core/classes/class-event.php index 509026897..f3f5b26c0 100644 --- a/includes/core/classes/class-event.php +++ b/includes/core/classes/class-event.php @@ -399,6 +399,14 @@ 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 ($cached_date !== false) { + return $cached_date; + } + $dt = $this->get_datetime(); $date = $dt[ sprintf( 'datetime_%s_gmt', $which ) ]; $dt['timezone'] = static::maybe_convert_offset( $dt['timezone'] ); @@ -419,6 +427,7 @@ protected function get_formatted_datetime( $date = wp_date( $format, $ts, $tz ); } + set_transient($cache_key, $date, 43200); return (string) $date; } From 5dc82b1f6483311dcf6cae5b56208b747063c197 Mon Sep 17 00:00:00 2001 From: deshabhishek007 <428211+deshabhishek007@users.noreply.github.com> Date: Thu, 25 Jan 2024 14:00:51 +0530 Subject: [PATCH 2/2] Update class-event.php - applied WPCS updates - transient time in easy to read format shared by Mike --- includes/core/classes/class-event.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/includes/core/classes/class-event.php b/includes/core/classes/class-event.php index f3f5b26c0..e492db3b4 100644 --- a/includes/core/classes/class-event.php +++ b/includes/core/classes/class-event.php @@ -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', @@ -399,11 +399,10 @@ protected function get_formatted_datetime( string $which = 'start', bool $local = true ): string { + $cache_key = 'formatted_datetime_' . md5( $format . $which . ( $local ? 'local' : 'gmt' ) ); - $cache_key = 'formatted_datetime_' . md5($format . $which . ($local ? 'local' : 'gmt')); - - $cached_date = get_transient($cache_key); - if ($cached_date !== false) { + $cached_date = get_transient( $cache_key ); + if ( false !== $cached_date ) { return $cached_date; } @@ -427,7 +426,8 @@ protected function get_formatted_datetime( $date = wp_date( $format, $ts, $tz ); } - set_transient($cache_key, $date, 43200); + set_transient( $cache_key, $date, HOUR_IN_SECONDS * 12 ); + return (string) $date; } @@ -877,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(