Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
gmazzap committed Nov 2, 2018
2 parents 239f9bb + d519d64 commit d0970df
Show file tree
Hide file tree
Showing 9 changed files with 472 additions and 42 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ php:
- 5.5
- 5.6
- 7.0
- 7.1
- 7.2
- hhvm

matrix:
Expand All @@ -18,4 +20,4 @@ script:
- php vendor/bin/phpunit -c phpunit.xml.dist

after_success:
- bash <(curl -s https://codecov.io/bash)
- bash <(curl -s https://codecov.io/bash)
49 changes: 30 additions & 19 deletions src/Branch/BranchPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,28 @@

namespace Brain\Hierarchy\Branch;

use Brain\Hierarchy\PostTemplates;

/**
* @author Giuseppe Mazzapica <giuseppe.mazzapica@gmail.com>
* @license http://opensource.org/licenses/MIT MIT
*/
final class BranchPage implements BranchInterface
{

/**
* @var \Brain\Hierarchy\PostTemplates
*/
private $postTemplates;

/**
* @param \Brain\Hierarchy\PostTemplates|null $postTemplates
*/
public function __construct(PostTemplates $postTemplates = null)
{
$this->postTemplates = $postTemplates ?: new PostTemplates();
}

/**
* {@inheritdoc}
*/
Expand All @@ -39,31 +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'];
}

$name = $pagename ? $pagename : $post->post_name;
$template = $this->postTemplates->findFor($post);
$pagename = $query->get('pagename');
(!$pagename && $post->ID) and $pagename = $post->post_name;

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

$template = ($post->ID && $post->post_type === 'page')
? filter_var(get_page_template_slug($post), FILTER_SANITIZE_URL)
: false;
if (!$pagename) {
return array_merge($leaves, $baseLeaves);
}

if (!empty($template) && validate_file($template) === 0) {
$dir = dirname($template);
$filename = pathinfo($template, PATHINFO_FILENAME);
$name = $dir === '.' ? $filename : "{$dir}/{$filename}";
array_unshift($leaves, $name);
$pagenameDecoded = urldecode($pagename);
if ($pagenameDecoded !== $pagename) {
$leaves[] = "page-{$pagenameDecoded}";
}

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

return array_merge($leaves, $baseLeaves);
}
}
18 changes: 18 additions & 0 deletions src/Branch/BranchSingle.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,27 @@

namespace Brain\Hierarchy\Branch;

use Brain\Hierarchy\PostTemplates;

/**
* @author Giuseppe Mazzapica <giuseppe.mazzapica@gmail.com>
* @license http://opensource.org/licenses/MIT MIT
*/
final class BranchSingle implements BranchInterface
{
/**
* @var \Brain\Hierarchy\PostTemplates
*/
private $postTemplates;

/**
* @param \Brain\Hierarchy\PostTemplates|null $postTemplates
*/
public function __construct(PostTemplates $postTemplates = null)
{
$this->postTemplates = $postTemplates ?: new PostTemplates();
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -54,6 +69,9 @@ public function leaves(\WP_Query $query)
array_unshift($leaves, "single-{$post->post_type}-{$decoded}");
}

$template = $this->postTemplates->findFor($post);
$template and array_unshift($leaves, $template);

return $leaves;
}
}
76 changes: 76 additions & 0 deletions src/PostTemplates.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/*
* This file is part of the Hierarchy package.
*
* (c) Giuseppe Mazzapica <giuseppe.mazzapica@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Brain\Hierarchy;

/**
* @author Giuseppe Mazzapica <giuseppe.mazzapica@gmail.com>
* @license http://opensource.org/licenses/MIT MIT
*/
class PostTemplates
{

/**
* @var array[]
*/
private $templates = [];

/**
* @param \WP_Post $post
* @return string
*/
public function findFor(\WP_Post $post)
{
if (!$post->ID || !$post->post_type) {
return '';
}

$stored = filter_var(get_page_template_slug($post), FILTER_SANITIZE_URL);
if (!$stored || validate_file($stored) !== 0) {
return '';
}

$stored = wp_normalize_path($stored);
$templates = $this->templatesForType($post->post_type);

foreach ($templates as $template) {
if ($template === $stored) {
$dir = dirname($template);
$filename = pathinfo($template, PATHINFO_FILENAME);

return $dir === '.' ? $filename : "{$dir}/{$filename}";
}
}

return '';
}

/**
* @param string $postType
* @return string[]
*/
private function templatesForType($postType)
{
if (array_key_exists($postType, $this->templates)) {
return $this->templates[$postType];
}

$this->templates[$postType] = [];
$templates = (array)wp_get_theme()->get_page_templates(null, $postType);
foreach ($templates as $template => $header) {
if ($template && is_string($template)) {
$sanitized = filter_var($template, FILTER_SANITIZE_URL);
$this->templates[$postType][] = wp_normalize_path($sanitized);
}
}

return $this->templates[$postType];
}
}
9 changes: 8 additions & 1 deletion tests/src/Functional/HierarchyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use Brain\Hierarchy\Hierarchy;
use Brain\Hierarchy\Tests\TestCase;
use Brain\Monkey\Functions;
use Brain\Monkey\WP\Filters;

/**
Expand All @@ -29,6 +30,8 @@ public function testGetHierarchy()
$post->post_name = '%E3%81%B2%E3%82%89';
$post->post_type = 'book';

Functions::when('get_page_template_slug')->justReturn(false);

$query = new \WP_Query(
['is_single' => true, 'is_singular' => true],
$post,
Expand Down Expand Up @@ -75,6 +78,8 @@ public function testGetHierarchyFiltered()
return $leaves;
});

Functions::when('get_page_template_slug')->justReturn(false);

$post = \Mockery::mock('WP_Post');
$post->ID = 1;
$post->post_name = '%E3%81%B2%E3%82%89';
Expand Down Expand Up @@ -116,6 +121,8 @@ public function testGetHierarchyNotAppliesFiltersIfNotFiltered()
return $leaves;
});

Functions::when('get_page_template_slug')->justReturn(false);

$post = \Mockery::mock('WP_Post');
$post->ID = 1;
$post->post_name = '%E3%81%B2%E3%82%89';
Expand Down Expand Up @@ -148,4 +155,4 @@ public function testGetHierarchyNotAppliesFiltersIfNotFiltered()

$this->assertSame($expected, $actual);
}
}
}
21 changes: 19 additions & 2 deletions tests/src/Functional/QueryTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,22 @@ public function testLoadPageCustom()
$post->post_name = 'a-page';
$post->post_type = 'page';

Functions::expect('get_page_template_slug')->with($post)->andReturn('page-templates/page-custom.php');
Functions::expect('validate_file')->with('page-templates/page-custom.php')->andReturn(0);
$theme = Mockery::mock('\WP_Theme');
$theme
->shouldReceive('get_page_templates')
->andReturn(['page-templates/page-custom.php' => 'Custom']);

Functions::expect('wp_get_theme')->andReturn($theme);

Functions::expect('get_page_template_slug')
->with($post)
->andReturn('page-templates/page-custom.php');
Functions::expect('validate_file')
->with('page-templates/page-custom.php')
->andReturn(0);
Functions::expect('wp_normalize_path')
->with('page-templates/page-custom.php')
->andReturn('page-templates/page-custom.php');

$wpQuery = new \WP_Query(['is_page' => true], $post, ['pagename' => 'a-page']);

Expand All @@ -48,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
Loading

0 comments on commit d0970df

Please sign in to comment.