New User Approve - Version 1.5.7

Version Description

  • fix bug that was preventing bulk approval/denials
Download this release

Release Info

Developer picklewagon
Plugin Icon 128x128 New User Approve
Version 1.5.7
Comparing to
See all releases

Code changes from version 1.5.6 to 1.5.7

Files changed (4) hide show
  1. includes/admin-approve.php +235 -225
  2. includes/user-list.php +386 -370
  3. new-user-approve.php +550 -564
  4. readme.txt +4 -1
includes/admin-approve.php CHANGED
@@ -7,234 +7,244 @@
7
 
8
  class pw_new_user_approve_admin_approve {
9
 
10
- var $_admin_page = 'new-user-approve-admin';
11
-
12
- /**
13
- * The only instance of pw_new_user_approve_admin_approve.
14
- *
15
- * @var pw_new_user_approve_admin_approve
16
- */
17
- private static $instance;
18
-
19
- /**
20
- * Returns the main instance.
21
- *
22
- * @return pw_new_user_approve_admin_approve
23
- */
24
- public static function instance() {
25
- if ( ! isset( self::$instance ) ) {
26
- self::$instance = new pw_new_user_approve_admin_approve();
27
- }
28
- return self::$instance;
29
- }
30
-
31
- private function __construct() {
32
- // Actions
33
- add_action( 'admin_menu', array( $this, 'admin_menu_link' ) );
34
- add_action( 'admin_init', array( $this, 'process_input' ) );
35
- add_action( 'admin_notices', array( $this, 'admin_notice' ) );
36
- add_action( 'admin_init', array( $this, 'notice_ignore' ) );
37
- }
38
-
39
- /**
40
- * Add the new menu item to the users portion of the admin menu
41
- *
42
- * @uses admin_menu
43
- */
44
- function admin_menu_link() {
45
- $show_admin_page = apply_filters( 'new_user_approve_show_admin_page', true );
46
-
47
- if ( $show_admin_page ) {
48
- $cap = apply_filters( 'new_user_approve_minimum_cap', 'edit_users' );
49
- add_users_page( __( 'Approve New Users', 'new-user-approve' ), __( 'Approve New Users', 'new-user-approve' ), $cap, $this->_admin_page, array( $this, 'approve_admin' ) );
50
- }
51
- }
52
-
53
- /**
54
- * Create the view for the admin interface
55
- */
56
- public function approve_admin() {
57
- if ( isset( $_GET['user'] ) && isset( $_GET['status'] ) ) {
58
- echo '<div id="message" class="updated fade"><p>'.__( 'User successfully updated.', 'new-user-approve' ).'</p></div>';
59
- }
60
-
61
- $active_tab = isset( $_GET[ 'tab' ] ) ? $_GET[ 'tab' ] : 'pending_users';
62
- ?>
63
- <div class="wrap">
64
- <h2><?php _e( 'User Registration Approval', 'new-user-approve' ); ?></h2>
65
-
66
- <h3 class="nav-tab-wrapper">
67
- <a href="<?php echo esc_url( admin_url( 'users.php?page=new-user-approve-admin&tab=pending_users' ) ); ?>" class="nav-tab<?php echo $active_tab == 'pending_users' ? ' nav-tab-active' : ''; ?>"><span><?php _e( 'Users Pending Approval', 'new-user-approve' ); ?></span></a>
68
- <a href="<?php echo esc_url( admin_url( 'users.php?page=new-user-approve-admin&tab=approved_users' ) ); ?>" class="nav-tab<?php echo $active_tab == 'approved_users' ? ' nav-tab-active' : ''; ?>"><span><?php _e( 'Approved Users', 'new-user-approve' ); ?></span></a>
69
- <a href="<?php echo esc_url( admin_url( 'users.php?page=new-user-approve-admin&tab=denied_users' ) ); ?>" class="nav-tab<?php echo $active_tab == 'denied_users' ? ' nav-tab-active' : ''; ?>"><span><?php _e( 'Denied Users', 'new-user-approve' ); ?></span></a>
70
- </h3>
71
-
72
- <?php if ( $active_tab == 'pending_users' ) : ?>
73
- <div id="pw_pending_users">
74
- <?php $this->user_table( 'pending' ); ?>
75
- </div>
76
- <?php elseif ( $active_tab == 'approved_users') : ?>
77
- <div id="pw_approved_users">
78
- <?php $this->user_table( 'approved' ); ?>
79
- </div>
80
- <?php elseif ( $active_tab == 'denied_users') : ?>
81
- <div id="pw_denied_users">
82
- <?php $this->user_table( 'denied' ); ?>
83
- </div>
84
- <?php endif; ?>
85
- </div>
86
- <?php
87
- }
88
-
89
- /**
90
- * Output the table that shows the registered users grouped by status
91
- *
92
- * @param string $status the filter to use for which the users will be queried. Possible values are pending, approved, or denied.
93
- */
94
- public function user_table( $status ) {
95
- global $current_user;
96
-
97
- $approve = ( 'denied' == $status || 'pending' == $status );
98
- $deny = ( 'approved' == $status || 'pending' == $status );
99
-
100
- $user_status = pw_new_user_approve()->get_user_statuses();
101
- $users = $user_status[$status];
102
-
103
- if ( count( $users ) > 0 ) {
104
- ?>
105
- <table class="widefat">
106
- <thead>
107
- <tr class="thead">
108
- <th><?php _e( 'Username', 'new-user-approve' ); ?></th>
109
- <th><?php _e( 'Name', 'new-user-approve' ); ?></th>
110
- <th><?php _e( 'E-mail', 'new-user-approve' ); ?></th>
111
- <?php if ( 'pending' == $status ) { ?>
112
- <th colspan="2" style="text-align: center"><?php _e( 'Actions', 'new-user-approve' ); ?></th>
113
- <?php } else { ?>
114
- <th style="text-align: center"><?php _e( 'Actions', 'new-user-approve' ); ?></th>
115
- <?php } ?>
116
- </tr>
117
- </thead>
118
- <tbody>
119
- <?php
120
- // show each of the users
121
- $row = 1;
122
- foreach ( $users as $user ) {
123
- $class = ( $row % 2 ) ? '' : ' class="alternate"';
124
- $avatar = get_avatar( $user->user_email, 32 );
125
-
126
- if ( $approve ) {
127
- $approve_link = get_option( 'siteurl' ) . '/wp-admin/users.php?page=' . $this->_admin_page . '&user=' . $user->ID . '&status=approve';
128
- if ( isset( $_REQUEST['tab'] ) )
129
- $approve_link = add_query_arg( array( 'tab' => esc_attr( $_REQUEST['tab'] ) ), $approve_link );
130
- $approve_link = wp_nonce_url( $approve_link, 'pw_new_user_approve_action_' . get_class( $this ) );
131
- }
132
- if ( $deny ) {
133
- $deny_link = get_option( 'siteurl' ) . '/wp-admin/users.php?page=' . $this->_admin_page . '&user=' . $user->ID . '&status=deny';
134
- if ( isset( $_REQUEST['tab'] ) )
135
- $deny_link = add_query_arg( 'tab', esc_attr( $_REQUEST['tab'] ), $deny_link );
136
- $deny_link = wp_nonce_url( $deny_link, 'pw_new_user_approve_action_' . get_class( $this ) );
137
- }
138
-
139
- if ( current_user_can( 'edit_user', $user->ID ) ) {
140
- if ($current_user->ID == $user->ID) {
141
- $edit_link = 'profile.php';
142
- } else {
143
- $edit_link = add_query_arg( 'wp_http_referer', urlencode( esc_url( stripslashes( $_SERVER['REQUEST_URI'] ) ) ), "user-edit.php?user_id=$user->ID" );
144
- }
145
- $edit = '<strong><a href="' . esc_url( $edit_link ) . '">' . esc_html( $user->user_login ) . '</a></strong>';
146
- } else {
147
- $edit = '<strong>' . esc_html( $user->user_login ) . '</strong>';
148
- }
149
-
150
- ?><tr <?php echo $class; ?>>
151
- <td><?php echo $avatar . ' ' . $edit; ?></td>
152
- <td><?php echo get_user_meta( $user->ID, 'first_name', true ) . ' ' . get_user_meta( $user->ID, 'last_name', true ); ?></td>
153
- <td><a href="mailto:<?php echo $user->user_email; ?>" title="<?php _e('email:', 'new-user-approve' ) ?> <?php echo $user->user_email; ?>"><?php echo $user->user_email; ?></a></td>
154
- <?php if ( $approve && $user->ID != get_current_user_id() ) { ?>
155
- <td align="center"><a href="<?php echo esc_url( $approve_link ); ?>" title="<?php _e( 'Approve', 'new-user-approve' ); ?> <?php echo $user->user_login; ?>"><?php _e( 'Approve', 'new-user-approve' ); ?></a></td>
156
- <?php } ?>
157
- <?php if ( $deny && $user->ID != get_current_user_id() ) { ?>
158
- <td align="center"><a href="<?php echo esc_url( $deny_link ); ?>" title="<?php _e( 'Deny', 'new-user-approve' ); ?> <?php echo $user->user_login; ?>"><?php _e( 'Deny', 'new-user-approve' ); ?></a></td>
159
- <?php } ?>
160
- <?php if ( $user->ID == get_current_user_id() ) : ?>
161
- <td colspan="2">&nbsp;</td>
162
- <?php endif; ?>
163
- </tr><?php
164
- $row++;
165
- }
166
- ?>
167
- </tbody>
168
- </table>
169
- <?php
170
- } else {
171
- $status_i18n = $status;
172
- if ( $status == 'approved' ) {
173
- $status_i18n = __( 'approved', 'new-user-approve' );
174
- } else if ( $status == 'denied' ) {
175
- $status_i18n = __( 'denied', 'new-user-approve' );
176
- } else if ( $status == 'pending' ) {
177
- $status_i18n = __( 'pending', 'new-user-approve' );
178
- }
179
-
180
- echo '<p>'.sprintf( __( 'There are no users with a status of %s', 'new-user-approve' ), $status_i18n ) . '</p>';
181
- }
182
- }
183
-
184
- /**
185
- * Accept input from admin to modify a user
186
- *
187
- * @uses init
188
- */
189
- public function process_input() {
190
- if ( ( isset( $_GET['page'] ) && $_GET['page'] == $this->_admin_page ) && isset( $_GET['status'] ) ) {
191
- $valid_request = check_admin_referer( 'pw_new_user_approve_action_' . get_class( $this ) );
192
-
193
- if ( $valid_request ) {
194
- $status = sanitize_key( $_GET['status'] );
195
- $user_id = absint( $_GET['user'] );
196
-
197
- pw_new_user_approve()->update_user_status( $user_id, $status );
198
- }
199
- }
200
- }
201
-
202
- /**
203
- * Display a notice on the legacy page that notifies the user of the new interface.
204
- *
205
- * @uses admin_notices
206
- */
207
- public function admin_notice() {
208
- $screen = get_current_screen();
209
-
210
- if ( $screen->id == 'users_page_new-user-approve-admin' ) {
211
- $user_id = get_current_user_id();
212
-
213
- // Check that the user hasn't already clicked to ignore the message
214
- if ( ! get_user_meta( $user_id, 'pw_new_user_approve_ignore_notice' ) ) {
215
- echo '<div class="updated"><p>';
216
- printf( __( 'You can now update user status on the <a href="%1$s">users admin page</a>. | <a href="%2$s">Hide Notice</a>', 'new-user-approve' ), admin_url( 'users.php' ), add_query_arg( array( 'new-user-approve-ignore-notice' => 1 ) ) );
217
- echo "</p></div>";
218
- }
219
- }
220
- }
221
-
222
- /**
223
- * If user clicks to ignore the notice, add that to their user meta
224
- *
225
- * @uses admin_init
226
- */
227
- public function notice_ignore() {
228
- if ( isset( $_GET['new-user-approve-ignore-notice'] ) && '1' == $_GET['new-user-approve-ignore-notice '] ) {
229
- $user_id = get_current_user_id();
230
- add_user_meta( $user_id, 'pw_new_user_approve_ignore_notice', '1', true );
231
- }
232
- }
233
-
 
 
 
 
 
 
 
 
 
 
234
  }
235
 
236
  function pw_new_user_approve_admin_approve() {
237
- return pw_new_user_approve_admin_approve::instance();
238
  }
239
 
240
  pw_new_user_approve_admin_approve();
7
 
