Skip to content

Instantly share code, notes, and snippets.

@QWp6t
Last active June 17, 2024 07:43
Show Gist options
  • Save QWp6t/1e055482d722e2b02dfff1bb21698194 to your computer and use it in GitHub Desktop.
Save QWp6t/1e055482d722e2b02dfff1bb21698194 to your computer and use it in GitHub Desktop.
Fix Laravel Valet when using WordPress subdirectory multisite + Bedrock
<?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);
}
}
@christian-thomas
Copy link

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
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
Copy link
Author

QWp6t commented Sep 19, 2019

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

@mike-sheppard
Copy link

Nice one for this @QWp6t really helpful!

I don't know if it's just my setup or a newer version of Valet? But I had to rename this file to BedrockMultisiteValetDriver.php for it to autoload at all.

As per the valet docs following the FrameworkValetDriver.php naming convention.

Once you have completed your custom Valet driver, place it in the ~/.config/valet/Drivers directory using the FrameworkValetDriver.php naming convention. For example, if you are writing a custom valet driver for WordPress, your file name should be WordPressValetDriver.php.

Anyways hope it helps someone else if it is the case!

@QWp6t
Copy link
Author

QWp6t commented Dec 17, 2020

Thanks @mike-sheppard!

I went ahead and renamed it in the gist as well.

@harry-huddle
Copy link

In case anyone else is still trying to use this, it just needs updating with namespaces and type hinting to work with valet 4.

https://github.com/laravel/valet/blob/master/UPGRADE.md

e.g.

namespace Valet\Drivers\Custom;

use Valet\Drivers\Specific\BedrockValetDriver;

and

public function frontControllerPath(string $sitePath, string $siteName, string $uri): ?string

@QWp6t
Copy link
Author

QWp6t commented Jul 27, 2023

Thanks @harry-huddle! 🙏

I updated the gist to reflect the updated API.

@chrillep
Copy link

if youve changed

Config::define('MULTISITE', true);

to

Config::define('MULTISITE', env('MULTISITE') ?: false);

to be able to control this via envs you need

     // .env has MULTISITE='True'
        $envFile = file_get_contents($sitePath . '/.env');
        $envFileHasMultisite = preg_match("/\bMULTISITE\s*=\s*['\"]?true['\"]?\b/i", $envFile);

full method

    /**
     * Determine if Bedrock installer is Multisite
     */
    protected function isMultisite(string $sitePath): bool
    {
        $applicationFile = file_get_contents($sitePath . '/bedrock/application.php');
        /** {@link https://github.com/fewagency/best-practices/blob/893dfef52442eb3c4aafc197926f29dee83f3cd0/Wordpress/WordPressMultisiteValetDriver.php#L37 Regex poached from similar driver} */
        $applicationFileHasMultisite = preg_match("/define\(\s*('|\")MULTISITE\\1\s*,\s*true\s*\)/mi", $applicationFile);

        // .env has MULTISITE='True'
        $envFile = file_get_contents($sitePath . '/.env');
        $envFileHasMultisite = preg_match("/\bMULTISITE\s*=\s*['\"]?true['\"]?\b/i", $envFile);
        return $applicationFileHasMultisite || $envFileHasMultisite;
    }

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