Skip to content

Commit

Permalink
LastLogin admin column and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joemaller committed Jan 7, 2023
1 parent 5797a47 commit 070680d
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ A common baseline of repeated functions, filters and actions used across our Wor
Adds a Template column to Pages admin and a summary table to the Appearance menu showing which templates have been assigned to pages.

- **Record Users' Last Login time**
Timestamps of successful logins are stored in user_meta and displayed in the WordPress Admin dashboard User listings for administrators.
User's last successful login are recorded and added to the WordPress Admin User table.

- **Enable and limit WP_POST_REVISIONS**
Revisions are set to 6, this overrides any constants set in wp-config.php.
Expand Down
20 changes: 19 additions & 1 deletion src/ThemeInit/Admin/LastLogin.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
<?php
namespace IdeasOnPurpose\ThemeInit\Admin;

use DateTimeZone;
use DateTimeImmutable;

/**
* Store a timestamp of the last_login time to user_meta
*
* NOTE: WordPress convention uses snake_case for meta_key values
* but kebab-case for column IDs. So this file uses both last_login
* and last-login.
*/
class LastLogin
{
Expand Down Expand Up @@ -51,7 +58,18 @@ public function column_content($output, $column_name, $user_id)
if ($last == 0) {
return '--';
}
$date = date('r', $last);

$dateTime = new DateTimeImmutable("@{$last}");
$timezone = new DateTimeZone(wp_timezone_string());
$dateTime = $dateTime->setTimezone($timezone);
$date_format = get_option('date_format');
$time_format = get_option('time_format');
$date = sprintf(
'%s at %s',
$dateTime->format($date_format),
$dateTime->format($time_format)
);

return sprintf('<span title="%s">%s ago</span>', $date, human_time_diff($last));
}
return $output;
Expand Down
56 changes: 56 additions & 0 deletions tests/LastLoginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,60 @@ public function testLogLastLogin()
$this->assertEquals($user_meta[$User->ID][0]['meta_key'], 'last_login');
$this->assertIsInt($user_meta[$User->ID][0]['meta_value']);
}

public function testAddColumn()
{
$LastLogin = new Admin\LastLogin();
$cols = ['cb' => 'checkbox', 'posts' => 'Posts'];
$actual = $LastLogin->add_column($cols);

$this->assertArrayHasKey('last-login', $actual);
$this->assertArrayHasKey('posts', $actual);
$this->assertArrayHasKey('cb', $actual);
}

public function testColumnContent()
{
global $user_meta, $options, $human_time_diff;
$options['date_format'] = 'F j, Y';
$options['time_format'] = 'H:i:s';

$human_time_diff = '37 mins';

$timezone = new \DateTimeZone('America/New_York');
$dateTime = \DateTimeImmutable::createFromFormat(
'Y-m-d H:i:s',
'2023-01-25 23:45:56',
$timezone
);
$user_meta = $dateTime->format('U');

$LastLogin = new Admin\LastLogin();
$actual = $LastLogin->column_content('output', 'last-login', 123);

$this->assertStringContainsString('<span', $actual);
$this->assertStringContainsString('37 mins ago', $actual);
$this->assertStringContainsString('2023', $actual);
$this->assertStringContainsString('January 25, 2023 at 23:45:56', $actual);
$this->assertStringContainsString('45', $actual);

$expected = 'output';
$actual = $LastLogin->column_content($expected, 'not-a-column', 123);
$this->assertEquals('output', $actual);

$user_meta = 0;
$actual = $LastLogin->column_content('output', 'last-login', 123);
$this->assertEquals('--', $actual);
}

public function testAdminStyles()
{
global $inline_styles;
$LastLogin = new Admin\LastLogin();

$LastLogin->admin_styles();
$actual = end($inline_styles);
$this->assertArrayHasKey('handle', $actual);
$this->assertStringContainsString('.column-last-login', $actual['data']);
}
}

0 comments on commit 070680d

Please sign in to comment.