🏡 index : ~doyle/koselig.git

author Jordan Doyle <jordan@doyle.wf> 2017-01-18 20:20:57.0 +00:00:00
committer Jordan Doyle <jordan@doyle.wf> 2017-01-18 20:20:57.0 +00:00:00
commit
f2f2fa4d7a78880643fdc5cd1f7bd438624e18f9 [patch]
tree
52bfece40f5401275eb68812cc2d0444a843923a
parent
0bbf6ed717dbbceba32bec56c8ba52e25827f077
download
f2f2fa4d7a78880643fdc5cd1f7bd438624e18f9.tar.gz

Category archive page routing



Diff

 src/Routing/CategoryRoute.php          | 56 +++++++++++++++++++++++++++++++++++-
 src/Routing/Routing.php                | 31 +++++++++++++++++++-
 src/Routing/RoutingServiceProvider.php |  1 +-
 src/Support/Wordpress.php              | 11 +++++++-
 4 files changed, 99 insertions(+)

diff --git a/src/Routing/CategoryRoute.php b/src/Routing/CategoryRoute.php
new file mode 100644
index 0000000..8018551
--- /dev/null
+++ b/src/Routing/CategoryRoute.php
@@ -0,0 +1,56 @@
<?php
namespace Koselig\Routing;

use Illuminate\Http\Request;
use Illuminate\Routing\Route;
use Koselig\Support\Wordpress;

/**
 * Category page route, used when Wordpress reports
 * this page is a category page.
 *
 * @author Jordan Doyle <jordan@doyle.wf>
 */
class CategoryRoute extends Route
{
    /**
     * Post types for this archive route to hook onto.
     *
     * @var array
     */
    private $categories;

    /**
     * Create a new Route instance.
     *
     * @param  array|string $methods
     * @param  array $postTypes
     * @param  \Closure|array $action
     *
     * @return void
     */
    public function __construct($methods, $postTypes, $action)
    {
        parent::__construct($methods, $postTypes, $action);

        $this->categories = $this->uri;
        $this->uri = 'category/' . (implode('/', $this->categories) ?: 'all');
    }

    /**
     * Determine if the route matches given request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  bool $includingMethod
     *
     * @return bool
     */
    public function matches(Request $request, $includingMethod = true)
    {
        if (!empty($this->getAction()['domain']) && !Wordpress::multisite($this->getAction()['domain'])) {
            return false;
        }

        return Wordpress::category($this->categories);
    }
}
diff --git a/src/Routing/Routing.php b/src/Routing/Routing.php
index 8d2752c..ae40345 100644
--- a/src/Routing/Routing.php
+++ b/src/Routing/Routing.php
@@ -28,6 +28,37 @@ class Routing
    }

    /**
     * Register a new category route with the router. Optionally supply
     * the categories you'd like to supply with this route.
     *
     * @param callable|string|array $categories
     * @param callable|array|string|null $action
     *
     * @return \Illuminate\Routing\Route
     */
    public function category($categories = [], $action = [])
    {
        if (empty($action)) {
            $action = $categories;
            $categories = [];
        }

        if (!is_array($categories)) {
            $categories = [$categories];
        }

        $action = $this->formatAction($action);

        $route = (new CategoryRoute($action['method'], $categories, $action))
            ->setRouter(app('router'))
            ->setContainer(app(Container::class));

        $route = $this->applyStack($route);

        return Route::getRoutes()->add($route);
    }

    /**
     * Register a new page route with the router.
     *
     * @param  string $slug slug to match
diff --git a/src/Routing/RoutingServiceProvider.php b/src/Routing/RoutingServiceProvider.php
index 356c7e5..e81e990 100644
--- a/src/Routing/RoutingServiceProvider.php
+++ b/src/Routing/RoutingServiceProvider.php
@@ -29,5 +29,6 @@ class RoutingServiceProvider extends ServiceProvider
        Router::macro('archive', [$routing, 'archive']);
        Router::macro('singular', [$routing, 'singular']);
        Router::macro('author', [$routing, 'author']);
        Router::macro('category', [$routing, 'category']);
    }
}
diff --git a/src/Support/Wordpress.php b/src/Support/Wordpress.php
index 868be71..b4fad05 100644
--- a/src/Support/Wordpress.php
+++ b/src/Support/Wordpress.php
@@ -35,6 +35,17 @@ class Wordpress
    }

    /**
     * Check if the current page is a category page.
     *
     * @param array|string $categories categories to test for
     * @return bool
     */
    public static function category($categories = '')
    {
        return query()->category($categories);
    }

    /**
     * Check if the current page is an archive page.
     *
     * @param string|array|null $types check if the archive page is for this type