Skip to content

Instantly share code, notes, and snippets.

@permpkin
Last active May 1, 2022 12:27
Show Gist options
  • Save permpkin/f1c0434796c3c9230f39a1704637a3f4 to your computer and use it in GitHub Desktop.
Save permpkin/f1c0434796c3c9230f39a1704637a3f4 to your computer and use it in GitHub Desktop.
Wordpress Factory Valet Driver
<?php
class WordpressFactoryValetDriver extends ValetDriver
{
/**
* Determine if the driver serves the request.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
* @return bool
*/
public function serves($sitePath, $siteName, $uri)
{
if (file_exists($sitePath.'/public') && file_exists($sitePath.'/vendor/permpkin/wp-factory-plugin')) {
$_SERVER['APP_PATH'] = $sitePath;
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)
{
if (file_exists($staticFilePath = $sitePath.'/public'.$uri) && !is_dir($sitePath.'/public'.$uri)) {
return $staticFilePath;
}
if (file_exists($staticFilePath = $sitePath.'/vendor/permpkin/wp-factory-plugin'.$uri)) {
return $staticFilePath;
}
if (file_exists($staticFilePath = $sitePath.'/vendor/permpkin/wp-factory-plugin/public/'.$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($sitePath, $siteName, $uri)
{
$_SERVER['PHP_SELF'] = $uri;
$_SERVER['SERVER_ADDR'] = '127.0.0.1';
$_SERVER['SERVER_NAME'] = $_SERVER['HTTP_HOST'];
if (substr( $uri, -4 ) === ".php") {
return $sitePath.'/public'.$uri;
}
if (
substr($uri, 0, 11) === "/wp/factory"
) {
if (
substr($uri, 0, 15) === "/wp/factory/api"
) {
// check if this is a factory api request.
return $sitePath.'/vendor/permpkin/wp-factory-plugin/index.php';
} else {
return $sitePath.'/vendor/permpkin/wp-factory-plugin/public/index.html';
}
}
if (
substr($uri, 0, 13) === "/wp/wp-admin/"
) {
return $sitePath.'/public/wp/wp-admin/index.php';
}
if (substr($uri, -1 * strlen('/wp-admin')) == '/wp-admin') {
header('Location: '.$uri.'/');
exit;
}
if (
substr($uri, 0, 4) === "/wp/"
) {
return $sitePath.'/public/wp/index.php';
}
return $sitePath.'/public/index.php';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment