Last active
June 17, 2024 07:43
-
-
Save QWp6t/1e055482d722e2b02dfff1bb21698194 to your computer and use it in GitHub Desktop.
Fix Laravel Valet when using WordPress subdirectory multisite + Bedrock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Valet\Drivers\Custom; | |
class BedrockMultisiteValetDriver extends \Valet\Drivers\Specific\BedrockValetDriver | |
{ | |
/** | |
* Determine if the incoming request is for a static file. | |
* | |
* @return string|false | |
*/ | |
public function isStaticFile(string $sitePath, string $siteName, string $uri) | |
{ | |
$uri = $this->rewriteMultisite($sitePath, $uri); | |
return parent::isStaticFile($sitePath, $siteName, $uri); | |
} | |
/** | |
* Get the fully resolved path to the application's front controller. | |
*/ | |
public function frontControllerPath(string $sitePath, string $siteName, string $uri): ?string | |
{ | |
$uri = $this->rewriteMultisite($sitePath, $uri); | |
return parent::frontControllerPath($sitePath, $siteName, $uri); | |
} | |
/** | |
* Imitate the rewrite rules for a multisite .htaccess | |
*/ | |
protected function rewriteMultisite(string $sitePath, string $uri): string | |
{ | |
if (!$this->isMultisite($sitePath)) { | |
return $uri; | |
} | |
if (preg_match('#^(/[^/]+)?(?!/wp-json)(/wp-.*)#', $uri, $matches) || preg_match('#^(/[^/]+)?(/.*\.php)#', $uri, $matches)) { | |
return "/wp{$matches[2]}"; | |
} | |
return $uri; | |
} | |
/** | |
* Determine if Bedrock installer is Multisite | |
*/ | |
protected function isMultisite(string $sitePath): bool | |
{ | |
$app = file_get_contents($sitePath.'/config/application.php'); | |
/** {@link https://github.com/fewagency/best-practices/blob/893dfef52442eb3c4aafc197926f29dee83f3cd0/Wordpress/WordPressMultisiteValetDriver.php#L37 Regex poached from similar driver} */ | |
return !! preg_match("/define\(\s*('|\")MULTISITE\\1\s*,\s*true\s*\)/mi", $app); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
if youve changed
to
to be able to control this via envs you need
full method