Skip to content

Instantly share code, notes, and snippets.

@eriktorsner
Created August 18, 2023 11:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eriktorsner/f51199ff66e145c4faccd2b9871629da to your computer and use it in GitHub Desktop.
Save eriktorsner/f51199ff66e145c4faccd2b9871629da to your computer and use it in GitHub Desktop.
Updated WordPress multisite driver for valet 4
<?php
namespace Valet\Drivers\Custom;
use Valet\Drivers\BasicValetDriver;
/*
Valet driver for Wordpress Multisite
Usage: Drop this file into your ~/.valet/Drivers/ directory
*/
class WordPressMultisiteValetDriver extends BasicValetDriver
{
/**
* @var string The public web directory, if deeper under the root directory
*/
protected $public_dir = '';
/**
* @var bool true if site is detected to be multisite
*/
protected $multisite = false;
/**
* Determine if the driver serves the request.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
* @return bool
*/
public function serves(string $sitePath, string $siteName, string $uri): bool
{
foreach (['', 'public'] as $public_directory) {
$this->public_dir = $public_directory;
$wp_config_path = $this->realSitePath($sitePath) . "/wp-config.php";
if (file_exists($wp_config_path)) {
// Look for define('MULTISITE', true in wp-config
$env_path = $sitePath . "/.env";
if (preg_match("/^define\(\s*('|\")MULTISITE\\1\s*,\s*true\s*\)/mi",
file_get_contents($wp_config_path))
or (file_exists($env_path) and preg_match("/^WP_MULTISITE=true$/mi",
file_get_contents($env_path)))
) {
$this->multisite = true;
}
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(string $sitePath, string $siteName, string $uri)/*: string|false */
{
$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(string $sitePath, string $siteName, string $uri): string
{
$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 ($this->multisite) {
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment