Skip to content

Updated for Valet 4 #10

@davemac

Description

@davemac

Can you update this for Valet 4, as I cannot make a pull request. Here is the updated driver:

<?php

namespace Valet\Drivers\Custom;

use Valet\Drivers\ValetDriver;

class WordPressMultisiteSubdirectoryValetDriver extends ValetDriver
{
    public $wp_root = false; // "wp"

    /**
     * Determine if the driver serves the request.
     *
     * @param  string  $sitePath
     * @param  string  $siteName
     * @param  string  $uri
     * @return bool
     */
    public function serves(string $sitePath, string $siteName, string $uri): bool
    {
        // Look for MULTISITE in wp-config.php. It should be there for multisite installs.
        return file_exists($sitePath . '/wp-config.php') && strpos(file_get_contents($sitePath . '/wp-config.php'), 'MULTISITE') !== 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(string $sitePath, string $siteName, string $uri)
    {
        // Check if the file exists in the wp-admin, wp-content, or wp-includes directories.
        if (stripos($uri, 'wp-admin') !== false || stripos($uri, 'wp-content') !== false || stripos($uri, 'wp-includes') !== false) {
            $new_uri = substr($uri, stripos($uri, '/wp-'));

            if ($this->wp_root !== false && file_exists($sitePath . "/{$this->wp_root}/wp-admin")) {
                $new_uri = "/{$this->wp_root}" . $new_uri;
            }

            if (file_exists($sitePath . $new_uri)) {
                return $sitePath . $new_uri;
            }
        }

        // Check if the file exists in the site's root directory.
        if (file_exists($staticFilePath = $sitePath . $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(string $sitePath, string $siteName, string $uri): string
    {
        $_SERVER['PHP_SELF']    = $uri;
        $_SERVER['SERVER_ADDR'] = '127.0.0.1';
        $_SERVER['SERVER_NAME'] = $_SERVER['HTTP_HOST'];

        // If URI contains one of the main WordPress directories, and it's not a request for the Network Admin,
        // drop the subdirectory segment before routing the request
        if ((stripos($uri, 'wp-admin') !== false || stripos($uri, 'wp-content') !== false || stripos($uri, 'wp-includes') !== false)) {
            if (stripos($uri, 'wp-admin/network') === false) {
                $uri = substr($uri, stripos($uri, '/wp-'));
            }

            if ($this->wp_root !== false && file_exists($sitePath . "/{$this->wp_root}/wp-admin")) {
                $uri = "/{$this->wp_root}" . $uri;
            }
        }

        // Handle wp-cron.php properly
        if (stripos($uri, 'wp-cron.php') !== false) {
            $new_uri = substr($uri, stripos($uri, '/wp-'));

            if (file_exists($sitePath . $new_uri)) {
                return $sitePath . $new_uri;
            }
        }

        // Return the front controller path.
        return $sitePath . '/index.php';
    }

    /**
     * Redirect to uri with trailing slash.
     *
     * @param  string $uri
     * @return string
     */
    private function forceTrailingSlash(string $uri): string
    {
        if (substr($uri, -1 * strlen('/wp-admin')) == '/wp-admin') {
            header('Location: ' . $uri . '/');
            die;
        }
        return $uri;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions