From a62549f1b4bef0be4434c936d05ed6cb2dbbbed0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9lian?= Date: Thu, 30 Jan 2025 04:50:02 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=89=20-=20D=C3=A9but=20d=C3=A9veloppem?= =?UTF-8?q?ent=20plugin=20CustomTreeFile?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CustomThemeTree.php | 146 +++++++++++++++++++++++++ TemplateLoader.php | 249 ++++++++++++++++++++++++++++++++++++++++++ custom-template.php | 29 +++++ custom-theme-tree.php | 29 +++++ functions.php | 32 ++++++ todolist.md | 3 + 6 files changed, 488 insertions(+) create mode 100644 CustomThemeTree.php create mode 100644 TemplateLoader.php create mode 100644 custom-template.php create mode 100644 custom-theme-tree.php create mode 100644 functions.php create mode 100644 todolist.md diff --git a/CustomThemeTree.php b/CustomThemeTree.php new file mode 100644 index 0000000..5556404 --- /dev/null +++ b/CustomThemeTree.php @@ -0,0 +1,146 @@ + "templates", + "stylesheets"=> "assets", + "templates_subdirs"=> [ + "archives"=> "archives", + "attachments"=> "attachments", + "authors"=> "authors", + "categories"=> "categories", + "components"=> "components", + "dates"=> "dates", + "errors"=> "errors", + "pages"=> "pages", + "search"=> "search", + "singles"=> "singles", + "tags"=> "tags", + "taxonomies"=> "taxonomies" + ], + "stylesheets_subdirs"=> [ + "css"=> "css" + ] + ); + + public function __construct($dirs = null, $config_file = "theme.php") { + + wp_set_template_globals(); + + if ($dirs) { + $this->default = array_merge_recursive_distinct($this->default, $dirs); + } + + $this->theme = wp_get_theme(); + $this->theme_directory = $this->theme->get_theme_root() . "/" . get_template(); + $this->config_directory = "config"; + + if (!file_exists($this->theme_directory . "/" . $this->config_directory . "/" . $config_file)) { + if (!is_dir($this->theme_directory . "/config" )) { + $this->config_directory = $this->create_directory("config"); + } + file_put_contents($this->theme_directory . "/" . $this->config_directory . "/theme.php", "theme_directory . "/" . $this->config_directory . "/" . $config_file; + + $this->init(); + } + + protected function init(): void + { + $this->create_custom_directories(); + $this->create_required_files(); + + TemplateLoader::init(); + + add_filter("index_template", [TemplateLoader::class, "getTemplate"], 100, 3); + add_filter("single_template", [TemplateLoader::class, "getTemplates"], 100, 3); + add_filter("404_template", [TemplateLoader::class, "getTemplate"], 100, 3); + + add_filter("frontpage_template", [TemplateLoader::class, "getTemplates"], 100, 3); + add_filter("home_template", [TemplateLoader::class, "getTemplates"], 100, 3); + add_filter("page_template", [TemplateLoader::class, "getTemplates"], 100, 3); + + add_filter("get_header", [TemplateLoader::class, "getComponents"], 100, 2); + add_filter("get_footer", [TemplateLoader::class, "getComponents"], 100, 2); + add_filter("get_sidebar", [TemplateLoader::class, "getComponents"], 100, 2); + add_filter("get_topbar", [TemplateLoader::class, "getComponents"], 100, 2); + + add_filter("archive_template", [TemplateLoader::class, "getTemplates"], 100, 3); + + add_filter("tag_template", [TemplateLoader::class, "getTags"], 100, 3); + + add_filter("category_template", [TemplateLoader::class, "getTemplates"], 100, 3); + add_filter("taxonomy_template", [TemplateLoader::class, "getTemplates"], 100, 3); + } + + private function create_custom_directories(): void { + // Make all directories for templates + + $this->templates_directory = $this->create_directory(TEMPLATES_DIR); + + if (!defined("TEMPLATES_SUBDIRS")) { + define("TEMPLATES_SUBDIRS", $this->default["templates_subdirs"]); + } + foreach (TEMPLATES_SUBDIRS as $subdir) { + $path = $this->theme_directory . "/" . TEMPLATES_DIR . "/" . $subdir; + if(is_dir($path) && is_dir_empty($path) && defined("USE_GITKEEP") && USE_GITKEEP) { + file_put_contents($path . "/.gitkeep", ""); + } else if (!USE_GITKEEP && file_exists($path . "/.gitkeep")) { + unlink($path . "/.gitkeep"); + } + $this->create_directory(TEMPLATES_DIR . "/" . $subdir); + } + + // Make all directories for stylesheets + $this->stylesheets_directory = $this->create_directory(STYLESHEETS_DIR); + + if (!defined("STYLESHEETS_SUBDIRS")) { + define("STYLESHEETS_SUBDIRS", $this->default["stylesheets_subdirs"]); + } + foreach (STYLESHEETS_SUBDIRS as $subdir) { + $this->create_directory(STYLESHEETS_DIR . "/" . $subdir); + } + } + + private function create_directory($dir_name): string|Error { + if (!file_exists($this->theme_directory . "/" . $dir_name)) { + $success = mkdir($this->theme_directory . "/" . $dir_name); + + if ($success) { + if (defined("USE_GITKEEP") && USE_GITKEEP) { + file_put_contents($this->theme_directory . "/" . $dir_name . "/" . ".gitkeep", ""); + } + return $this->theme_directory . "/" . $dir_name; + } else { + return new Error("Could not create the directory '" . $dir_name); + } + } + + return $this->theme_directory . "/" . $dir_name; + } + + private function create_required_files(): void { +// if (!file_exists($this->theme_directory . "/" . TEMPLATES_DIR . "/index.php")) file_put_contents($this->theme_directory . "/" . TEMPLATES_DIR . "/index.php", "New Files tree is operational !';"); + if (!file_exists($this->theme_directory . "/" . TEMPLATES_DIR . "/components/header.php")) file_put_contents($this->theme_directory . "/" . TEMPLATES_DIR . "/components/header.php", "theme_directory . "/" . TEMPLATES_DIR . "/components/footer.php")) file_put_contents($this->theme_directory . "/" . TEMPLATES_DIR . "/components/footer.php", "theme_directory . "/" . STYLESHEETS_DIR . "/style.css")) file_put_contents($this->theme_directory . "/" . STYLESHEETS_DIR . "/css/style.css", ""); + } + + public function custom_template_loader($name): void { + var_dump($name); + } +} \ No newline at end of file diff --git a/TemplateLoader.php b/TemplateLoader.php new file mode 100644 index 0000000..d6ce8f2 --- /dev/null +++ b/TemplateLoader.php @@ -0,0 +1,249 @@ + $t) { + $filename = preg_replace("/^taxonomy-/m", "", $t); + $tax = ""; + preg_match("/[A-z0-9]+/", $filename, $tax); + + if (count($templates) - 1 <= $i) { + $tax = ""; + } else { + $tax = $tax[0]."/"; + } + + $filename = str_replace( $tax . "-", "", $filename); + + $templates_parts[] = $template_path . "/" . $tax . $filename; + } + } else { + foreach ($templates as $t) { + $t = preg_replace("/^$type-/m", "", $t); + $templates_parts[] = $template_path . "/" . $t; + } + } + $templates_parts[] = get_template_directory() . "/" . TEMPLATES_DIR . "/index.php"; + do_action("template_render", $templates_parts, $type); + } + + + + // Old code + public static function getTemplate($template, $type, $templates): string|null { + if ($type === "index") { + return get_theme_root() . "/" . get_template() . "/" . TEMPLATES_DIR . "/" . $type . ".php"; + } elseif ($type === "single") { + $type = str_replace(".php", "", $templates[count($templates) - 2]); + return get_theme_root() . "/" . get_template() . "/" . TEMPLATES_DIR . "/" . TEMPLATES_SUBDIRS["components"] . "/" . $type . ".php"; + } else { + return get_theme_root() . "/" . get_template() . "/" . TEMPLATES_DIR . "/" . TEMPLATES_SUBDIRS["components"] . "/" . $type . ".php"; + } + } + + public static function getPages($template, $type, $templates): string|Error { + global $wp_stylesheet_path; + $wp_stylesheet_path = get_theme_root() ."/" . get_template() . "/" . TEMPLATES_DIR . "/" . TEMPLATES_SUBDIRS["pages"]; + + if ($type === "frontpage") { + $file = $wp_stylesheet_path . "/$type.php"; + + return file_exists($file) ? $file : $wp_stylesheet_path . "/index.php"; + } elseif ($type === "home") { + $file = $wp_stylesheet_path . "/home.php"; + + return file_exists($file) ? $file : $wp_stylesheet_path . "/index.php"; + } + + $template_name = ""; + foreach($templates as $i => $t) { + $filename = preg_replace("/^page-/m", "", $t); + + + if (file_exists($wp_stylesheet_path . "/" . $filename)) { + $template_name = $filename; + break; + } elseif ($filename === "page.php" && !file_exists($wp_stylesheet_path . "/" . $filename)) { + $template_name = "index.php"; + } else { + $wp_stylesheet_path = $wp_stylesheet_path . "/"; + $template_name = "index.php"; + } + } + + $file = $wp_stylesheet_path . "/" . $template_name; + if (!file_exists($file)) { + do_action("template_error", $template_name, $templates); + return throw new Error("Template file not found: " . $file); + } + + return $file; + } + /*public static function getPages($template, $type, $templates): string|Error { + global $wp_stylesheet_path; + $wp_stylesheet_path = get_theme_root() ."/" . get_template() . "/" . TEMPLATES_DIR . "/" . TEMPLATES_SUBDIRS["pages"]; + + if ($type === "frontpage") { + $file = $wp_stylesheet_path . "/$type.php"; + + return file_exists($file) ? $file : $wp_stylesheet_path . "/index.php"; + } elseif ($type === "home") { + $file = $wp_stylesheet_path . "/home.php"; + + return file_exists($file) ? $file : $wp_stylesheet_path . "/index.php"; + } + + $template_name = ""; + foreach($templates as $i => $t) { + $filename = preg_replace("/^page-/m", "", $t); + + + if (file_exists($wp_stylesheet_path . "/" . $filename)) { + $template_name = $filename; + break; + } elseif ($filename === "page.php" && !file_exists($wp_stylesheet_path . "/" . $filename)) { + $template_name = "index.php"; + } else { + $wp_stylesheet_path = $wp_stylesheet_path . "/"; + $template_name = "index.php"; + } + } + + $file = $wp_stylesheet_path . "/" . $template_name; + if (!file_exists($file)) { + do_action("template_error", $template_name, $templates); + return throw new Error("Template file not found: " . $file); + } + + return $file; + }*/ + + + + public static function getComponents($name, $args):string { + global $wp_stylesheet_path; + $wp_stylesheet_path = get_theme_root() . "/" . get_template() . "/" . TEMPLATES_DIR . "/" . TEMPLATES_SUBDIRS["components"]; + + $hook_name = current_filter(); + + $component_name = preg_replace("/^get_/m", "", $hook_name); + + $file = $wp_stylesheet_path . "/" . $component_name . ".php"; + + if (file_exists($file)) { + return $file; + } else { + return throw new Error("Template file not found: " . $file); + } + } + + public static function getArchives($template, $type, $templates): string { + global $wp_stylesheet_path; + $wp_stylesheet_path = get_theme_root() . "/" . get_template() . "/" . TEMPLATES_DIR . "/" . TEMPLATES_SUBDIRS["archives"]; + + $template_name = preg_replace("/^archive-/m", "", str_replace(".php", "", $templates[count($templates) - 2])); + + return $wp_stylesheet_path . "/" . $template_name . ".php"; + } + + public static function getTags($template, $type, $templates): string|Error { + global $wp_stylesheet_path; + $wp_stylesheet_path = get_theme_root() . "/" . get_template() . "/" . TEMPLATES_DIR . "/" . TEMPLATES_SUBDIRS["tags"]; + + $template_name = ""; + foreach($templates as $i => $t) { + $filename = preg_replace("/^tag-/m", "", $t); + if (file_exists($wp_stylesheet_path . "/" . $filename)) { + $template_name = $filename; + break; + } else { + $template_name = "index.php"; + } + } + + $file = $wp_stylesheet_path . "/" . $template_name; + if (!file_exists($file)) return throw new Error("Template file not found: " . $file); + + return $file; + } + + public static function getCategories($template, $type, $templates): string|Error { + global $wp_stylesheet_path; + $wp_stylesheet_path = get_theme_root() . "/" . get_template() . "/" . TEMPLATES_DIR . "/" . TEMPLATES_SUBDIRS["categories"]; + + $template_name = ""; + foreach($templates as $i => $t) { + $filename = preg_replace("/^category-/m", "", $t); + if (file_exists($wp_stylesheet_path . "/" . $filename)) { + $template_name = $filename; + break; + } else { + $template_name = "index.php"; + } + } + + $file = $wp_stylesheet_path . "/" . $template_name; + if (!file_exists($file)) return throw new Error("Template file not found: " . $file); + + return $file; + } + + public static function getTaxonomies($template, $type, $templates): string|Error { + global $wp_stylesheet_path; + $wp_stylesheet_path = get_theme_root() . "/" . get_template() . "/" . TEMPLATES_DIR . "/" . TEMPLATES_SUBDIRS["taxonomies"]; + + $template_name = ""; + foreach($templates as $i => $t) { + $filename = preg_replace("/^taxonomy-/m", "", $t); + $tax = ""; + preg_match("/[A-z0-9]+/", $filename, $tax); + + $filename = str_replace($tax[0] . "-", "", $filename); + var_dump($filename); + if (file_exists($wp_stylesheet_path . "/" . $tax[0] . "/" . $filename)) { + $template_name = $tax[0] . "/" . str_replace($tax[0] . "-", "", $filename); + break; + } else if (file_exists($wp_stylesheet_path . "/" . $tax[0] . "/index.php")) { + $template_name = $tax[0] . "/index.php"; + break; + } else { + $template_name = "index.php"; + } + } + + $file = $wp_stylesheet_path . "/" . $template_name; + if (!file_exists($file)) return throw new Error("Template file not found: " . $file); + + return $file; + } +} \ No newline at end of file diff --git a/custom-template.php b/custom-template.php new file mode 100644 index 0000000..13e4c3c --- /dev/null +++ b/custom-template.php @@ -0,0 +1,29 @@ + $value) { + if (is_array($value) && array_key_exists($key, $merged) && is_array($merged[$key])) { + // Appel récursif pour fusionner les tableaux enfants + $merged[$key] = array_merge_recursive_distinct($merged[$key], $value); + } elseif (is_array($value) && !array_key_exists($key, $merged)) { + // Ajouter les nouveaux tableaux si la clé n'existe pas encore + $merged[$key] = $value; + } elseif (!is_array($value)) { + if (is_int($key)) { + // Ajouter une valeur unique dans les clés numériques + if (!in_array($value, $merged, true)) { + $merged[] = $value; + } + } else { + // Écraser pour les clés associatives + $merged[$key] = $value; + } + } + } + } + + return $merged; +} + +function is_dir_empty($dir): bool { + return (count(scandir($dir)) == 2); +} \ No newline at end of file diff --git a/todolist.md b/todolist.md new file mode 100644 index 0000000..e800247 --- /dev/null +++ b/todolist.md @@ -0,0 +1,3 @@ +# Idée pour le plugin + +- [ ] Ajouté des composants supportés par le plugin (carousel, formulaires) \ No newline at end of file