Skip to content

Commit

Permalink
Tpate page template is took into account even when no pagename
Browse files Browse the repository at this point in the history
See #16
  • Loading branch information
gmazzap committed Sep 28, 2018
1 parent 5eabd7c commit 3634a94
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/Branch/BranchPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,26 @@ public function leaves(\WP_Query $query)
{
/** @var \WP_Post $post */
$post = $query->get_queried_object();

$post instanceof \WP_Post or $post = new \WP_Post((object) ['ID' => 0]);
$pagename = $query->get('pagename');

if (empty($post->post_name) && empty($pagename)) {
return ['page'];
}
$template = $this->postTemplates->findFor($post);
$pagename = $query->get('pagename');
(!$pagename && $post->ID) and $pagename = $post->post_name;

$name = $pagename ? $pagename : $post->post_name;
$leaves = $template ? [$template] : [];
$baseLeaves = $post->ID ? ["page-{$post->ID}", 'page'] : ['page'];

$leaves = ["page-{$name}"];
$post->ID and $leaves[] = "page-{$post->ID}";
$leaves[] = 'page';
if (!$pagename) {
return array_merge($leaves, $baseLeaves);
}

if ($post->post_type === 'page') {
$template = $this->postTemplates->findFor($post);
$template and array_unshift($leaves, $template);
$pagenameDecoded = urldecode($pagename);
if ($pagenameDecoded !== $pagename) {
$leaves[] = "page-{$pagenameDecoded}";
}

return $leaves;
$leaves[] = "page-{$pagename}";

return array_merge($leaves, $baseLeaves);
}
}
3 changes: 3 additions & 0 deletions tests/src/Functional/QueryTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public function testLoadPageSingular()
$post = Mockery::mock('\WP_Post');
$post->ID = 1;
$post->post_type = 'page';
$post->post_name = 'foo';

Functions::expect('get_page_template_slug')->with($post)->andReturn('');

$wpQuery = new \WP_Query(['is_page' => true, 'is_singular' => true], $post);

Expand Down
27 changes: 27 additions & 0 deletions tests/src/Unit/Branch/BranchPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/
final class BranchPageTest extends TestCase
{

public function testLeavesNoPageNoPagename()
{
$post = Mockery::mock('\WP_Post');
Expand Down Expand Up @@ -135,4 +136,30 @@ public function testLeavesPagePagenameTemplateFolder()

assertSame($expected, $branch->leaves($query));
}

public function testPageNameReturnTemplateIfNoPagename()
{
Functions::when('sanitize_title')->alias('strtolower');

$post = Mockery::mock('\WP_Post');
$post->ID = 1;
$post->post_name = '';
$post->post_title = 'foo';
$post->post_type = 'page';

$query = new \WP_Query(['is_preview' => true], $post, ['page_id' => 1, 'pagename' => '']);

$postTemplates = Mockery::mock(PostTemplates::class);
$postTemplates
->shouldReceive('findFor')
->once()
->with($post)
->andReturn('page-foo');

$branch = new BranchPage($postTemplates);

$expected = ['page-foo', 'page-1', 'page'];

assertSame($expected, $branch->leaves($query));
}
}

0 comments on commit 3634a94

Please sign in to comment.