8
  class pw_new_user_approve_admin_approve {
9
 
10
+ var $_admin_page = 'new-user-approve-admin';
11
+
12
+ /**
13
+ * The only instance of pw_new_user_approve_admin_approve.
14
+ *
15
+ * @var pw_new_user_approve_admin_approve
16
+ */
17
+ private static $instance;
18
+
19
+ /**
20
+ * Returns the main instance.
21
+ *
22
+ * @return pw_new_user_approve_admin_approve
23
+ */
24
+ public static function instance() {
25
+ if ( !isset( self::$instance ) ) {
26
+ self::$instance = new pw_new_user_approve_admin_approve();
27
+ }
28
+ return self::$instance;
29
+ }
30
+
31
+ private function __construct() {
32
+ // Actions
33
+ add_action( 'admin_menu', array( $this, 'admin_menu_link' ) );
34
+ add_action( 'admin_init', array( $this, 'process_input' ) );
35
+ add_action( 'admin_notices', array( $this, 'admin_notice' ) );
36
+ add_action( 'admin_init', array( $this, 'notice_ignore' ) );
37
+ }
38
+
39
+ /**
40
+ * Add the new menu item to the users portion of the admin menu
41
+ *
42
+ * @uses admin_menu
43
+ */
44
+ function admin_menu_link() {
45
+ $show_admin_page = apply_filters( 'new_user_approve_show_admin_page', true );
46
+
47
+ if ( $show_admin_page ) {
48
+ $cap = apply_filters( 'new_user_approve_minimum_cap', 'edit_users' );
49
+ add_users_page( __( 'Approve New Users', 'new-user-approve' ), __( 'Approve New Users', 'new-user-approve' ), $cap, $this->_admin_page, array( $this, 'approve_admin' ) );
50
+ }
51
+ }
52
+
53
+ /**
54
+ * Create the view for the admin interface
55
+ */
56
+ public function approve_admin() {
57
+ if ( isset( $_GET['user'] ) && isset( $_GET['status'] ) ) {
58
+ echo '<div id="message" class="updated fade"><p>' . __( 'User successfully updated.', 'new-user-approve' ) . '</p></div>';
59
+ }
60
+
61
+ $active_tab = isset( $_GET['tab'] ) ? $_GET['tab'] : 'pending_users';
62
+ ?>
63
+ <div class="wrap">
64
+ <h2><?php _e( 'User Registration Approval', 'new-user-approve' ); ?></h2>
65
+
66
+ <h3 class="nav-tab-wrapper">
67
+ <a href="<?php echo esc_url( admin_url( 'users.php?page=new-user-approve-admin&tab=pending_users' ) ); ?>"
68
+ class="nav-tab<?php echo $active_tab == 'pending_users' ? ' nav-tab-active' : ''; ?>"><span><?php _e( 'Users Pending Approval', 'new-user-approve' ); ?></span></a>
69
+ <a href="<?php echo esc_url( admin_url( 'users.php?page=new-user-approve-admin&tab=approved_users' ) ); ?>"
70
+ class="nav-tab<?php echo $active_tab == 'approved_users' ? ' nav-tab-active' : ''; ?>"><span><?php _e( 'Approved Users', 'new-user-approve' ); ?></span></a>
71
+ <a href="<?php echo esc_url( admin_url( 'users.php?page=new-user-approve-admin&tab=denied_users' ) ); ?>"
72
+ class="nav-tab<?php echo $active_tab == 'denied_users' ? ' nav-tab-active' : ''; ?>"><span><?php _e( 'Denied Users', 'new-user-approve' ); ?></span></a>
73
+ </h3>
74
+
75
+ <?php if ( $active_tab == 'pending_users' ) : ?>
76
+ <div id="pw_pending_users">
77
+ <?php $this->user_table( 'pending' ); ?>
78
+ </div>
79
+ <?php elseif ( $active_tab == 'approved_users' ) : ?>
80
+ <div id="pw_approved_users">
81
+ <?php $this->user_table( 'approved' ); ?>
82
+ </div>
83
+ <?php
84
+ elseif ( $active_tab == 'denied_users' ) : ?>
85
+ <div id="pw_denied_users">
86
+ <?php $this->user_table( 'denied' ); ?>
87
+ </div>
88
+ <?php endif; ?>
89
+ </div>
90
+ <?php
91
+ }
92
+
93
+ /**
94
+ * Output the table that shows the registered users grouped by status
95
+ *
96
+ * @param string $status the filter to use for which the users will be queried. Possible values are pending, approved, or denied.
97
+ */
98
+ public function user_table( $status ) {
99
+ global $current_user;
100
+
101
+ $approve = ( 'denied' == $status || 'pending' == $status );
102
+ $deny = ( 'approved' == $status || 'pending' == $status );
103
+
104
+ $user_status = pw_new_user_approve()->get_user_statuses();
105
+ $users = $user_status[$status];
106
+
107
+ if ( count( $users ) > 0 ) {
108
+ ?>
109
+ <table class="widefat">
110
+ <thead>
111
+ <tr class="thead">
112
+ <th><?php _e( 'Username', 'new-user-approve' ); ?></th>
113
+ <th><?php _e( 'Name', 'new-user-approve' ); ?></th>
114
+ <th><?php _e( 'E-mail', 'new-user-approve' ); ?></th>
115
+ <?php if ( 'pending' == $status ) { ?>
116
+ <th colspan="2" style="text-align: center"><?php _e( 'Actions', 'new-user-approve' ); ?></th>
117
+ <?php } else { ?>
118
+ <th style="text-align: center"><?php _e( 'Actions', 'new-user-approve' ); ?></th>
119
+ <?php } ?>
120
+ </tr>
121
+ </thead>
122
+ <tbody>
123
+ <?php
124
+ // show each of the users
125
+ $row = 1;
126
+ foreach ( $users as $user ) {
127
+ $class = ( $row % 2 ) ? '' : ' class="alternate"';
128
+ $avatar = get_avatar( $user->user_email, 32 );
129
+
130
+ if ( $approve ) {
131
+ $approve_link = get_option( 'siteurl' ) . '/wp-admin/users.php?page=' . $this->_admin_page . '&user=' . $user->ID . '&status=approve';
132
+ if ( isset( $_REQUEST['tab'] ) )
133
+ $approve_link = add_query_arg( array( 'tab' => esc_attr( $_REQUEST['tab'] ) ), $approve_link );
134
+ $approve_link = wp_nonce_url( $approve_link, 'pw_new_user_approve_action_' . get_class( $this ) );
135
+ }
136
+ if ( $deny ) {
137
+ $deny_link = get_option( 'siteurl' ) . '/wp-admin/users.php?page=' . $this->_admin_page . '&user=' . $user->ID . '&status=deny';
138
+ if ( isset( $_REQUEST['tab'] ) )
139
+ $deny_link = add_query_arg( 'tab', esc_attr( $_REQUEST['tab'] ), $deny_link );
140
+ $deny_link = wp_nonce_url( $deny_link, 'pw_new_user_approve_action_' . get_class( $this ) );
141
+ }
142
+
143
+ if ( current_user_can( 'edit_user', $user->ID ) ) {
144
+ if ( $current_user->ID == $user->ID ) {
145
+ $edit_link = 'profile.php';
146
+ } else {
147
+ $edit_link = add_query_arg( 'wp_http_referer', urlencode( esc_url( stripslashes( $_SERVER['REQUEST_URI'] ) ) ), "user-edit.php?user_id=$user->ID" );
148
+ }
149
+ $edit = '<strong><a href="' . esc_url( $edit_link ) . '">' . esc_html( $user->user_login ) . '</a></strong>';
150
+ } else {
151
+ $edit = '<strong>' . esc_html( $user->user_login ) . '</strong>';
152
+ }
153
+
154
+ ?>
155
+ <tr <?php echo $class; ?>>
156
+ <td><?php echo $avatar . ' ' . $edit; ?></td>
157
+ <td><?php echo get_user_meta( $user->ID, 'first_name', true ) . ' ' . get_user_meta( $user->ID, 'last_name', true ); ?></td>
158
+ <td><a href="mailto:<?php echo $user->user_email; ?>"
159
+ title="<?php _e( 'email:', 'new-user-approve' ) ?> <?php echo $user->user_email; ?>"><?php echo $user->user_email; ?></a>
160
+ </td>
161
+ <?php if ( $approve && $user->ID != get_current_user_id() ) { ?>
162
+ <td align="center"><a href="<?php echo esc_url( $approve_link ); ?>"
163
+ title="<?php _e( 'Approve', 'new-user-approve' ); ?> <?php echo $user->user_login; ?>"><?php _e( 'Approve', 'new-user-approve' ); ?></a>
164
+ </td>
165
+ <?php } ?>
166
+ <?php if ( $deny && $user->ID != get_current_user_id() ) { ?>
167
+ <td align="center"><a href="<?php echo esc_url( $deny_link ); ?>"
168
+ title="<?php _e( 'Deny', 'new-user-approve' ); ?> <?php echo $user->user_login; ?>"><?php _e( 'Deny', 'new-user-approve' ); ?></a>
169
+ </td>
170
+ <?php } ?>
171
+ <?php if ( $user->ID == get_current_user_id() ) : ?>
172
+ <td colspan="2">&nbsp;</td>
173
+ <?php endif; ?>
174
+ </tr><?php
175
+ $row++;
176
+ }
177
+ ?>
178
+ </tbody>
179
+ </table>
180
+ <?php
181
+ } else {
182
+ $status_i18n = $status;
183
+ if ( $status == 'approved' ) {
184
+ $status_i18n = __( 'approved', 'new-user-approve' );
185
+ } else if ( $status == 'denied' ) {
186
+ $status_i18n = __( 'denied', 'new-user-approve' );
187
+ } else if ( $status == 'pending' ) {
188
+ $status_i18n = __( 'pending', 'new-user-approve' );
189
+ }
190
+
191
+ echo '<p>' . sprintf( __( 'There are no users with a status of %s', 'new-user-approve' ), $status_i18n ) . '</p>';
192
+ }
193
+ }
194
+
195
+ /**
196
+ * Accept input from admin to modify a user
197
+ *
198
+ * @uses init
199
+ */
200
+ public function process_input() {
201
+ if ( ( isset( $_GET['page'] ) && $_GET['page'] == $this->_admin_page ) && isset( $_GET['status'] ) ) {
202
+ $valid_request = check_admin_referer( 'pw_new_user_approve_action_' . get_class( $this ) );
203
+
204
+ if ( $valid_request ) {
205
+ $status = sanitize_key( $_GET['status'] );
206
+ $user_id = absint( $_GET['user'] );
207
+
208
+ pw_new_user_approve()->update_user_status( $user_id, $status );
209
+ }
210
+ }
211
+ }
212
+
213
+ /**
214
+ * Display a notice on the legacy page that notifies the user of the new interface.
215
+ *
216
+ * @uses admin_notices
217
+ */
218
+ public function admin_notice() {
219
+ $screen = get_current_screen();
220
+
221
+ if ( $screen->id == 'users_page_new-user-approve-admin' ) {
222
+ $user_id = get_current_user_id();
223
+
224
+ // Check that the user hasn't already clicked to ignore the message
225
+ if ( !get_user_meta( $user_id, 'pw_new_user_approve_ignore_notice' ) ) {
226
+ echo '<div class="updated"><p>';
227
+ printf( __( 'You can now update user status on the <a href="%1$s">users admin page</a>. | <a href="%2$s">Hide Notice</a>', 'new-user-approve' ), admin_url( 'users.php' ), add_query_arg( array( 'new-user-approve-ignore-notice' => 1 ) ) );
228
+ echo "</p></div>";
229
+ }
230
+ }
231
+ }
232
+
233
+ /**
234
+ * If user clicks to ignore the notice, add that to their user meta
235
+ *
236
+ * @uses admin_init
237
+ */
238
+ public function notice_ignore() {
239
+ if ( isset( $_GET['new-user-approve-ignore-notice'] ) && '1' == $_GET['new-user-approve-ignore-notice '] ) {
240
+ $user_id = get_current_user_id();
241
+ add_user_meta( $user_id, 'pw_new_user_approve_ignore_notice', '1', true );
242
+ }
243
+ }
244
  }
245
 
246
  function pw_new_user_approve_admin_approve() {
247
+ return pw_new_user_approve_admin_approve::instance();
248
  }
249
 
250
  pw_new_user_approve_admin_approve();
includes/user-list.php CHANGED
@@ -2,379 +2,395 @@
2
 
