Skip to content

Commit

Permalink
Fix bug in paged routes auto-building
Browse files Browse the repository at this point in the history
  • Loading branch information
gmazzap committed Feb 18, 2016
1 parent 7e5bfc5 commit cf5da01
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 17 deletions.
37 changes: 22 additions & 15 deletions src/Cortex/Route/PriorityRouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
final class PriorityRouteCollection implements RouteCollectionInterface
{
private static $pagedFlags = [RouteInterface::PAGED_ARCHIVE, RouteInterface::PAGED_SINGLE];

/**
* @var \SplPriorityQueue
*/
Expand Down Expand Up @@ -48,7 +50,18 @@ public function addRoute(RouteInterface $route)
$route->offsetSet('priority', $next);
}

$priority = $this->maybeAddPaged($route, (int) $route->offsetGet('priority'));
$priority = $route->offsetGet('priority');

$paged = $this->maybeBuildPaged($route, (int) $route->offsetGet('priority'));
if (
$paged instanceof RouteInterface
&& ! in_array($paged['paged'], self::$pagedFlags, true) // ensure avoid infinite loops
) {
$paged->offsetSet('priority', $priority);
$priority++;
$this->addRoute($paged);
}

in_array($priority, $this->priorities, true) or $this->priorities[] = $priority;
$this->queue->insert($route, ((-1) * $priority));

Expand Down Expand Up @@ -105,31 +118,25 @@ public function count()

/**
* @param \Brain\Cortex\Route\RouteInterface $route
* @param int $priority
* @return int
*/
private function maybeAddPaged(RouteInterface $route, $priority)
private function maybeBuildPaged(RouteInterface $route)
{
$pagedOpts = [RouteInterface::PAGED_ARCHIVE, RouteInterface::PAGED_SINGLE];
$pagedArg = $route->offsetExists('paged') ? $route->offsetGet('paged') : '';
$path = $route->offsetExists('path') ? $route->offsetGet('path') : '';
if (in_array($pagedArg, $pagedOpts, true) && $path && is_string($path)) {
if (in_array($pagedArg, self::$pagedFlags, true) && $path && is_string($path)) {
$base = 'page';
/** @var \WP_Rewrite $wp_rewrite */
global $wp_rewrite;
$wp_rewrite instanceof \WP_Rewrite and $base = $wp_rewrite->pagination_base;
$paged = clone $route;
$newPath = $pagedArg === RouteInterface::PAGED_ARCHIVE
$array = $route->toArray();
$array['id'] = $route->id().'_paged';
$array['paged'] = RouteInterface::PAGED_UNPAGED;
$array['path'] = $pagedArg === RouteInterface::PAGED_ARCHIVE
? $path.'/'.$base.'/{paged:\d+}'
: $path.'/{page:\d+}';
$paged->offsetSet('path', $newPath);
$paged->offsetSet('id', $route->offsetGet('id').'_paged');
$paged->offsetSet('paged', '');
$paged->offsetSet('priority', $priority);
$this->addRoute($paged);
$priority++;
}

return $priority;
return apply_filters('cortex.paged-route', new Route($array), $route);
}
}
}
1 change: 1 addition & 0 deletions src/Cortex/Route/RouteInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ interface RouteInterface extends \ArrayAccess
{
const PAGED_SINGLE = 'paged_single';
const PAGED_ARCHIVE = 'paged_archive';
const PAGED_UNPAGED = '';
const PAGED_SEARCH = 'paged_archive';
const PAGED_FRONT_PAGE = 'paged_archive';
const NOT_PAGED = 'not_paged';
Expand Down
2 changes: 1 addition & 1 deletion src/Cortex/Router/ResultHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function handle(MatchingResult $result, \WP $wp, $doParseRequest)
is_callable($before) and $before($vars, $wp);
is_callable($handler) and $doParseRequest = $handler($vars, $wp);
is_callable($after) and $after($vars, $wp);
is_string($template) and $this->setTemplate($template);
(is_string($template) && $template) and $this->setTemplate($template);

do_action('cortex.matched-after', $result, $wp, $doParseRequest);

Expand Down
29 changes: 28 additions & 1 deletion tests/src/Functional/CortexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ public function testRouterReturnRouteResultsWhenGiven()

assertTrue($do);
assertFalse(isset($wp->query_vars));

}

public function testCortexMatchStaticRoute()
Expand Down Expand Up @@ -138,6 +137,34 @@ public function testCortexMatchStaticRoute()
assertFalse($do);
}

public function testCortexMatchPagedRoute()
{
Functions::when('home_url')->justReturn('http://example.com/');
Functions::when('remove_all_filters')->justReturn();

Actions::expectFired('cortex.routes')
->once()
->whenHappen(function (RouteCollectionInterface $routes) {
$routes->addRoute(new QueryRoute('/bar', function ($vars) {
isset($vars['paged']) and $vars['paged'] = (int) $vars['paged'];

return $vars;
}, ['paged' => QueryRoute::PAGED_ARCHIVE]));
});

Actions::expectFired('cortex.matched')->once();

$request = self::buildPsrRequest('http://example.com/bar/page/3');

$cortex = new Proxy(new Cortex());
$wp = \Mockery::mock('WP');

$do = $cortex->doBoot($wp, true, $request);

assertSame(['paged' => 3], $wp->query_vars);
assertFalse($do);
}

public function testCortexNotMatchIfDifferentMethod()
{
Functions::when('home_url')->justReturn('http://example.com/foo/');
Expand Down

0 comments on commit cf5da01

Please sign in to comment.