Skip to content

Commit 47d921e

Browse files
Copilotswissspidy
andcommitted
Improve URL detection logic and add test coverage for domain-only inputs
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 1079223 commit 47d921e

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

features/site.feature

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,3 +843,51 @@ Feature: Manage sites in a multisite installation
843843
Error: Could not find site with URL: http://example.com/nonexistent/
844844
"""
845845
And the return code should be 1
846+
847+
Scenario: Get site by domain without scheme
848+
Given a WP multisite subdirectory install
849+
850+
When I run `wp site create --slug=noscheme --porcelain`
851+
Then STDOUT should be a number
852+
And save STDOUT as {SITE_ID}
853+
854+
When I run `wp site get example.com/noscheme/ --field=blog_id`
855+
Then STDOUT should be:
856+
"""
857+
{SITE_ID}
858+
"""
859+
860+
Scenario: Get site by simple domain path
861+
Given a WP multisite subdirectory install
862+
863+
When I run `wp site create --slug=simplepath --porcelain`
864+
Then STDOUT should be a number
865+
And save STDOUT as {SITE_ID}
866+
867+
When I run `wp site get example.com/simplepath --field=blog_id`
868+
Then STDOUT should be:
869+
"""
870+
{SITE_ID}
871+
"""
872+
873+
Scenario: Get site by subdomain without scheme
874+
Given a WP multisite subdomain install
875+
876+
When I run `wp site create --slug=subdomain --porcelain`
877+
Then STDOUT should be a number
878+
And save STDOUT as {SITE_ID}
879+
880+
When I run `wp site get subdomain.example.com --field=blog_id`
881+
Then STDOUT should be:
882+
"""
883+
{SITE_ID}
884+
"""
885+
886+
Scenario: Get main site by domain
887+
Given a WP multisite install
888+
889+
When I run `wp site get example.com --field=blog_id`
890+
Then STDOUT should be:
891+
"""
892+
1
893+
"""

src/Site_Command.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -460,12 +460,12 @@ public function get( $args, $assoc_args ) {
460460
$site_arg = $args[0];
461461
$site = null;
462462

463-
// Check if the argument is a URL (contains :// or starts with www.)
464-
if ( false !== strpos( $site_arg, '://' ) || 0 === strpos( $site_arg, 'www.' ) ) {
465-
// Normalize scheme-less URLs starting with www. for proper parsing.
463+
// Check if the argument is a URL or a domain (non-numeric)
464+
if ( ! is_numeric( $site_arg ) ) {
465+
// Normalize URLs without a scheme for proper parsing.
466466
$url_to_parse = $site_arg;
467-
if ( 0 === strpos( $site_arg, 'www.' ) && false === strpos( $site_arg, '://' ) ) {
468-
$url_to_parse = 'http://' . $site_arg;
467+
if ( false === strpos( $url_to_parse, '://' ) ) {
468+
$url_to_parse = 'http://' . $url_to_parse;
469469
}
470470

471471
// Parse the URL to get domain and path

0 commit comments

Comments
 (0)