3
  class pw_new_user_approve_user_list {
4
 
5
- /**
6
- * The only instance of pw_new_user_approve_user_list.
7
- *
8
- * @var pw_new_user_approve_user_list
9
- */
10
- private static $instance;
11
-
12
- /**
13
- * Returns the main instance.
14
- *
15
- * @return pw_new_user_approve_user_list
16
- */
17
- public static function instance() {
18
- if ( ! isset( self::$instance ) ) {
19
- self::$instance = new pw_new_user_approve_user_list();
20
- }
21
- return self::$instance;
22
- }
23
-
24
- private function __construct() {
25
- // Actions
26
- add_action( 'load-users.php', array( $this, 'update_action' ) );
27
- add_action( 'restrict_manage_users', array( $this, 'status_filter' ) );
28
- add_action( 'pre_user_query', array( $this, 'filter_by_status' ) );
29
- add_action( 'admin_footer-users.php', array( $this, 'admin_footer' ) );
30
- add_action( 'load-users.php', array( $this, 'bulk_action' ) );
31
- add_action( 'admin_notices', array( $this, 'admin_notices' ) );
32
- add_action( 'show_user_profile', array( $this, 'profile_status_field' ) );
33
- add_action( 'edit_user_profile', array( $this, 'profile_status_field' ) );
34
- add_action( 'edit_user_profile_update', array( $this, 'save_profile_status_field' ) );
35
-
36
- // Filters
37
- add_filter( 'user_row_actions', array( $this, 'user_table_actions' ), 10, 2 );
38
- add_filter( 'manage_users_columns', array( $this, 'add_column' ) );
39
- add_filter( 'manage_users_custom_column', array( $this, 'status_column' ), 10, 3 );
40
- }
41
-
42
- /**
43
- * Update the user status if the approve or deny link was clicked.
44
- *
45
- * @uses load-users.php
46
- */
47
- public function update_action() {
48
- if ( isset( $_GET['action'] ) && ( in_array( $_GET['action'], array( 'approve', 'deny' ) ) ) ) {
49
- check_admin_referer( 'new-user-approve' );
50
-
51
- $sendback = remove_query_arg( array( 'approved', 'denied', 'deleted', 'ids', 'new_user_approve_filter', 'pw-status-query-submit', 'new_role' ), wp_get_referer() );
52
- if ( ! $sendback )
53
- $sendback = admin_url( 'users.php' );
54
-
55
- $wp_list_table = _get_list_table( 'WP_Users_List_Table' );
56
- $pagenum = $wp_list_table->get_pagenum();
57
- $sendback = add_query_arg( 'paged', $pagenum, $sendback );
58
-
59
- $status = sanitize_key( $_GET['action'] );
60
- $user = absint( $_GET['user'] );
61
-
62
- pw_new_user_approve()->update_user_status( $user, $status );
63
-
64
- if ( $_GET['action'] == 'approve' ) {
65
- $sendback = add_query_arg( array( 'approved' => 1, 'ids' => $user ), $sendback );
66
- } else {
67
- $sendback = add_query_arg( array( 'denied' => 1, 'ids' => $user ), $sendback );
68
- }
69
-
70
- wp_redirect( $sendback );
71
- exit;
72
- }
73
- }
74
-
75
- /**
76
- * Add the approve or deny link where appropriate.
77
- *
78
- * @uses user_row_actions
79
- * @param array $actions
80
- * @param object $user
81
- * @return array
82
- */
83
- public function user_table_actions( $actions, $user ) {
84
- if ( $user->ID == get_current_user_id() )
85
- return $actions;
86
-
87
- $user_status = pw_new_user_approve()->get_user_status( $user->ID );
88
-
89
- $approve_link = wp_nonce_url( add_query_arg( array( 'action' => 'approve', 'user' => $user->ID ) ), 'new-user-approve' );
90
- $deny_link = wp_nonce_url( add_query_arg( array( 'action' => 'deny', 'user' => $user->ID ) ), 'new-user-approve' );
91
-
92
- $approve_action = '<a href="' . esc_url( $approve_link ) . '">' . __( 'Approve', 'new-user-approve' ) . '</a>';
93
- $deny_action = '<a href="' . esc_url( $deny_link ) . '">' . __( 'Deny', 'new-user-approve' ) . '</a>';
94
-
95
- if ( $user_status == 'pending' ) {
96
- $actions[] = $approve_action;
97
- $actions[] = $deny_action;
98
- } else if ( $user_status == 'approved' ) {
99
- $actions[] = $deny_action;
100
- } else if ( $user_status == 'denied' ) {
101
- $actions[] = $approve_action;
102
- }
103
-
104
- return $actions;
105
- }
106
-
107
- /**
108
- * Add the status column to the user table
109
- *
110
- * @uses manage_users_columns
111
- * @param array $columns
112
- * @return array
113
- */
114
- public function add_column( $columns ) {
115
- $the_columns['pw_user_status'] = __( 'Status', 'new-user-approve' );
116
-
117
- $newcol = array_slice( $columns, 0, -1 );
118
- $newcol = array_merge( $newcol, $the_columns );
119
- $columns = array_merge( $newcol, array_slice( $columns, 1 ) );
120
-
121
- return $columns;
122
- }
123
-
124
- /**
125
- * Show the status of the user in the status column
126
- *
127
- * @uses manage_users_custom_column
128
- * @param string $val
129
- * @param string $column_name
130
- * @param int $user_id
131
- * @return string
132
- */
133
- public function status_column( $val, $column_name, $user_id ) {
134
- switch ( $column_name ) {
135
- case 'pw_user_status' :
136
- return pw_new_user_approve()->get_user_status( $user_id );
137
- break;
138
-
139
- default:
140
- }
141
-
142
- return $val;
143
- }
144
-
145
- /**
146
- * Add a filter to the user table to filter by user status
147
- *
148
- * @uses restrict_manage_users
149
- */
150
- public function status_filter() {
151
- $filter_button = submit_button( __( 'Filter', 'new-user-approve' ), 'button', 'pw-status-query-submit', false, array( 'id' => 'pw-status-query-submit' ) );
152
- $filtered_status = (isset( $_GET['new_user_approve_filter'] ) ) ? esc_attr( $_GET['new_user_approve_filter'] ) : '';
153
-
154
- ?>
155
- <label class="screen-reader-text" for="new_user_approve_filter"><?php _e( 'View all users', 'new-user-approve' ); ?></label>
156
- <select id="new_user_approve_filter" name="new_user_approve_filter" style="float: none; margin: 0 0 0 15px;">
157
- <option value=""><?php _e( 'View all users', 'new-user-approve' ); ?></option>
158
- <?php foreach ( pw_new_user_approve()->get_valid_statuses() as $status ) : ?>
159
- <option value="<?php echo esc_attr( $status ); ?>"<?php selected( $status, $filtered_status ); ?>><?php echo esc_html( $status ); ?></option>
160
- <?php endforeach; ?>
161
- </select>
162
- <?php echo apply_filters( 'new_user_approve_filter_button', $filter_button ); ?>
163
- <style>
164
- #pw-status-query-submit {
165
- float: right;
166
- margin: 2px 0 0 5px;
167
- }
168
- </style>
169
- <?php
170
- }
171
-
172
- /**
173
- * Modify the user query if the status filter is being used.
174
- *
175
- * @uses pre_user_query
176
- * @param $query
177
- */
178
- public function filter_by_status( $query ) {
179
- global $wpdb;
180
-
181
- if ( !is_admin() )
182
- return;
183
-
184
- $screen = get_current_screen();
185
- if ( 'users' != $screen->id )
186
- return;
187
-
188
- if ( isset( $_GET['new_user_approve_filter'] ) && $_GET['new_user_approve_filter'] != '' ) {
189
- $filter = esc_attr( $_GET['new_user_approve_filter'] );
190
-
191
- $query->query_from .= " INNER JOIN {$wpdb->usermeta} wp_usermeta ON ( {$wpdb->users}.ID = wp_usermeta.user_id )";
192
-
193
- if ( 'approved' == $filter ) {
194
- $query->query_fields = "DISTINCT SQL_CALC_FOUND_ROWS {$wpdb->users}.ID";
195
- $query->query_from .= " LEFT JOIN {$wpdb->usermeta} AS mt1 ON ({$wpdb->users}.ID = mt1.user_id AND mt1.meta_key = 'pw_user_status')";
196
- $query->query_where .= " AND ( ( wp_usermeta.meta_key = 'pw_user_status' AND CAST(wp_usermeta.meta_value AS CHAR) = 'approved' ) OR mt1.user_id IS NULL )";
197
- } else {
198
- $query->query_where .= " AND ( (wp_usermeta.meta_key = 'pw_user_status' AND CAST(wp_usermeta.meta_value AS CHAR) = '{$filter}') )";
199
- }
200
- }
201
- }
202
-
203
- /**
204
- * Use javascript to add the ability to bulk modify the status of users.
205
- *
206
- * @uses admin_footer-users.php
207
- */
208
- public function admin_footer() {
209
- $screen = get_current_screen();
210
-
211
- if ( $screen->id == 'users' ) : ?>
212
- <script type="text/javascript">
213
- jQuery(document).ready(function($) {
214
- $('<option>').val('approve').text('<?php _e( 'Approve', 'new-user-approve' )?>').appendTo("select[name='action']")
215
- $('<option>').val('approve').text('<?php _e( 'Approve', 'new-user-approve' )?>').appendTo("select[name='action2']");
216
-
217
- $('<option>').val('deny').text('<?php _e( 'Deny', 'new-user-approve' )?>').appendTo("select[name='action']")
218
- $('<option>').val('deny').text('<?php _e( 'Deny', 'new-user-approve' )?>').appendTo("select[name='action2']");
219
- });
220
- </script>
221
- <?php endif;
222
- }
223
-
224
- /**
225
- * Process the bulk status updates
226
- *
227
- * @uses load-users.php
228
- */
229
- public function bulk_action() {
230
- $screen = get_current_screen();
231
-
232
- if ( $screen->id == 'users' ) {
233
-
234
- // get the action
235
- $wp_list_table = _get_list_table( 'WP_Users_List_Table' );
236
- $action = $wp_list_table->current_action();
237
-
238
- $allowed_actions = array( 'approve', 'deny' );
239
- if ( !in_array( $action, $allowed_actions ) )
240
- return;
241
-
242
- // security check
243
- check_admin_referer( 'bulk-users' );
244
-
245
- // make sure ids are submitted
246
- if ( isset( $_REQUEST['users'] ) ) {
247
- $user_ids = array_map( 'intval', $_REQUEST['users'] );
248
- }
249
-
250
- if ( empty( $user_ids ) )
251
- return;
252
-
253
- $sendback = remove_query_arg( array( 'approved', 'denied', 'deleted', 'ids', 'new_user_approve_filter', 'pw-status-query-submit', 'new_role' ), wp_get_referer() );
254
- if ( ! $sendback )
255
- $sendback = admin_url( "users.php" );
256
-
257
- $pagenum = $wp_list_table->get_pagenum();
258
- $sendback = add_query_arg( 'paged', $pagenum, $sendback );
259
-
260
- switch($action) {
261
- case 'approve':
262
- $approved = 0;
263
- foreach( $user_ids as $user_id ) {
264
- pw_new_user_approve()->update_user_status( $user_id, 'approve' );
265
- $approved++;
266
- }
267
-
268
- $sendback = add_query_arg( array( 'approved' => $approved, 'ids' => join(',', $user_ids ) ), $sendback );
269
- break;
270
-
271
- case 'deny':
272
- $denied = 0;
273
- foreach( $user_ids as $user_id ) {
274
- pw_new_user_approve()->update_user_status( $user_id, 'deny' );
275
- $denied++;
276
- }
277
-
278
- $sendback = add_query_arg( array( 'denied' => $denied, 'ids' => join(',', $user_ids ) ), $sendback );
279
- break;
280
-
281
- default: return;
282
- }
283
-
284
- $sendback = remove_query_arg( array('action', 'action2', 'tags_input', 'post_author', 'comment_status', 'ping_status', '_status', 'post', 'bulk_edit', 'post_view'), $sendback );
285
-
286
- wp_redirect( $sendback );
287
- exit();
288
- }
289
- }
290
-
291
- /**
292
- * Show a message on the users page if a status has been updated.
293
- *
294
- * @uses admin_notices
295
- */
296
- public function admin_notices() {
297
- $screen = get_current_screen();
298
-
299
- if ( $screen->id != 'users' )
300
- return;
301
-
302
- $message = null;
303
-
304
- if ( isset( $_REQUEST['denied'] ) && (int) $_REQUEST['denied']) {
305
- $message = sprintf( _n( 'User denied.', '%s users denied.', $_REQUEST['denied'], 'new-user-approve' ), number_format_i18n( $_REQUEST['denied'] ) );
306
- }
307
-
308
- if ( isset( $_REQUEST['approved'] ) && (int) $_REQUEST['approved']) {
309
- $message = sprintf( _n( 'User approved.', '%s users approved.', $_REQUEST['approved'], 'new-user-approve' ), number_format_i18n( $_REQUEST['approved'] ) );
310
- }
311
-
312
- if ( !empty( $message ) ) {
313
- echo '<div class="updated"><p>' . $message . '</p></div>';
314
- }
315
- }
316
-
317
- /**
318
- * Display the dropdown on the user profile page to allow an admin to update the user status.
319
- *
320
- * @uses show_user_profile
321
- * @uses edit_user_profile
322
- * @param object $user
323
- */
324
- public function profile_status_field( $user ) {
325
- if ( $user->ID == get_current_user_id() )
326
- return;
327
-
328
- $user_status = pw_new_user_approve()->get_user_status( $user->ID );
329
- ?>
330
- <table class="form-table">
331
- <tr>
332
- <th><label for="new_user_approve_status"><?php _e( 'Access Status', 'new-user-approve' ); ?></label></th>
333
- <td>
334
- <select id="new_user_approve_status" name="new_user_approve_status">
335
- <?php if ( $user_status == 'pending' ) : ?>
336
- <option value=""><?php _e( '-- Status --', 'new-user-approve' ); ?></option>
337
- <?php endif; ?>
338
- <?php foreach ( array( 'approved', 'denied' ) as $status ) : ?>
339
- <option value="<?php echo esc_attr( $status ); ?>"<?php selected( $status, $user_status ); ?>><?php echo esc_html( $status ); ?></option>
340
- <?php endforeach; ?>
341
- </select>
342
- <span class="description"><?php _e( 'If user has access to sign in or not.', 'new-user-approve' ); ?></span>
343
- <?php if ( $user_status == 'pending' ) : ?>
344
- <br /><span class="description"><?php _e( 'Current user status is <strong>pending</strong>.', 'new-user-approve' ); ?></span>
345
- <?php endif; ?>
346
- </td>
347
- </tr>
348
- </table>
349
- <?php
350
- }
351
-
352
- /**
353
- * Save the user status when updating from the user profile.
354
- *
355
- * @uses edit_user_profile_update
356
- * @param int $user_id
357
- * @return bool
358
- */
359
- public function save_profile_status_field( $user_id ) {
360
- if ( !current_user_can( 'edit_user', $user_id ) )
361
- return false;
362
-
363
- if ( ! empty( $_POST['new_user_approve_status'] ) ) {
364
- $new_status = esc_attr( $_POST['new_user_approve_status'] );
365
-
366
- if ( $new_status == 'approved' )
367
- $new_status = 'approve';
368
- else if ( $new_status == 'denied' )
369
- $new_status = 'deny';
370
-
371
- pw_new_user_approve()->update_user_status( $user_id, $new_status );
372
- }
373
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
374
  }
