-
Notifications
You must be signed in to change notification settings - Fork 0
/
quick_user_delete.php
103 lines (74 loc) · 3.36 KB
/
quick_user_delete.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
/*
Plugin Name: Quick User Delete.
Plugin URI: http://warrenholmes.co.za
Description: Quickly delete a user.
Version: 1.0.0
Author: Warren Holmes
Author URI: http://warrenholmes.co.za/
License: GPL version 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/
/* Copyright 2012 WooThemes (email : [email protected])
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
//add action to create the delete link on user listings
add_filter( 'user_row_actions', 'quick_user_delete_link', 10, 2 );
//hook onto admin_init for the delete
add_action( 'admin_init', 'do_quick_delete_user' );
//add javascript
add_action( 'admin_enqueue_scripts', 'enqueue_styles_scripts' );
function quick_user_delete_link( $actions, $user ){
$actions['quick_delete'] = '<a href="' . create_quick_user_delete_link( $user->ID ) . '" class="quick_user_delete">' . __( 'Quick Delete', 'quick_user_delete' ) . '</a>';
return $actions;
}
//Code which creates the link. Can be used anywhere
function create_quick_user_delete_link( $user_id ){
return wp_nonce_url( add_query_arg( array(
'action' => 'do_quick_delete_user',
'user_id' => $user_id
), admin_url() ), "do_quick_delete_user_{$user_id}" );
}
function enqueue_styles_scripts(){
$suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
wp_enqueue_script( 'quick_user_delete', plugin_dir_url( __FILE__ ) . 'js/admin' . $suffix . '.js', array(), 1.0 );
}
//Code to actually delete a user
function do_quick_delete_user(){
global $current_user;
if( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'do_quick_delete_user' ) {
$redirect = 'users.php';
if ( is_multisite() )
wp_die( __('User deletion is not currently possible with this plugin.') );
if ( empty($_REQUEST['user_id']) ) {
wp_redirect( admin_url() );
exit();
}
if ( ! current_user_can( 'delete_users' ) )
wp_die(__('You can’t delete users.'));
$user_id = $_REQUEST['user_id'];
$update = 'del';
$delete_count = 0;
check_admin_referer("do_quick_delete_user_{$user_id}");
$id = (int) $user_id;
if ( ! current_user_can( 'delete_user', $id ) )
wp_die(__( 'You can’t delete that user.' ) );
if ( $id == $current_user->ID )
$update = 'err_admin_del';
if ( current_user_can('delete_user', $id) )
wp_delete_user($id);
++$delete_count;
$redirect = add_query_arg( array( 'delete_count' => $delete_count, 'update' => $update ), $redirect );
wp_redirect( $redirect );
exit();
}
}