<?php
declare(strict_types=1);
namespace MeinWeimar;
final class Templates
{
public static function hooks(): void
{
add_filter('single_template', [self::class, 'single_template']);
add_filter('archive_template', [self::class, 'archive_template']);
add_filter('template_include', [self::class, 'front_page_template'], 99);
}
/**
* Provide a usable public discovery home only for the untouched WordPress
* posts index. A configured static page or Elementor front page always
* remains in control of the site's homepage.
*/
public static function front_page_template(string $template): string
{
if (!is_front_page() || !is_home() || get_queried_object_id()) {
return $template;
}
$fallback = DIR . '/templates/front-page-mw.php';
return file_exists($fallback) ? $fallback : $template;
}
public static function single_template(string $template): string
{
$post_types = array_values(ContentTypes::public_post_types());
if (!is_singular($post_types)) {
return $template;
}
$post_type = get_post_type();
$theme_template = locate_template(['single-' . $post_type . '.php']);
if ($theme_template) {
return $theme_template;
}
$plugin_template = DIR . '/templates/single-' . $post_type . '.php';
if (file_exists($plugin_template)) {
return $plugin_template;
}
$generic_template = DIR . '/templates/single-mw-resource.php';
return file_exists($generic_template) ? $generic_template : $template;
}
public static function archive_template(string $template): string
{
$post_types = array_values(ContentTypes::public_post_types());
if (!is_post_type_archive($post_types)) {
return $template;
}
$post_type = get_query_var('post_type');
if (is_array($post_type)) {
$post_type = reset($post_type);
}
$post_type = is_string($post_type) ? $post_type : '';
if (!$post_type || !in_array($post_type, $post_types, true)) {
return $template;
}
// A theme or Elementor archive template always wins. The plugin only
// supplies a usable fallback for a clean WordPress installation.
$theme_template = locate_template(['archive-' . $post_type . '.php', 'archive.php']);
if ($theme_template) {
return $theme_template;
}
$plugin_template = DIR . '/templates/archive-mw-resource.php';
return file_exists($plugin_template) ? $plugin_template : $template;
}
}
