Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions src/URLFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,34 @@ class URLFixer
/**
* Add filters to verify / fix URLs.
*/
public function addFilters()
public function addFilters(): void
{
// Do not interfere while network admin is processing a form submission.
// This prevents the filters from corrupting the values being saved.
if ($this->isSavingContext()) {
return;
}

add_filter('option_home', [$this, 'fixHomeURL']);
add_filter('option_siteurl', [$this, 'fixSiteURL']);
add_filter('network_site_url', [$this, 'fixNetworkSiteURL'], 10, 3);
}

/**
* Whether we are currently saving options in the network admin.
*/
protected function isSavingContext(): bool
{
return is_admin() && is_network_admin() && $_SERVER['REQUEST_METHOD'] === 'POST';
}

/**
* Ensure that home URL does not contain the /wp subdirectory.
*
* @param string $value the unchecked home URL
* @return string the verified home URL
*/
public function fixHomeURL($value)
public function fixHomeURL(string $value): string
{
if (substr($value, -3) === '/wp') {
$value = substr($value, 0, -3);
Expand All @@ -40,7 +54,7 @@ public function fixHomeURL($value)
* @param string $url the unchecked site URL
* @return string the verified site URL
*/
public function fixSiteURL($url)
public function fixSiteURL(string $url): string
{
if (substr($url, -3) !== '/wp' && (is_main_site() || is_subdomain_install())) {
$url .= '/wp';
Expand All @@ -56,7 +70,7 @@ public function fixSiteURL($url)
* @param string $scheme the URL scheme
* @return string the verified network site URL
*/
public function fixNetworkSiteURL($url, $path, $scheme)
public function fixNetworkSiteURL(string $url, string $path, string $scheme): string
{
$path = ltrim($path, '/');
$url = substr($url, 0, strlen($url) - strlen($path));
Expand Down