From f719c020502c37d8bacf2b85b1776e1a5b893f1e Mon Sep 17 00:00:00 2001 From: slawkens Date: Thu, 25 Jan 2024 23:06:10 +0100 Subject: [PATCH] Feature: auto-load themes (previously templates) from plugins/*/themes/* --- system/functions.php | 4 ++++ system/libs/plugins.php | 26 ++++++++++++++++++++++++++ system/template.php | 9 ++++++++- 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/system/functions.php b/system/functions.php index 2a052363..5c18b67c 100644 --- a/system/functions.php +++ b/system/functions.php @@ -798,6 +798,10 @@ function get_templates() $ret[] = $file; } + foreach (Plugins::getThemes() as $name => $path) { + $ret[] = $name; + } + return $ret; } diff --git a/system/libs/plugins.php b/system/libs/plugins.php index eb67c0d3..9f34536d 100644 --- a/system/libs/plugins.php +++ b/system/libs/plugins.php @@ -147,6 +147,32 @@ class Plugins { return $routes; } + public static function getThemes() + { + $cache = Cache::getInstance(); + if ($cache->enabled()) { + $tmp = ''; + if ($cache->fetch('plugins_themes', $tmp)) { + return unserialize($tmp); + } + } + + $themes = []; + $pluginThemes = glob(PLUGINS . '*/themes/*', GLOB_ONLYDIR); + foreach ($pluginThemes as $path) { + $path = str_replace(PLUGINS, 'plugins/', $path); + $name = pathinfo($path, PATHINFO_FILENAME); + + $themes[$name] = $path; + } + + if ($cache->enabled()) { + $cache->set('plugins_themes', serialize($themes), 600); + } + + return $themes; + } + public static function getHooks() { $cache = Cache::getInstance(); diff --git a/system/template.php b/system/template.php index 0a8775b0..0a47aa90 100644 --- a/system/template.php +++ b/system/template.php @@ -46,7 +46,14 @@ if(setting('core.template_allow_change')) } } } -$template_path = 'templates/' . $template_name; + +$themes = Plugins::getThemes(); +if (isset($themes[$template_name])) { + $template_path = $themes[$template_name]; +} +else { + $template_path = 'templates/' . $template_name; +} if(file_exists(BASE . $template_path . '/index.php')) { $template_index = 'index.php';