Skip to content

Commit

Permalink
Merge pull request #29 from WP-API/creation-time
Browse files Browse the repository at this point in the history
Display creation time for tokens
  • Loading branch information
rmccue authored Jul 3, 2017
2 parents d389b78 + 63c4690 commit e159009
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 32 deletions.
110 changes: 79 additions & 31 deletions inc/admin/profile/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
* Bootstrap actions for the profile screen.
*/
function bootstrap() {
add_action( 'personal_options', __NAMESPACE__ . '\\render_profile_section', 50 );
add_action( 'show_user_profile', __NAMESPACE__ . '\\render_profile_section' );
add_action( 'edit_user_profile', __NAMESPACE__ . '\\render_profile_section' );
add_action( 'all_admin_notices', __NAMESPACE__ . '\\output_profile_messages' );
add_action( 'personal_options_update', __NAMESPACE__ . '\\handle_revocation', 10, 1 );
add_action( 'edit_user_profile_update', __NAMESPACE__ . '\\handle_revocation', 10, 1 );
Expand All @@ -26,40 +27,87 @@ function bootstrap() {
function render_profile_section( WP_User $user ) {
$tokens = Access_Token::get_for_user( $user );
?>
<table class="form-table">
<tbody>
<h2><?php _e( 'Authorized Applications', 'oauth2' ) ?></h2>
<?php if ( ! empty( $tokens ) ): ?>
<table class="widefat">
<thead>
<tr>
<th scope="row"><?php _e( 'Authorized Applications', 'oauth2' ) ?></th>
<td>
<?php if ( ! empty( $tokens ) ): ?>
<table class="widefat">
<thead>
<tr>
<th style="padding-left:10px;"><?php esc_html_e( 'Application Name', 'oauth2' ); ?></th>
<th></th>
</tr>
</thead>
<tbody>
<?php foreach ( $tokens as $token ): ?>
<?php
/** @var Access_Token $token */
$client = $token->get_client();
?>
<tr>
<td><?php echo $client->get_name() ?></td>
<td><button class="button" name="oauth2_revoke" value="<?php echo esc_attr( $token->get_key() ) ?>"><?php esc_html_e( 'Revoke', 'oauth2' ) ?></button>
</tr>

<?php endforeach ?>
</tbody>
</table>
<?php else: ?>
<p class="description"><?php esc_html_e( 'No applications authorized.', 'oauth2' ) ?></p>
<?php endif ?>
</td>
<th style="padding-left:10px;"><?php esc_html_e( 'Application Name', 'oauth2' ); ?></th>
<th></th>
</tr>
</thead>
<tbody>
<?php
foreach ( $tokens as $token ) {
render_token_row( $user, $token );
}
?>
</tbody>
</table>
<?php else: ?>
<p class="description"><?php esc_html_e( 'No applications authorized.', 'oauth2' ) ?></p>
<?php endif ?>
<?php
}

/**
* Render a single row.
*/
function render_token_row( WP_User $user, Access_Token $token ) {
$client = $token->get_client();

$creation_time = $token->get_creation_time();
$details = [
sprintf(
/* translators: %1$s: formatted date, %2$s: formatted time */
esc_html__( 'Authorized %1$s at %2$s', 'oauth2' ),
date( get_option( 'date_format' ), $creation_time ),
date( get_option( 'time_format' ), $creation_time )
),
];

/**
* Filter details shown for an access token on the profile screen.
*
* @param string[] $details List of HTML snippets to render in table.
* @param Access_Token $token Token being displayed.
* @param WP_User $user User whose profile is being rendered.
*/
$details = apply_filters( 'oauth2.admin.profile.render_token_row.details', $details, $token, $user );

// Build actions.
$button_title = sprintf(
/* translators: %s: app name */
__( 'Revoke access for "%s"', 'oauth2' ),
$client->get_name()
);
$actions = [
sprintf(
'<button class="button" name="oauth2_revoke" title="%s" value="%s">%s</button>',
$button_title,
esc_attr( $token->get_key() ),
esc_html__( 'Revoke', 'oauth2' )
),
];

/**
* Filter actions shown for an access token on the profile screen.
*
* @param string[] $actions List of HTML snippets to render in table.
* @param Access_Token $token Token being displayed.
* @param WP_User $user User whose profile is being rendered.
*/
$actions = apply_filters( 'oauth2.admin.profile.render_token_row.actions', $actions, $token, $user );
?>
<tr>
<td>
<p><strong><?php echo $client->get_name() ?></strong></p>
<p><?php echo implode( ' | ', $details ) ?></p>
</td>
<td style="vertical-align: middle">
<?php echo implode( '', $actions ) ?>
</td>
</tr>
<?php
}

Expand Down
12 changes: 11 additions & 1 deletion inc/tokens/class-access-token.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ public function get_client() {
return Client::get_by_id( $this->value['client'] );
}

/**
* Get creation time for the token.
*
* @return int Creation timestamp.
*/
public function get_creation_time() {
return $this->value['created'];
}

/**
* Revoke the token.
*
Expand Down Expand Up @@ -116,7 +125,8 @@ public static function create( Client $client, WP_User $user ) {
}

$data = array(
'client' => $client->get_id(),
'client' => $client->get_id(),
'created' => time(),
);
$key = wp_generate_password( static::KEY_LENGTH, false );
$meta_key = static::META_PREFIX . $key;
Expand Down

0 comments on commit e159009

Please sign in to comment.