-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsparxstar-user-environment-check.php
More file actions
191 lines (168 loc) · 5.55 KB
/
Copy pathsparxstar-user-environment-check.php
File metadata and controls
191 lines (168 loc) · 5.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
/**
* Plugin Name: SPARXSTAR User Environment Check
* Description: Logs browser diagnostics with a database-first architecture.
* Version: 0.9.1
* Author: Starisian
* Text Domain: sparxstar-user-environment-check
* Copyright: 2023-2026 Starisian Technologies
* License: Proprietary - All Rights Reserved
*/
declare(strict_types=1);
// Prevent direct access to the file.
if (! defined('ABSPATH')) {
exit;
}
// 1. Define Constants
// =========================================================================
// 1. BOOTSTRAP GUARD & CORE CONSTANTS
// =========================================================================
if (defined('SPX_ENV_CHECK_LOADED')) {
return;
}
define('SPX_ENV_CHECK_LOADED', true);
if (defined('SPX_UEC_LOADED')) {
return;
}
define('SPX_UEC_LOADED', true);
/**
* Absolute path to the current plugin file.
*/
if (! defined('SPX_ENV_CHECK_PLUGIN_FILE')) {
define('SPX_ENV_CHECK_PLUGIN_FILE', __FILE__);
}
/**
* Directory path to the plugin root.
*/
if (! defined('SPX_ENV_CHECK_PLUGIN_PATH')) {
define('SPX_ENV_CHECK_PLUGIN_PATH', plugin_dir_path(__FILE__));
}
/**
* Current plugin version string.
*/
if (! defined('SPX_ENV_CHECK_VERSION')) {
define('SPX_ENV_CHECK_VERSION', '0.5.0');
}
/**
* Text domain identifier used for translations.
*/
if (! defined('SPX_ENV_CHECK_TEXT_DOMAIN')) {
define('SPX_ENV_CHECK_TEXT_DOMAIN', 'sparxstar_user_environment_check');
}
/**
* Database table name for storing environment snapshots.
*/
if (! defined('SPX_ENV_CHECK_DB_TABLE_NAME')) {
define('SPX_ENV_CHECK_DB_TABLE_NAME', 'sparxstar_uec_snapshots');
}
// --- Logger Configuration ---
if (! defined('STARLOGGER_LOG_LEVEL')) {
define('STARLOGGER_LOG_LEVEL', 'debug');
}
if (! defined('STARLOGGER_LOG_FILE')) {
define('STARLOGGER_LOG_FILE', '');
}
/**
* Whether to delete all plugin data on uninstall.
*/
if (! defined('SPX_ENV_CHECK_DELETE_ON_UNINSTALL')) {
define('SPX_ENV_CHECK_DELETE_ON_UNINSTALL', false);
}
// =========================================================================
// 2. COMPOSER AUTOLOADER
// =========================================================================
$autoloader = SPX_ENV_CHECK_PLUGIN_PATH . 'vendor/autoload.php';
if (! file_exists($autoloader)) {
// Deactivate plugin and alert user.
if (function_exists('deactivate_plugins')) {
deactivate_plugins(plugin_basename(__FILE__));
}
wp_die(
esc_html__('SPARXSTAR User Environment Check Error: Plugin deactivated as dependencies are missing. Please run composer install.', 'sparxstar_user_environment_check'),
esc_html__('Plugin Activation Error', 'sparxstar_user_environment_check'),
array('back_link' => true)
);
} else {
require_once $autoloader;
// APPLY CONFIGURED LOG LEVEL (this was missing)
if (defined('STARLOGGER_LOG_LEVEL')) {
\Starisian\SparxstarUEC\helpers\StarLogger::setMinLogLevel(STARLOGGER_LOG_LEVEL);
}
}
// =========================================================================
// 3. LIFECYCLE HOOKS (ACTIVATION, DEACTIVATION, UNINSTALL)
// =========================================================================
/**
* Uninstall handler for permanent cleanup.
* This must be a standalone function or a static method.
*/
function spx_uec_on_uninstall(): void
{
if (! current_user_can('activate_plugins')) {
return;
}
// Include the dedicated uninstall script for cleanup tasks.
$uninstall_file = SPX_ENV_CHECK_PLUGIN_PATH . 'uninstall.php';
if (file_exists($uninstall_file)) {
require_once $uninstall_file;
}
flush_rewrite_rules();
}
// 3. Register Activation & Deactivation Hooks
// This points to the newly named SparxstarUECInstaller class.
register_activation_hook(SPX_ENV_CHECK_PLUGIN_FILE, ['Starisian\SparxstarUEC\core\SparxstarUECInstaller', 'spx_uec_activate']);
register_deactivation_hook(SPX_ENV_CHECK_PLUGIN_FILE, ['Starisian\SparxstarUEC\core\SparxstarUECInstaller', 'spx_uec_deactivate']);
// Multisite: ensure new sites are initialised automatically.
add_action('wp_initialize_site', ['Starisian\SparxstarUEC\core\SparxstarUECInstaller', 'spx_uec_initialize_new_site'], 10, 1);
add_action('wpmu_new_blog', ['Starisian\SparxstarUEC\core\SparxstarUECInstaller', 'spx_uec_initialize_new_site'], 10, 1);
/**
* Bootstrap the orchestrator once all plugins are loaded.
*/
add_action(
'plugins_loaded',
function () {
try {
// Skip initialization for background/automated WordPress requests
if (
(defined('DOING_CRON') && DOING_CRON) ||
(defined('DOING_AJAX') && DOING_AJAX)
) {
return;
}
// Additional guard to prevent multiple initializations in same request
if (did_action('sparxstar_uec_initialized')) {
return;
}
Starisian\SparxstarUEC\SparxstarUserEnvironmentCheck::spx_uec_get_instance();
do_action('sparxstar_uec_initialized');
} catch (\Throwable $e) {
// Log critical bootstrap error
if (class_exists('\Starisian\SparxstarUEC\helpers\StarLogger')) {
\Starisian\SparxstarUEC\helpers\StarLogger::log(
'Bootstrap',
$e,
'error',
[
'context' => 'plugins_loaded',
'message' => 'Critical error during plugin initialization',
]
);
}
// Display admin notice for critical errors
if (is_admin()) {
add_action(
'admin_notices',
function () use ($e) {
printf(
'<div class="notice notice-error"><p><strong>%s</strong> %s: %s</p></div>',
esc_html__('SPARXSTAR User Environment Check Error', 'sparxstar-user-environment-check'),
esc_html__('Plugin initialization failed', 'sparxstar-user-environment-check'),
esc_html($e->getMessage())
);
}
);
}
}
},
10
);