From a98df65de995454fe1aa31f82759fab9e3504f7c Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Tue, 4 Oct 2016 20:48:57 +0100 Subject: [PATCH] Multisite routing support --- 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(-) diff --git a/src/Routing/ArchiveRoute.php b/src/Routing/ArchiveRoute.php index 49113ed..dfb0d36 100644 --- a/src/Routing/ArchiveRoute.php +++ b/src/Routing/ArchiveRoute.php @@ -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); } } diff --git a/src/Routing/PageRoute.php b/src/Routing/PageRoute.php index 7b97b65..5641d91 100644 --- a/src/Routing/PageRoute.php +++ b/src/Routing/PageRoute.php @@ -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; } } diff --git a/src/Routing/Routing.php b/src/Routing/Routing.php index 3d218e1..1ff2d70 100644 --- a/src/Routing/Routing.php +++ b/src/Routing/Routing.php @@ -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; + } } diff --git a/src/Routing/SingularRoute.php b/src/Routing/SingularRoute.php index 1d0bc55..8753b29 100644 --- a/src/Routing/SingularRoute.php +++ b/src/Routing/SingularRoute.php @@ -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); } } diff --git a/src/Routing/TemplateRoute.php b/src/Routing/TemplateRoute.php index 65d46b2..93cb598 100644 --- a/src/Routing/TemplateRoute.php +++ b/src/Routing/TemplateRoute.php @@ -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; } } diff --git a/src/Support/Wordpress.php b/src/Support/Wordpress.php index e6a51cd..933bf60 100644 --- a/src/Support/Wordpress.php +++ b/src/Support/Wordpress.php @@ -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 -- libgit2 1.7.2