|
<?php |
|
class TemplateOverrideContainer { |
|
public $active = []; |
|
public $slugs = []; |
|
public $force; |
|
public $templates = []; |
|
public $found; |
|
public $core_template; |
|
|
|
public $options = [ 'index' => [], '404' => [], 'archive' => [], 'author' => [], 'category' => [], 'tag' => [], 'taxonomy' => [], 'date' => [], |
|
'embed' => [], 'home' => [], 'frontpage' => [], 'privacypolicy' => [], 'page' => [], 'paged' => [], 'search' => [], 'single' => [], 'singular' => [], 'attachment']; |
|
|
|
public function __construct($core_template, $force = false) |
|
{ |
|
$this->core_template = $core_template; |
|
$this->force = $force; |
|
} |
|
|
|
public function runTemplate($template) |
|
{ |
|
if($this->slugs[$this->found] ?? null) { |
|
call_user_func($this->slugs[$this->found]); |
|
} |
|
|
|
return $template; |
|
} |
|
|
|
public function blockTemplates($templates) |
|
{ |
|
$this->templates = $templates; |
|
|
|
return $this->force ? [] : $templates; |
|
} |
|
|
|
public function process($current, $type) |
|
{ |
|
$templates = $this->options[$type]; |
|
|
|
foreach ($this->templates as $temp) { |
|
if(in_array($temp, $templates)) { |
|
$this->found = $temp; |
|
|
|
return $this->core_template; |
|
} |
|
} |
|
|
|
return $current; |
|
} |
|
} |
|
|
|
class TemplateOverride { |
|
|
|
protected $container; |
|
|
|
public function __construct($core_template, $force = false) |
|
{ |
|
$this->container = new TemplateOverrideContainer($core_template, $force); |
|
} |
|
|
|
public function index($slug, $caller) |
|
{ |
|
return $this->template('index', $slug, $caller); |
|
} |
|
|
|
public function error($slug, $caller) |
|
{ |
|
return $this->template('404', $slug, $caller); |
|
} |
|
|
|
public function archive($slug, $caller) |
|
{ |
|
return $this->template('archive', $slug, $caller); |
|
} |
|
|
|
public function author($slug, $caller) |
|
{ |
|
return $this->template('author', $slug, $caller); |
|
} |
|
|
|
public function category($slug, $caller) |
|
{ |
|
return $this->template('category', $slug, $caller); |
|
} |
|
|
|
public function tag($slug, $caller) |
|
{ |
|
return $this->template('tag', $slug, $caller); |
|
} |
|
|
|
public function taxonomy($slug, $caller) |
|
{ |
|
return $this->template('taxonomy', $slug, $caller); |
|
} |
|
|
|
public function date($slug, $caller) |
|
{ |
|
return $this->template('date', $slug, $caller); |
|
} |
|
|
|
public function paged($slug, $caller) |
|
{ |
|
return $this->template('paged', $slug, $caller); |
|
} |
|
|
|
public function single($slug, $caller) |
|
{ |
|
return $this->template('single', $slug, $caller); |
|
} |
|
|
|
public function singular($slug, $caller) |
|
{ |
|
return $this->template('singular', $slug, $caller); |
|
} |
|
|
|
public function attachment($slug, $caller) |
|
{ |
|
return $this->template('attachment', $slug, $caller); |
|
} |
|
|
|
public function page($slug, $caller) |
|
{ |
|
return $this->template('page', $slug, $caller); |
|
} |
|
|
|
public function privacy($slug, $caller) |
|
{ |
|
return $this->template('privacypolicy', $slug, $caller); |
|
} |
|
|
|
public function front($slug, $caller) |
|
{ |
|
return $this->template('frontpage', $slug, $caller); |
|
} |
|
|
|
public function home($slug, $caller) |
|
{ |
|
return $this->template('home', $slug, $caller); |
|
} |
|
|
|
public function embed($slug, $caller) |
|
{ |
|
return $this->template('embed', $slug, $caller); |
|
} |
|
|
|
public function search($slug, $caller) |
|
{ |
|
return $this->template('search', $slug, $caller); |
|
} |
|
|
|
/** |
|
* @param string $type |
|
* @param string $slug |
|
* @param string|callable $caller |
|
* |
|
* @return $this |
|
*/ |
|
public function template($type, $slug, $caller) |
|
{ |
|
$this->container->active[$type] = true; |
|
$this->container->options[$type][] = $slug; |
|
$this->container->slugs[$slug] = $caller; |
|
|
|
return $this; |
|
} |
|
|
|
/** |
|
* Init hooks after templates have been registered |
|
*/ |
|
public function initHooks() |
|
{ |
|
foreach ($this->container->active as $type => $bool) { |
|
add_filter( "{$type}_template_hierarchy", [$this->container, 'blockTemplates'] ); |
|
add_filter( "{$type}_template", [$this->container, 'process'], 10, 2); |
|
add_filter( "template_include", [$this->container, 'runTemplate'], 100); |
|
} |
|
} |
|
} |