1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\BlackBar; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\BlackBar\Modules\Console; |
6
|
|
|
use GeminiLabs\BlackBar\Modules\Globals; |
7
|
|
|
use GeminiLabs\BlackBar\Modules\Hooks; |
8
|
|
|
use GeminiLabs\BlackBar\Modules\Profiler; |
9
|
|
|
use GeminiLabs\BlackBar\Modules\Queries; |
10
|
|
|
use GeminiLabs\BlackBar\Modules\Templates; |
11
|
|
|
|
12
|
|
|
final class Application |
13
|
|
|
{ |
14
|
|
|
public const CONSOLE_HOOK = 'console'; |
15
|
|
|
public const ID = 'blackbar'; |
16
|
|
|
public const PROFILER_START_HOOK = 'timer:start'; |
17
|
|
|
public const PROFILER_STOP_HOOK = 'timer:stop'; |
18
|
|
|
|
19
|
|
|
public $console; |
20
|
|
|
public $file; |
21
|
|
|
public $globals; |
22
|
|
|
public $hooks; |
23
|
|
|
public $profiler; |
24
|
|
|
public $queries; |
25
|
|
|
public $templates; |
26
|
|
|
public $version; |
27
|
|
|
|
28
|
|
|
private static $instance; |
29
|
|
|
|
30
|
|
|
public function __construct() |
31
|
|
|
{ |
32
|
|
|
$file = wp_normalize_path((new \ReflectionClass($this))->getFileName()); |
33
|
|
|
$this->console = new Console($this); |
34
|
|
|
$this->file = str_replace('plugin/Application', static::ID, $file); |
35
|
|
|
$this->globals = new Globals($this); |
36
|
|
|
$this->hooks = new Hooks($this); |
37
|
|
|
$this->profiler = new Profiler($this); |
38
|
|
|
$this->queries = new Queries($this); |
39
|
|
|
$this->templates = new Templates($this); |
40
|
|
|
$this->version = get_file_data($this->file, ['Version' => 'Version'])['Version']; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function errorHandler(int $errno, string $message, string $file, int $line): bool |
44
|
|
|
{ |
45
|
|
|
$path = explode(ABSPATH, $file); |
46
|
|
|
$location = sprintf('%s:%s', array_pop($path), $line); |
47
|
|
|
$this->console->store($message, (string) $errno, $location); |
48
|
|
|
return true; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function init(): void |
52
|
|
|
{ |
53
|
|
|
$controller = new Controller($this); |
54
|
|
|
add_action('all', [$controller, 'initConsole']); |
55
|
|
|
add_action('all', [$controller, 'initHooks']); |
56
|
|
|
add_action('all', [$controller, 'initProfiler']); |
57
|
|
|
do_action('blackbar/profiler/start'); // start profiler |
58
|
|
|
do_action('blackbar/profiler/noise'); // measure profiler noise |
59
|
|
|
add_action('plugins_loaded', [$controller, 'registerLanguages']); |
60
|
|
|
add_action('init', function () use ($controller) { |
61
|
|
|
if (!apply_filters('blackbar/enabled', current_user_can('administrator'))) { |
62
|
|
|
return; |
63
|
|
|
} |
64
|
|
|
add_action('admin_enqueue_scripts', [$controller, 'enqueueAssets']); |
65
|
|
|
add_action('wp_enqueue_scripts', [$controller, 'enqueueAssets']); |
66
|
|
|
add_action('admin_footer', [$controller, 'renderBar'], 99999); |
67
|
|
|
add_action('wp_footer', [$controller, 'renderBar'], 99999); |
68
|
|
|
add_filter('admin_body_class', [$controller, 'filterBodyClasses']); |
69
|
|
|
}); |
70
|
|
|
set_error_handler([$this, 'errorHandler'], E_ALL | E_STRICT); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return static |
75
|
|
|
*/ |
76
|
|
|
public static function load() |
77
|
|
|
{ |
78
|
|
|
if (empty(self::$instance)) { |
79
|
|
|
self::$instance = new static(); |
80
|
|
|
} |
81
|
|
|
return self::$instance; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function path(string $file = '', bool $realpath = true): string |
85
|
|
|
{ |
86
|
|
|
$path = $realpath |
87
|
|
|
? plugin_dir_path($this->file) |
88
|
|
|
: trailingslashit(WP_PLUGIN_DIR).basename(dirname($this->file)); |
89
|
|
|
return trailingslashit($path).ltrim(trim($file), '/'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function render(string $view, array $data = []): void |
93
|
|
|
{ |
94
|
|
|
$file = $this->path(sprintf('views/%s.php', str_replace('.php', '', $view))); |
95
|
|
|
if (!file_exists($file)) { |
96
|
|
|
return; |
97
|
|
|
} |
98
|
|
|
extract($data); |
99
|
|
|
include $file; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function url(string $path = ''): string |
103
|
|
|
{ |
104
|
|
|
return esc_url(plugin_dir_url($this->file).ltrim(trim($path), '/')); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|