From 9af055643a41f0b62dc3f9918371dc8aacb5dc02 Mon Sep 17 00:00:00 2001 From: Luis Henrique Mulinari Date: Mon, 14 Aug 2023 16:23:26 -0300 Subject: [PATCH] Code review suggestions --- security/user-last-seen.php | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/security/user-last-seen.php b/security/user-last-seen.php index a8ee44c798a..299d20ef889 100644 --- a/security/user-last-seen.php +++ b/security/user-last-seen.php @@ -1,30 +1,32 @@ ID; + $is_api_request = ( ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ); - try { - if ( wp_cache_get( $cache_key ) ) { - return; - } + var_dump($is_api_request); + + if ( ! $current_user->ID ) { + return; + } - update_user_meta( $current_user->ID, 'vip_last_seen', time() ); + $cache_key = LAST_SEEN_UPDATE_USER_META_CACHE_KEY_PREFIX . $current_user->ID; + + if ( wp_cache_get( $cache_key, 'wpvip' ) ) { + // Last seen meta was updated recently, no need to update it again + return; + } - wp_cache_set( $cache_key, true, null, LAST_SEEN_UPDATE_USER_META_CACHE_TTL ); - } catch ( \Exception $e ) { - trigger_error( - sprintf( 'failed to update user last seen meta', esc_html( $e->getMessage() ) ), - E_USER_WARNING - ); + if ( wp_cache_add( $cache_key, true, 'wpvip', LAST_SEEN_UPDATE_USER_META_CACHE_TTL ) ) { + update_user_meta( $current_user->ID, 'wpvip_last_seen', time() ); } } @@ -35,7 +37,7 @@ function users_columns($cols) { function users_custom_column( $default, $column_name, $user_id ) { if ( 'last_seen' == $column_name ) { - $last_seen_timestamp = get_user_meta( $user_id, 'vip_last_seen', true ); + $last_seen_timestamp = get_user_meta( $user_id, 'wpvip_last_seen', true ); if ( $last_seen_timestamp ) { $formatted_date = sprintf(