Skip to content

Instantly share code, notes, and snippets.

@FrankM1
Last active November 30, 2018 06:58
Show Gist options
  • Save FrankM1/a2279d34170af478e1cd70bafe33c6eb to your computer and use it in GitHub Desktop.
Save FrankM1/a2279d34170af478e1cd70bafe33c6eb to your computer and use it in GitHub Desktop.
Wordpress Multisite Valet Driver
<?php
class LocalValetDriver extends WordPressValetDriver {
/**
* @var string The public web directory, if deeper under the root directory
*/
protected $public_dir = '';
/**
* Determine if the driver serves the request.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
* @return bool
*/
public function serves($sitePath, $siteName, $uri) {
if ( file_exists($sitePath . '/wp-config.php') ) {
return true;
}
return false;
}
/**
* Determine if the incoming request is for a static file.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
* @return string|false
*/
public function isStaticFile($sitePath, $siteName, $uri) {
$uri = $this->rewriteMultisite($sitePath, $uri);
$sitePath = $this->realSitePath($sitePath);
if ($this->isActualFile($staticFilePath = $sitePath . $uri)) {
return $staticFilePath;
}
return false;
}
/**
* Get the fully resolved path to the application's front controller.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
* @return string
*/
public function frontControllerPath($sitePath, $siteName, $uri) {
$uri = $this->rewriteMultisite($sitePath, $uri);
$sitePath = $this->realSitePath($sitePath);
return parent::frontControllerPath($sitePath, $siteName, $uri);
}
/**
* Translate the site path to the actual public directory
*
* @param $sitePath
* @return string
*/
protected function realSitePath($sitePath) {
if ($this->public_dir) {
$sitePath .= $this->public_dir;
}
return $sitePath;
}
/**
* Imitate the rewrite rules for a multisite .htaccess
*
* @param $sitePath
* @param $uri
* @return string
*/
protected function rewriteMultisite( $sitePath, $uri ) {
if ( strpos( file_get_contents($sitePath . '/wp-config.php'), 'MULTISITE') !== false ) {
if (preg_match('/^(.*)?(\/wp-(content|admin|includes).*)/', $uri, $matches)) {
//RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
$uri = $matches[2];
} elseif (preg_match('/^(.*)?(\/.*\.php)$/', $uri, $matches)) {
//RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
$uri = $matches[2];
}
}
return $uri;
}
}
@FrankM1
Copy link
Author

FrankM1 commented Nov 30, 2018

Automatically detects a multisite installation and loads it correctly when using Laravel Valet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment