-
Notifications
You must be signed in to change notification settings - Fork 1
/
LocalValetDriver.php
88 lines (72 loc) · 2.68 KB
/
LocalValetDriver.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
<?php
/**
* Put this file in the root of your Wordpress site if you have Wordpress core
* files installed inside a subdirectory. This helps valet rewrite the urls so
* that you can access wp-admin for the main site and all subsites of a
* multisite install.
*/
class LocalValetDriver extends BasicValetDriver {
public $wp_root = "/wp";
public function serves($sitePath, $siteName, $uri) {
return is_dir($sitePath . $this->wp_root . "/") &&
file_exists($sitePath . "/wp-config.php") &&
(is_dir($sitePath . "/config/") || file_exists($sitePath . "/.env"));
}
public function frontControllerPath($sitePath, $siteName, $uri) {
$_SERVER["PHP_SELF"] = $uri;
$_SERVER["SERVER_ADDR"] = "127.0.0.1";
$_SERVER["SERVER_NAME"] = $_SERVER["HTTP_HOST"];
$uri = $this->stripMultisiteSubdir($uri);
if (preg_match('/^\/.+(\/wp-admin\/.*)$/', $uri, $matches)) {
$uri = $matches[1];
}
$uri = $this->forceTrailingSlash($uri);
$candidates = [
$this->asActualWordpressRootFile($sitePath, $uri),
$this->asWordpressRootPhpIndexFileInDirectory($sitePath, $uri),
$this->asWordpressRootHtmlIndexFileInDirectory($sitePath, $uri),
];
foreach ($candidates as $candidate) {
if ($this->isActualFile($candidate)) {
$_SERVER["SCRIPT_FILENAME"] = $candidate;
$_SERVER["SCRIPT_NAME"] = str_replace($sitePath, "", $candidate);
$_SERVER["DOCUMENT_ROOT"] = $sitePath;
return $candidate;
}
}
return parent::frontControllerPath($sitePath, $siteName, $uri);
}
public function isStaticFile($sitePath, $siteName, $uri) {
$uri = $this->stripMultisiteSubdir($uri);
if (
$this->isActualFile($staticFilePath = $sitePath . $this->wp_root . $uri)
) {
return $staticFilePath;
} elseif ($this->isActualFile($staticFilePath = $sitePath . $uri)) {
return $staticFilePath;
}
return false;
}
public function stripMultisiteSubdir($uri) {
if (preg_match('/^(.*?)(\/wp-[a-z-]+(\/.*|\.php))$/', $uri, $matches)) {
$uri = $matches[2];
}
return $uri;
}
private function forceTrailingSlash($uri) {
if (substr($uri, -1 * strlen("/wp-admin")) == "/wp-admin") {
header("Location: " . $uri . "/");
die();
}
return $uri;
}
protected function asActualWordpressRootFile($sitePath, $uri) {
return $sitePath . $this->wp_root . $uri;
}
protected function asWordpressRootPhpIndexFileInDirectory($sitePath, $uri) {
return $sitePath . $this->wp_root . rtrim($uri, "/") . "/index.php";
}
protected function asWordpressRootHtmlIndexFileInDirectory($sitePath, $uri) {
return $sitePath . $this->wp_root . rtrim($uri, "/") . "/index.html";
}
}