Skip to content

Commit eeda304

Browse files
committed
Add the ability to delete clients
1 parent 8b62719 commit eeda304

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

admin.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
add_action( 'admin_action_json-oauth-add', 'json_oauth_admin_edit_page' );
1010
add_action( 'admin_action_json-oauth-edit', 'json_oauth_admin_edit_page' );
11+
add_action( 'admin_action_json-oauth-delete', 'json_oauth_admin_delete' );
1112

1213
add_action( 'personal_options', 'json_oauth_profile_section', 50 );
1314

@@ -372,3 +373,22 @@ function json_oauth_profile_save( $user_id ) {
372373
wp_redirect($redirect);
373374
exit;
374375
}
376+
377+
function json_oauth_admin_delete() {
378+
if ( empty( $_GET['id'] ) ) {
379+
return;
380+
}
381+
382+
$id = $_GET['id'];
383+
check_admin_referer( 'json-oauth-delete:' . $id );
384+
385+
if ( ! rest_client_delete( $id ) ) {
386+
$message = 'Invalid consumer ID';
387+
wp_die( $message );
388+
return;
389+
}
390+
391+
$redirect = admin_url( 'users.php?action=json-oauth' );
392+
wp_redirect( $redirect );
393+
exit;
394+
}

client.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,19 @@ function rest_create_client( $type, $params ) {
7474

7575
return get_post( $ID );
7676
}
77+
78+
/**
79+
* Delete a client.
80+
*
81+
* @param string $type Client type.
82+
* @param int $id Client post ID.
83+
* @return bool True if delete, false otherwise.
84+
*/
85+
function rest_delete_client( $id ) {
86+
$post = get_post( $id );
87+
if ( empty( $id ) || empty( $post ) || $post->post_type !== 'json_consumer' ) {
88+
return false;
89+
}
90+
91+
return (bool) wp_delete_post( $id, true );
92+
}

lib/class-wp-json-authentication-oauth1-listtable.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,18 @@ protected function column_name( $item ) {
6060
),
6161
admin_url( 'admin.php' )
6262
);
63+
$delete_link = add_query_arg(
64+
array(
65+
'action' => 'json-oauth-delete',
66+
'id' => $item->ID,
67+
),
68+
admin_url( 'admin.php' )
69+
);
70+
$delete_link = wp_nonce_url( $delete_link, 'json-oauth-delete:' . $item->ID );
6371

6472
$actions = array(
6573
'edit' => sprintf( '<a href="%s">%s</a>', $edit_link, __( 'Edit' ) ),
74+
'delete' => sprintf( '<a href="%s">%s</a>', $delete_link, __( 'Delete' ) ),
6675
);
6776
$action_html = $this->row_actions( $actions );
6877

0 commit comments

Comments
 (0)