Skip to content

Instantly share code, notes, and snippets.

@forsvunnet
Last active August 28, 2023 11:50
Show Gist options
  • Save forsvunnet/e0c3a81a17f642a3108c1f377ae21dbb to your computer and use it in GitHub Desktop.
Save forsvunnet/e0c3a81a17f642a3108c1f377ae21dbb to your computer and use it in GitHub Desktop.
A WordPress valet driver
<?php
class AWordPressValetDriver extends BasicValetDriver
{
/**
* Determine if the driver serves the request.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
* @return bool
*/
public function serves($sitePath, $siteName, $uri)
{
return file_exists($sitePath . '/public/wp');
}
/**
* 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)
{
$_SERVER['PHP_SELF'] = $uri;
$_SERVER['SERVER_ADDR'] = '127.0.0.1';
$_SERVER['SERVER_NAME'] = $_SERVER['HTTP_HOST'];
$file = $sitePath. '/public/'. $uri;
if (preg_match('/.*\/wp-admin(.*)$/', $uri, $matches)) {
$file = $sitePath. '/public/wp/wp-admin'. $matches[1];
}
if (preg_match('/.*\/wp-login.php$/', $uri, $matches)) {
$file = $sitePath. '/public/wp/wp-login.php';
}
if (file_exists($file) && is_dir($file)) {
$file = rtrim($file, '/') . '/index.php';
}
if (file_exists($file)) {
return $file;
}
return parent::frontControllerPath(
$sitePath, $siteName, $this->forceTrailingSlash($uri)
);
}
/**
* 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)
{
$names = [
'fjellrevenshop',
'aclimashop',
];
$names = implode('|', $names);
$path = preg_replace("/^\/({$names})/", '', $uri);
if (preg_match('/.*\/(wp-includes|wp-admin)(.*)$/', $path, $matches)) {
$file = $sitePath. '/public/wp/'. $matches[1] . $matches[2];
if (file_exists($file) && ! is_dir($file) && 'php' !== pathinfo($file, PATHINFO_EXTENSION)) {
return $file;
}
}
if (preg_match('/.*\/(wp-content)(.*)$/', $path, $matches)) {
$file = $sitePath. '/public/'. $matches[1] . $matches[2];
if (file_exists($file) && ! is_dir($file) && 'php' !== pathinfo($file, PATHINFO_EXTENSION)) {
return $file;
}
}
return false;
}
/**
* Redirect to uri with trailing slash.
*
* @param string $uri
* @return string
*/
private function forceTrailingSlash($uri)
{
if (substr($uri, -1 * strlen('/wp-admin')) == '/wp-admin') {
header('Location: '.$uri.'/'); die;
}
return $uri;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment