Skip to content

Commit

Permalink
Add slug assoc arg
Browse files Browse the repository at this point in the history
  • Loading branch information
i-am-chitti committed Apr 26, 2024
1 parent 946ad54 commit 62861bc
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions src/Site_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,9 @@ public function create( $args, $assoc_args ) {
* default: 100
* ---
*
* [--slug=<slug>]
* : Path for the new site. Subdomain on subdomain installs, directory on subdirectory installs.
*
* [--email=<email>]
* : Email for admin user. User will be created if none exists. Assignment to super admin if not included.
*
Expand Down Expand Up @@ -535,10 +538,27 @@ public function generate( $args, $assoc_args ) {
'count' => 100,
'email' => '',
'network_id' => 1,
'slug' => 'site',
];

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

// Base.
$base = $assoc_args['slug'];
if ( preg_match( '|^([a-zA-Z0-9-])+$|', $base ) ) {
$base = strtolower( $base );
}

$is_subdomain_install = is_subdomain_install();
// If not a subdomain install, make sure the domain isn't a reserved word
if ( ! $is_subdomain_install ) {
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Calling WordPress native hook.
$subdirectory_reserved_names = apply_filters( 'subdirectory_reserved_names', [ 'page', 'comments', 'blog', 'files', 'feed' ] );
if ( in_array( $base, $subdirectory_reserved_names, true ) ) {
WP_CLI::error( 'The following words are reserved and cannot be used as blog names: ' . implode( ', ', $subdirectory_reserved_names ) );
}
}

// Network.
if ( ! empty( $assoc_args['network_id'] ) ) {
$network = $this->get_network( $assoc_args['network_id'] );
Expand Down Expand Up @@ -572,7 +592,7 @@ public function generate( $args, $assoc_args ) {
$user_id = email_exists( $email );
if ( ! $user_id ) {
$password = wp_generate_password( 24, false );
$user_id = wpmu_create_user( 'site-admin', $password, $email );
$user_id = wpmu_create_user( $base . '-admin', $password, $email );

if ( false === $user_id ) {
WP_CLI::error( "Can't create user." );
Expand All @@ -581,8 +601,6 @@ public function generate( $args, $assoc_args ) {
}
}

$is_subdomain_install = is_subdomain_install();

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

$notify = false;
Expand All @@ -591,18 +609,15 @@ public function generate( $args, $assoc_args ) {
}

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

$new_domain = '';
$path = '';
$current_base = $base . $index;
$title = ucfirst( $base ) . ' ' . $index;

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

$wpdb->hide_errors();
Expand Down

0 comments on commit 62861bc

Please sign in to comment.