init();
}
return self::$_instance;
}
private function init()
{
try {
$this->constant();
// If dependencies fail to load, stop initialization gracefully
if (!$this->includes()) {
$this->bootFailed = true;
return;
}
$this->preLoad();
$this->load();
} catch (Throwable $e) {
$this->bootFailed = true;
$this->log_error('Plugin Boot', $e);
}
}
private function constant()
{
$defaultHeaders = [
'name' => 'Plugin Name',
'version' => 'Version',
];
$pluginData = function_exists('get_file_data') && defined('ACADLIX_PLUGIN_FILE')
? get_file_data(ACADLIX_PLUGIN_FILE, $defaultHeaders)
: ['name' => 'Acadlix', 'version' => '1.0.0'];
$constants = [
'ACADLIX_PLUGIN_NAME' => $pluginData['name'] ?? 'Acadlix',
'ACADLIX_VERSION' => $pluginData['version'] ?? '1.0.0',
'ACADLIX_MARKETPLACE_URL' => 'https://acadlix.com/',
'ACADLIX_DOCUMENTATION_URL' => 'https://acadlix.com/docs/acadlix/',
'ACADLIX_CONTACT_US_URL' => 'https://acadlix.com/contact-us/',
'ACADLIX_MARKETING_DOMAIN' => 'acadlix.com',
'ACADLIX_YOUTUBE_CHANNEL_URL' => 'https://www.youtube.com/@AcadLix',
'ACADLIX_WHATSAPP_URL' => 'https://wa.me/918755077650?text=Hi+I+need+support+with+Acadlix+Free+version.&utm_source=chatgpt.com',
'ACADLIX_SUPPORT_URL' => 'https://wordpress.org/support/plugin/acadlix/',
];
foreach ($constants as $constant => $value) {
if (!defined($constant)) {
define($constant, $value); // phpcs:ignore
}
}
$this->isDev = defined('WP_ENVIRONMENT_TYPE') && WP_ENVIRONMENT_TYPE === 'local';
$this->version = ACADLIX_VERSION;
}
private function includes()
{
$dependencies = [
'/vendor/autoload.php' => true,
// 'bootstrap.php' => true
];
foreach ($dependencies as $path => $shouldRequire) {
$fullPath = defined('ACADLIX_PLUGIN_DIR') ? ACADLIX_PLUGIN_DIR . $path : '';
if (empty($fullPath) || !file_exists($fullPath)) {
// Log error and notify admin instead of wp_die()
add_action('admin_notices', function () {
if (current_user_can('activate_plugins')) {
echo '
' . esc_html__('Acadlix is missing required vendor files and could not load safely. Please reinstall the plugin.', 'acadlix') . '
';
}
});
return false;
}
if ($shouldRequire) {
require_once ACADLIX_PLUGIN_DIR . $path;
}
}
$this->loadVersion();
return true;
}
private function loadVersion()
{
$proDir = defined('ACADLIX_INCLUDES_PATH') && is_dir(ACADLIX_INCLUDES_PATH . 'Pro');
$this->pro = $proDir;
$this->versionPath = $proDir ? 'Pro' : 'Common';
}
private function preLoad()
{
$this->database = $this->safeClassInit('Common\Models\Database');
$this->license = $this->pro ? $this->safeClassInit('Pro\License\License') : null;
$this->helper = $this->pro ? $this->safeClassInit('Pro\Helper\Helper') : $this->safeClassInit('Common\Helper\Helper');
$this->feedback = $this->safeClassInit('Common\Feedback\Feedback');
}
private function load()
{
$modules = [
'blocks' => ['pro' => 'Common\Blocks\Blocks', 'common' => 'Common\Blocks\Blocks'],
'admin' => ['pro' => 'Pro\Admin\Admin', 'common' => 'Common\Admin\Admin'],
'assets' => ['pro' => 'Pro\Assets\Assets', 'common' => 'Common\Assets\Assets'],
'controller' => ['pro' => 'Pro\Controller\Controller', 'common' => 'Common\Controller\Controller'],
'view' => ['pro' => 'Pro\View\View', 'common' => 'Common\View\View'],
'cpt' => ['pro' => 'Pro\CPT\CPT', 'common' => 'Common\CPT\CPT'],
'migration' => ['pro' => 'Pro\Migrations\Migrations', 'common' => 'Common\Migrations\Migrations'],
'model' => ['pro' => 'Pro\Models\Models', 'common' => 'Common\Models\Models'],
'seeder' => ['pro' => 'Pro\Seeder\Seeder', 'common' => 'Common\Seeder\Seeder'],
'rest' => ['pro' => 'Pro\REST\REST', 'common' => 'Common\REST\REST'],
'ai' => ['pro' => 'Pro\Ai\Ai', 'common' => 'Common\Ai\Ai'],
'integrations' => ['pro' => 'Pro\Integrations\Integrations', 'common' => 'Common\Integrations\Integrations'],
'payments' => ['pro' => 'Pro\Payments\Payments', 'common' => 'Common\Payments\Payments'],
'schedule' => ['pro' => 'Pro\Schedule\ScheduleAction', 'common' => 'Common\Schedule\ScheduleAction'],
'notifications' => ['pro' => 'Pro\Notifications\Notifications', 'common' => 'Common\Notifications\Notifications'],
'authentications' => ['pro' => 'Pro\Authentications\Authentications', 'common' => 'Common\Authentications\Authentications'],
'email' => ['pro' => 'Pro\Email\Email', 'common' => 'Common\Email\Email'],
'sms' => ['pro' => 'Pro\SMS\SMS', 'common' => 'Common\SMS\SMS'],
];
foreach ($modules as $property => $classes) {
$className = $this->pro ? $classes['pro'] : $classes['common'];
$this->$property = $this->safeClassInit($className);
}
}
/**
* Safely instantiate classes without crashing execution if a class or dependency is missing.
*/
private function safeClassInit(string $classSuffix)
{
$fullClass = __NAMESPACE__ . '\\' . $classSuffix;
if (!class_exists($fullClass)) {
// Try fallback to Common namespace if Pro class was missing
if (strpos($classSuffix, 'Pro\\') === 0) {
$fallbackSuffix = str_replace('Pro\\', 'Common\\', $classSuffix);
$fullClass = __NAMESPACE__ . '\\' . $fallbackSuffix;
}
}
if (!class_exists($fullClass)) {
$this->log_error("Class Loading", new \Exception("Class {$fullClass} not found."));
return new \stdClass(); // Return safe dummy object to prevent "Call to a member function on null"
}
try {
return new $fullClass();
} catch (Throwable $e) {
$this->log_error("Instantiation {$fullClass}", $e);
return new \stdClass();
}
}
/**
* Log error safely to debug log
*/
private function log_error(string $context, Throwable $e): void
{
error_log(sprintf(
'[Acadlix Fatal Prevention] %s Error: %s in %s:%d',
$context,
$e->getMessage(),
$e->getFile(),
$e->getLine()
));
}
}
}
namespace {
// Exit if accessed directly.
if (!defined('ABSPATH')) {
exit;
}
function acadlix()
{
return Yuvayana\Acadlix\Acadlix::instance();
}
}