Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Fix Laravel Valet when using WordPress subdirectory multisite + Bedrock
<?php
class BedrockMultisiteDriver extends BedrockValetDriver
{
/**
* 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);
return parent::isStaticFile($sitePath, $siteName, $uri);
}
/**
* 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);
return parent::frontControllerPath($sitePath, $siteName, $uri);
}
/**
* Determine if Bedrock installer is Multisite
*
* @param $sitePath
* @return bool
*/
protected function isMultisite($sitePath)
{
$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);
}
/**
* Imitate the rewrite rules for a multisite .htaccess
*
* @param $sitePath
* @param $uri
* @return string
*/
protected function rewriteMultisite($sitePath, $uri)
{
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;
}
}
@QWp6t

This comment has been minimized.

Copy link
Owner Author

QWp6t commented Mar 7, 2018

@christian-thomas

This comment has been minimized.

Copy link

christian-thomas commented Jan 3, 2019

For anyone that's using Config::define in config/application.php and sub-directory installations aren't working/being recognised by the driver.

Change line 44 from:

return !! preg_match("/^define\(\s*('|\")MULTISITE\\1\s*,\s*true\s*\)/mi", $app);

To:

return !! preg_match("/define\(\s*('|\")MULTISITE\\1\s*,\s*true\s*\)/mi", $app);
@christian-thomas

This comment has been minimized.

Copy link

christian-thomas commented Jan 4, 2019

And anyone that can't access wp-json routes...

Change line 56 from:

if (preg_match('#^(/[^/]+)?(/wp-.*)#', $uri, $matches) || preg_match('#^(/[^/]+)?(/.*\.php)#', $uri, $matches)) {

To:

if (preg_match('#^(?!/wp-json)(/[^/]+)?(/wp-.*)#', $uri, $matches) || preg_match('#^(/[^/]+)?(/.*\.php)#', $uri, $matches)) { 
@wunc

This comment has been minimized.

Copy link

wunc commented Sep 19, 2019

To:

if (preg_match('#^(?!/wp-json)(/[^/]+)?(/wp-.*)#', $uri, $matches) || preg_match('#^(/[^/]+)?(/.*\.php)#', $uri, $matches)) { 

I think this has the /wp-json exclusion in the wrong place (it would only work for the main site). The following worked for me (and got the Gutenburg editor working) for all subfolder sites:

if (preg_match('#^(/[^/]+)?(?!/wp-json)(/wp-.*)#', $uri, $matches) || preg_match('#^(/[^/]+)?(/.*\.php)#', $uri, $matches)) {
@QWp6t

This comment has been minimized.

Copy link
Owner Author

QWp6t commented Sep 19, 2019

Thanks guys. I updated the gist to reflect your findings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.