Remove wrapper and template classes
This commit is contained in:
@@ -1,46 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Roots\Sage;
|
|
||||||
|
|
||||||
use Roots\Sage\Template\Partial;
|
|
||||||
use Roots\Sage\Template\WrapperInterface;
|
|
||||||
|
|
||||||
class Template
|
|
||||||
{
|
|
||||||
/** @var Template[] */
|
|
||||||
public static $instances = [];
|
|
||||||
|
|
||||||
/** @var WrapperInterface */
|
|
||||||
protected $wrapper;
|
|
||||||
|
|
||||||
public function __construct(WrapperInterface $wrapper)
|
|
||||||
{
|
|
||||||
$this->wrapper = $wrapper;
|
|
||||||
self::$instances[$wrapper->slug()] = $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string Layout (FQPN of, e.g., `base-page.php`, `base.php`)
|
|
||||||
*/
|
|
||||||
public function layout()
|
|
||||||
{
|
|
||||||
return $this->wrapper->wrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string Main template (FQPN of, e.g., `page.php`, `single.php`, `singular.php`)
|
|
||||||
*/
|
|
||||||
public function main()
|
|
||||||
{
|
|
||||||
return $this->wrapper->unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $template Delimited template path
|
|
||||||
* @return string Partial template (FQPN of, e.g., `content.php`, `page-header.php`
|
|
||||||
*/
|
|
||||||
public function partial($template)
|
|
||||||
{
|
|
||||||
return (new Partial($template, $this->main()))->path();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,72 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Roots\Sage\Template;
|
|
||||||
|
|
||||||
class Partial
|
|
||||||
{
|
|
||||||
protected static $cache = [];
|
|
||||||
|
|
||||||
public $main;
|
|
||||||
public $template;
|
|
||||||
public $delimiter = '-';
|
|
||||||
|
|
||||||
public function __construct($template, $main = '')
|
|
||||||
{
|
|
||||||
$this->template = $template;
|
|
||||||
$this->main = $main;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function __toString()
|
|
||||||
{
|
|
||||||
return (string) $this->path();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Converts template into array of parts to be passed to locate_template()
|
|
||||||
*
|
|
||||||
* Here's an example of what happens:
|
|
||||||
* (new Template('partials/content-single-audio'))->parts();
|
|
||||||
* // => ['partials/content-single-audio.php', 'partials/content-single.php', 'partials/content.php']
|
|
||||||
* @return array Array of parts to pass to locate_template()
|
|
||||||
*/
|
|
||||||
public function parts()
|
|
||||||
{
|
|
||||||
if ($parts = $this->cache('parts')) {
|
|
||||||
return $parts;
|
|
||||||
}
|
|
||||||
$parts = explode($this->delimiter, str_replace('.php', '', $this->template));
|
|
||||||
$templates[] = array_shift($parts);
|
|
||||||
foreach ($parts as $i => $part) {
|
|
||||||
$templates[] = $templates[$i] . $this->delimiter . $part;
|
|
||||||
}
|
|
||||||
if ($this->main) {
|
|
||||||
$templates = array_merge($templates, array_map(function ($template) {
|
|
||||||
return $template . $this->delimiter . basename($this->main, '.php');
|
|
||||||
}, $templates));
|
|
||||||
}
|
|
||||||
$templates = array_map(function ($template) {
|
|
||||||
return $template . '.php';
|
|
||||||
}, $templates);
|
|
||||||
return $this->cache('parts', array_reverse($templates));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Passes $this->parts() to locate_template() to retrieve template location
|
|
||||||
* @return string Location of template
|
|
||||||
*/
|
|
||||||
public function path()
|
|
||||||
{
|
|
||||||
if (!$path = $this->cache('path')) {
|
|
||||||
$path = $this->cache('path', locate_template($this->parts()));
|
|
||||||
}
|
|
||||||
return apply_filters('sage/partial_' . basename($path, '.php'), $path, $this->parts()) ?: $path;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function cache($key, $value = null)
|
|
||||||
{
|
|
||||||
if ($value !== null) {
|
|
||||||
self::$cache[$this->template][$key] = $value;
|
|
||||||
}
|
|
||||||
return isset(self::$cache[$this->template][$key]) ? self::$cache[$this->template][$key] : null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,64 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Roots\Sage\Template;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Class Wrapper
|
|
||||||
* @package Roots\Sage
|
|
||||||
* @author QWp6t
|
|
||||||
*/
|
|
||||||
class Wrapper implements WrapperInterface
|
|
||||||
{
|
|
||||||
/** @var string Wrapper slug */
|
|
||||||
protected $slug;
|
|
||||||
|
|
||||||
/** @var string Template file that is being wrapped */
|
|
||||||
protected $template = '';
|
|
||||||
|
|
||||||
/** @var string[] Array of template wrappers; e.g., `base-singular.php`, `base-page.php`, `base.php` */
|
|
||||||
protected $wrapper = [];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Wrapper constructor
|
|
||||||
*
|
|
||||||
* @param string $template Template file, as from Template Hierarchy; e.g., `page.php`, `single.php`, `singular.php`
|
|
||||||
* @param string $base Wrapper's base template, this is what will wrap around $template
|
|
||||||
*/
|
|
||||||
public function __construct($template, $base = 'layouts/base.php')
|
|
||||||
{
|
|
||||||
$this->slug = sanitize_title(basename($base, '.php'));
|
|
||||||
$this->wrapper = [$base];
|
|
||||||
$this->template = $template;
|
|
||||||
$str = substr($base, 0, -4);
|
|
||||||
array_unshift($this->wrapper, sprintf($str . '-%s.php', basename($template, '.php')));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
* @see getTemplate
|
|
||||||
*/
|
|
||||||
public function __toString()
|
|
||||||
{
|
|
||||||
return $this->unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
/** {@inheritdoc} */
|
|
||||||
public function wrap()
|
|
||||||
{
|
|
||||||
$wrappers = apply_filters('sage/wrap_' . $this->slug, $this->wrapper) ?: $this->wrapper;
|
|
||||||
return locate_template($wrappers);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** {@inheritdoc} */
|
|
||||||
public function slug()
|
|
||||||
{
|
|
||||||
return $this->slug;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** {@inheritdoc} */
|
|
||||||
public function unwrap()
|
|
||||||
{
|
|
||||||
$template = apply_filters('sage/unwrap_' . $this->slug, $this->template) ?: $this->template;
|
|
||||||
return locate_template($template) ?: $template;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Roots\Sage\Template;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Interface WrapperInterface
|
|
||||||
* @package Roots\Sage
|
|
||||||
* @author QWp6t
|
|
||||||
*/
|
|
||||||
interface WrapperInterface
|
|
||||||
{
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get wrapper template file
|
|
||||||
*
|
|
||||||
* @return string Wrapper template (FQPN of, e.g., `base-page.php`, `base.php`)
|
|
||||||
*/
|
|
||||||
public function wrap();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string Wrapped template (FQPN of, e.g., `page.php`, `single.php`, `singular.php`)
|
|
||||||
*/
|
|
||||||
public function unwrap();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string Slug of the WrapperInterface; e.g., `base`
|
|
||||||
*/
|
|
||||||
public function slug();
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user