🚚 - Place les fichiers de class dans le dossier src et modifie les chemins pour les fichiers inclus

This commit is contained in:
2025-02-06 05:40:32 +01:00
parent bc01dc75a9
commit 8c2060586a
2 changed files with 2 additions and 2 deletions

191
src/CustomThemeTree.php Normal file
View File

@@ -0,0 +1,191 @@
<?php
namespace Skycel\CustomTree;
use WP_Theme;
use Error;
// Load Template Loader first
require_once __DIR__ . '/TemplateLoader.php';
/**
* Class for managing custom theme directory structures and settings.
* Provides functionality for initializing themes, creating custom directories,
* handling templates and stylesheets, and loading configurations.
*/
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",
"pages"=> "pages",
"singles"=> "singles",
"tags"=> "tags",
"taxonomies"=> "taxonomies"
],
"stylesheets_subdirs"=> [
"css"=> "css"
]
);
/**
* Constructor method to initialize theme settings and configuration.
* Handles the creation of a configuration directory and file if they do not exist,
* and merges custom configurations if enabled.
*
* @return void
*/
public function __construct() {
$this->includes();
$this->theme = wp_get_theme();
$this->theme_directory = $this->theme->get_theme_root() . "/" . get_template();
$this->config_directory = "config";
if (!file_exists(trailingslashit($this->theme_directory) . trailingslashit($this->config_directory) . "theme.php")) {
if (!is_dir(trailingslashit($this->theme_directory) . $this->config_directory )) {
$this->config_directory = $this->create_directory($this->config_directory);
}
file_put_contents($this->theme_directory . "/" . $this->config_directory . "/theme.php", "<?php\n\nconst USE_GITKEEP = true;\n\nconst USE_CUSTOMTREE_PLUGIN = true;\nconst CUSTOMTREE = array()");
}
require_once $this->theme_directory . "/" . $this->config_directory . "/" . "theme.php";
if (!USE_CUSTOMTREE_PLUGIN) return;
if (is_array(CUSTOMTREE) && count(CUSTOMTREE) > 0) {
$this->default = array_merge_recursive_distinct($this->default, CUSTOMTREE);
}
define("Skycel\CustomTree\TEMPLATES_DIR", $this->default["templates"]);
define("Skycel\CustomTree\STYLESHEETS_DIR", $this->default["stylesheets"]);
$this->init();
}
protected function includes(): void {
require_once __DIR__ . "/../includes/custom-template.inc.php";
require_once __DIR__ . "/../includes/functions.inc.php";
}
/**
* Initializes custom directories, required files, and sets up template and component loaders.
*
* @return void
*/
protected function init(): void
{
$this->create_custom_directories();
$this->create_required_files();
TemplateLoader::init();
$template_types = get_default_block_template_types();
foreach ($template_types as $type => $opt) {
add_filter("{$type}_template", [TemplateLoader::class, 'getTemplates'], 10, 3);
}
// Component loader
$default_components = [
"header", "footer",
"sidebar", "search_form",
"topbar"
];
foreach ($default_components as $component) {
add_filter("get_$component", [TemplateLoader::class, 'getComponents'], 10, 2);
}
}
/**
* Creates custom directories for templates and stylesheets, including handling subdirectories and optional .gitkeep files.
*
* @return void
*/
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);
}
}
/**
* Creates a specified directory within the theme directory. If the directory does
* not exist, it creates it and optionally adds a .gitkeep file if enabled.
*
* @param string $dir_name The name of the directory to be created.
* @return string|Error The path to the created directory or an error object if the creation fails.
*/
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;
}
/**
* Creates required template and stylesheet files if they do not already exist.
*
* @return void
*/
private function create_required_files(): void {
// Create Wordpress required files
if (!file_exists($this->theme_directory . "/index.php")) file_put_contents($this->theme_directory . "/index.php", "<?php\n/**\n * This is a required file for WordPress themes\n */\n\n// Silence is golden.");
if (!file_exists($this->theme_directory . "/header.php")) file_put_contents($this->theme_directory . "/header.php", "<?php\n/**\n * This is a required file for WordPress themes\n */\n\n// Silence is golden.");
if (!file_exists($this->theme_directory . "/footer.php")) file_put_contents($this->theme_directory . "/footer.php", "<?php\n/**\n * This is a required file for WordPress themes\n */\n\n// Silence is golden.");
if (!file_exists($this->theme_directory . "/" . STYLESHEETS_DIR . "/style.css")) file_put_contents($this->theme_directory . "/" . STYLESHEETS_DIR . "/css/style.css", "");
// Create CustomThemeTree required files
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");
}
}

