From 62861bc6986fd22711975036f21440a9ca9fecf8 Mon Sep 17 00:00:00 2001 From: Deepak Kumar Date: Fri, 26 Apr 2024 18:53:24 +0530 Subject: [PATCH] Add slug assoc arg --- src/Site_Command.php | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/src/Site_Command.php b/src/Site_Command.php index e71e349f..abe10d14 100644 --- a/src/Site_Command.php +++ b/src/Site_Command.php @@ -500,6 +500,9 @@ public function create( $args, $assoc_args ) { * default: 100 * --- * + * [--slug=] + * : Path for the new site. Subdomain on subdomain installs, directory on subdirectory installs. + * * [--email=] * : Email for admin user. User will be created if none exists. Assignment to super admin if not included. * @@ -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'] ); @@ -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." ); @@ -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; @@ -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();