-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstarmus-audio-recorder.php
More file actions
487 lines (436 loc) · 14.4 KB
/
Copy pathstarmus-audio-recorder.php
File metadata and controls
487 lines (436 loc) · 14.4 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
<?php
/**
* SPARXSTAR Starmus Audio
*
* Production bootloader for the Sparxstar Starmus Audio plugin.
*
* Responsibilities:
* - Load Composer autoloader
* - Initialize logging
* - Gate plugin execution on SparxStar SCF Runtime state
* - Boot core Starmus services
*
* Non-Responsibilities:
* - SCF lifecycle or version management
* - Schema installation or registration (JSON is installer)
* - ACF / SCF internals probing
*
* @package Starisian\Sparxstar\Starmus
* @author Starisian Technologies
* @copyright 2023-2026 Starisian Technologies
* @license Starisian Technologies Proprietary
*
* @wordpress-plugin
* Plugin Name: Sparxstar Starmus Audio
* Plugin URI: https://starisian.com
* Description: Mobile-friendly audio recorder optimized for emerging markets.
* Version: 0.9.2
* Requires at least: 6.9
* Requires PHP: 8.2
* Text Domain: starmus-audio-recorder
* Domain Path: /languages
* Author: Starisian Technologies
* Author URI: https://starisian.com
* Update URI: https://starisian.com/sparxstar/starmus-audio-recorder/update
*/
declare(strict_types=1);
namespace Starisian\Sparxstar\Starmus;
if ( ! defined('ABSPATH')) {
exit;
}
define('STARMUS_VERSION', '0.9.2');
define('STARMUS_MAIN_FILE', __FILE__);
define('STARMUS_PATH', plugin_dir_path(STARMUS_MAIN_FILE));
define('STARMUS_URL', plugin_dir_url(STARMUS_MAIN_FILE));
define('STARMUS_PLUGIN_PREFIX', 'starmus');
define('STARMUS_PLUGIN_DIR', plugin_dir_path(STARMUS_MAIN_FILE));
define('STARMUS_VENDOR_DIR', STARMUS_PATH . 'vendor/');
if ( ! defined('STARMUS_LOG_LEVEL')) {
define('STARMUS_LOG_LEVEL', 8);
}
if ( ! defined('STARMUS_TUS_ENDPOINT')) {
define('STARMUS_TUS_ENDPOINT', 'https://upload.sparxstar.com/files/');
}
if ( ! defined('STARMUS_R2_ENDPOINT')) {
define('STARMUS_R2_ENDPOINT', 'https://cdn.sparxstar.com/');
}
if ( ! defined('STARMUS_TUS_WEBHOOK_SECRET')) {
define('STARMUS_TUS_WEBHOOK_SECRET', 'starmus_secret_123');
}
if ( ! defined('STARMUS_REST_NAMESPACE')) {
define('STARMUS_REST_NAMESPACE', 'star-starmus-audio-recorder/v1');
}
if ( ! defined('STARMUS_DELETE_ON_UNINSTALL')) {
define('STARMUS_DELETE_ON_UNINSTALL', false);
}
if ( ! defined('STARMUS_DAL_OVERRIDE_KEY')) {
define('STARMUS_DAL_OVERRIDE_KEY', 'starmus_dal_override');
}
/**
* Check for headers sent on init to prevent "headers already sent" errors later.
*
* @since 0.9.0
*/
add_action('init', function () {
if (headers_sent($file, $line)) {
error_log("HEADERS SENT from $file:$line");
}
});
// -------------------------------------------------------------------------
// 2. COMPOSER AUTOLOAD (Deferred to Class)
// -------------------------------------------------------------------------
/**
* Main Plugin Bootstrapper Class.
*
* Implements the Singleton pattern to ensure only one instance of the
* initialization logic runs. Handles environment checks, SCF bootstrapping,
* and hooking the orchestrator into WordPress.
*
* @since 0.9.0
*/
final class StarmusAudioRecorder
{
/**
* Minimum PHP Requirement.
*
* @var string
*/
private const MINIMUM_PHP_VERSION = '8.2';
/**
* Minimum WordPress Requirement.
*
* @var string
*/
private const MINIMUM_WP_VERSION = '6.8';
/**
* Flag indicating if environment requirements are met.
*
* @var bool
*/
private bool $requirements_met = false;
/**
* Flag indicating if the autoloader has been explicitly loaded by the class.
*
* @var bool
*/
private bool $autoloader_loaded = false;
/**
* Singleton Instance.
*
* @var StarmusAudioRecorder|null
*/
private static ?StarmusAudioRecorder $instance = null;
/**
* Retrieve the singleton instance.
*
* @since 0.9.0
* @return StarmusAudioRecorder The singleton instance.
*/
public static function starmusGetInstance(): StarmusAudioRecorder
{
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Private Constructor.
*
* Runs immediate checks and bootstrapping logic.
*
* @since 0.9.0
*/
private function __construct()
{
$this->starmusCheckRequirements();
$this->starmusBootSCF();
$this->starmusConfigureSCF();
$this->starmusRegisterHooks();
}
/**
* Register core plugin hooks.
*
* Defers the main orchestrator boot to 'plugins_loaded'.
*
* @since 0.9.0
* @return void
*/
private function starmusRegisterHooks(): void
{
// Defer orchestrator boot to plugins_loaded if requirements met.
if ($this->requirements_met) {
add_action('plugins_loaded', [$this, 'starmusRun']);
}
// Register fatal error catcher
register_shutdown_function([$this, 'starmusHandleShutdown']);
}
/**
* Verify PHP and WordPress version requirements.
*
* Sets the internal $requirements_met flag.
*
* @since 0.9.0
* @return void
*/
private function starmusCheckRequirements(): void
{
global $wp_version;
$php_ok = version_compare(PHP_VERSION, self::MINIMUM_PHP_VERSION, '>=');
$wp_ok = isset($wp_version) && version_compare($wp_version, self::MINIMUM_WP_VERSION, '>=');
if ($php_ok && $wp_ok) {
$this->requirements_met = true;
return;
}
$this->requirements_met = false;
$this->starmusHandleRequirementsFailed($php_ok, $wp_ok);
}
/**
* Display admin notices if requirements are not met.
*
* @since 0.9.0
* @param bool $php_ok Whether PHP version requirement is met.
* @param bool $wp_ok Whether WP version requirement is met.
* @return void
*/
private function starmusHandleRequirementsFailed(bool $php_ok, bool $wp_ok): void
{
$messages = [];
if ( ! $php_ok) {
$messages[] = sprintf(
/* translators: %s: Required PHP version */
__('SPARXSTAR Starmus requires PHP version %s or higher.', 'starmus-audio-recorder'),
self::MINIMUM_PHP_VERSION
);
}
if ( ! $wp_ok) {
$messages[] = sprintf(
/* translators: %s: Required WordPress version */
__('SPARXSTAR Starmus requires WordPress version %s or higher.', 'starmus-audio-recorder'),
self::MINIMUM_WP_VERSION
);
}
if ( ! empty($messages)) {
add_action(
'admin_notices',
function () use ($messages): void {
printf(
'<div class="notice notice-error"><p><strong>%s</strong> %s</p></div>',
esc_html__('SPARXSTAR Starmus:', 'starmus-audio-recorder'),
wp_kses_post(implode(' ', $messages))
);
}
);
}
}
/**
* Handle shutdown to catch fatal errors.
*
* @since 0.9.0
* @return void
*/
public function starmusHandleShutdown(): void
{
$error = error_get_last();
if ($error && in_array($error['type'], [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR], true)) {
error_log('[STARMUS FATAL] ' . $error['message'] . ' in ' . $error['file'] . ':' . $error['line']);
}
}
/**
* Boot the bundled Secure Custom Fields (SCF) plugin if not already loaded.
*
* Checks for the existence of the ACF class to avoid conflicts.
*
* @since 0.9.0
* @return void
*/
private function starmusBootSCF(): void
{
$scfFile = STARMUS_PATH . 'vendor/secure-custom-fields/secure-custom-fields.php';
// -------------------------------------------------------------------------
// 3. BUNDLE & BOOT SCF (If Not Loaded)
// -------------------------------------------------------------------------
if ( ! class_exists('ACF')) {
error_log('Starmus Info: Booting bundled Secure Custom Fields plugin.');
// Define path and URL to the bundled Secure Custom Fields plugin
if (file_exists($scfFile)) {
// 5. Load the Plugin
require_once $scfFile;
// 3. (Optional) Hide the SCF admin menu
//add_filter('acf/settings/show_admin', '__return_true', 100);
// 4. (Optional) Hide Updates
//add_filter('acf/settings/show_updates', '__return_false', 100);
} else {
error_log('Starmus Error: Bundled SCF not found at ' . $scfFile);
}
}
}
/**
* Configure Secure Custom Fields settings.
*
* Injects the local JSON save/load path for ACF/SCF.
*
* @since 0.9.0
* @return void
*/
private function starmusConfigureSCF(): void
{
$acfJSONPath = STARMUS_PATH . 'acf-json';
if (class_exists('ACF') && file_exists($acfJSONPath)) {
error_log('Starmus Info: Secure Custom Fields plugin loaded successfully.');
// Configure ACF to use local JSON path
add_filter(
'acf/settings/load_json',
function ($paths) {
// Append our custom path
$paths[] = STARMUS_PATH . 'acf-json';
return $paths;
}
);
} else {
error_log('Starmus Error: Secure Custom Fields plugin failed to load.');
}
}
/**
* Main execution entry point.
*
* Instantiates the core application orchestrator.
*
* @since 0.9.0
* @return void
*/
public function starmusRun(): void
{
if ( ! $this->requirements_met) {
return;
}
if ( ! $this->starmusLoadAutoloader()) {
return;
}
if ( ! class_exists(\Starisian\Sparxstar\Starmus\StarmusAudioRecorder::class)) {
$this->starmusHandleOrchestratorNotFound();
return;
}
try {
// Boot the App Instance
\Starisian\Sparxstar\Starmus\StarmusAudioRecorder::starmusRun();
} catch (\Throwable $e) {
error_log('SPARXSTAR Starmus Audio Boot Failed: ' . $e->getMessage());
}
}
/**
* Load the Composer Autoloader if not already loaded.
*
* @since 0.9.0
* @return bool True if autoloader is present and loaded, false otherwise.
*/
private function starmusLoadAutoloader(): bool
{
if ($this->autoloader_loaded) {
return true;
}
$autoloader = STARMUS_VENDOR_DIR . 'autoload.php';
if ( ! file_exists($autoloader)) {
add_action(
'admin_notices',
function (): void {
printf(
'<div class="notice notice-error"><p><strong>%s</strong> %s</p></div>',
esc_html__('SPARXSTAR Starmus Audio Recorder:', 'starmus-audio-recorder'),
esc_html__('Composer autoloader not found. Please run "composer install".', 'starmus-audio-recorder')
);
}
);
return false;
}
require_once $autoloader;
$this->autoloader_loaded = true;
return true;
}
/**
* Handle the failure case where the main orchestrator class is missing.
*
* @since 0.9.0
* @return void
*/
private function starmusHandleOrchestratorNotFound(): void
{
if (defined('WP_DEBUG') && WP_DEBUG && ( ! defined('WP_ENVIRONMENT_TYPE') || wp_get_environment_type() !== 'production')) {
error_log('SPARXSTAR Starmus Audio: Main orchestrator class not found.');
}
add_action(
'admin_notices',
function (): void {
printf(
'<div class="notice notice-error"><p><strong>%s</strong> %s</p></div>',
esc_html__('SPARXSTAR Starmus:', 'starmus-audio-recorder'),
esc_html__('Failed to load main orchestrator class.', 'starmus-audio-recorder')
);
}
);
}
/**
* Plugin Activation Hook.
*
* Triggers post type registration and cron scheduling.
*
* @since 0.9.0
* @return void
*/
public static function starmusOnActivate(): void
{
try {
if (class_exists(\Starisian\Sparxstar\Starmus\core\StarmusPostTypeLoader::class)) {
\Starisian\Sparxstar\Starmus\core\StarmusPostTypeLoader::starmusGetInstance();
}
if (class_exists(\Starisian\Sparxstar\Starmus\cron\StarmusCron::class)) {
\Starisian\Sparxstar\Starmus\cron\StarmusCron::starmusActivate();
}
} catch (\Throwable $e) {
error_log('Starmus Activation Error: ' . $e->getMessage());
}
flush_rewrite_rules();
}
/**
* Plugin Deactivation Hook.
*
* Cleans up scheduled cron jobs.
*
* @since 0.9.0
* @return void
*/
public static function starmusOnDeactivate(): void
{
try {
if (class_exists(\Starisian\Sparxstar\Starmus\cron\StarmusCron::class)) {
\Starisian\Sparxstar\Starmus\cron\StarmusCron::starmusDeactivate();
}
} catch (\Throwable $e) {
error_log('Starmus Deactivation Error: ' . $e->getMessage());
}
flush_rewrite_rules();
}
/**
* Plugin Uninstall Hook.
*
* Handled by uninstall.php, but this method exists for potential manual invocation.
*
* @since 0.9.0
* @return void
*/
public static function starmusOnUninstall(): void
{
try {
$file = STARMUS_PATH . 'uninstall.php';
if (file_exists($file)) {
require_once $file;
}
} catch (\Throwable $e) {
error_log('Starmus uninstall error: ' . $e->getMessage());
}
}
}
register_activation_hook(__FILE__, [StarmusAudioRecorder::class, 'starmusOnActivate']);
register_deactivation_hook(__FILE__, [StarmusAudioRecorder::class, 'starmusOnDeactivate']);
register_uninstall_hook(__FILE__, [StarmusAudioRecorder::class, 'starmusOnUninstall']);
// Initialize the plugin
StarmusAudioRecorder::starmusGetInstance();