Skip to content

Commit

Permalink
Linting fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
luismulinari committed Aug 21, 2023
1 parent 267e26f commit 48b1594
Showing 1 changed file with 42 additions and 34 deletions.
76 changes: 42 additions & 34 deletions security/user-last-seen.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
namespace Automattic\VIP\Security;

class User_Last_Seen {
const LAST_SEEN_META_KEY = 'wpvip_last_seen';
const LAST_SEEN_CACHE_GROUP = 'wpvip_last_seen';
const LAST_SEEN_UPDATE_USER_META_CACHE_TTL = MINUTE_IN_SECONDS * 5; // Store last seen once every five minute to avoid too many write DB operations
const LAST_SEEN_META_KEY = 'wpvip_last_seen';
const LAST_SEEN_CACHE_GROUP = 'wpvip_last_seen';
const LAST_SEEN_UPDATE_USER_META_CACHE_TTL = MINUTE_IN_SECONDS * 5; // Store last seen once every five minute to avoid too many write DB operations
const LAST_SEEN_RELEASE_DATE_TIMESTAMP_OPTION_KEY = 'wpvip_last_seen_release_date_timestamp';

public function init() {
Expand All @@ -27,13 +27,13 @@ public function init() {

add_filter( 'manage_users_sortable_columns', array( $this, 'add_last_seen_sortable_column' ) );
add_filter( 'manage_users-network_sortable_columns', array( $this, 'add_last_seen_sortable_column' ) );
add_filter( 'users_list_table_query_args', array( $this, 'last_seen_order_by_query_args') );
add_filter( 'users_list_table_query_args', array( $this, 'last_seen_order_by_query_args' ) );
}

if ( $this->is_block_action_enabled() ) {
add_filter( 'views_users', array( $this, 'add_blocked_users_filter' ) );
add_filter( 'views_users-network', array( $this, 'add_blocked_users_filter' ) );
add_filter( 'users_list_table_query_args', array( $this, 'last_seen_blocked_users_filter_query_args') );
add_filter( 'users_list_table_query_args', array( $this, 'last_seen_blocked_users_filter_query_args' ) );

add_action( 'admin_init', array( $this, 'last_seen_unblock_action' ) );
}
Expand All @@ -56,6 +56,7 @@ public function determine_current_user( $user_id ) {
return new \WP_Error( 'inactive_account', __( '<strong>Error</strong>: Your account has been flagged as inactive. Please contact your site administrator.', 'inactive-account-lockdown' ) );
}

// phpcs:ignore WordPressVIPMinimum.Performance.LowExpiryCacheTime.CacheTimeUndetermined
if ( wp_cache_add( $user_id, true, self::LAST_SEEN_CACHE_GROUP, self::LAST_SEEN_UPDATE_USER_META_CACHE_TTL ) ) {
update_user_meta( $user_id, self::LAST_SEEN_META_KEY, time() );
}
Expand All @@ -69,14 +70,17 @@ public function authenticate( $user ) {
}

if ( $user->ID && $this->is_block_action_enabled() && $this->is_considered_inactive( $user->ID ) ) {
return new \WP_Error( 'inactive_account', __( '<strong>Error</strong>: Your account has been flagged as inactive. Please contact your site administrator.', 'inactive-account-lockdown' ) );;
return new \WP_Error(
'inactive_account',
__( '<strong>Error</strong>: Your account has been flagged as inactive. Please contact your site administrator.', 'inactive-account-lockdown' )
);
}

return $user;
}

public function add_last_seen_column_head( $columns ) {
$columns[ 'last_seen' ] = __( 'Last seen' );
$columns['last_seen'] = __( 'Last seen' );
return $columns;
}

Expand All @@ -87,20 +91,21 @@ public function add_last_seen_sortable_column( $columns ) {
}

public function last_seen_order_by_query_args( $vars ) {
if ( isset( $vars['orderby'] ) && $vars['orderby'] === 'last_seen' ) {
$vars[ 'meta_key' ] = self::LAST_SEEN_META_KEY;
$vars[ 'orderby' ] = 'meta_value_num';
if ( isset( $vars['orderby'] ) && 'last_seen' === $vars['orderby'] ) {
$vars['meta_key'] = self::LAST_SEEN_META_KEY;
$vars['orderby'] = 'meta_value_num';
}

return $vars;
}

public function last_seen_blocked_users_filter_query_args($vars ) {
if ( isset( $_REQUEST[ 'last_seen_filter' ] ) && $_REQUEST[ 'last_seen_filter' ] === 'blocked' ) {
$vars[ 'meta_key' ] = self::LAST_SEEN_META_KEY;
$vars[ 'meta_value' ] = $this->get_inactivity_timestamp();
$vars[ 'meta_type' ] = 'NUMERIC';
$vars[ 'meta_compare' ] = '<';
public function last_seen_blocked_users_filter_query_args( $vars ) {
if ( isset( $_REQUEST['last_seen_filter'] ) && 'blocked' === $_REQUEST['last_seen_filter'] && wp_verify_nonce( $_GET['last_seen_filter_nonce'], 'last_seen_filter' ) ) {
$vars['meta_key'] = self::LAST_SEEN_META_KEY;
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
$vars['meta_value'] = $this->get_inactivity_timestamp();
$vars['meta_type'] = 'NUMERIC';
$vars['meta_compare'] = '<';
}

return $vars;
Expand All @@ -117,53 +122,56 @@ public function add_last_seen_column_date( $default, $column_name, $user_id ) {
return $default;
}

$formatted_date = sprintf(
$date = sprintf(
/* translators: 1: Comment date, 2: Comment time. */
__( '%1$s at %2$s' ),
date_i18n( get_option('date_format'), $last_seen_timestamp ),
date_i18n( get_option('time_format'), $last_seen_timestamp )
date_i18n( get_option( 'date_format' ), $last_seen_timestamp ),
date_i18n( get_option( 'time_format' ), $last_seen_timestamp )
);

if ( ! $this->is_block_action_enabled() || ! $this->is_considered_inactive( $user_id ) ) {
return sprintf( '<span>%s</span>', esc_html__( $formatted_date ) );
return sprintf( '<span>%s</span>', esc_html__( $date ) );
}

$unblock_link = '';
if ( current_user_can( 'edit_user', $user_id ) ) {
$url = add_query_arg( array(
'action' => 'reset_last_seen',
'user_id' => $user_id,
'action' => 'reset_last_seen',
'user_id' => $user_id,
'reset_last_seen_nonce' => wp_create_nonce( 'reset_last_seen_action' )
) );

$unblock_link = "<div class='row-actions'><span>User blocked due to inactivity. <a class='reset_last_seen_action' href='" . esc_url( $url ) . "'>" . __( 'Unblock' ) . "</a></span></div>";
$unblock_link = "<div class='row-actions'><span>User blocked due to inactivity. <a class='reset_last_seen_action' href='" . esc_url( $url ) . "'>" . __( 'Unblock' ) . '</a></span></div>';
}
return sprintf( '<span class="wp-ui-text-notification">%s</span>' . $unblock_link, esc_html__( $formatted_date ) );
return sprintf( '<span class="wp-ui-text-notification">%s</span>' . $unblock_link, esc_html__( $date ) );
}

public function add_blocked_users_filter( $views ) {
$blog_id = is_network_admin() ? null : get_current_blog_id();

$users_query = new \WP_User_Query(
array(
'blog_id' => $blog_id,
'fields' => 'ID',
'meta_key' => self::LAST_SEEN_META_KEY,
'meta_value' => $this->get_inactivity_timestamp(),
'meta_type' => 'NUMERIC',
'blog_id' => $blog_id,
'fields' => 'ID',
'meta_key' => self::LAST_SEEN_META_KEY,
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
'meta_value' => $this->get_inactivity_timestamp(),
'meta_type' => 'NUMERIC',
'meta_compare' => '<',
'count_total' => true,
'count_total' => true,
),
);
$count = (int) $users_query->get_total();

$view = __( 'Blocked Users' );
if ( $count ) {
$class = isset( $_REQUEST[ 'last_seen_filter' ] ) ? 'current' : '';

$url = add_query_arg( array(
'last_seen_filter' => 'blocked',
'last_seen_filter_nonce' => wp_create_nonce( 'last_seen_filter' )
) );

$class = isset( $_REQUEST[ 'last_seen_filter' ] ) ? 'current' : '';

$view = '<a class="' . esc_attr( $class ) . '" href="' . esc_url( $url ) . '">' . esc_html( $view ) . '</a>';
}
$views['blocked_users'] = $view . ' (' . $count . ')';
Expand All @@ -183,14 +191,14 @@ public function last_seen_unblock_action() {
} );
}

if ( ! isset( $_GET['action'] ) || $_GET['action'] !== 'reset_last_seen' ) {
if ( ! isset( $_GET['action'] ) || 'reset_last_seen' !== $_GET['action'] ) {
return;
}

$user_id = absint( $_GET['user_id'] );

$error = null;
if ( ! wp_verify_nonce( $_GET['reset_last_seen_nonce'], 'reset_last_seen_action' ) ) {
if ( ! isset( $_GET['reset_last_seen_nonce'] ) || ! wp_verify_nonce( $_GET['reset_last_seen_nonce'], 'reset_last_seen_action' ) ) {
$error = __( 'Unable to verify your request', 'inactive-account-lockdown' );
}

Expand Down

0 comments on commit 48b1594

Please sign in to comment.