|
11 | 11 |
|
12 | 12 | add_action( 'personal_options', 'json_oauth_profile_section', 50 );
|
13 | 13 |
|
| 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 | + |
14 | 19 | /**
|
15 | 20 | * Register the admin page
|
16 | 21 | */
|
@@ -282,28 +287,66 @@ function json_oauth_profile_section( $user ) {
|
282 | 287 | <tr>
|
283 | 288 | <th scope="row"><?php _e( 'Authorized Applications', 'json_oauth' ) ?></th>
|
284 | 289 | <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> |
296 | 293 | <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> |
299 | 295 | </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 ?> |
304 | 313 | </td>
|
305 | 314 | </tr>
|
306 | 315 | </tbody>
|
307 | 316 | </table>
|
308 | 317 | <?php
|
309 | 318 | }
|
| 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