🎉 - Début développement plugin CustomTreeFile
This commit is contained in:
commit
a62549f1b4
146
CustomThemeTree.php
Normal file
146
CustomThemeTree.php
Normal file
@ -0,0 +1,146 @@
|
||||
<?php
|
||||
|
||||
namespace Skycel\CustomTree;
|
||||
|
||||
use WP_Theme;
|
||||
use Error;
|
||||
|
||||
class CustomThemeTree extends TemplateLoader {
|
||||
public WP_Theme $theme;
|
||||
|
||||
public string $theme_directory;
|
||||
public string $templates_directory;
|
||||
public string $stylesheets_directory;
|
||||
public string $config_directory;
|
||||
|
||||
public array $default = array(
|
||||
"templates"=> "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", "<?php\n\nconst TEMPLATES_DIR = 'templates';\nconst STYLESHEETS_DIR = 'assets';\n");
|
||||
}
|
||||
require_once $this->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", "<?php\n\necho '<h1 style=\'text-align:center;\'>New Files tree is operational !</h1>';");
|
||||
if (!file_exists($this->theme_directory . "/" . TEMPLATES_DIR . "/components/header.php")) file_put_contents($this->theme_directory . "/" . TEMPLATES_DIR . "/components/header.php", "<?php\n\n");
|
||||
if (!file_exists($this->theme_directory . "/" . TEMPLATES_DIR . "/components/footer.php")) file_put_contents($this->theme_directory . "/" . TEMPLATES_DIR . "/components/footer.php", "<?php\n\n");
|
||||
|
||||
if (!file_exists($this->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);
|
||||
}
|
||||
}
|
249
TemplateLoader.php
Normal file
249
TemplateLoader.php
Normal file
@ -0,0 +1,249 @@
|
||||
<?php
|
||||
|
||||
namespace Skycel\CustomTree;
|
||||
|
||||
use Error;
|
||||
use WP_Error;
|
||||
|
||||
class TemplateLoader {
|
||||
|
||||
public array $templates_parts;
|
||||
|
||||
protected function init() {
|
||||
add_filter("template_render", function($templates_path, $type) {
|
||||
// dd($templates_path);
|
||||
foreach ($templates_path as $path) {
|
||||
if (file_exists($path)) {
|
||||
// dd($path);
|
||||
load_template($path);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
return new WP_Error("404", "Template not found");
|
||||
}, 10, 2);
|
||||
}
|
||||
|
||||
public static function getTemplates($template, $type, $templates) {
|
||||
|
||||
if ($type === "home" || $type === "frontpage") $type = "page";
|
||||
$key = preg_replace("/y$/m", "ie", $type)."s";
|
||||
|
||||
$template_path = get_template_directory() . "/" . TEMPLATES_DIR . "/" . TEMPLATES_SUBDIRS[$key];
|
||||
|
||||
if ($type === "taxonomy") {
|
||||
foreach($templates as $i => $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;
|
||||
}
|
||||
}
|
29
custom-template.php
Normal file
29
custom-template.php
Normal file
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
// Add some functions to quickly load template
|
||||
|
||||
function tree_get_topbar($name = null, $args = array()) {
|
||||
|
||||
do_action("get_topbar", $name, $args);
|
||||
|
||||
return false;
|
||||
|
||||
$templates = array();
|
||||
$name = (string) $name;
|
||||
if ( '' !== $name ) {
|
||||
$templates[] = "topbar-{$name}.php";
|
||||
}
|
||||
|
||||
$templates[] = 'topbar.php';
|
||||
|
||||
if ( ! locate_template( $templates, true, true, $args ) ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function get_blog_url($blog_id = null) {
|
||||
if (null === $blog_id) $blog_id = get_current_blog_id();
|
||||
|
||||
if (get_current_blog_id() !== $blog_id && get_blogaddress_by_id($blog_id)) switch_to_blog($blog_id);
|
||||
|
||||
return get_post_type_archive_link( 'post' );
|
||||
}
|
29
custom-theme-tree.php
Normal file
29
custom-theme-tree.php
Normal file
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
/**
|
||||
* Plugin Name: Embedded themes tree
|
||||
* Description: Embed your theme tree files or custom as needed
|
||||
* Version: 1.0.0
|
||||
* Author: Skycel
|
||||
* Author URI: https://skycel.me
|
||||
*/
|
||||
|
||||
use Skycel\CustomTree\CustomThemeTree;
|
||||
|
||||
require_once __DIR__ . "/custom-template.php";
|
||||
require_once __DIR__ . "/functions.php";
|
||||
|
||||
function test_plugin(): void {
|
||||
if (!\defined('USE_CUSTOMTREE_PLUGIN') || USE_CUSTOMTREE_PLUGIN !== true) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!\defined("CUSTOMTREE") || !is_array(CUSTOMTREE)) {
|
||||
new CustomThemeTree();
|
||||
} else {
|
||||
new CustomThemeTree(\CUSTOMTREE);
|
||||
}
|
||||
|
||||
}
|
||||
add_action("after_setup_theme", "test_plugin");
|
||||
|
||||
//new CustomThemeTree();
|
32
functions.php
Normal file
32
functions.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
function array_merge_recursive_distinct(array ...$arrays): array {
|
||||
$merged = [];
|
||||
|
||||
foreach ($arrays as $array) {
|
||||
foreach ($array as $key => $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);
|
||||
}
|
3
todolist.md
Normal file
3
todolist.md
Normal file
@ -0,0 +1,3 @@
|
||||
# Idée pour le plugin
|
||||
|
||||
- [ ] Ajouté des composants supportés par le plugin (carousel, formulaires)
|
Loading…
x
Reference in New Issue
Block a user