375
 
376
  function pw_new_user_approve_user_list() {
377
- return pw_new_user_approve_user_list::instance();
378
  }
379
 
380
  pw_new_user_approve_user_list();
2
 
3
  class pw_new_user_approve_user_list {
4
 
5
+ /**
6
+ * The only instance of pw_new_user_approve_user_list.
7
+ *
8
+ * @var pw_new_user_approve_user_list
9
+ */
10
+ private static $instance;
11
+
12
+ /**
13
+ * Returns the main instance.
14
+ *
15
+ * @return pw_new_user_approve_user_list
16
+ */
17
+ public static function instance() {
18
+ if ( !isset( self::$instance ) ) {
19
+ self::$instance = new pw_new_user_approve_user_list();
20
+ }
21
+ return self::$instance;
22
+ }
23
+
24
+ private function __construct() {
25
+ // Actions
26
+ add_action( 'load-users.php', array( $this, 'update_action' ) );
27
+ add_action( 'restrict_manage_users', array( $this, 'status_filter' ) );
28
+ add_action( 'pre_user_query', array( $this, 'filter_by_status' ) );
29
+ add_action( 'admin_footer-users.php', array( $this, 'admin_footer' ) );
30
+ add_action( 'load-users.php', array( $this, 'bulk_action' ) );
31
+ add_action( 'admin_notices', array( $this, 'admin_notices' ) );
32
+ add_action( 'show_user_profile', array( $this, 'profile_status_field' ) );
33
+ add_action( 'edit_user_profile', array( $this, 'profile_status_field' ) );
34
+ add_action( 'edit_user_profile_update', array( $this, 'save_profile_status_field' ) );
35
+
36
+ // Filters
37
+ add_filter( 'user_row_actions', array( $this, 'user_table_actions' ), 10, 2 );
38
+ add_filter( 'manage_users_columns', array( $this, 'add_column' ) );
39
+ add_filter( 'manage_users_custom_column', array( $this, 'status_column' ), 10, 3 );
40
+ }
41
+
42
+ /**
43
+ * Update the user status if the approve or deny link was clicked.
44
+ *
45
+ * @uses load-users.php
46
+ */
47
+ public function update_action() {
48
+ if ( isset( $_GET['action'] ) && in_array( $_GET['action'], array( 'approve', 'deny' ) ) && !isset( $_GET['new_role'] ) ) {
49
+ check_admin_referer( 'new-user-approve' );
50
+
51
+ $sendback = remove_query_arg( array( 'approved', 'denied', 'deleted', 'ids', 'new_user_approve_filter', 'pw-status-query-submit', 'new_role' ), wp_get_referer() );
52
+ if ( !$sendback )
53
+ $sendback = admin_url( 'users.php' );
54
+
55
+ $wp_list_table = _get_list_table( 'WP_Users_List_Table' );
56
+ $pagenum = $wp_list_table->get_pagenum();
57
+ $sendback = add_query_arg( 'paged', $pagenum, $sendback );
58
+
59
+ $status = sanitize_key( $_GET['action'] );
60
+ $user = absint( $_GET['user'] );
61
+
62
+ pw_new_user_approve()->update_user_status( $user, $status );
63
+
64
+ if ( $_GET['action'] == 'approve' ) {
65
+ $sendback = add_query_arg( array( 'approved' => 1, 'ids' => $user ), $sendback );
66
+ } else {
67
+ $sendback = add_query_arg( array( 'denied' => 1, 'ids' => $user ), $sendback );
68
+ }
69
+
70
+ wp_redirect( $sendback );
71
+ exit;
72
+ }
73
+ }
74
+
75
+ /**
76
+ * Add the approve or deny link where appropriate.
77
+ *
78
+ * @uses user_row_actions
79
+ * @param array $actions
80
+ * @param object $user
81
+ * @return array
82
+ */
83
+ public function user_table_actions( $actions, $user ) {
84
+ if ( $user->ID == get_current_user_id() )
85
+ return $actions;
86
+
87
+ $user_status = pw_new_user_approve()->get_user_status( $user->ID );
88
+
89
+ $approve_link = wp_nonce_url( add_query_arg( array( 'action' => 'approve', 'user' => $user->ID ) ), 'new-user-approve' );
90
+ $deny_link = wp_nonce_url( add_query_arg( array( 'action' => 'deny', 'user' => $user->ID ) ), 'new-user-approve' );
91
+
92
+ $approve_action = '<a href="' . esc_url( $approve_link ) . '">' . __( 'Approve', 'new-user-approve' ) . '</a>';
93
+ $deny_action = '<a href="' . esc_url( $deny_link ) . '">' . __( 'Deny', 'new-user-approve' ) . '</a>';
94
+
95
+ if ( $user_status == 'pending' ) {
96
+ $actions[] = $approve_action;
97
+ $actions[] = $deny_action;
98
+ } else if ( $user_status == 'approved' ) {
99
+ $actions[] = $deny_action;
100
+ } else if ( $user_status == 'denied' ) {
101
+ $actions[] = $approve_action;
102
+ }
103
+
104
+ return $actions;
105
+ }
106
+
107
+ /**
108
+ * Add the status column to the user table
109
+ *
110
+ * @uses manage_users_columns
111
+ * @param array $columns
112
+ * @return array
113
+ */
114
+ public function add_column( $columns ) {
115
+ $the_columns['pw_user_status'] = __( 'Status', 'new-user-approve' );
116
+
117
+ $newcol = array_slice( $columns, 0, -1 );
118
+ $newcol = array_merge( $newcol, $the_columns );
119
+ $columns = array_merge( $newcol, array_slice( $columns, 1 ) );
120
+
121
+ return $columns;
122
+ }
123
+
124
+ /**
125
+ * Show the status of the user in the status column
126
+ *
127
+ * @uses manage_users_custom_column
128
+ * @param string $val
129
+ * @param string $column_name
130
+ * @param int $user_id
131
+ * @return string
132
+ */
133
+ public function status_column( $val, $column_name, $user_id ) {
134
+ switch ( $column_name ) {
135
+ case 'pw_user_status' :
136
+ return pw_new_user_approve()->get_user_status( $user_id );
137
+ break;
138
+
139
+ default:
140
+ }
141
+
142
+ return $val;
143
+ }
144
+
145
+ /**
146
+ * Add a filter to the user table to filter by user status
147
+ *
148
+ * @uses restrict_manage_users
149
+ */
150
+ public function status_filter() {
151
+ $filter_button = submit_button( __( 'Filter', 'new-user-approve' ), 'button', 'pw-status-query-submit', false, array( 'id' => 'pw-status-query-submit' ) );
152
+ $filtered_status = ( isset( $_GET['new_user_approve_filter'] ) ) ? esc_attr( $_GET['new_user_approve_filter'] ) : '';
153
+
154
+ ?>
155
+ <label class="screen-reader-text"
156
+ for="new_user_approve_filter"><?php _e( 'View all users', 'new-user-approve' ); ?></label>
157
+ <select id="new_user_approve_filter" name="new_user_approve_filter" style="float: none; margin: 0 0 0 15px;">
158
+ <option value=""><?php _e( 'View all users', 'new-user-approve' ); ?></option>
159
+ <?php foreach ( pw_new_user_approve()->get_valid_statuses() as $status ) : ?>
160
+ <option
161
+ value="<?php echo esc_attr( $status ); ?>"<?php selected( $status, $filtered_status ); ?>><?php echo esc_html( $status ); ?></option>
162
+ <?php endforeach; ?>
163
+ </select>
164
+ <?php echo apply_filters( 'new_user_approve_filter_button', $filter_button ); ?>
165
+ <style>
166
+ #pw-status-query-submit {
167
+ float: right;
168
+ margin: 2px 0 0 5px;
169
+ }
170
+ </style>
171
+ <?php
172
+ }
173
+
174
+ /**
175
+ * Modify the user query if the status filter is being used.
176
+ *
177
+ * @uses pre_user_query
178
+ * @param $query
179
+ */
180
+ public function filter_by_status( $query ) {
181
+ global $wpdb;
182
+
183
+ if ( !is_admin() ) {
184
+ return;
185
+ }
186
+
187
+ $screen = get_current_screen();
188
+ if ( 'users' != $screen->id ) {
189
+ return;
190
+ }
191
+
192
+ if ( isset( $_GET['new_user_approve_filter'] ) && $_GET['new_user_approve_filter'] != '' ) {
193
+ $filter = esc_attr( $_GET['new_user_approve_filter'] );
194
+
195
+ $query->query_from .= " INNER JOIN {$wpdb->usermeta} wp_usermeta ON ( {$wpdb->users}.ID = wp_usermeta.user_id )";
196
+
197
+ if ( 'approved' == $filter ) {
198
+ $query->query_fields = "DISTINCT SQL_CALC_FOUND_ROWS {$wpdb->users}.ID";
199
+ $query->query_from .= " LEFT JOIN {$wpdb->usermeta} AS mt1 ON ({$wpdb->users}.ID = mt1.user_id AND mt1.meta_key = 'pw_user_status')";
200
+ $query->query_where .= " AND ( ( wp_usermeta.meta_key = 'pw_user_status' AND CAST(wp_usermeta.meta_value AS CHAR) = 'approved' ) OR mt1.user_id IS NULL )";
201
+ } else {
202
+ $query->query_where .= " AND ( (wp_usermeta.meta_key = 'pw_user_status' AND CAST(wp_usermeta.meta_value AS CHAR) = '{$filter}') )";
203
+ }
204
+ }
205
+ }
206
+
207
+ /**
208
+ * Use javascript to add the ability to bulk modify the status of users.
209
+ *
210
+ * @uses admin_footer-users.php
211
+ */
212
+ public function admin_footer() {
213
+ $screen = get_current_screen();
214
+
215
+ if ( $screen->id == 'users' ) : ?>
216
+ <script type="text/javascript">
217
+ jQuery(document).ready(function ($) {
218
+ $('<option>').val('approve').text('<?php _e( 'Approve', 'new-user-approve' )?>').appendTo("select[name='action']");
219
+ $('<option>').val('approve').text('<?php _e( 'Approve', 'new-user-approve' )?>').appendTo("select[name='action2']");
220
+
221
+ $('<option>').val('deny').text('<?php _e( 'Deny', 'new-user-approve' )?>').appendTo("select[name='action']");
222
+ $('<option>').val('deny').text('<?php _e( 'Deny', 'new-user-approve' )?>').appendTo("select[name='action2']");
223
+ });
224
+ </script>
225
+ <?php endif;
226
+ }
227
+
228
+ /**
229
+ * Process the bulk status updates
230
+ *
231
+ * @uses load-users.php
232
+ */
233
+ public function bulk_action() {
234
+ $screen = get_current_screen();
235
+
236
+ if ( $screen->id == 'users' ) {
237
+
238
+ // get the action
239
+ $wp_list_table = _get_list_table( 'WP_Users_List_Table' );
240
+ $action = $wp_list_table->current_action();
241
+
242
+ $allowed_actions = array( 'approve', 'deny' );
243
+ if ( !in_array( $action, $allowed_actions ) ) {
244
+ return;
245
+ }
246
+
247
+ // security check
248
+ check_admin_referer( 'bulk-users' );
249
+
250
+ // make sure ids are submitted
251
+ if ( isset( $_REQUEST['users'] ) ) {
252
+ $user_ids = array_map( 'intval', $_REQUEST['users'] );
253
+ }
254
+
255
+ if ( empty( $user_ids ) ) {
256
+ return;
257
+ }
258
+
259
+ $sendback = remove_query_arg( array( 'approved', 'denied', 'deleted', 'ids', 'new_user_approve_filter', 'pw-status-query-submit', 'new_role' ), wp_get_referer() );
260
+ if ( !$sendback ) {
261
+ $sendback = admin_url( 'users.php' );
262
+ }
263
+
264
+ $pagenum = $wp_list_table->get_pagenum();
265
+ $sendback = add_query_arg( 'paged', $pagenum, $sendback );
266
+
267
+ switch ( $action ) {
268
+ case 'approve':
269
+ $approved = 0;
270
+ foreach ( $user_ids as $user_id ) {
271
+ pw_new_user_approve()->update_user_status( $user_id, 'approve' );
272
+ $approved++;
273
+ }
274
+
275
+ $sendback = add_query_arg( array( 'approved' => $approved, 'ids' => join( ',', $user_ids ) ), $sendback );
276
+ break;
277
+
278
+ case 'deny':
279
+ $denied = 0;
280
+ foreach ( $user_ids as $user_id ) {
281
+ pw_new_user_approve()->update_user_status( $user_id, 'deny' );
282
+ $denied++;
283
+ }
284
+
285
+ $sendback = add_query_arg( array( 'denied' => $denied, 'ids' => join( ',', $user_ids ) ), $sendback );
286
+ break;
287
+
288
+ default:
289
+ return;
290
+ }
291
+
292
+ $sendback = remove_query_arg( array( 'action', 'action2', 'tags_input', 'post_author', 'comment_status', 'ping_status', '_status', 'post', 'bulk_edit', 'post_view' ), $sendback );
293
+
294
+ wp_redirect( $sendback );
295
+ exit();
296
+ }
297
+ }
298
+
299
+ /**
300
+ * Show a message on the users page if a status has been updated.
301
+ *
302
+ * @uses admin_notices
303
+ */
304
+ public function admin_notices() {
305
+ $screen = get_current_screen();
306
+
307
+ if ( $screen->id != 'users' ) {
308
+ return;
309
+ }
310
+
311
+ $message = null;
312
+
313
+ if ( isset( $_REQUEST['denied'] ) && (int) $_REQUEST['denied'] ) {
314
+ $denied = esc_attr( $_REQUEST['denied'] );
315
+ $message = sprintf( _n( 'User denied.', '%s users denied.', $denied, 'new-user-approve' ), number_format_i18n( $denied ) );
316
+ }
317
+
318
+ if ( isset( $_REQUEST['approved'] ) && (int) $_REQUEST['approved'] ) {
319
+ $approved = esc_attr( $_REQUEST['approved'] );
320
+ $message = sprintf( _n( 'User approved.', '%s users approved.', $approved, 'new-user-approve' ), number_format_i18n( $approved ) );
321
+ }
322
+
323
+ if ( !empty( $message ) ) {
324
+ echo '<div class="updated"><p>' . $message . '</p></div>';
325
+ }
326
+ }
327
+
328
+ /**
329
+ * Display the dropdown on the user profile page to allow an admin to update the user status.
330
+ *
331
+ * @uses show_user_profile
332
+ * @uses edit_user_profile
333
+ * @param object $user
334
+ */
335
+ public function profile_status_field( $user ) {
336
+ if ( $user->ID == get_current_user_id() ) {
337
+ return;
338
+ }
339
+
340
+ $user_status = pw_new_user_approve()->get_user_status( $user->ID );
341
+ ?>
342
+ <table class="form-table">
343
+ <tr>
344
+ <th><label for="new_user_approve_status"><?php _e( 'Access Status', 'new-user-approve' ); ?></label>
345
+ </th>
346
+ <td>
347
+ <select id="new_user_approve_status" name="new_user_approve_status">
348
+ <?php if ( $user_status == 'pending' ) : ?>
349
+ <option value=""><?php _e( '-- Status --', 'new-user-approve' ); ?></option>
350
+ <?php endif; ?>
351
+ <?php foreach ( array( 'approved', 'denied' ) as $status ) : ?>
352
+ <option
353
+ value="<?php echo esc_attr( $status ); ?>"<?php selected( $status, $user_status ); ?>><?php echo esc_html( $status ); ?></option>
354
+ <?php endforeach; ?>
355
+ </select>
356
+ <span
357
+ class="description"><?php _e( 'If user has access to sign in or not.', 'new-user-approve' ); ?></span>
358
+ <?php if ( $user_status == 'pending' ) : ?>
359
+ <br/><span
360
+ class="description"><?php _e( 'Current user status is <strong>pending</strong>.', 'new-user-approve' ); ?></span>
361
+ <?php endif; ?>
362
+ </td>
363
+ </tr>
364
+ </table>
365
+ <?php
366
+ }
367
+
368
+ /**
369
+ * Save the user status when updating from the user profile.
370
+ *
371
+ * @uses edit_user_profile_update
372
+ * @param int $user_id
373
+ * @return bool
374
+ */
375
+ public function save_profile_status_field( $user_id ) {
376
+ if ( !current_user_can( 'edit_user', $user_id ) ) {
377
+ return false;
378
+ }
379
+
380
+ if ( !empty( $_POST['new_user_approve_status'] ) ) {
381
+ $new_status = esc_attr( $_POST['new_user_approve_status'] );
382
+
383
+ if ( $new_status == 'approved' )
384
+ $new_status = 'approve'; else if ( $new_status == 'denied' )
385
+ $new_status = 'deny';
386
+
387
+ pw_new_user_approve()->update_user_status( $user_id, $new_status );
388
+ }
389
+ }
390
  }
391
 
392
  function pw_new_user_approve_user_list() {
393
+ return pw_new_user_approve_user_list::instance();
394
  }
395
 
396
  pw_new_user_approve_user_list();
new-user-approve.php CHANGED
@@ -4,63 +4,63 @@
4
  Plugin URI: http://www.picklewagon.com/wordpress/new-user-approve/
5
  Description: Allow administrators to approve users once they register. Only approved users will be allowed to access the blog. For support, please go to the <a href="http://wordpress.org/support/plugin/new-user-approve">support forums</a> on wordpress.org.
6
  Author: Josh Harrison
7
- Version: 1.5.6
8
  Author URI: http://picklewagon.com/
9
  */
10
 
11
  class pw_new_user_approve {
12
 
13
- /**
14
- * The only instance of pw_new_user_approve.
15
- *
16
- * @var pw_new_user_approve
17
- */
18
- private static $instance;
19
-
20
- /**
21
- * Returns the main instance.
22
- *
23
- * @return pw_new_user_approve
24
- */
25
- public static function instance() {
26
- if ( ! isset( self::$instance ) ) {
27
- self::$instance = new pw_new_user_approve();
28
- }
29
- return self::$instance;
30
- }
31
 
32
  private function __construct() {
33
  // Load up the localization file if we're using WordPress in a different language
34
  // Just drop it in this plugin's "localization" folder and name it "new-user-approve-[value in wp-config].mo"
35
  load_plugin_textdomain( 'new-user-approve', false, dirname( plugin_basename( __FILE__ ) ) . '/localization' );
36
 
37
- register_activation_hook( __FILE__, array( $this, 'activation' ) );
38
- register_deactivation_hook( __FILE__, array( $this, 'deactivation' ) );
39
-
40
- // Actions
41
- add_action( 'wp_loaded', array( $this, 'admin_loaded' ) );
42
- add_action( 'rightnow_end', array( $this, 'dashboard_stats' ) );
43
- add_action( 'user_register', array( $this, 'delete_new_user_approve_transient' ), 11 );
44
- add_action( 'new_user_approve_approve_user', array( $this, 'delete_new_user_approve_transient' ), 11 );
45
- add_action( 'new_user_approve_deny_user', array( $this, 'delete_new_user_approve_transient' ), 11 );
46
- add_action( 'deleted_user', array( $this, 'delete_new_user_approve_transient' ) );
47
- add_action( 'register_post', array( $this, 'request_admin_approval_email' ), 10, 3 );
48
- add_action( 'register_post', array( $this, 'create_new_user' ), 10, 3 );
49
- add_action( 'lostpassword_post', array( $this, 'lost_password' ) );
50
- add_action( 'user_register', array( $this, 'add_user_status' ) );
51
- add_action( 'new_user_approve_approve_user', array( $this, 'approve_user' ) );
52
- add_action( 'new_user_approve_deny_user', array( $this, 'deny_user' ) );
53
-
54
- // Filters
55
- add_filter( 'wp_authenticate_user', array( $this, 'authenticate_user' ) );
56
- add_filter( 'registration_errors', array( $this, 'show_user_pending_message' ) );
57
- add_filter( 'login_message', array( $this, 'welcome_user' ) );
58
- add_filter( 'new_user_approve_validate_status_update', array( $this, 'validate_status_update' ), 10, 3 );
59
  }
60
 
61
- public function get_plugin_url() {
62
- return plugin_dir_url( __FILE__ );
63
- }
64
 
65
  public function get_plugin_dir() {
66
  return plugin_dir_path( __FILE__ );
@@ -68,7 +68,7 @@ class pw_new_user_approve {
68
 
69
  /**
70
  * Require a minimum version of WordPress on activation
71
- *
72
  * @uses register_activation_hook
73
  */
74
  public function activation() {
@@ -79,11 +79,11 @@ class pw_new_user_approve {
79
  if ( version_compare( $wp_version, $min_wp_version, '<' ) ) {
80
  exit( $exit_msg );
81
  }
82
-
83
  // since the right version of WordPress is being used, run a hook
84
  do_action( 'new_user_approve_activate' );
85
  }
86
-
87
  /**
88
  * @uses register_deactivation_hook
89
  */
@@ -91,527 +91,513 @@ class pw_new_user_approve {
91
  do_action( 'new_user_approve_deactivate' );
92
  }
93
 
94
- /**
95
- * Makes it possible to disable the user admin integration. Must happen after
96
- * WordPress is loaded.
97
- *
98
- * @uses wp_loaded
99
- */
100
- public function admin_loaded() {
101
- $user_admin_integration = apply_filters( 'new_user_approve_user_admin_integration', true );
102
- if ( $user_admin_integration ) {
103
- require_once( dirname( __FILE__ ) . '/includes/user-list.php' );
104
- }
105
-
106
- $legacy_panel = apply_filters( 'new_user_approve_user_admin_legacy', true );
107
- if ( $legacy_panel ) {
108
- require_once( dirname( __FILE__ ) . '/includes/admin-approve.php' );
109
- }
110
- }
111
-
112
- /**
113
- * Get the status of a user.
114
- *
115
- * @param int $user_id
116
- * @return string the status of the user
117
- */
118
- public function get_user_status( $user_id ) {
119
- $user_status = get_user_meta( $user_id, 'pw_user_status', true );
120
-
121
- if ( empty( $user_status ) )
122
- $user_status = 'approved';
123
-
124
- return $user_status;
125
- }
126
-
127
- /**
128
- * Update the status of a user. The new status must be either 'approve' or 'deny'.
129
- *
130
- * @param int $user
131
- * @param string $status
132
- */
133
- public function update_user_status( $user, $status ) {
134
- $user_id = absint( $user );
135
- if ( ! $user_id )
136
- return;
137
-
138
- if ( ! in_array( $status, array( 'approve', 'deny' ) ) )
139
- return;
140
-
141
- $do_update = apply_filters( 'new_user_approve_validate_status_update', true, $user_id, $status );
142
-
143
- if ( !$do_update )
144
- return;
145
-
146
- // where it all happens
147
- do_action( 'new_user_approve_' . $status . '_user', $user_id );
148
- do_action( 'new_user_approve_user_status_update', $user_id, $status );
149
- }
150
-
151
- /**
152
- * Get the valid statuses. Anything outside of the returned array is an invalid status.
153
- *
154
- * @return array
155
- */
156
- public function get_valid_statuses() {
157
- return array( 'pending', 'approved', 'denied' );
158
- }
159
-
160
- /**
161
- * Only validate the update if the status has been updated to prevent unnecessary update
162
- * and especially emails.
163
- *
164
- * @param bool $do_update
165
- * @param int $user_id
166
- * @param string $status either 'approve' or 'deny'
167
- */
168
- public function validate_status_update( $do_update, $user_id, $status ) {
169
- $current_status = pw_new_user_approve()->get_user_status( $user_id );
170
-
171
- if ( $status == 'approve' )
172
- $new_status = 'approved';
173
- else
174
- $new_status = 'denied';
175
-
176
- if ( $current_status == $new_status )
177
- $do_update = false;
178
-
179
- return $do_update;
180
- }
181
-
182
- /**
183
- * Determine if the user is good to sign in based on their status.
184
- *
185
- * @uses wp_authenticate_user
186
- * @param array $userdata
187
- */
188
- public function authenticate_user( $userdata ) {
189
- $status = $this->get_user_status( $userdata->ID );
190
-
191
- if ( empty( $status ) ) {
192
- // the user does not have a status so let's assume the user is good to go
193
- return $userdata;
194
- }
195
-
196
- $message = false;
197
- switch ( $status ) {
198
- case 'pending':
199
- $pending_message = __( '<strong>ERROR</strong>: Your account is still pending approval.', 'new-user-approve' );
200
- $pending_message = apply_filters( 'new_user_approve_pending_error', $pending_message );
201
-
202
- $message = new WP_Error( 'pending_approval', $pending_message );
203
- break;
204
- case 'denied':
205
- $denied_message = __( '<strong>ERROR</strong>: Your account has been denied access to this site.', 'new-user-approve' );
206
- $denied_message = apply_filters( 'new_user_approve_denied_error', $denied_message );
207
-
208
- $message = new WP_Error( 'denied_access', $denied_message );
209
- break;
210
- case 'approved':
211
- $message = $userdata;
212
- break;
213
- }
214
-
215
- return $message;
216
- }
217
-
218
- /**
219
- * Get a status of all the users and save them using a transient
220
- */
221
- public function get_user_statuses() {
222
- $valid_stati = $this->get_valid_statuses();
223
- $user_status = get_transient( 'new_user_approve_user_statuses' );
224
-
225
- if ( false === $user_status ) {
226
- $user_status = array();
227
-
228
- foreach ( $valid_stati as $status ) {
229
- // Query the users table
230
- if ( $status != 'approved' ) {
231
- // Query the users table
232
- $query = array(
233
- 'meta_key' => 'pw_user_status',
234
- 'meta_value' => $status,
235
- );
236
- $wp_user_search = new WP_User_Query( $query );
237
- } else {
238
- // get all approved users and any user without a status
239
- $query = array(
240
- 'meta_query' => array(
241
- 'relation' => 'OR',
242
- array(
243
- 'key' => 'pw_user_status',
244
- 'value' => 'approved',
245
- 'compare' => '='
246
- ),
247
- array(
248
- 'key' => 'pw_user_status',
249
- 'value' => '',
250
- 'compare' => 'NOT EXISTS'
251
- ),
252
- ),
253
- );
254
- $wp_user_search = new WP_User_Query( $query );
255
- }
256
-
257
- $user_status[$status] = $wp_user_search->get_results();
258
- }
259
-
260
- set_transient( 'new_user_approve_user_statuses', $user_status );
261
- }
262
-
263
- foreach ( $valid_stati as $status ) {
264
- $user_status[$status] = apply_filters( 'new_user_approve_user_status', $user_status[$status], $status );
265
- }
266
-
267
- return $user_status;
268
- }
269
-
270
- /**
271
- * Delete the transient storing all of the user statuses.
272
- *
273
- * @uses user_register
274
- * @uses deleted_user
275
- * @uses new_user_approve_approve_user
276
- * @uses new_user_approve_deny_user
277
- */
278
- public function delete_new_user_approve_transient() {
279
- delete_transient( 'new_user_approve_user_statuses' );
280
- }
281
-
282
- /**
283
- * Display the stats on the WP dashboard. Will show 1 line with a count
284
- * of users and their status.
285
- *
286
- * @uses rightnow_end
287
- */
288
- public function dashboard_stats() {
289
- $user_status = $this->get_user_statuses();
290
- ?>
291
- <div>
292
- <p><span style="font-weight:bold;"><a href="<?php echo apply_filters( 'new_user_approve_dashboard_link', 'users.php' ); ?>"><?php _e( 'Users', 'new-user-approve' ); ?></a></span>:
293
- <?php foreach ( $user_status as $status => $users ) :
294
- print count( $users ) . " " . __( $status, 'new-user-approve' ) . "&nbsp;&nbsp;&nbsp;";
295
- endforeach; ?>
296
- </p>
297
- </div>
298
- <?php
299
- }
300
-
301
- /**
302
- * The default notification message that is sent to site admin when requesting approval.
303
- *
304
- * @return string
305
- */
306
- public function default_notification_message() {
307
- $message = __( 'USERNAME (USEREMAIL) has requested a username at SITENAME', 'new-user-approve' ) . "\n\n";
308
- $message .= "SITEURL\n\n";
309
- $message .= __( 'To approve or deny this user access to SITENAME go to', 'new-user-approve' ) . "\n\n";
310
- $message .= "ADMINURL\n\n";
311
-
312
- return $message;
313
- }
314
-
315
- /**
316
- * Send an email to the admin to request approval. If there are already errors,
317
- * just go back and let core do it's thing.
318
- *
319
- * @uses register_post
320
- * @param string $user_login
321
- * @param string $user_email
322
- * @param object $errors
323
- */
324
- public function request_admin_approval_email( $user_login, $user_email, $errors ) {
325
- if ( $errors->get_error_code() ) {
326
- return $errors;
327
- }
328
-
329
- // The blogname option is escaped with esc_html on the way into the database in sanitize_option
330
- // we want to reverse this for the plain text arena of emails.
331
- $blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
332
-
333
- $default_admin_url = admin_url( 'users.php?s&pw-status-query-submit=Filter&new_user_approve_filter=pending&paged=1' );
334
- $admin_url = apply_filters( 'new_user_approve_admin_link', $default_admin_url );
335
-
336
- /* send email to admin for approval */
337
- $message = apply_filters( 'new_user_approve_request_approval_message_default', $this->default_notification_message() );
338
-
339
- $message = str_replace( 'USERNAME', $user_login, $message );
340
- $message = str_replace( 'USEREMAIL', $user_email, $message );
341
- $message = str_replace( 'SITENAME', $blogname, $message );
342
- $message = str_replace( 'SITEURL', get_option( 'siteurl' ), $message );
343
- $message = str_replace( 'ADMINURL', $admin_url, $message );
344
-
345
- $message = apply_filters( 'new_user_approve_request_approval_message', $message, $user_login, $user_email );
346
-
347
- $subject = sprintf( __( '[%s] User Approval', 'new-user-approve' ), $blogname );
348
- $subject = apply_filters( 'new_user_approve_request_approval_subject', $subject );
349
-
350
- $to = apply_filters( 'new_user_approve_email_admins', array( get_option( 'admin_email' ) ) );
351
- $to = array_unique( $to );
352
-
353
- // send the mail
354
- wp_mail( $to, $subject, $message, $this->email_message_headers() );
355
- }
356
-
357
- /**
358
- * Create a new user after the registration has been validated. Normally,
359
- * when a user registers, an email is sent to the user containing their
360
- * username and password. The email does not get sent to the user until
361
- * the user is approved when using the default behavior of this plugin.
362
- *
363
- * @uses register_post
364
- * @param string $user_login
365
- * @param string $user_email
366
- * @param object $errors
367
- */
368
- public function create_new_user( $user_login, $user_email, $errors ) {
369
- if ( $errors->get_error_code() ) {
370
- return $errors;
371
- }
372
-
373
- // create the user
374
- $user_pass = wp_generate_password( 12, false );
375
- $user_id = wp_create_user( $user_login, $user_pass, $user_email );
376
- if ( ! $user_id ) {
377
- $errors->add( 'registerfail', sprintf( __( '<strong>ERROR</strong>: Couldn&#8217;t register you... please contact the <a href="mailto:%s">webmaster</a> !' ), get_option( 'admin_email' ) ) );
378
- return $errors;
379
- }
380
- }
381
-
382
- /**
383
- * Admin approval of user
384
- *
385
- * @uses new_user_approve_approve_user
386
- */
387
- public function approve_user( $user_id ) {
388
- $user = new WP_User( $user_id );
389
-
390
- // password should only be reset for users that:
391
- // * have never logged in
392
- // * are just approved for the first time
393
-
394
- // If the password has already been reset for this user,
395
- // $password_reset will be a unix timestamp
396
- $password_reset = get_user_meta( $user_id, 'pw_user_approve_password_reset' );
397
-
398
- // Get the current user status. By default each user is given a pending
399
- // status when the user is created (with this plugin activated). If the
400
- // user was created while this plugin was not active, the user will not
401
- // have a status set.
402
- $user_status = get_user_meta( $user_id, 'pw_user_status' );
403
-
404
- // Default behavior is to reset password
405
- $bypass_password_reset = false;
406
-
407
- // if no status is set, don't reset password
408
- if ( empty( $user_status ) )
409
- $bypass_password_reset = true;
410
-
411
- // if the password has already been reset, absolutely bypass
412
- if ( !empty( $password_reset ) )
413
- $bypass_password_reset = true;
414
-
415
- $bypass_password_reset = apply_filters( 'new_user_approve_bypass_password_reset', $bypass_password_reset );
416
-
417
- if ( ! $bypass_password_reset ) {
418
- global $wpdb;
419
-
420
- // reset password to know what to send the user
421
- $new_pass = wp_generate_password( 12, false );
422
- $data = array(
423
- 'user_pass' => md5( $new_pass ),
424
- 'user_activation_key' => '',
425
- );
426
- $where = array(
427
- 'ID' => $user->ID,
428
- );
429
- $wpdb->update( $wpdb->users, $data, $where, array( '%s', '%s' ), array( '%d' ) );
430
-
431
- // Set up the Password change nag.
432
- update_user_option( $user->ID, 'default_password_nag', true, true );
433
-
434
- // Set this meta field to track that the password has been reset by
435
- // the plugin. Don't reset it again.
436
- update_user_meta( $user->ID, 'pw_user_approve_password_reset', time() );
437
- }
438
-
439
- wp_cache_delete( $user->ID, 'users' );
440
- wp_cache_delete( $user->data->user_login, 'userlogins' );
441
-
442
- // send email to user telling of approval
443
- $user_login = stripslashes( $user->data->user_login );
444
- $user_email = stripslashes( $user->data->user_email );
445
-
446
- // format the message
447
- $message = sprintf( __( 'You have been approved to access %s', 'new-user-approve' ), get_option( 'blogname' ) ) . "\r\n";
448
- $message .= sprintf( __( 'Username: %s', 'new-user-approve' ), $user_login ) . "\r\n";
449
- if ( ! $bypass_password_reset ) {
450
- $message .= sprintf( __( 'Password: %s', 'new-user-approve' ), $new_pass ) . "\r\n";
451
- }
452
- $message .= wp_login_url() . "\r\n";
453
-
454
- $message = apply_filters( 'new_user_approve_approve_user_message', $message, $user );
455
-
456
- $subject = sprintf( __( '[%s] Registration Approved', 'new-user-approve' ), get_option( 'blogname' ) );
457
- $subject = apply_filters( 'new_user_approve_approve_user_subject', $subject );
458
-
459
- // send the mail
460
- wp_mail( $user_email, $subject, $message, $this->email_message_headers() );
461
-
462
- // change usermeta tag in database to approved
463
- update_user_meta( $user->ID, 'pw_user_status', 'approved' );
464
-
465
- do_action( 'new_user_approve_user_approved', $user );
466
- }
467
-
468
- /**
469
- * Admin denial of user
470
- *
471
- * @uses new_user_approve_deny_user
472
- */
473
- public function deny_user( $user_id ) {
474
- $user = new WP_User( $user_id );
475
-
476
- // send email to user telling of denial
477
- $user_email = stripslashes( $user->user_email );
478
-
479
- // format the message
480
- $message = sprintf( __( 'You have been denied access to %s', 'new-user-approve' ), get_option( 'blogname' ) );
481
- $message = apply_filters( 'new_user_approve_deny_user_message', $message, $user );
482
-
483
- $subject = sprintf( __( '[%s] Registration Denied', 'new-user-approve' ), get_option( 'blogname' ) );
484
- $subject = apply_filters( 'new_user_approve_deny_user_subject', $subject );
485
-
486
- // send the mail
487
- @wp_mail( $user_email, $subject, $message, $this->email_message_headers() );
488
-
489
- // change usermeta tag in database to denied
490
- update_user_meta( $user->ID, 'pw_user_status', 'denied' );
491
-
492
- do_action( 'new_user_approve_user_denied', $user );
493
- }
494
-
495
- public function email_message_headers() {
496
- $admin_email = get_option( 'admin_email' );
497
- if ( empty( $admin_email ) )
498
- $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
499
-
500
- $from_name = get_option( 'blogname' );
501
-
502
- $headers = array(
503
- "From: \"{$from_name}\" <{$admin_email}>\n",
504
- "Content-Type: text/plain; charset=\"" . get_option( 'blog_charset' ) . "\"\n",
505
- );
506
-
507
- $headers = apply_filters( 'new_user_approve_email_header', $headers );
508
-
509
- return $headers;
510
- }
511
-
512
- /**
513
- * Display a message to the user after they have registered
514
- *
515
- * @uses registration_errors
516
- */
517
- public function show_user_pending_message($errors) {
518
- if ( ! empty( $_POST['redirect_to'] ) ) {
519
- // if a redirect_to is set, honor it
520
- wp_safe_redirect( $_POST['redirect_to'] );
521
- exit();
522
- }
523
-
524
- // if there is an error already, let it do it's thing
525
- if ( $errors->get_error_code() )
526
- return $errors;
527
-
528
- $message = sprintf( __( 'An email has been sent to the site administrator. The administrator will review the information that has been submitted and either approve or deny your request.', 'new-user-approve' ) );
529
- $message .= ' ';
530
- $message .= sprintf( __( 'You will receive an email with instructions on what you will need to do next. Thanks for your patience.', 'new-user-approve' ) );
531
- $message = apply_filters( 'new_user_approve_pending_message', $message );
532
-
533
- $errors->add( 'registration_required', $message, 'message' );
534
-
535
- $success_message = __( 'Registration successful.', 'new-user-approve' );
536
- $success_message = apply_filters( 'new_user_approve_registration_message', $success_message );
537
-
538
- login_header( __( 'Pending Approval', 'new-user-approve' ), '<p class="message register">' . $success_message . '</p>', $errors );
539
- login_footer();
540
-
541
- // an exit is necessary here so the normal process for user registration doesn't happen
542
- exit();
543
- }
544
-
545
- /**
546
- * Only give a user their password if they have been approved
547
- *
548
- * @uses lostpassword_post
549
- */
550
- public function lost_password() {
551
- $is_email = strpos( $_POST['user_login'], '@' );
552
- if ( $is_email === false ) {
553
- $username = sanitize_user( $_POST['user_login'] );
554
- $user_data = get_user_by( 'login', trim( $username ) );
555
- } else {
556
- $email = is_email( $_POST['user_login'] );
557
- $user_data = get_user_by( 'email', $email );
558
- }
559
-
560
- if ( $user_data->pw_user_status && $user_data->pw_user_status != 'approved' ) {
561
- wp_redirect( 'wp-login.php' );
562
- exit();
563
- }
564
- }
565
-
566
- /**
567
- * Add message to login page saying registration is required.
568
- *
569
- * @uses login_message
570
- * @param string $message
571
- * @return string
572
- */
573
- public function welcome_user($message) {
574
- if ( ! isset( $_GET['action'] ) ) {
575
- $welcome = sprintf( __( 'Welcome to %s. This site is accessible to approved users only. To be approved, you must first register.', 'new-user-approve' ), get_option( 'blogname' ) );
576
- $welcome = apply_filters( 'new_user_approve_welcome_message', $welcome );
577
-
578
- if ( ! empty( $welcome ) ) {
579
- $message .= '<p class="message register">' . $welcome . '</p>';
580
- }
581
- }
582
-
583
- if ( isset( $_GET['action'] ) && $_GET['action'] == 'register' && ! $_POST ) {
584
- $instructions = sprintf( __( 'After you register, your request will be sent to the site administrator for approval. You will then receive an email with further instructions.', 'new-user-approve' ) );
585
- $instructions = apply_filters( 'new_user_approve_register_instructions', $instructions );
586
-
587
- if ( ! empty( $instructions ) ) {
588
- $message .= '<p class="message register">' . $instructions . '</p>';
589
- }
590
- }
591
-
592
- return $message;
593
- }
594
-
595
- /**
596
- * Give the user a status
597
- *
598
- * @uses user_register
599
- * @param int $user_id
600
- */
601
- public function add_user_status( $user_id ) {
602
- $status = 'pending';
603
-
604
- // This check needs to happen when a user is created in the admin
605
- if ( isset( $_REQUEST['action'] ) && 'createuser' == $_REQUEST['action'] ) {
606
- $status = 'approved';
607
- }
608
- update_user_meta( $user_id, 'pw_user_status', $status );
609
- }
610
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
611
  } // End Class
612
 
613
  function pw_new_user_approve() {
614
- return pw_new_user_approve::instance();
615
  }
616
 
617
  pw_new_user_approve();
4
  Plugin URI: http://www.picklewagon.com/wordpress/new-user-approve/
5
  Description: Allow administrators to approve users once they register. Only approved users will be allowed to access the blog. For support, please go to the <a href="http://wordpress.org/support/plugin/new-user-approve">support forums</a> on wordpress.org.
6
  Author: Josh Harrison
7
+ Version: 1.5.7
8
  Author URI: http://picklewagon.com/
9
  */
10
 
11
  class pw_new_user_approve {
12
 
13
+ /**
14
+ * The only instance of pw_new_user_approve.
15
+ *
16
+ * @var pw_new_user_approve
17
+ */
18
+ private static $instance;
19
+
20
+ /**
21
+ * Returns the main instance.
22
+ *
23
+ * @return pw_new_user_approve
24
+ */
25
+ public static function instance() {
26
+ if ( !isset( self::$instance ) ) {
27
+ self::$instance = new pw_new_user_approve();
28
+ }
29
+ return self::$instance;
30
+ }
31
 
32
  private function __construct() {
33
  // Load up the localization file if we're using WordPress in a different language
34
  // Just drop it in this plugin's "localization" folder and name it "new-user-approve-[value in wp-config].mo"
35
  load_plugin_textdomain( 'new-user-approve', false, dirname( plugin_basename( __FILE__ ) ) . '/localization' );
36
 
37
+ register_activation_hook( __FILE__, array( $this, 'activation' ) );
38
+ register_deactivation_hook( __FILE__, array( $this, 'deactivation' ) );
39
+
40
+ // Actions
41
+ add_action( 'wp_loaded', array( $this, 'admin_loaded' ) );
42
+ add_action( 'rightnow_end', array( $this, 'dashboard_stats' ) );
43
+ add_action( 'user_register', array( $this, 'delete_new_user_approve_transient' ), 11 );
44
+ add_action( 'new_user_approve_approve_user', array( $this, 'delete_new_user_approve_transient' ), 11 );
45
+ add_action( 'new_user_approve_deny_user', array( $this, 'delete_new_user_approve_transient' ), 11 );
46
+ add_action( 'deleted_user', array( $this, 'delete_new_user_approve_transient' ) );
47
+ add_action( 'register_post', array( $this, 'request_admin_approval_email' ), 10, 3 );
48
+ add_action( 'register_post', array( $this, 'create_new_user' ), 10, 3 );
49
+ add_action( 'lostpassword_post', array( $this, 'lost_password' ) );
50
+ add_action( 'user_register', array( $this, 'add_user_status' ) );
51
+ add_action( 'new_user_approve_approve_user', array( $this, 'approve_user' ) );
52
+ add_action( 'new_user_approve_deny_user', array( $this, 'deny_user' ) );
53
+
54
+ // Filters
55
+ add_filter( 'wp_authenticate_user', array( $this, 'authenticate_user' ) );
56
+ add_filter( 'registration_errors', array( $this, 'show_user_pending_message' ) );
57
+ add_filter( 'login_message', array( $this, 'welcome_user' ) );
58
+ add_filter( 'new_user_approve_validate_status_update', array( $this, 'validate_status_update' ), 10, 3 );
59
  }
60
 
61
+ public function get_plugin_url() {
62
+ return plugin_dir_url( __FILE__ );
63
+ }
64
 
65
  public function get_plugin_dir() {
66
  return plugin_dir_path( __FILE__ );
68
 
69
  /**
70
  * Require a minimum version of WordPress on activation
71
+ *
72
  * @uses register_activation_hook
73
  */
74
  public function activation() {
79
  if ( version_compare( $wp_version, $min_wp_version, '<' ) ) {
80
  exit( $exit_msg );
81
  }
82
+
83
  // since the right version of WordPress is being used, run a hook
84
  do_action( 'new_user_approve_activate' );
85
  }
86
+
87
  /**
88
  * @uses register_deactivation_hook
89
  */
91
  do_action( 'new_user_approve_deactivate' );
92
  }
93
 
94
+ /**
95
+ * Makes it possible to disable the user admin integration. Must happen after
96
+ * WordPress is loaded.
97
+ *
98
+ * @uses wp_loaded
99
+ */
100
+ public function admin_loaded() {
101
+ $user_admin_integration = apply_filters( 'new_user_approve_user_admin_integration', true );
102
+ if ( $user_admin_integration ) {
103
+ require_once( dirname( __FILE__ ) . '/includes/user-list.php' );
104
+ }
105
+
106
+ $legacy_panel = apply_filters( 'new_user_approve_user_admin_legacy', true );
107
+ if ( $legacy_panel ) {
108
+ require_once( dirname( __FILE__ ) . '/includes/admin-approve.php' );
109
+ }
110
+ }
111
+
112
+ /**
113
+ * Get the status of a user.
114
+ *
115
+ * @param int $user_id
116
+ * @return string the status of the user
117
+ */
118
+ public function get_user_status( $user_id ) {
119
+ $user_status = get_user_meta( $user_id, 'pw_user_status', true );
120
+
121
+ if ( empty( $user_status ) ) {
122
+ $user_status = 'approved';
123
+ }
124
+
125
+ return $user_status;
126
+ }
127
+
128
+ /**
129
+ * Update the status of a user. The new status must be either 'approve' or 'deny'.
130
+ *
131
+ * @param int $user
132
+ * @param string $status
133
+ */
134
+ public function update_user_status( $user, $status ) {
135
+ $user_id = absint( $user );
136
+ if ( !$user_id ) {
137
+ return;
138
+ }
139
+
140
+ if ( !in_array( $status, array( 'approve', 'deny' ) ) ) {
141
+ return;
142
+ }
143
+
144
+ $do_update = apply_filters( 'new_user_approve_validate_status_update', true, $user_id, $status );
145
+ if ( !$do_update ) {
146
+ return;
147
+ }
148
+
149
+ // where it all happens
150
+ do_action( 'new_user_approve_' . $status . '_user', $user_id );
151
+ do_action( 'new_user_approve_user_status_update', $user_id, $status );
152
+ }
153
+
154
+ /**
155
+ * Get the valid statuses. Anything outside of the returned array is an invalid status.
156
+ *
157
+ * @return array
158
+ */
159
+ public function get_valid_statuses() {
160
+ return array( 'pending', 'approved', 'denied' );
161
+ }
162
+
163
+ /**
164
+ * Only validate the update if the status has been updated to prevent unnecessary update
165
+ * and especially emails.
166
+ *
167
+ * @param bool $do_update
168
+ * @param int $user_id
169
+ * @param string $status either 'approve' or 'deny'
170
+ */
171
+ public function validate_status_update( $do_update, $user_id, $status ) {
172
+ $current_status = pw_new_user_approve()->get_user_status( $user_id );
173
+
174
+ if ( $status == 'approve' ) {
175
+ $new_status = 'approved';
176
+ } else {
177
+ $new_status = 'denied';
178
+ }
179
+
180
+ if ( $current_status == $new_status ) {
181
+ $do_update = false;
182
+ }
183
+
184
+ return $do_update;
185
+ }
186
+
187
+ /**
188
+ * Determine if the user is good to sign in based on their status.
189
+ *
190
+ * @uses wp_authenticate_user
191
+ * @param array $userdata
192
+ */
193
+ public function authenticate_user( $userdata ) {
194
+ $status = $this->get_user_status( $userdata->ID );
195
+
196
+ if ( empty( $status ) ) {
197
+ // the user does not have a status so let's assume the user is good to go
198
+ return $userdata;
199
+ }
200
+
201
+ $message = false;
202
+ switch ( $status ) {
203
+ case 'pending':
204
+ $pending_message = __( '<strong>ERROR</strong>: Your account is still pending approval.', 'new-user-approve' );
205
+ $pending_message = apply_filters( 'new_user_approve_pending_error', $pending_message );
206
+
207
+ $message = new WP_Error( 'pending_approval', $pending_message );
208
+ break;
209
+ case 'denied':
210
+ $denied_message = __( '<strong>ERROR</strong>: Your account has been denied access to this site.', 'new-user-approve' );
211
+ $denied_message = apply_filters( 'new_user_approve_denied_error', $denied_message );
212
+
213
+ $message = new WP_Error( 'denied_access', $denied_message );
214
+ break;
215
+ case 'approved':
216
+ $message = $userdata;
217
+ break;
218
+ }
219
+
220
+ return $message;
221
+ }
222
+
223
+ /**
224
+ * Get a status of all the users and save them using a transient
225
+ */
226
+ public function get_user_statuses() {
227
+ $valid_stati = $this->get_valid_statuses();
228
+ $user_status = get_transient( 'new_user_approve_user_statuses' );
229
+
230
+ if ( false === $user_status ) {
231
+ $user_status = array();
232
+
233
+ foreach ( $valid_stati as $status ) {
234
+ // Query the users table
235
+ if ( $status != 'approved' ) {
236
+ // Query the users table
237
+ $query = array( 'meta_key' => 'pw_user_status', 'meta_value' => $status, );
238
+ $wp_user_search = new WP_User_Query( $query );
239
+ } else {
240
+ // get all approved users and any user without a status
241
+ $query = array( 'meta_query' => array( 'relation' => 'OR', array( 'key' => 'pw_user_status', 'value' => 'approved', 'compare' => '=' ), array( 'key' => 'pw_user_status', 'value' => '', 'compare' => 'NOT EXISTS' ), ), );
242
+ $wp_user_search = new WP_User_Query( $query );
243
+ }
244
+
245
+ $user_status[$status] = $wp_user_search->get_results();
246
+ }
247
+
248
+ set_transient( 'new_user_approve_user_statuses', $user_status );
249
+ }
250
+
251
+ foreach ( $valid_stati as $status ) {
252
+ $user_status[$status] = apply_filters( 'new_user_approve_user_status', $user_status[$status], $status );
253
+ }
254
+
255
+ return $user_status;
256
+ }
257
+
258
+ /**
259
+ * Delete the transient storing all of the user statuses.
260
+ *
261
+ * @uses user_register
262
+ * @uses deleted_user
263
+ * @uses new_user_approve_approve_user
264
+ * @uses new_user_approve_deny_user
265
+ */
266
+ public function delete_new_user_approve_transient() {
267
+ delete_transient( 'new_user_approve_user_statuses' );
268
+ }
269
+
270
+ /**
271
+ * Display the stats on the WP dashboard. Will show 1 line with a count
272
+ * of users and their status.
273
+ *
274
+ * @uses rightnow_end
275
+ */
276
+ public function dashboard_stats() {
277
+ $user_status = $this->get_user_statuses();
278
+ ?>
279
+ <div>
280
+ <p><span style="font-weight:bold;"><a
281
+ href="<?php echo apply_filters( 'new_user_approve_dashboard_link', 'users.php' ); ?>"><?php _e( 'Users', 'new-user-approve' ); ?></a></span>:
282
+ <?php foreach ( $user_status as $status => $users ) :
283
+ print count( $users ) . " " . __( $status, 'new-user-approve' ) . "&nbsp;&nbsp;&nbsp;";
284
+ endforeach; ?>
285
+ </p>
286
+ </div>
287
+ <?php
288
+ }
289
+
290
+ /**
291
+ * The default notification message that is sent to site admin when requesting approval.
292
+ *
293
+ * @return string
294
+ */
295
+ public function default_notification_message() {
296
+ $message = __( 'USERNAME (USEREMAIL) has requested a username at SITENAME', 'new-user-approve' ) . "\n\n";
297
+ $message .= "SITEURL\n\n";
298
+ $message .= __( 'To approve or deny this user access to SITENAME go to', 'new-user-approve' ) . "\n\n";
299
+ $message .= "ADMINURL\n\n";
300
+
301
+ return $message;
302
+ }
303
+
304
+ /**
305
+ * Send an email to the admin to request approval. If there are already errors,
306
+ * just go back and let core do it's thing.
307
+ *
308
+ * @uses register_post
309
+ * @param string $user_login
310
+ * @param string $user_email
311
+ * @param object $errors
312
+ */
313
+ public function request_admin_approval_email( $user_login, $user_email, $errors ) {
314
+ if ( $errors->get_error_code() ) {
315
+ return;
316
+ }
317
+
318
+ // The blogname option is escaped with esc_html on the way into the database in sanitize_option
319
+ // we want to reverse this for the plain text arena of emails.
320
+ $blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
321
+
322
+ $default_admin_url = admin_url( 'users.php?s&pw-status-query-submit=Filter&new_user_approve_filter=pending&paged=1' );
323
+ $admin_url = apply_filters( 'new_user_approve_admin_link', $default_admin_url );
324
+
325
+ /* send email to admin for approval */
326
+ $message = apply_filters( 'new_user_approve_request_approval_message_default', $this->default_notification_message() );
327
+
328
+ $message = str_replace( 'USERNAME', $user_login, $message );
329
+ $message = str_replace( 'USEREMAIL', $user_email, $message );
330
+ $message = str_replace( 'SITENAME', $blogname, $message );
331
+ $message = str_replace( 'SITEURL', get_option( 'siteurl' ), $message );
332
+ $message = str_replace( 'ADMINURL', $admin_url, $message );
333
+
334
+ $message = apply_filters( 'new_user_approve_request_approval_message', $message, $user_login, $user_email );
335
+
336
+ $subject = sprintf( __( '[%s] User Approval', 'new-user-approve' ), $blogname );
337
+ $subject = apply_filters( 'new_user_approve_request_approval_subject', $subject );
338
+
339
+ $to = apply_filters( 'new_user_approve_email_admins', array( get_option( 'admin_email' ) ) );
340
+ $to = array_unique( $to );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
341
 
342
+ // send the mail
343
+ wp_mail( $to, $subject, $message, $this->email_message_headers() );
344
+ }
345
+
346
+ /**
347
+ * Create a new user after the registration has been validated. Normally,
348
+ * when a user registers, an email is sent to the user containing their
349
+ * username and password. The email does not get sent to the user until
350
+ * the user is approved when using the default behavior of this plugin.
351
+ *
352
+ * @uses register_post
353
+ * @param string $user_login
354
+ * @param string $user_email
355
+ * @param object $errors
356
+ */
357
+ public function create_new_user( $user_login, $user_email, $errors ) {
358
+ if ( $errors->get_error_code() ) {
359
+ return;
360
+ }
361
+
362
+ // create the user
363
+ $user_pass = wp_generate_password( 12, false );
364
+ $user_id = wp_create_user( $user_login, $user_pass, $user_email );
365
+ if ( !$user_id ) {
366
+ $errors->add( 'registerfail', sprintf( __( '<strong>ERROR</strong>: Couldn&#8217;t register you... please contact the <a href="mailto:%s">webmaster</a> !' ), get_option( 'admin_email' ) ) );
367
+ }
368
+ }
369
+
370
+ /**
371
+ * Admin approval of user
372
+ *
373
+ * @uses new_user_approve_approve_user
374
+ */
375
+ public function approve_user( $user_id ) {
376
+ $user = new WP_User( $user_id );
377
+
378
+ // password should only be reset for users that:
379
+ // * have never logged in
380
+ // * are just approved for the first time
381
+
382
+ // If the password has already been reset for this user,
383
+ // $password_reset will be a unix timestamp
384
+ $password_reset = get_user_meta( $user_id, 'pw_user_approve_password_reset' );
385
+
386
+ // Get the current user status. By default each user is given a pending
387
+ // status when the user is created (with this plugin activated). If the
388
+ // user was created while this plugin was not active, the user will not
389
+ // have a status set.
390
+ $user_status = get_user_meta( $user_id, 'pw_user_status' );
391
+
392
+ // Default behavior is to reset password
393
+ $bypass_password_reset = false;
394
+
395
+ // if no status is set, don't reset password
396
+ if ( empty( $user_status ) ) {
397
+ $bypass_password_reset = true;
398
+ }
399
+
400
+ // if the password has already been reset, absolutely bypass
401
+ if ( !empty( $password_reset ) ) {
402
+ $bypass_password_reset = true;
403
+ }
404
+
405
+ $bypass_password_reset = apply_filters( 'new_user_approve_bypass_password_reset', $bypass_password_reset );
406
+
407
+ if ( !$bypass_password_reset ) {
408
+ global $wpdb;
409
+
410
+ // reset password to know what to send the user
411
+ $new_pass = wp_generate_password( 12, false );
412
+ $data = array( 'user_pass' => md5( $new_pass ), 'user_activation_key' => '', );
413
+ $where = array( 'ID' => $user->ID, );
414
+ $wpdb->update( $wpdb->users, $data, $where, array( '%s', '%s' ), array( '%d' ) );
415
+
416
+ // Set up the Password change nag.
417
+ update_user_option( $user->ID, 'default_password_nag', true, true );
418
+
419
+ // Set this meta field to track that the password has been reset by
420
+ // the plugin. Don't reset it again.
421
+ update_user_meta( $user->ID, 'pw_user_approve_password_reset', time() );
422
+ }
423
+
424
+ wp_cache_delete( $user->ID, 'users' );
425
+ wp_cache_delete( $user->data->user_login, 'userlogins' );
426
+
427
+ // send email to user telling of approval
428
+ $user_login = stripslashes( $user->data->user_login );
429
+ $user_email = stripslashes( $user->data->user_email );
430
+
431
+ // format the message
432
+ $message = sprintf( __( 'You have been approved to access %s', 'new-user-approve' ), get_option( 'blogname' ) ) . "\r\n";
433
+ $message .= sprintf( __( 'Username: %s', 'new-user-approve' ), $user_login ) . "\r\n";
434
+ if ( !$bypass_password_reset ) {
435
+ $message .= sprintf( __( 'Password: %s', 'new-user-approve' ), $new_pass ) . "\r\n";
436
+ }
437
+ $message .= wp_login_url() . "\r\n";
438
+
439
+ $message = apply_filters( 'new_user_approve_approve_user_message', $message, $user );
440
+
441
+ $subject = sprintf( __( '[%s] Registration Approved', 'new-user-approve' ), get_option( 'blogname' ) );
442
+ $subject = apply_filters( 'new_user_approve_approve_user_subject', $subject );
443
+
444
+ // send the mail
445
+ wp_mail( $user_email, $subject, $message, $this->email_message_headers() );
446
+
447
+ // change usermeta tag in database to approved
448
+ update_user_meta( $user->ID, 'pw_user_status', 'approved' );
449
+
450
+ do_action( 'new_user_approve_user_approved', $user );
451
+ }
452
+
453
+ /**
454
+ * Admin denial of user
455
+ *
456
+ * @uses new_user_approve_deny_user
457
+ */
458
+ public function deny_user( $user_id ) {
459
+ $user = new WP_User( $user_id );
460
+
461
+ // send email to user telling of denial
462
+ $user_email = stripslashes( $user->user_email );
463
+
464
+ // format the message
465
+ $message = sprintf( __( 'You have been denied access to %s', 'new-user-approve' ), get_option( 'blogname' ) );
466
+ $message = apply_filters( 'new_user_approve_deny_user_message', $message, $user );
467
+
468
+ $subject = sprintf( __( '[%s] Registration Denied', 'new-user-approve' ), get_option( 'blogname' ) );
469
+ $subject = apply_filters( 'new_user_approve_deny_user_subject', $subject );
470
+
471
+ // send the mail
472
+ @wp_mail( $user_email, $subject, $message, $this->email_message_headers() );
473
+
474
+ // change usermeta tag in database to denied
475
+ update_user_meta( $user->ID, 'pw_user_status', 'denied' );
476
+
477
+ do_action( 'new_user_approve_user_denied', $user );
478
+ }
479
+
480
+ public function email_message_headers() {
481
+ $admin_email = get_option( 'admin_email' );
482
+ if ( empty( $admin_email ) ) {
483
+ $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
484
+ }
485
+
486
+ $from_name = get_option( 'blogname' );
487
+
488
+ $headers = array(
489
+ "From: \"{$from_name}\" <{$admin_email}>\n",
490
+ "Content-Type: text/plain; charset=\"" . get_option( 'blog_charset' ) . "\"\n",
491
+ );
492
+
493
+ $headers = apply_filters( 'new_user_approve_email_header', $headers );
494
+
495
+ return $headers;
496
+ }
497
+
498
+ /**
499
+ * Display a message to the user after they have registered
500
+ *
501
+ * @uses registration_errors
502
+ */
503
+ public function show_user_pending_message( $errors ) {
504
+ if ( !empty( $_POST['redirect_to'] ) ) {
505
+ // if a redirect_to is set, honor it
506
+ wp_safe_redirect( $_POST['redirect_to'] );
507
+ exit();
508
+ }
509
+
510
+ // if there is an error already, let it do it's thing
511
+ if ( $errors->get_error_code() ) {
512
+ return $errors;
513
+ }
514
+
515
+ $message = sprintf( __( 'An email has been sent to the site administrator. The administrator will review the information that has been submitted and either approve or deny your request.', 'new-user-approve' ) );
516
+ $message .= ' ';
517
+ $message .= sprintf( __( 'You will receive an email with instructions on what you will need to do next. Thanks for your patience.', 'new-user-approve' ) );
518
+ $message = apply_filters( 'new_user_approve_pending_message', $message );
519
+
520
+ $errors->add( 'registration_required', $message, 'message' );
521
+
522
+ $success_message = __( 'Registration successful.', 'new-user-approve' );
523
+ $success_message = apply_filters( 'new_user_approve_registration_message', $success_message );
524
+
525
+ login_header( __( 'Pending Approval', 'new-user-approve' ), '<p class="message register">' . $success_message . '</p>', $errors );
526
+ login_footer();
527
+
528
+ // an exit is necessary here so the normal process for user registration doesn't happen
529
+ exit();
530
+ }
531
+
532
+ /**
533
+ * Only give a user their password if they have been approved
534
+ *
535
+ * @uses lostpassword_post
536
+ */
537
+ public function lost_password() {
538
+ $is_email = strpos( $_POST['user_login'], '@' );
539
+ if ( $is_email === false ) {
540
+ $username = sanitize_user( $_POST['user_login'] );
541
+ $user_data = get_user_by( 'login', trim( $username ) );
542
+ } else {
543
+ $email = is_email( $_POST['user_login'] );
544
+ $user_data = get_user_by( 'email', $email );
545
+ }
546
+
547
+ if ( $user_data->pw_user_status && $user_data->pw_user_status != 'approved' ) {
548
+ wp_redirect( 'wp-login.php' );
549
+ exit();
550
+ }
551
+ }
552
+
553
+ /**
554
+ * Add message to login page saying registration is required.
555
+ *
556
+ * @uses login_message
557
+ * @param string $message
558
+ * @return string
559
+ */
560
+ public function welcome_user( $message ) {
561
+ if ( !isset( $_GET['action'] ) ) {
562
+ $welcome = sprintf( __( 'Welcome to %s. This site is accessible to approved users only. To be approved, you must first register.', 'new-user-approve' ), get_option( 'blogname' ) );
563
+ $welcome = apply_filters( 'new_user_approve_welcome_message', $welcome );
564
+
565
+ if ( !empty( $welcome ) ) {
566
+ $message .= '<p class="message register">' . $welcome . '</p>';
567
+ }
568
+ }
569
+
570
+ if ( isset( $_GET['action'] ) && $_GET['action'] == 'register' && !$_POST ) {
571
+ $instructions = sprintf( __( 'After you register, your request will be sent to the site administrator for approval. You will then receive an email with further instructions.', 'new-user-approve' ) );
572
+ $instructions = apply_filters( 'new_user_approve_register_instructions', $instructions );
573
+
574
+ if ( !empty( $instructions ) ) {
575
+ $message .= '<p class="message register">' . $instructions . '</p>';
576
+ }
577
+ }
578
+
579
+ return $message;
580
+ }
581
+
582
+ /**
583
+ * Give the user a status
584
+ *
585
+ * @uses user_register
586
+ * @param int $user_id
587
+ */
588
+ public function add_user_status( $user_id ) {
589
+ $status = 'pending';
590
+
591
+ // This check needs to happen when a user is created in the admin
592
+ if ( isset( $_REQUEST['action'] ) && 'createuser' == $_REQUEST['action'] ) {
593
+ $status = 'approved';
594
+ }
595
+ update_user_meta( $user_id, 'pw_user_status', $status );
596
+ }
597
  } // End Class
598
 
599
  function pw_new_user_approve() {
600
+ return pw_new_user_approve::instance();
601
  }
602
 
603
  pw_new_user_approve();
readme.txt CHANGED
@@ -4,7 +4,7 @@ Donate link: http://picklewagon.com/wordpress/new-user-approve/donate
4
  Tags: users, registration, sign up, user management
5
  Requires at least: 3.5.1
6
  Tested up to: 3.8.1
7
- Stable tag: 1.5.6
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -68,6 +68,9 @@ this feature.
68
 
69
  == Changelog ==
70
 
 
 
 
71
  = 1.5.6 =
72
  * add more translations
73
 
4
  Tags: users, registration, sign up, user management
5
  Requires at least: 3.5.1
6
  Tested up to: 3.8.1
7
+ Stable tag: 1.5.7
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
68
 
69
  == Changelog ==
70
 
71
+ = 1.5.7 =
72
+ * fix bug that was preventing bulk approval/denials
73
+
74
  = 1.5.6 =
75
  * add more translations
76