Skip to content
Next Next commit
Add site generator command
  • Loading branch information
i-am-chitti committed Apr 26, 2024
commit 5f279bfff450680c7050f34b1d8bd2aadc2093ff
142 changes: 142 additions & 0 deletions src/Site_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,148 @@ public function create( $args, $assoc_args ) {
}
}

/**
* Generate some sites.
*
* Creates a specified number of new sites.
*
* ## OPTIONS
*
* [--count=<number>]
* : How many sites to generates?
* ---
* default: 100
* ---
*
* [--email=<email>]
* : Email for admin user. User will be created if none exists. Assignment to super admin if not included.
*
* [--network_id=<network-id>]
* : Network to associate new site with. Defaults to current network (typically 1).
*
* [--private]
* : If set, the new site will be non-public (not indexed)
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: progress
* options:
* - progress
* - ids
* ---
*
* ## EXAMPLES
*
* # Generate 10 sites.
* $ wp site generate --count=10
* Generating sites 100% [================================================] 0:01 / 0:04
*/
public function generate( $args, $assoc_args ) {
if ( ! is_multisite() ) {
WP_CLI::error( 'This is not a multisite installation.' );
}

global $wpdb, $current_site;

$defaults = [
'count' => 100,
'email' => '',
'network_id' => 1,
];

$assoc_args = array_merge( $defaults, $assoc_args );

// Network.
if ( ! empty( $assoc_args['network_id'] ) ) {
$network = $this->get_network( $assoc_args['network_id'] );
if ( false === $network ) {
WP_CLI::error( "Network with id {$assoc_args['network_id']} does not exist." );
}
} else {
$network = $current_site;
}

// Public.
$public = ! Utils\get_flag_value( $assoc_args, 'private' );

// Limit.
$limit = $assoc_args['count'];

// Email.
$email = sanitize_email( $assoc_args['email'] );
if ( empty( $email ) || ! is_email( $email ) ) {
$super_admins = get_super_admins();
$email = '';
if ( ! empty( $super_admins ) && is_array( $super_admins ) ) {
$super_login = reset( $super_admins );
$super_user = get_user_by( 'login', $super_login );
if ( $super_user ) {
$email = $super_user->user_email;
}
}
}

$user_id = email_exists( $email );
if ( ! $user_id ) {
$password = wp_generate_password( 24, false );
$user_id = wpmu_create_user( 'site-admin', $password, $email );

if ( false === $user_id ) {
WP_CLI::error( "Can't create user." );
} else {
User_Command::wp_new_user_notification( $user_id, $password );
}
}

$is_subdomain_install = is_subdomain_install();

$format = Utils\get_flag_value( $assoc_args, 'format', 'progress' );

$notify = false;
if ( 'progress' === $format ) {
$notify = Utils\make_progress_bar( 'Generating sites', $limit );
}

for ( $index = 1; $index <= $limit; $index++ ) {
$base = 'site' . $index;
$title = 'Site ' . $index;

$new_domain = '';
$path = '';

if ( $is_subdomain_install ) {
$new_domain = $base . '.' . preg_replace( '|^www\.|', '', $network->domain );
$path = $network->path;
} else {
$new_domain = $network->domain;
$path = $network->path . $base . '/';
}

$wpdb->hide_errors();
$title = wp_slash( $title );
$id = wpmu_create_blog( $new_domain, $path, $title, $user_id, [ 'public' => $public ], $network->id );
$wpdb->show_errors();
if ( ! is_wp_error( $id ) ) {
if ( ! is_super_admin( $user_id ) && ! get_user_option( 'primary_blog', $user_id ) ) {
update_user_option( $user_id, 'primary_blog', $id, true );
}
} else {
WP_CLI::error( $id->get_error_message() );
}

if ( 'progress' === $format ) {
$notify->tick();
} else {
WP_CLI::line( $id );
}
}

if ( 'progress' === $format ) {
$notify->finish();
}
}

/**
* Gets network data for a given id.
*
Expand Down