Skip to content

Commit 83a6c69

Browse files
committed
Add revocation UI
1 parent e84f337 commit 83a6c69

File tree

1 file changed

+60
-17
lines changed

1 file changed

+60
-17
lines changed

admin.php

Lines changed: 60 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111

1212
add_action( 'personal_options', 'json_oauth_profile_section', 50 );
1313

14+
add_action( 'all_admin_notices', 'json_oauth_profile_messages' );
15+
16+
add_action( 'personal_options_update', 'json_oauth_profile_save', 10, 1 );
17+
add_action( 'edit_user_profile_update', 'json_oauth_profile_save', 10, 1 );
18+
1419
/**
1520
* Register the admin page
1621
*/
@@ -282,28 +287,66 @@ function json_oauth_profile_section( $user ) {
282287
<tr>
283288
<th scope="row"><?php _e( 'Authorized Applications', 'json_oauth' ) ?></th>
284289
<td>
285-
<table class="widefat sessions-table">
286-
<thead>
287-
<tr>
288-
<th scope="col"><?php _e( 'Application Name', 'wpsm' ); ?></th>
289-
</tr>
290-
</thead>
291-
<tbody>
292-
<?php foreach ( $approved as $row ): ?>
293-
<?php
294-
$application = $authenticator->get_consumer( $row['consumer'] );
295-
?>
290+
<?php if ( ! empty( $approved ) ): ?>
291+
<table class="widefat sessions-table">
292+
<thead>
296293
<tr>
297-
<td><?php echo esc_html( $application->post_title ) ?></td>
298-
<td><button class="button" name="oauth_revoke" value="<?php echo esc_attr( $row['key'] ) ?>"><?php esc_html_e( 'Revoke', 'json_oauth' ) ?></button>
294+
<th scope="col"><?php _e( 'Application Name', 'wpsm' ); ?></th>
299295
</tr>
300-
301-
<?php endforeach ?>
302-
</tbody>
303-
</table>
296+
</thead>
297+
<tbody>
298+
<?php foreach ( $approved as $row ): ?>
299+
<?php
300+
$application = $authenticator->get_consumer( $row['consumer'] );
301+
?>
302+
<tr>
303+
<td><?php echo esc_html( $application->post_title ) ?></td>
304+
<td><button class="button" name="oauth_revoke" value="<?php echo esc_attr( $row['key'] ) ?>"><?php esc_html_e( 'Revoke', 'json_oauth' ) ?></button>
305+
</tr>
306+
307+
<?php endforeach ?>
308+
</tbody>
309+
</table>
310+
<?php else: ?>
311+
<p class="description"><?php esc_html_e( 'No applications authorized.' ) ?></p>
312+
<?php endif ?>
304313
</td>
305314
</tr>
306315
</tbody>
307316
</table>
308317
<?php
309318
}
319+
320+
function json_oauth_profile_messages() {
321+
global $pagenow;
322+
if ( $pagenow !== 'profile.php' && $pagenow !== 'user-edit.php' ) {
323+
return;
324+
}
325+
326+
if ( ! empty( $_GET['oauth_revoked'] ) ) {
327+
echo '<div id="message" class="updated"><p>' . __( 'Token revoked.' ) . '</p></div>';
328+
}
329+
if ( ! empty( $_GET['oauth_revocation_failed'] ) ) {
330+
echo '<div id="message" class="updated"><p>' . __( 'Unable to revoke token.' ) . '</p></div>';
331+
}
332+
}
333+
334+
function json_oauth_profile_save( $user_id ) {
335+
if ( empty( $_POST['oauth_revoke'] ) ) {
336+
return;
337+
}
338+
339+
$key = wp_unslash( $_POST['oauth_revoke'] );
340+
341+
$authenticator = new WP_JSON_Authentication_OAuth1();
342+
343+
$result = $authenticator->revoke_access_token( $key );
344+
if ( is_wp_error( $result ) ) {
345+
$redirect = add_query_arg( 'oauth_revocation_failed', true, get_edit_user_link( $user_id ) );
346+
}
347+
else {
348+
$redirect = add_query_arg( 'oauth_revoked', $key, get_edit_user_link( $user_id ) );
349+
}
350+
wp_redirect($redirect);
351+
exit;
352+
}

0 commit comments

Comments
 (0)