Last active
July 31, 2025 13:39
-
-
Save dr5hn/8412f9523a9f6f90ac50511d710679ff to your computer and use it in GitHub Desktop.
Roots Radicle Valet Driver which Supports Multisite with Subdirectory Installation
This file contains hidden or 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; | |
| use Valet\Drivers\BasicValetDriver; | |
| class RadicleValetDriver extends BasicValetDriver | |
| { | |
| /** | |
| * Determine if the driver serves the request. | |
| */ | |
| public function serves(string $sitePath, string $siteName, string $uri): bool | |
| { | |
| return file_exists($sitePath.'/public/content/mu-plugins/bedrock-autoloader.php') && | |
| file_exists($sitePath.'/public/wp-config.php') && | |
| file_exists($sitePath.'/bedrock/application.php'); | |
| } | |
| /** | |
| * Determine if the incoming request is for a static file. | |
| * | |
| * @return string|false | |
| */ | |
| public function isStaticFile(string $sitePath, string $siteName, string $uri)/* : string|false */ | |
| { | |
| $staticFilePath = $sitePath.'/public'.$uri; | |
| if ($this->isActualFile($staticFilePath)) { | |
| return $staticFilePath; | |
| } | |
| return false; | |
| } | |
| /** | |
| * Get the fully resolved path to the application's front controller. | |
| */ | |
| public function frontControllerPath(string $sitePath, string $siteName, string $uri): string | |
| { | |
| $_SERVER['PHP_SELF'] = $uri; | |
| // Cache the public path to avoid repeated concatenation | |
| $publicPath = $sitePath . '/public'; | |
| // Handle direct /wp/ requests | |
| if (str_starts_with($uri, '/wp/')) { | |
| $fullPath = $publicPath . $uri; | |
| return is_dir($fullPath) | |
| ? $fullPath . (str_ends_with($uri, '/') ? '' : '/') . 'index.php' | |
| : $fullPath; | |
| } | |
| // Handle /wp-json/ requests | |
| if (str_starts_with($uri, '/wp-json/')) { | |
| return $publicPath . '/index.php'; | |
| } | |
| // Handle subsite /wp/ requests - only if it contains the pattern | |
| if (str_contains($uri, '/wp/') && preg_match('/^\/([^\/]+)\/wp\/(.*)$/', $uri, $matches)) { | |
| $wpPath = '/wp/' . $matches[2]; | |
| $fullPath = $publicPath . $wpPath; | |
| return is_dir($fullPath) | |
| ? $fullPath . (str_ends_with($wpPath, '/') ? '' : '/') . 'index.php' | |
| : $fullPath; | |
| } | |
| return $publicPath . '/index.php'; | |
| } | |
| /** | |
| * Redirect to uri with trailing slash. | |
| * | |
| * @return string | |
| */ | |
| private function forceTrailingSlash(string $uri) | |
| { | |
| if (substr($uri, -1 * strlen('/wp/wp-admin')) == '/wp/wp-admin') { | |
| header('Location: '.$uri.'/'); | |
| exit; | |
| } | |
| return $uri; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment