enabled ) ) { return; } add_filter( 'manage_users_columns', array( $this, 'add_columns' ) ); add_filter( 'manage_users_custom_column', array( $this, 'render' ), 10, 3 ); add_filter( 'manage_users_sortable_columns', array( $this, 'sortable' ) ); add_filter( 'users_list_table_query_args', array( $this, 'apply_sort' ) ); add_action( 'admin_head-users.php', array( $this, 'print_styles' ) ); } /** * Append the enabled column headers. * * @param array $columns Existing columns. * @return array */ public function add_columns( array $columns ): array { $labels = array( self::COL_ID => __( 'ID', 'all-in-one-utilities' ), self::COL_REGISTERED => __( 'Registered', 'all-in-one-utilities' ), self::COL_AGE => __( 'Account Age', 'all-in-one-utilities' ), self::COL_LAST_LOGIN => __( 'Last Login', 'all-in-one-utilities' ), ); foreach ( $labels as $key => $label ) { if ( in_array( $key, $this->enabled, true ) ) { $columns[ $key ] = $label; } } return $columns; } /** * Render one cell. * * @param string $output Existing cell output. * @param string $column_name Column key. * @param int $user_id User id. * @return string */ public function render( string $output, string $column_name, int $user_id ): string { if ( ! in_array( $column_name, $this->enabled, true ) ) { return $output; } $user = get_userdata( $user_id ); if ( ! $user ) { return $output; } switch ( $column_name ) { case self::COL_ID: return esc_html( (string) $user_id ); case self::COL_REGISTERED: $registered = LoginTracker::registered( $user ); return sprintf( '%2$s', esc_attr( wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $registered ) ), esc_html( wp_date( get_option( 'date_format' ), $registered ) ) ); case self::COL_AGE: return esc_html( human_time_diff( LoginTracker::registered( $user ) ) ); case self::COL_LAST_LOGIN: return self::last_login_html( $user ); } return $output; } /** * "Today, 14:05" / "Yesterday" / a date / "Never" / "Not recorded". * * @param \WP_User $user User. * @return string Escaped HTML. */ public static function last_login_html( \WP_User $user ): string { $last = LoginTracker::last_login( $user->ID ); if ( ! $last ) { if ( LoginTracker::is_known_never( $user ) ) { return '' . esc_html__( 'Never', 'all-in-one-utilities' ) . ''; } $since = LoginTracker::tracking_since(); return sprintf( '%2$s', esc_attr( sprintf( /* translators: %s: date login tracking started */ __( 'No login recorded since tracking began on %s.', 'all-in-one-utilities' ), wp_date( get_option( 'date_format' ), $since ? $since : time() ) ) ), esc_html__( 'Not recorded', 'all-in-one-utilities' ) ); } $day = wp_date( 'Y-m-d', $last ); if ( wp_date( 'Y-m-d' ) === $day ) { /* translators: %s: time of day */ $text = sprintf( __( 'Today, %s', 'all-in-one-utilities' ), wp_date( get_option( 'time_format' ), $last ) ); } elseif ( wp_date( 'Y-m-d', time() - DAY_IN_SECONDS ) === $day ) { $text = __( 'Yesterday', 'all-in-one-utilities' ); } else { $text = wp_date( get_option( 'date_format' ), $last ); } return sprintf( '%2$s', esc_attr( wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $last ) ), esc_html( $text ) ); } /** * Make the date-based columns sortable. Registered and ID map onto * `WP_User_Query`'s own native orderby values. * * @param array $columns Sortable columns. * @return array */ public function sortable( array $columns ): array { $map = array( self::COL_ID => 'ID', self::COL_REGISTERED => 'registered', self::COL_LAST_LOGIN => self::COL_LAST_LOGIN, ); foreach ( $map as $key => $orderby ) { if ( in_array( $key, $this->enabled, true ) ) { $columns[ $key ] = $orderby; } } return $columns; } /** * Route a "Last Login" sort through `UserListQuery`, which keeps users * without a recorded login in the list. * * @param array $args List table query args. * @return array */ public function apply_sort( array $args ): array { if ( self::COL_LAST_LOGIN === ( $args['orderby'] ?? '' ) ) { $args = UserListQuery::sort_by_meta( $args, LoginTracker::META_LAST_LOGIN ); } return $args; } /** * Keep the short columns narrow, and reserve room for Username -- core * leaves it unsized, so extra columns squeeze it until its row actions * wrap one word per line. * * @return void */ public function print_styles(): void { echo ''; } }