better singleton instance

This commit is contained in:
2025-05-07 20:26:08 -07:00
parent a9aca43246
commit 60a0472e53
3 changed files with 17 additions and 11 deletions

View File

@@ -3,6 +3,7 @@
return [ return [
"debug" => true, "debug" => true,
"flex-file-prefix" => "flex", "flex-file-prefix" => "flex",
"excerpt-more-text" => "…",
/** /**
* excerpt-length * excerpt-length
@@ -75,8 +76,6 @@ return [
"menus", "menus",
], ],
"excerpt_more_text" => "…",
/** /**
* disable * disable
* *

View File

@@ -5,12 +5,7 @@ require __DIR__ . '/vendor/autoload.php';
use ofc\Site; use ofc\Site;
# Declare a new Site object
$site = new Site();
# Allow the Site object to be accessed in other files
function site(): Site function site(): Site
{ {
global $site; return Site::getInstance();
return $site;
} }

View File

@@ -17,7 +17,18 @@ class Site
private $templateDirectory; private $templateDirectory;
public $cptSlugs = []; public $cptSlugs = [];
public function __construct($config = []) // singleton instance goes here
private static ?Site $instance = null;
public static function getInstance(): Site
{
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
private function __construct($config = [])
{ {
if ($config == [] && file_exists(TEMPLATEPATH . "/config.php")) { if ($config == [] && file_exists(TEMPLATEPATH . "/config.php")) {
$config = include(TEMPLATEPATH . "/config.php"); $config = include(TEMPLATEPATH . "/config.php");
@@ -34,8 +45,8 @@ class Site
wp_dequeue_style('classic-theme-styles'); wp_dequeue_style('classic-theme-styles');
}, 20); }, 20);
if (isset($this->config["excerpt_more_text"])) { if (isset($this->config["excerpt-more-text"])) {
add_filter('excerpt_more', fn ($more) => $this->config["excerpt_more_text"]); add_filter('excerpt_more', fn ($more) => $this->config["excerpt-more-text"]);
} }
// disable various things // disable various things
@@ -601,6 +612,7 @@ class Site
"flex" => \ofc\RadThemeEngine::processFlex(), "flex" => \ofc\RadThemeEngine::processFlex(),
"nl2br" => \ofc\RadThemeEngine::nl2br(), "nl2br" => \ofc\RadThemeEngine::nl2br(),
"assetURL" => \ofc\RadThemeEngine::assetURL(), "assetURL" => \ofc\RadThemeEngine::assetURL(),
"assetUrl" => \ofc\RadThemeEngine::assetURL(),
"assetContents" => \ofc\RadThemeEngine::assetContents(), "assetContents" => \ofc\RadThemeEngine::assetContents(),
]; ];
foreach ($helpers as $name => $callback) { foreach ($helpers as $name => $callback) {