Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for Issue #507: Optimize get_formatted_datetime Performance #508

Merged
merged 2 commits into from
Jan 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading