diff --git a/src/URLFixer.php b/src/URLFixer.php index cb88f1b..d5ee270 100644 --- a/src/URLFixer.php +++ b/src/URLFixer.php @@ -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); @@ -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'; @@ -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));