261
src/TemplateLoader.php Normal file
View File

@@ -0,0 +1,261 @@
<?php
namespace Skycel\CustomTree;
use WP_Error;
/**
* Handles loading and rendering of WordPress templates based on their type
* and subdirectories. Supports various template types, such as pages, archives,
* taxonomies, components, and more.
*/
class TemplateLoader {
static array $templates_parts;
/**
* Initializes the template rendering process.
*
* @return void
*/
protected function init() {
add_filter("template_render", function ($templates_path, $type) {
foreach ($templates_path as $path) {
if (file_exists($path)) {
load_template($path);
exit;
}
}
return new WP_Error("404", "Template not found");
}, 10, 2);
}
/**
* Retrieves and processes templates based on the specified type and template data.
*
* @param string $template The primary template name or path.
* @param string $type The type of template being requested (e.g., "home", "frontpage").
* @param array $templates A list of additional template names or paths.
*
* @return void
*/
public static function getTemplates($template, $type, $templates) {
if ($type === "home" || $type === "frontpage") $type = "page";
$key = (int)$type !== 0 ? "errors" : preg_replace("/y$/m", "ie", $type)."s";
$template_path = get_template_directory() . "/" . TEMPLATES_DIR . "/" . @TEMPLATES_SUBDIRS[$key];
$func_name = "get".ucfirst($key);
call_user_func([self::class, $func_name], $template_path, $type, $templates);
self::$templates_parts[] = get_template_directory() . "/" . TEMPLATES_DIR . "/index.php";
do_action("template_render", self::$templates_parts, $type);
}
/**
* Processes templates and builds template parts based on the given type and path.
*
* @param string $template_path The base path for the templates.
* @param string $type The type prefix to be removed from template names.
* @param array $templates List of templates to process.
* @return void
*/
public static function getDefault($template_path, $type, $templates): void {
foreach ($templates as $t) {
$t = preg_replace("/^$type-/m", "", $t);
self::$templates_parts[] = $template_path . "/" . $t;
}
}
/**
* Retrieves pages based on the provided template path, type, and templates.
*
* @param string $template_path The path to the template.
* @param string $type The type of the template.
* @param array $templates The list of templates.
* @return void
*/
public static function getPages($template_path, $type, $templates): void {
self::getDefault($template_path, $type, $templates);
}
/**
* Retrieves and processes archive templates.
*
* @param string $template_path Path to the template.
* @param string $type Type of template.
* @param array $templates List of templates.
* @return void
*/
public static function getArchives($template_path, $type, $templates): void {
self::getDefault($template_path, $type, $templates);
}
/**
* Retrieves authors based on the provided parameters.
*
* @param string $template_path The path of the template.
* @param string $type The type of the template.
* @param array $templates The list of templates.
* @return void
*/
public static function getAuthors($template_path, $type, $templates): void {
self::getDefault($template_path, $type, $templates);
}
/**
* Retrieves errors by processing templates with a default method.
*
* @param string $template_path Path to the template directory.
* @param string $type Type of templates being processed.
* @param array $templates List of template files.
* @return void
*/
public static function getErrors($template_path, $type, $templates): void {
self::getDefault($template_path, $type, $templates);
}
/**
* Retrieves and processes category templates.
*
* @param string $template_path Path to the template directory.
* @param string $type Type of the template.
* @param array $templates List of templates to process.
* @return void
*/
public static function getCategories($template_path, $type, $templates): void {
self::getDefault($template_path, $type, $templates);
}
/**
* Generates an array of single templates with processed paths and stores them in the template parts.
*
* @param string $template_path The base path to the templates.
* @param string $type The type of template being processed.
* @param array $templates A list of template names to process.
* @return void
*/
public static function getSingles($template_path, $type, $templates): void {
$tmpl = [];
foreach($templates as $i => $t) {
$filename = preg_replace("/^single-/m", "", $t);
$tax = "";
preg_match("/[A-z0-9]+/", $filename, $tax);
$tax = count($templates) - 1 <= $i ? "" : trailingslashit($tax[0]);
$filename = str_replace( str_replace("/", "", $tax) . "-", "", $filename);
$tmpl[] = trailingslashit($template_path) . $tax . $filename;
}
$tmpl[] = $template_path . "/singular.php";
self::$templates_parts = $tmpl;
}
/**
* Generates and sets attachment file paths based on provided templates.
*
* @param string $template_path Base path for the templates.
* @param string $type Type of attachment (not explicitly used in method logic).
* @param array $templates List of template filenames.
* @return void
*/
public static function getAttachments($template_path, $type, $templates): void {
foreach($templates as $i => $t) {
$folder = "";
preg_match("/^([A-z]+)-/m", $t, $folder);
$folder = count($folder) > 1 ? trailingslashit($folder[1]) : "";
$filename = sanitize_file_name(str_replace(sanitize_file_name($folder), "", $t));
self::$templates_parts[] = trailingslashit($template_path) . $folder . $filename;
}
}
/**
* Updates the list of templates with taxonomy-specific paths based on the provided data.
*
* @param string $template_path The base path to the templates.
* @param string $type The type of taxonomy being processed.
* @param array $templates An array of template file names to process.
*
* @return void
*/
public static function getTaxonomies($template_path, $type, $templates): void {
$tmpl = [];
foreach($templates as $i => $t) {
$filename = preg_replace("/^taxonomy-/m", "", $t);
$tax = "";
preg_match("/[A-z0-9]+/", $filename, $tax);
$tax = count($templates) - 1 <= $i ? "" : trailingslashit($tax[0]);
$filename = str_replace( str_replace("/", "", $tax) . "-", "", $filename);
$tmpl[] = trailingslashit($template_path) . $tax . $filename;
}
self::$templates_parts = $tmpl;
}
/**
* Retrieves tags based on the provided parameters.
*
* @param string $template_path The path to the template.
* @param string $type The type of the template.
* @param array $templates The list of templates.
* @return void
*/
public static function getTags($template_path, $type, $templates): void {
self::getDefault($template_path, $type, $templates);
}
/**
* Generates date-based template paths and appends them to the templates parts list.
*
* @param string $template_path The base path for the templates.
* @param string $type The type of template to append.
* @param array $templates Not used in this method.
* @return void
*/
public static function getDates($template_path, $type, $templates): void {
self::$templates_parts[] = is_year() ? $template_path . "/year.php" : (is_month() ? $template_path ."/month.php" : $template_path . "/day.php");
self::$templates_parts[] = $template_path . "/$type.php";
}
/**
* Appends a search template path to the templates array.
*
* @param string $template_path The base path to the templates.
* @param string $type The type of the search template.
* @param array $templates The list of existing templates.
* @return void
*/
public static function getSearchs($template_path, $type, $templates): void {
self::$templates_parts[] = $template_path . "/$type.php";
}
/**
* Retrieves and processes the components associated with the current filter hook.
*
* @param string $name The name of the component being retrieved.
* @param mixed|null $args Optional. Additional arguments passed to the component.
* @return void
*/
public static function getComponents($name, $args = null) {
$hook_name = current_filter();
$component_name = preg_replace("/^get_/m", "", $hook_name);
$template_path = get_template_directory() . "/" . TEMPLATES_DIR . "/" . TEMPLATES_SUBDIRS["components"];
$components[] = $template_path . "/" . $component_name . ".php";
do_action("template_render", $components, $hook_name);
}
}