Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated the up function to use the new generate_password function #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 38 additions & 10 deletions up.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,34 @@
*/
class WP_CLI_Up {

/**
* Generates a random password drawn from the defined set of characters.
* Taking from WP cores wp_generate_password function
*
* Uses wp_rand() is used to create passwords with far less predictability
* than similar native PHP functions like `rand()` or `mt_rand()`.
*
* @param int $length Optional. The length of password to generate. Default 12.
* @param bool $special_chars Optional. Whether to include standard special characters.
* Default true.
*
* @return string The random password.
*/
protected function generate_password($length = 12, $special_chars = true)
{
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
if ($special_chars) {
$chars .= '!@#$%^&*()';
}

$password = '';
for ($i = 0; $i < $length; $i++) {
$password .= substr($chars, wp_rand(0, strlen($chars) - 1), 1);
}

return $password;
}

/**
* Removes a site from the wp-cli-up multipass instance
*
Expand Down Expand Up @@ -141,16 +169,16 @@ public function add( $args, $assoc_args ) {

$username = $this->get_username_from_domain( $domain );

$defaults = [
'dbname' => $username,
'dbuser' => $username,
'dbpass' => base_convert( uniqid( 'pass', true ), 10, 20 ),
'dbprefix' => 'wp_',
'title' => $domain,
'admin_user' => 'admin',
'admin_password' => base_convert( uniqid( 'pass', true ), 10, 20 ),
'admin_email' => '[email protected]'
];
$defaults = [
'dbname' => $username,
'dbuser' => $username,
'dbpass' => $this->generate_password(16, true),
'dbprefix' => 'wp_',
'title' => $domain,
'admin_user' => 'admin',
'admin_password' => $this->generate_password(16, true),
'admin_email' => '[email protected]'
];
$args = array_merge( $defaults, $assoc_args );

$args['domain'] = $domain;
Expand Down