Skip to content

Commit 8a75d2d

Browse files
Copilotswissspidy
andcommitted
Address code review feedback - improve validation and slug derivation
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 4a3f2f4 commit 8a75d2d

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

features/site-create.feature

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,29 @@ Feature: Create a new site on a WP multisite
163163
"""
164164
And the return code should be 1
165165
166+
Scenario: Error when numeric-only domain is provided without slug
167+
Given a WP multisite subdomain install
168+
169+
When I try `wp site create --site-url=http://123.example.com`
170+
Then STDERR should be:
171+
"""
172+
Error: Could not derive a valid slug from the domain. Please provide --slug explicitly.
173+
"""
174+
And the return code should be 1
175+
176+
Scenario: Create site with different domain in subdirectory multisite
177+
Given a WP multisite subdirectory install
178+
179+
When I run `wp site create --site-url=http://custom.example.com/mypath/`
180+
Then STDOUT should contain:
181+
"""
182+
Success: Site 2 created:
183+
"""
184+
And STDOUT should contain:
185+
"""
186+
://custom.example.com/mypath/
187+
"""
188+
166189
Scenario: Preserve existing slug behavior
167190
Given a WP multisite subdomain install
168191

src/Site_Command.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -468,15 +468,22 @@ public function create( $args, $assoc_args ) {
468468
// For subdomain installs, use the first part of the domain as the base
469469
$domain_parts = explode( '.', $custom_domain );
470470
$base = $domain_parts[0];
471+
472+
// Validate that the derived base is suitable for use as a slug
473+
if ( empty( $base ) || is_numeric( $base ) ) {
474+
WP_CLI::error( 'Could not derive a valid slug from the domain. Please provide --slug explicitly.' );
475+
}
471476
} else {
472477
// For subdirectory installs, use the path as the base
473478
$base = trim( $custom_path, '/' );
474479
// Use the last part of the path if there are multiple segments
475-
$path_parts = explode( '/', $base );
476-
$base = end( $path_parts );
477-
// If base is empty (root path), generate a random one
480+
if ( ! empty( $base ) ) {
481+
$path_parts = explode( '/', $base );
482+
$base = $path_parts[ count( $path_parts ) - 1 ];
483+
}
484+
// If base is empty (root path), generate an auto slug
478485
if ( empty( $base ) ) {
479-
$base = 'site-' . wp_generate_password( 8, false );
486+
$base = 'auto-' . time();
480487
}
481488
}
482489
} else {

0 commit comments

Comments
 (0)