Multisite routing support
Diff
src/Routing/ArchiveRoute.php | 4 ++++
src/Routing/PageRoute.php | 4 ++++
src/Routing/Routing.php | 35 ++++++++++++++++++++++++++++++++++-
src/Routing/SingularRoute.php | 4 ++++
src/Routing/TemplateRoute.php | 4 ++++
src/Support/Wordpress.php | 30 ++++++++++++++++++++++++++++++
6 files changed, 80 insertions(+), 1 deletion(-)
@@ -55,6 +55,10 @@ class ArchiveRoute extends Route
*/
public function matches(Request $request, $includingMethod = true)
{
if (!empty($this->getAction()['domain']) && !Wordpress::multisite($this->getAction()['domain'])) {
return false;
}
return Wordpress::archive($this->postTypes);
}
}
@@ -39,6 +39,10 @@ class PageRoute extends Route
return false;
}
if (!empty($this->getAction()['domain']) && !Wordpress::multisite($this->getAction()['domain'])) {
return false;
}
return $this->uri === $id;
}
}
@@ -21,6 +21,8 @@ class Routing
->setRouter(app('router'))
->setContainer(app(Container::class));
$route = $this->applyStack($route);
return Route::getRoutes()->add($route);
}
@@ -39,6 +41,8 @@ class Routing
->setRouter(app('router'))
->setContainer(app(Container::class));
$route = $this->applyStack($route);
return Route::getRoutes()->add($route);
}
@@ -67,6 +71,8 @@ class Routing
->setRouter(app('router'))
->setContainer(app(Container::class));
$route = $this->applyStack($route);
return Route::getRoutes()->add($route);
}
@@ -90,6 +96,8 @@ class Routing
->setRouter(app('router'))
->setContainer(app(Container::class));
$route = $this->applyStack($route);
return Route::getRoutes()->add($route);
}
@@ -99,7 +107,7 @@ class Routing
* @param $action
* @return array|string
*/
private function formatAction($action)
protected function formatAction($action)
{
if (!($action instanceof $action) && (is_string($action) || (isset($action['uses'])
&& is_string($action['uses'])))) {
@@ -127,4 +135,29 @@ class Routing
return $action;
}
/**
* Apply group stack properties to the route and apply global "wheres" to the
* route.
*
* @param $route
* @return mixed
*/
protected function applyStack($route)
{
// If we have groups that need to be merged, we will merge them now after this
// route has already been created and is ready to go. After we're done with
// the merge we will be ready to return the route back out to the caller.
if (Route::hasGroupStack()) {
$action = Route::mergeWithLastGroup($route->getAction());
$route->setAction($action);
}
$where = isset($route->getAction()['where']) ? $route->getAction()['where'] : [];
$route->where(array_merge(Route::getPatterns(), $where));
return $route;
}
}
@@ -60,6 +60,10 @@ class SingularRoute extends Route
return false;
}
if (!empty($this->getAction()['domain']) && !Wordpress::multisite($this->getAction()['domain'])) {
return false;
}
return Wordpress::singular($this->types);
}
}
@@ -39,6 +39,10 @@ class TemplateRoute extends Route
return false;
}
if (!empty($this->getAction()['domain']) && !Wordpress::multisite($this->getAction()['domain'])) {
return false;
}
return $this->uri === $slug;
}
}
@@ -66,6 +66,36 @@ class Wordpress
}
/**
* Check if we are on a multisite, and optionally check the multisite we are on.
*
* @param null|int|array $id id (or ids) to check against the site, or null if you want to just check
* if we are actually on a multisite
* @return bool
*/
public static function multisite($id = null)
{
if (is_array($id)) {
foreach ($id as $i) {
if (static::multisite($i)) {
return true;
}
}
}
return $id === null ? is_multisite() : ($id === static::getMultisite());
}
/**
* Get the current multisite id.
*
* @return int
*/
public static function getMultisite()
{
return get_current_blog_id();
}
/**
* Get the current logged in user.
*
* @return \WP_User