Skip to content

Commit e159009

Browse files
authored
Merge pull request #29 from WP-API/creation-time
Display creation time for tokens
2 parents d389b78 + 63c4690 commit e159009

File tree

2 files changed

+90
-32
lines changed

2 files changed

+90
-32
lines changed

inc/admin/profile/namespace.php

Lines changed: 79 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
* Bootstrap actions for the profile screen.
1313
*/
1414
function bootstrap() {
15-
add_action( 'personal_options', __NAMESPACE__ . '\\render_profile_section', 50 );
15+
add_action( 'show_user_profile', __NAMESPACE__ . '\\render_profile_section' );
16+
add_action( 'edit_user_profile', __NAMESPACE__ . '\\render_profile_section' );
1617
add_action( 'all_admin_notices', __NAMESPACE__ . '\\output_profile_messages' );
1718
add_action( 'personal_options_update', __NAMESPACE__ . '\\handle_revocation', 10, 1 );
1819
add_action( 'edit_user_profile_update', __NAMESPACE__ . '\\handle_revocation', 10, 1 );
@@ -26,40 +27,87 @@ function bootstrap() {
2627
function render_profile_section( WP_User $user ) {
2728
$tokens = Access_Token::get_for_user( $user );
2829
?>
29-
<table class="form-table">
30-
<tbody>
30+
<h2><?php _e( 'Authorized Applications', 'oauth2' ) ?></h2>
31+
<?php if ( ! empty( $tokens ) ): ?>
32+
<table class="widefat">
33+
<thead>
3134
<tr>
32-
<th scope="row"><?php _e( 'Authorized Applications', 'oauth2' ) ?></th>
33-
<td>
34-
<?php if ( ! empty( $tokens ) ): ?>
35-
<table class="widefat">
36-
<thead>
37-
<tr>
38-
<th style="padding-left:10px;"><?php esc_html_e( 'Application Name', 'oauth2' ); ?></th>
39-
<th></th>
40-
</tr>
41-
</thead>
42-
<tbody>
43-
<?php foreach ( $tokens as $token ): ?>
44-
<?php
45-
/** @var Access_Token $token */
46-
$client = $token->get_client();
47-
?>
48-
<tr>
49-
<td><?php echo $client->get_name() ?></td>
50-
<td><button class="button" name="oauth2_revoke" value="<?php echo esc_attr( $token->get_key() ) ?>"><?php esc_html_e( 'Revoke', 'oauth2' ) ?></button>
51-
</tr>
52-
53-
<?php endforeach ?>
54-
</tbody>
55-
</table>
56-
<?php else: ?>
57-
<p class="description"><?php esc_html_e( 'No applications authorized.', 'oauth2' ) ?></p>
58-
<?php endif ?>
59-
</td>
35+
<th style="padding-left:10px;"><?php esc_html_e( 'Application Name', 'oauth2' ); ?></th>
36+
<th></th>
6037
</tr>
38+
</thead>
39+
<tbody>
40+
<?php
41+
foreach ( $tokens as $token ) {
42+
render_token_row( $user, $token );
43+
}
44+
?>
6145
</tbody>
6246
</table>
47+
<?php else: ?>
48+
<p class="description"><?php esc_html_e( 'No applications authorized.', 'oauth2' ) ?></p>
49+
<?php endif ?>
50+
<?php
51+
}
52+
53+
/**
54+
* Render a single row.
55+
*/
56+
function render_token_row( WP_User $user, Access_Token $token ) {
57+
$client = $token->get_client();
58+
59+
$creation_time = $token->get_creation_time();
60+
$details = [
61+
sprintf(
62+
/* translators: %1$s: formatted date, %2$s: formatted time */
63+
esc_html__( 'Authorized %1$s at %2$s', 'oauth2' ),
64+
date( get_option( 'date_format' ), $creation_time ),
65+
date( get_option( 'time_format' ), $creation_time )
66+
),
67+
];
68+
69+
/**
70+
* Filter details shown for an access token on the profile screen.
71+
*
72+
* @param string[] $details List of HTML snippets to render in table.
73+
* @param Access_Token $token Token being displayed.
74+
* @param WP_User $user User whose profile is being rendered.
75+
*/
76+
$details = apply_filters( 'oauth2.admin.profile.render_token_row.details', $details, $token, $user );
77+
78+
// Build actions.
79+
$button_title = sprintf(
80+
/* translators: %s: app name */
81+
__( 'Revoke access for "%s"', 'oauth2' ),
82+
$client->get_name()
83+
);
84+
$actions = [
85+
sprintf(
86+
'<button class="button" name="oauth2_revoke" title="%s" value="%s">%s</button>',
87+
$button_title,
88+
esc_attr( $token->get_key() ),
89+
esc_html__( 'Revoke', 'oauth2' )
90+
),
91+
];
92+
93+
/**
94+
* Filter actions shown for an access token on the profile screen.
95+
*
96+
* @param string[] $actions List of HTML snippets to render in table.
97+
* @param Access_Token $token Token being displayed.
98+
* @param WP_User $user User whose profile is being rendered.
99+
*/
100+
$actions = apply_filters( 'oauth2.admin.profile.render_token_row.actions', $actions, $token, $user );
101+
?>
102+
<tr>
103+
<td>
104+
<p><strong><?php echo $client->get_name() ?></strong></p>
105+
<p><?php echo implode( ' | ', $details ) ?></p>
106+
</td>
107+
<td style="vertical-align: middle">
108+
<?php echo implode( '', $actions ) ?>
109+
</td>
110+
</tr>
63111
<?php
64112
}
65113

inc/tokens/class-access-token.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ public function get_client() {
2727
return Client::get_by_id( $this->value['client'] );
2828
}
2929

30+
/**
31+
* Get creation time for the token.
32+
*
33+
* @return int Creation timestamp.
34+
*/
35+
public function get_creation_time() {
36+
return $this->value['created'];
37+
}
38+
3039
/**
3140
* Revoke the token.
3241
*
@@ -116,7 +125,8 @@ public static function create( Client $client, WP_User $user ) {
116125
}
117126

118127
$data = array(
119-
'client' => $client->get_id(),
128+
'client' => $client->get_id(),
129+
'created' => time(),
120130
);
121131
$key = wp_generate_password( static::KEY_LENGTH, false );
122132
$meta_key = static::META_PREFIX . $key;

0 commit comments

Comments
 (0)