Created
August 25, 2020 16:58
-
-
Save lightningspirit/b3d4bc92594dc4824cc936d1e8f77968 to your computer and use it in GitHub Desktop.
Must-use WordPress plugin with a CLI command that resets passwords and generates a list of users with new passwords. It does not notify them. Plain simples user password reset mechanism that can be used for internal purposes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Password management made easy | |
* | |
* @author Move Your Digital, Inc. | |
* @package Mu-Passwords | |
* @version 1.0.0 | |
*/ | |
/* | |
This program is free software; you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation; either version 2 of the License, or | |
(at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program; if not, write to the Free Software | |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
*/ | |
class WP_Cli_Mu_Passwords { | |
/** | |
* Resets users passwords | |
* | |
* ## OPTIONS | |
* [--format=<format>] | |
* : Which format the data is used. | |
* --- | |
* default: table | |
* options: | |
* - csv | |
* - table | |
* - json | |
* - yaml | |
* --- | |
* | |
* [--roles=<roles>] | |
* : Optionally filters by roles | |
* | |
* [--email=<email>] | |
* : Optionally filter by email | |
* | |
* [--fields=<fields>] | |
* : Optionally display these filters | |
* --- | |
* default: ID,display_name,user_login,user_email | |
* --- | |
* | |
* ## EXAMPLES | |
* | |
* wp password reset --roles=subscriber,editor --format=csv | |
* | |
* @when after_wp_load | |
*/ | |
public function reset( $args, $params ) { | |
$query = []; | |
if ( isset( $params['roles'] ) ) { | |
$query['role__in'] = explode( ',', $params['roles'] ); | |
} | |
if ( isset( $params['email'] ) ) { | |
$users = [ | |
get_user_by( 'email', $params['email'] ) | |
]; | |
if ( isset( $params['roles'] ) ) { | |
WP_CLI::warning( 'roles parameter ignored as email parameter is set' ); | |
} | |
} else { | |
$users = get_users( $query ); | |
} | |
foreach ( $users as $user ) { | |
$user->new_user_pass = wp_generate_password(); | |
wp_set_password( $user->new_user_pass, $user->ID ); | |
} | |
WP_CLI\Utils\format_items( $params['format'], $users, array_merge( | |
explode( ',', $params['fields'] ), [ 'new_user_pass' ] | |
) ); | |
} | |
} | |
/** | |
* Fires once all must-use and network-activated plugins have loaded. | |
* | |
* @since 1.0.0 | |
*/ | |
add_action( 'muplugins_loaded', function () { | |
if ( defined( 'WP_CLI' ) && WP_CLI ) { | |
WP_CLI::add_command( 'password', 'WP_Cli_Mu_Passwords' ); | |
} | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment