Skip to content

Instantly share code, notes, and snippets.

@ben-rogerson
Created May 14, 2018 21:00
Show Gist options
  • Save ben-rogerson/bde7237b9d32187206372051eeab1e5c to your computer and use it in GitHub Desktop.
Save ben-rogerson/bde7237b9d32187206372051eeab1e5c to your computer and use it in GitHub Desktop.
Driver to allow correct image proxying with Valet and Wordpress
<?php
class BetterWordPressValetDriver 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.'/wp-config.php') || file_exists($sitePath.'/wp-config-sample.php');
}
/**
* 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)
{
$path = parent::frontControllerPath(
$sitePath, $siteName, $this->forceTrailingSlash($uri)
);
$_SERVER['PHP_SELF'] = str_replace( $sitePath, '', $path );
return $path;
}
/**
* 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