· 9 years ago · Jan 02, 2017, 04:12 AM
1<?php
2/*
3Plugin Name: Bulk Password Reset
4Plugin URI: http://rubenwoudsma.nl/bulk-password-reset
5Description: This plugin makes it possible to execute a bulk password reset for users within a specific group. You can send an activation key to all the users or perform a hard reset of the password.
6Author: Ruben Woudsma
7Version: 1.3.3
8Author URI: http://rubenwoudsma.nl
9*/
10if (!class_exists('bulk_password_reset')) {
11 class bulk_password_reset {
12 var $hook = 'bulk_password_reset';
13 var $longname = 'Bulk Password Reset';
14 var $shortname = 'Bulk Password Reset';
15 var $optionname = 'bpr_options';
16 //Class Functions
17 /**
18 * PHP 4 Compatible Constructor
19 */
20 function bulk_password_reset(){$this->__construct();}
21 /**
22 * PHP 5 Constructor
23 */
24 function __construct(){
25 //Language Setup
26 $locale = get_locale();
27 $mo = dirname(__FILE__) . "/languages/" . $this->hook . "-".$locale.".mo";
28 load_textdomain($this->hook, $mo);
29
30 //Actions
31 if(is_admin()) {
32 add_action('admin_menu', array(&$this, 'add_usermenu_item') );
33 add_action('admin_print_scripts', array(&$this, 'do_script') );
34 add_action('admin_print_styles', array(&$this, 'do_css') );
35 add_action('admin_head', array(&$this, 'config_page_head') );
36
37 // admin actions/filters for bulk_actions user menu
38 add_action('admin_footer', array(&$this, 'bpr_cust_bulk_admin_footer'));
39 add_action('load-edit.php', array(&$this, 'bpr_cust_bulk_action'));
40 add_action('load-users.php', array(&$this, 'bpr_cust_bulk_action'));
41 add_action('admin_notices', array(&$this, 'bpr_cust_bulk_admin_notices'));
42 }
43
44 //Filter + set upload folder
45 add_filter('upload_dir', array(&$this, 'set_correct_logfolder') );
46 $upload = wp_upload_dir();
47 remove_filter('upload_dir', array(&$this, 'set_correct_logfolder') );
48
49 }
50
51 function set_correct_logfolder($upload) {
52 $upload['subdir'] = '/bulk-password-reset' . $upload['subdir'];
53 $upload['path'] = $upload['basedir'] . $upload['subdir'];
54 $upload['url'] = $upload['baseurl'] . $upload['subdir'];
55 return $upload;
56 }
57
58 /**
59 * @desc Adds the page to the users sub menu
60 */
61 function add_usermenu_item() {
62 add_submenu_page('users.php', $this->longname, $this->shortname, 'administrator', $this->hook, array(&$this, 'bulk_password_reset_page') );
63 }
64
65 /**
66 * @desc Adds some code to the head of the settings/options page
67 */
68 function config_page_head() {
69 if(isset($_GET['page'])) {
70 if ($_GET['page'] == $this->hook) {
71 wp_enqueue_script('jquery');
72 ?>
73 <script type="text/javascript" charset="utf-8">
74 jQuery(document).ready(function(){
75 jQuery('#advancedsettings').change(function(){
76 if (jQuery('#advancedsettings').is(':checked')) {
77 jQuery('#advancedbprsettings').css("display","block");
78 if (jQuery('#uselogfile').is(':checked')) {
79 jQuery('#logfilesbpr').css("display","block");
80 }
81 } else {
82 jQuery('#advancedbprsettings').css("display","none");
83 jQuery('#logfilesbpr').css("display","none");
84 }
85 }).change();
86 jQuery('#uselogfile').change(function(){
87 if (jQuery('#uselogfile').is(':checked')) {
88 jQuery('#logfilesbpr').css("display","block");
89 } else {
90 jQuery('#logfilesbpr').css("display","none");
91 }
92 }).change();
93 });
94 </script>
95 <?php
96 }
97 }
98 }
99
100 /**
101 * @desc Adds settings/options page
102 */
103 function bulk_password_reset_page() {
104
105 $options = get_option($this->optionname);
106
107 if ( (isset($_POST['reset']) && $_POST['reset'] == "true") || !is_array($options) ) {
108 $this->set_defaults();
109 $options = get_option($this->optionname);
110 echo "<div class=\"updated\"><p>" . __('Bulk Password reset settings reset to default','bulk_password_reset') . "</p></div>\n";
111 }
112
113 if ( isset($_POST['submit']) ) {
114 if (!current_user_can('manage_options')) die(__('You cannot edit the plugin Bulk Password Reset for WordPress options.','bulk_password_reset'));
115 check_admin_referer('bprsettings-config');
116
117 //Text options
118 foreach (array('custommessage', 'customauthkey', 'custompassword') as $option_name) {
119 if (isset($_POST[$option_name]))
120 $options[$option_name] = $_POST[$option_name];
121 else
122 $options[$option_name] = '';
123 }
124
125 //Checkbox options
126 foreach (array('sendnotification', 'advancedsettings', 'hardreset', 'passwordnag') as $option_name) {
127 if (isset($_POST[$option_name]))
128 $options[$option_name] = true;
129 else
130 $options[$option_name] = false;
131 }
132
133 update_option($this->optionname, $options);
134 echo "<div id=\"updatemessage\" class=\"updated fade\"><p>" . __('Bulk Password Reset settings updated.', 'bulk_password_reset') . "</p></div>\n";
135 echo "<script type=\"text/javascript\">setTimeout(function(){jQuery('#updatemessage').hide('slow');}, 3000);</script>";
136
137 //Here we need to process al the users within the role groups
138 if (isset($_POST['dobulkreset'])) {
139 $err_msg = '';
140
141 //Validate if roles have been selected
142 if ( !isset($_POST['send_roles']) || !is_array($_POST['send_roles']) || empty($_POST['send_roles']) ) {
143 $err_msg = __('No reset has been executed as no group was selected. You need to select at least one group to let the plugin do his work.', 'bulk_password_reset');
144 }
145 else {
146 $send_roles = $_POST['send_roles'];
147
148 $user_ID = get_current_user_id();
149 $recipients = $this->get_recipients_from_roles($send_roles, $user_ID);
150
151 if (empty($recipients)) {
152 $err_msg = __('No recipients where found in the selected groups', 'bulk_password_reset');
153 }
154 else {
155 //do mail processing with for each loop
156 foreach ($recipients as $recipient) {
157 // no do sending for each recipient
158 if ( $options['hardreset'] ) {
159 $errors = $this->reset_password($recipient->id);
160 } else {
161 $errors = $this->sent_activation_mail($recipient->id);
162 }
163 }
164
165 if ( is_wp_error($errors) ) {
166 $err_msg = $errors->get_error_message();
167 }
168 }
169 }
170
171 // If error, show error message else show success message
172 if ( $err_msg !='' ) {
173 // Show error message
174 echo "<div id=\"errormessage\" class=\"error\"><p>" . $err_msg . "</p></div>\n";
175 echo "<script type=\"text/javascript\">setTimeout(function(){jQuery('#errormessage').hide('slow');}, 6000);</script>";
176 } else {
177 //Show success message
178 echo "<div id=\"successmessage\" class=\"updated fade\"><p>" . __('Successfully reset the password for the selected role(s).', 'bulk_password_reset') . "</p></div>\n";
179 echo "<script type=\"text/javascript\">setTimeout(function(){jQuery('#successmessage').hide('slow');}, 6000);</script>";
180 }
181
182 }
183
184 }
185
186 ?>
187 <div class="wrap">
188 <h2><?php _e('Bulk Password Reset','bulk_password_reset') ?></h2>
189
190 <!-- Main section -->
191 <div class="postbox-container" style="width:70%;">
192 <div class="metabox-holder">
193 <div class="meta-box-sortables">
194 <form action="" method="post" id="bpr-config">
195 <?php wp_nonce_field('bprsettings-config'); ?>
196 <div id="bprsettings" class="postbox">
197 <div class="handlediv" title="Click to toggle"><br /></div>
198 <h3 class="hndle"><span><?php _e('Bulk Password Reset Settings','bulk_password_reset'); ?> </span></h3>
199 <div class="bprbox">
200 <table class="form-table">
201 <tr>
202 <td colspan="2"><?php _e('With the options down below you can select groups for which you want to do a password reset','bulk_password_reset') ?></td>
203 </tr>
204 <tr>
205 <th scope="row" valign="top"><label for="send_roles"><?php _e('Recipients', 'bulk_password_reset') ?>
206 <br/><br/>
207 <small><?php _e('You can select multiple groups by pressing the CTRL key.', 'bulk_password_reset') ?></small>
208 <br/><br/>
209 <small><?php _e('Only the groups having at least one user appear here.', 'bulk_password_reset') ?></small></label></th>
210 <td>
211 <select id="send_roles" name="send_roles[]" multiple="multiple" size="8" style="width: 554px; height: 150px;">
212 <?php
213 $roles = $this->get_roles($user_ID);
214 foreach ($roles as $key => $value) {
215 $name = translate_user_role($value );
216 ?>
217 <option value="<?php echo $key; ?>" <?php
218 echo (in_array($key, (array) $send_roles) ? ' selected="selected"' : '');?>>
219 <?php echo __('Role', 'bulk_password_reset') . ' - ' . $name; ?>
220 </option>
221 <?php
222 }
223 ?>
224 </select>
225 </td>
226 </tr>
227 <tr>
228 <th valign="top" scrope="row"><label for="dobulkreset"><?php _e('Do the bulk reset:', 'bulk_password_reset') ?></label><br/><small><?php _e('Activate this check box when you actually would like to do the reset, else only the settings are saved.', 'bulk_password_reset') ?></small></th>
229 <td valign="top"><input type="checkbox" id="dobulkreset" name="dobulkreset" /></td>
230 </tr>
231 <tr>
232 <th valign="top" scrope="row"><label for="advancedsettings"><?php _e('Show advanced settings:', 'bulk_password_reset') ?></label><br/><small><?php _e('Only advised for users who want to change the default reset options', 'bulk_password_reset') ?></small></th>
233 <td valign="top"><input type="checkbox" id="advancedsettings" name="advancedsettings" <?php echo checked($options['advancedsettings'],true,false); ?>/></td>
234 </tr>
235 </table>
236 </div>
237 </div>
238
239 <!-- Advanced settings -->
240 <div id="advancedbprsettings" class="postbox">
241 <div class="handlediv" title="Click to toggle"><br /></div>
242 <h3 class="hndle"><span><?php _e('Advanced Settings', 'bulk_password_reset') ?></span></h3>
243 <div class="bprbox">
244 <table class="form-table">
245 <tr>
246 <th valign="top" scrope="row"><label for="hardreset"><?php _e('Direct password reset', 'bulk_password_reset') ?>:</label><br/><small><?php _e('Not recommended, as this immediately changes the password of the users.','bulk_password_reset') ?></small></th>
247 <td valign="top"><input type="checkbox" id="hardreset" name="hardreset" <?php echo checked($options['hardreset'],true,false); ?> /></td>
248 </tr>
249 <tr>
250 <th valign="top" scrope="row"><label for="sendnotification"><?php _e('E-mail notification', 'bulk_password_reset') ?>:</label><br/><small><?php _e('Not recommended to deactivate, as this change the default reset functionality.','bulk_password_reset') ?></small></th>
251 <td valign="top"><input type="checkbox" id="sendnotification" name="sendnotification" <?php echo checked($options['sendnotification'],true,false); ?> /></td>
252 </tr>
253 <tr>
254 <th valign="top" scrope="row"><label for="passwordnag"><?php _e('Password nag', 'bulk_password_reset') ?>:</label><br/><small><?php _e('Not recommended to deactivate, as this will display a note to users to change their password.','bulk_password_reset') ?></small></th>
255 <td valign="top"><input type="checkbox" id="passwordnag" name="passwordnag" <?php echo checked($options['passwordnag'],true,false); ?> /></td>
256 </tr>
257 <tr>
258 <th valign="top" scrope="row"><label for="uselogfile"><?php _e('Use logfile?', 'bulk_password_reset') ?>:</label><br/><small><?php _e('Write the result of the password reset to a text log file for track and trace','bulk_password_reset') ?></small></th>
259 <td valign="top"><input type="checkbox" id="uselogfile" name="uselogfile" <?php echo checked($options['uselogfile'],true,false); ?> /></td>
260 </tr>
261 <tr>
262 <th valign="top" scrope="row"><label for="custompassword"><?php _e('Set a custom password:', 'bulk_password_reset') ?></label></th>
263 <td valign="top"><input type="text" id="custompassword" name="custompassword" size="30" value="<?php echo $options['custompassword']; ?>"/></td>
264 </tr>
265 <tr>
266 <th valign="top" scrope="row"><label for="customauthkey"><?php _e('Set a custom authentication key:', 'bulk_password_reset') ?></label></th>
267 <td valign="top"><input type="text" id="customauthkey" name="customauthkey" size="30" value="<?php echo $options['customauthkey']; ?>"/></td>
268 </tr>
269 <tr>
270 <th valign="top" scrope="row"><label for="custommessage"><?php _e('Additional information for in the e-mail body:', 'bulk_password_reset') ?></label></th>
271 <td valign="top"><textarea rows="8" cols="70" name="custommessage" id="custommessage"><?php echo $options['custommessage']; ?></textarea></td>
272 </tr>
273 </table>
274 </div>
275 </div>
276
277 <!-- Show logfiles for download -->
278 <div id="logfilesbpr" class="postbox">
279 <div class="handlediv" title="Click to toggle"><br /></div>
280 <h3 class="hndle"><span><?php _e('Bulk Password Reset - Log files', 'bulk_password_reset') ?></span></h3>
281 <div class="bprbox">
282 <p><?php _e('Down below you will find an overview of the log files belonging to the earlier resets', 'bulk_password_reset') ?></p>
283 <?php
284 //path to directory to scan
285 $directory = $upload['path'];
286
287 //get all files with a .log extension.
288 $logfiles = glob($directory . "*.log");
289
290 //print each file name
291 foreach($logfiles as $logfile)
292 {
293 echo $logfile;
294 }
295
296 ?>
297 </div>
298 </div>
299 <!-- End logfiles section -->
300
301 <div class="submit"><input type="submit" class="button-primary" name="submit" value="<?php _e('Save the bulk Password Reset settings and reset passwords »', 'bulk_password_reset') ?>" /></div>
302
303 </form> <form action="" method="post"> <input type="hidden" name="reset" value="true"/> <div class="submit"><input type="submit" value="<?php _e('Reset Default Settings »', 'bulk_password_reset'); ?>" /></div> </form> </div> </div> </div> <div class="postbox-container" style="width:20%;"> <div class="metabox-holder"> <div class="meta-box-sortables"> <?php $this->plugin_like(); $this->plugin_support(); $this->news(); ?> </div> <br/><br/><br/> </div> </div> </div> <?php } /** * @desc Set the default options used within the plugin */ function set_defaults() { $options = get_option($this->optionname); $options['custommessage'] = ''; $options['customauthkey'] = ''; $options['custompassword'] = ''; $options['sendnotification'] = true; $options['advancedsettings'] = false; $options['hardreset'] = false; $options['passwordnag'] = true; $options['uselogfile'] = false; update_option($this->optionname,$options); }
304 /** * @desc Create a "plugin like" box. */ function plugin_like() { $content = '<p>'.__('Why not do any or all of the following:','bulk_password_reset').'</p>'; $content .= '<ul>'; $content .= '<li><a href="http://rubenwoudsma.nl/bulk-password-reset">'.__('Link to it so other folks can find out about it.','bulk_password_reset').'</a></li>'; $content .= '<li><a href="http://wordpress.org/extend/plugins/bulk_password_reset/">'.__('Give it a good rating on WordPress.org.','bulk_password_reset').'</a></li>'; $content .= '<li><a href="http://wordpress.org/extend/plugins/bulk-password-reset/">'.__('Visit the plugin website.','bulk_password_reset').'</a></li>'; $content .= '</ul>'; $this->postbox($this->hook.'like', 'Like this plugin?', $content); } /** * @desc Info box with link to the support forums. */ function plugin_support() { $content = '<p>'.__('If you have any problems with this plugin or good ideas for improvements or new features, please talk about them in the','bulk_password_reset').' <a href="http://wordpress.org/tags/bulk_password_reset">'.__("Support forums",'bulk_password_reset').'</a>.</p>'; $this->postbox($this->hook.'support', 'Need support?', $content); }
305 /**
306 * @desc Box with latest news from rubenwoudsma.nl
307 */
308 function news() {
309 require_once(ABSPATH.WPINC.'/feed.php');
310 //Get the SimplePie feed from feedburner! $rss = fetch_feed( 'http://feeds.feedburner.com/rubenwoudsma' );
311 if ( ! is_wp_error( $rss ) ) { // Checks that the object is created correctly $maxitems = $rss->get_item_quantity( 5 ); $rss_items = $rss->get_items( 0, $maxitems ); } $content = '<ul>'; if ( $maxitems == 0 ) { $content .= '<li class="brp-authorinfo">' . __('Nothing to say...','bulk_password_reset') . '</li>'; } else { foreach ( $rss_items as $item ) { $content .= '<li class="brp-authorinfo">'; $content .= '<a class="rsswidget" href="'.esc_url( $item->get_permalink() ).'">'. esc_html( $item->get_title() ) .'</a> '; $content .= '</li>'; } $content .= '<li class="rss"><a href="http://rubenwoudsma.nl/feed/">Subscribe with RSS</a></li>'; } $content .= '</ul>'; $this->postbox('rubenlatest', __('Latest news from rubenwoudsma.nl','bulk_password_reset'), $content); } /** * @desc Create a postbox widget */ function postbox($id, $title, $content) { ?> <div id="<?php echo $id; ?>" class="postbox"> <div class="handlediv" title="<?php _e('Click to toggle','bulk_password_reset') ?>"><br /></div> <h3 class="hndle"><span><?php echo $title; ?></span></h3> <div class="bprbox"> <?php echo $content; ?> </div> </div> <?php } /** * @desc Load the plugin related css */ function do_css() { if ( isset( $_GET['page'] ) && $_GET['page'] == $this->hook ) { wp_enqueue_style('dashboard'); wp_enqueue_style('thickbox'); wp_enqueue_style('global'); wp_enqueue_style('wp-admin'); wp_enqueue_style('bprextra-admin-css', plugin_dir_url( __FILE__ ) . 'bulk_password_reset.css'); } } /** * @desc Load the plugin related javascript */ function do_script() { if ( isset( $_GET['page'] ) && $_GET['page'] == $this->hook ) { wp_enqueue_script('postbox'); wp_enqueue_script('dashboard'); wp_enqueue_script('thickbox'); wp_enqueue_script('media-upload'); } } /** * @desc Retrieve the roles from the users */ function get_roles( $exclude_id='') { $roles = array(); $wp_roles = new WP_Roles();
312 foreach ($wp_roles->get_names() as $key => $value) { $users_in_role = $this->get_recipients_from_roles(array($key), $exclude_id); if (!empty($users_in_role)) { $roles[$key] = $value; } } return $roles; } /** * @desc Retrieve the users from the roles */ function get_recipients_from_roles($roles, $exclude_id='') { global $wpdb; if (empty($roles)) { return array(); }
313 // Build role filter for the list of roles
314 $role_count = count($roles);
315 $capability_filter = '';
316 for ($i=0; $i<$role_count; $i++) {
317 $capability_filter .= "meta_value like '%" . $roles[$i] . "%'";
318 if ($i!=$role_count-1) {
319 $capability_filter .= ' OR ';
320 }
321 }
322 $users = $wpdb->get_results(
323 "SELECT id, user_login, user_email, display_name "
324 . "FROM $wpdb->usermeta, $wpdb->users "
325 . "WHERE "
326 . " (user_id = id) "
327 . ( $exclude_id!='' ? ' AND (id<>' . $exclude_id . ')' : '' )
328 . " AND (meta_key = '" . $wpdb->prefix . "capabilities') "
329 . " AND (" . $capability_filter . ") " );
330 return $users;
331 }
332 /**
333 * @desc Write message to debug.log based upon wp-config.php WP_DEBUG setting
334 */
335 function log_msg ( $message ) {
336 //Only write to debug.log is WP_DEBUG is set
337 if( WP_DEBUG === true ){
338 //Check to see if array needs to be written, then use print_r
339 if( is_array( $message ) || is_object( $message ) ){
340 error_log( print_r( $message, true ) );
341 } else {
342 error_log( $message );
343 }
344 }
345 }
346 /**
347 * @desc Sent the activation email to the users. Function copied from WP core and adjusted.
348 */
349 function sent_activation_mail($user_id) {
350 global $wpdb, $wp_hasher;
351 $options = get_option($this->optionname);
352 $user_data = get_user_by('id', $user_id); // redefining user_login ensures we return the right case in the email
353 $user_login = $user_data->user_login;
354 $user_email = $user_data->user_email;
355 //$key = $wpdb->get_var($wpdb->prepare("SELECT user_activation_key FROM $wpdb->users WHERE user_login = %s", $user_login)); //if ( empty($key) ) {
356 if ( isset($options['customauthkey']) && $options['customauthkey'] != "" ) { $key = $options['customauthkey']; } else { // Generate something random for a key... $key = wp_generate_password(20, false); }
357 // Now insert the key, hashed, into the DB.
358 if ( empty( $wp_hasher ) ) {
359 require_once( ABSPATH . WPINC . '/class-phpass.php' );
360 $wp_hasher = new PasswordHash( 8, true );
361 }
362 $hashed = time() . ':' . $wp_hasher->HashPassword( $key );
363 // Now insert the new md5 key into the db
364 $wpdb->update($wpdb->users, array('user_activation_key' => $hashed), array('user_login' => $user_login));
365 //}
366 if ( $options['sendnotification'] ) {
367 if ( isset($options['custommessage']) && $options['custommessage'] != "" ) {
368 $message = $options['custommessage'] . "\r\n\r\n";
369 } else {
370 $message = '';
371 }
372 $message .= __('Hello,', 'bulk_password_reset') . "\r\n\r\n";
373 $message .= sprintf(__('The site administrator of %s has automatically requested to changes your password due to violation issues.', 'bulk_password_reset'), get_option('siteurl')) . "\r\n\r\n";
374 $message .= sprintf(__('User name: %s', 'bulk_password_reset'), $user_login) . "\r\n\r\n";
375 $message .= __('To reset your password visit the following address and change your password.', 'bulk_password_reset') . "\r\n\r\n";
376 $message .= network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user_login), 'login') . "\r\n";
377 $message .= __('Kind regards,', 'bulk_password_reset') . "\r\n\r\n";
378 $message .= __('The admin team of the website', 'bulk_password_reset');
379 if ( is_multisite() )
380 $blogname = $GLOBALS['current_site']->site_name;
381 else
382 $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
383 $title = sprintf(__('[%s] Password Reset Request', 'bulk_password_reset'), $blogname);
384 if ( $message && !wp_mail($user_email, $title, $message) ) {
385 die('<p>' . __('The email could not be sent.', 'bulk_password_reset') . "<br />\n" . __('Possible reason: your host may have disabled the mail() function...', 'bulk_password_reset') . '</p>');
386 $this->log_msg ('ERROR: Not able to sent activation message to ' . $user_login);
387 } else {
388 $this->log_msg ('INFO: Successfully sent activation message to ' . $user_login);
389 }
390 }
391 return true; }
392 /**
393 * @desc Reset the users password. Function copied and adjusted from WP core.
394 */
395 function reset_password($user_id) {
396 global $wpdb;
397 $options = get_option($this->optionname);
398 $user = get_user_by('id', $user_id);
399 if ( empty( $user ) ) return new WP_Error('invalid_user', __('Invalid user login supplied', 'bulk_password_reset')); if ( isset($options['custompassword']) && $options['custompassword'] != "" ) { $new_pass = $options['custompassword']; } else { // Generate something random for a password... $new_pass = wp_generate_password(); }
400 wp_set_password($new_pass, $user->ID);
401 if ( $options['passwordnag'] ) { update_user_meta($user->ID, 'default_password_nag', true); //Set up the Password change nag. }
402 if ( $options['sendnotification'] ) { if ( isset($options['custommessage']) && $options['custommessage'] != "" ) { $message = $options['custommessage'] . "\r\n\r\n"; } else { $message = ''; } $message .= sprintf(__('The site administrator of %s has automatically changed your password.', 'bulk_password_reset'), get_option('siteurl')) . "\r\n\r\n"; $message .= sprintf(__('Username: %s', 'bulk_password_reset'), $user->user_login) . "\r\n"; $message .= sprintf(__('Password: %s', 'bulk_password_reset'), $new_pass) . "\r\n"; $message .= network_site_url('wp-login.php', 'login') . "\r\n"; $title = sprintf(__('[%s] Your new password', 'bulk_password_reset'), get_option('blogname'));
403 if ( $message && !wp_mail($user->user_email, $title, $message) ) {
404 die('<p>' . __('The e-mail could not be sent.', 'bulk_password_reset') . "<br />\n" . __('Possible reason: your host may have disabled the mail() function...', 'bulk_password_reset') . '</p>');
405 $this->log_msg ('ERROR: Not able to sent reset password message to ' . $user->user_login);
406 } else { $this->log_msg ('INFO: Successfully sent reset password message to ' . $user->user_login); } }
407 return true;
408 }
409 /**
410 * Step 1: add the custom Bulk Action to the select menus
411 */
412 function bpr_cust_bulk_admin_footer() {
413 $screen = get_current_screen();
414 if ( $screen->id != "users" ) // Only add to users.php page
415 return;
416 ?>
417 <script type="text/javascript">
418 jQuery(document).ready(function() {
419 jQuery('<option>').val('perform_reset').text('<?php _e('Execute password reset', 'bulk_password_reset')?>').appendTo("select[name='action']");
420 jQuery('<option>').val('perform_reset').text('<?php _e('Execute password reset', 'bulk_password_reset')?>').appendTo("select[name='action2']");
421 });
422 </script>
423 <?php
424 }
425/**
426 * Step 2: handle the custom Bulk Action
427 *
428 * Based on the post http://wordpress.stackexchange.com/questions/29822/custom-bulk-action
429 */
430 function bpr_cust_bulk_action() {
431 // get the action
432 $wp_list_table = _get_list_table('WP_Users_List_Table'); // depending on your resource type this could be WP_Posts_List_Table, WP_Comments_List_Table, etc
433 $action = $wp_list_table->current_action();
434 $allowed_actions = array("perform_reset");
435 if(!in_array($action, $allowed_actions)) return;
436 // make sure ids are submitted. depending on the resource type, this may be 'media' or 'ids' if(isset($_REQUEST['users'])) {
437 $user_ids = array_map('intval', $_REQUEST['users']);
438 } if(empty($user_ids)) return;
439 // this is based on wp-admin/users.php
440 $sendback = remove_query_arg( array('resetted', 'deleted', 'users'), wp_get_referer() ); // ???????
441 if ( ! $sendback )
442 $sendback = admin_url( "users.php" ); $pagenum = $wp_list_table->get_pagenum();
443 $sendback = add_query_arg( 'paged', $pagenum, $sendback );
444 switch($action) { case 'perform_reset':
445 $resetted = 0;
446 foreach( $user_ids as $user_id ) { if ( !$this->reset_password($user_id) )
447 wp_die( __('Error resetting password of user.') ); $resetted++; }
448 $sendback = add_query_arg( array('resetted' => $resetted, 'ids' => join(',', $user_ids) ), $sendback );
449 break;
450 default: return;
451 }
452 $sendback = remove_query_arg( array('action', 'action2', 'new_role', '_status', 'bulk_edit'), $sendback );
453 wp_redirect($sendback); exit();
454 }
455 /**
456 * Step 3: display an admin notice on the user page after resetting password(s)
457 */
458 function bpr_cust_bulk_admin_notices() {
459 global $pagenow;
460 if($pagenow == 'users.php' && isset($_REQUEST['resetted']) && (int) $_REQUEST['resetted']) { $message = sprintf( _n( 'Password reset has been sent.', '%s password resets have been sent.', $_REQUEST['resetted'] ), number_format_i18n( $_REQUEST['resetted'] ) );
461 echo "<div class=\"updated\"><p>{$message}</p></div>";
462 }
463 }
464 } //End Class
465} //End if class exists statement
466//instantiate the class
467if (class_exists('bulk_password_reset')) {
468 $bulk_password_reset_var = new bulk_password_reset();
469}
470?>