diff --git a/src/wp-admin/includes/network.php b/src/wp-admin/includes/network.php index 15c58c65a3148..504ae962eadc8 100644 --- a/src/wp-admin/includes/network.php +++ b/src/wp-admin/includes/network.php @@ -150,7 +150,8 @@ function network_step1( $errors = false ) { } // Strip standard port from hostname. - $hostname = preg_replace( '/(?::80|:443)$/', '', get_clean_basedomain() ); + $port_pattern = '/(?:' . implode( '|', array_map( 'preg_quote', ms_default_port_suffixes() ) ) . ')$/'; + $hostname = preg_replace( $port_pattern, '', get_clean_basedomain() ); echo '
'; diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index 9a688b9866ce0..b8085ef2f3b0d 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -759,6 +759,28 @@ function is_serialized_string( $data ) { } } +/** + * Retrieves the port suffixes considered "default" for a Multisite domain. + * + * These port suffixes are stripped from a hostname during the Multisite + * bootstrap process, and used to suggest a default network hostname when + * setting up a new network. + * + * @since 7.2.0 + * + * @return string[] Array of default port suffixes, e.g. array( ':80', ':443' ). + */ +function ms_default_port_suffixes() { + /** + * Filters the port suffixes considered "default" for a Multisite domain. + * + * @since 7.2.0 + * + * @param string[] $port_suffixes Array of default port suffixes, e.g. array( ':80', ':443' ). + */ + return apply_filters( 'ms_default_port_suffixes', array( ':80', ':443' ) ); +} + /** * Retrieves post title from XML-RPC XML. * diff --git a/src/wp-includes/ms-settings.php b/src/wp-includes/ms-settings.php index 2e3c3dcfa739a..e161ce08e9426 100644 --- a/src/wp-includes/ms-settings.php +++ b/src/wp-includes/ms-settings.php @@ -60,12 +60,12 @@ if ( ! isset( $current_site ) || ! isset( $current_blog ) ) { $domain = strtolower( stripslashes( $_SERVER['HTTP_HOST'] ?? '' ) ); - if ( str_ends_with( $domain, ':80' ) ) { - $domain = substr( $domain, 0, -3 ); - $_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -3 ); - } elseif ( str_ends_with( $domain, ':443' ) ) { - $domain = substr( $domain, 0, -4 ); - $_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -4 ); + foreach ( ms_default_port_suffixes() as $port_suffix ) { + if ( str_ends_with( $domain, $port_suffix ) ) { + $domain = substr( $domain, 0, -strlen( $port_suffix ) ); + $_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -strlen( $port_suffix ) ); + break; + } } $path = stripslashes( $_SERVER['REQUEST_URI'] ); diff --git a/tests/phpunit/tests/functions/msDefaultPortSuffixes.php b/tests/phpunit/tests/functions/msDefaultPortSuffixes.php new file mode 100644 index 0000000000000..5b7d52e353a9b --- /dev/null +++ b/tests/phpunit/tests/functions/msDefaultPortSuffixes.php @@ -0,0 +1,32 @@ +assertSame( array( ':80', ':443' ), ms_default_port_suffixes() ); + } + + /** + * @ticket 55488 + */ + public function test_result_is_filterable() { + add_filter( 'ms_default_port_suffixes', array( $this, 'filter_port_suffixes' ) ); + + $this->assertSame( array( ':8080' ), ms_default_port_suffixes() ); + } + + public function filter_port_suffixes() { + return array( ':8080' ); + } +}