Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. <?php
  2. /*
  3. Valet driver for Wordpress Multisite
  4. Usage: Drop this file into your ~/.valet/Drivers/ directory
  5. */
  6. class WordPressMultisiteValetDriver extends WordPressValetDriver
  7. {
  8. /**
  9. * @var string The public web directory, if deeper under the root directory
  10. */
  11. protected $public_dir = '';
  12. /**
  13. * @var bool true if site is detected to be multisite
  14. */
  15. protected $multisite = false;
  16. /**
  17. * Determine if the driver serves the request.
  18. *
  19. * @param string $sitePath
  20. * @param string $siteName
  21. * @param string $uri
  22. * @return bool
  23. */
  24. public function serves($sitePath, $siteName, $uri)
  25. {
  26. foreach (['', 'public', 'htdocs'] as $public_directory) {
  27. $this->public_dir = $public_directory;
  28. $wp_config_path = $this->realSitePath($sitePath) . "/wp-config.php";
  29. if (file_exists($wp_config_path)) {
  30. // Look for define('MULTISITE', true in wp-config
  31. $env_path = $sitePath . "/.env";
  32. if (preg_match("/^define\(\s*('|\")MULTISITE\\1\s*,\s*true\s*\)/mi",
  33. file_get_contents($wp_config_path))
  34. or (file_exists($env_path) and preg_match("/^WP_MULTISITE=true$/mi",
  35. file_get_contents($env_path)))
  36. ) {
  37. $this->multisite = true;
  38. }
  39. return true;
  40. }
  41. }
  42. return false;
  43. }
  44. /**
  45. * Determine if the incoming request is for a static file.
  46. *
  47. * @param string $sitePath
  48. * @param string $siteName
  49. * @param string $uri
  50. * @return string|false
  51. */
  52. public function isStaticFile($sitePath, $siteName, $uri)
  53. {
  54. $uri = $this->rewriteMultisite($sitePath, $uri);
  55. $sitePath = $this->realSitePath($sitePath);
  56. if ($this->isActualFile($staticFilePath = $sitePath . $uri)) {
  57. return $staticFilePath;
  58. }
  59. return false;
  60. }
  61. /**
  62. * Get the fully resolved path to the application's front controller.
  63. *
  64. * @param string $sitePath
  65. * @param string $siteName
  66. * @param string $uri
  67. * @return string
  68. */
  69. public function frontControllerPath($sitePath, $siteName, $uri)
  70. {
  71. $uri = $this->rewriteMultisite($sitePath, $uri);
  72. $sitePath = $this->realSitePath($sitePath);
  73. return parent::frontControllerPath($sitePath, $siteName, $uri);
  74. }
  75. /**
  76. * Translate the site path to the actual public directory
  77. *
  78. * @param $sitePath
  79. * @return string
  80. */
  81. protected function realSitePath($sitePath)
  82. {
  83. if ($this->public_dir) {
  84. $sitePath .= "/" . $this->public_dir;
  85. }
  86. return $sitePath;
  87. }
  88. /**
  89. * Imitate the rewrite rules for a multisite .htaccess
  90. *
  91. * @param $sitePath
  92. * @param $uri
  93. * @return string
  94. */
  95. protected function rewriteMultisite($sitePath, $uri)
  96. {
  97. if ($this->multisite) {
  98. if (preg_match('/^(.*)?(\/wp-(content|admin|includes).*)/', $uri, $matches)) {
  99. //RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
  100. $uri = $matches[2];
  101. } elseif (preg_match('/^(.*)?(\/.*\.php)$/', $uri, $matches)) {
  102. //RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
  103. $uri = $matches[2];
  104. }
  105. }
  106. return $uri;
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement