-
Notifications
You must be signed in to change notification settings - Fork 0
/
WordPressMultisiteValetDriver.php
130 lines (110 loc) · 3.56 KB
/
WordPressMultisiteValetDriver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
namespace Valet\Drivers\Custom;
/*
Valet driver for Wordpress Multisite
Usage: Drop this file into your ~/.config/valet/Drivers/ directory
*/
use Valet\Drivers\Specific\WordPressValetDriver;
class WordPressMultisiteValetDriver extends WordPressValetDriver
{
/**
* @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(?:'|\")s*,\s*true\s*\)/mi", file_get_contents($wp_config_path) )
|| (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($sitePath, $siteName, $uri)
{
// dump( $uri, $sitePath );
$uri = $this->rewriteMultisite($sitePath, $uri);
// dump( $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;
}
}