1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\BlackBar; |
4
|
|
|
|
5
|
|
|
final class Application |
6
|
|
|
{ |
7
|
|
|
const CONSOLE_HOOK = 'console'; |
8
|
|
|
const ID = 'blackbar'; |
9
|
|
|
const PROFILER_HOOK = 'profile'; |
10
|
|
|
|
11
|
|
|
public $actions; |
12
|
|
|
public $console; |
13
|
|
|
public $errors = []; |
14
|
|
|
public $file; |
15
|
|
|
public $profiler; |
16
|
|
|
|
17
|
|
|
protected static $instance; |
18
|
|
|
|
19
|
|
|
public function __construct() |
20
|
|
|
{ |
21
|
|
|
$file = wp_normalize_path((new \ReflectionClass($this))->getFileName()); |
22
|
|
|
$this->actions = new SlowActions(); |
23
|
|
|
$this->console = new Console(); |
24
|
|
|
$this->file = str_replace('plugin/Application', static::ID, $file); |
25
|
|
|
$this->profiler = new Profiler(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function errorHandler(int $errno, string $message, string $file, int $line): void |
29
|
|
|
{ |
30
|
|
|
$errname = array_key_exists($errno, Console::ERROR_CODES) |
31
|
|
|
? Console::ERROR_CODES[$errno] |
32
|
|
|
: 'Unknown'; |
33
|
|
|
$hash = md5($errno.$message.$file.$line); |
34
|
|
|
if (array_key_exists($hash, $this->errors)) { |
35
|
|
|
++$this->errors[$hash]['count']; |
36
|
|
|
} else { |
37
|
|
|
$this->errors[$hash] = [ |
38
|
|
|
'count' => 0, |
39
|
|
|
'errno' => $errno, |
40
|
|
|
'file' => $file, |
41
|
|
|
'line' => $line, |
42
|
|
|
'message' => $message, |
43
|
|
|
'name' => $errname, |
44
|
|
|
]; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function init(): void |
49
|
|
|
{ |
50
|
|
|
$controller = new Controller(); |
|
|
|
|
51
|
|
|
add_filter('all', [$controller, 'initConsole']); |
52
|
|
|
add_filter('all', [$controller, 'initProfiler']); |
53
|
|
|
add_filter('all', [$controller, 'measureSlowActions']); |
54
|
|
|
add_action('plugins_loaded', [$controller, 'registerLanguages']); |
55
|
|
|
add_action('init', function () use ($controller) { |
56
|
|
|
if (!apply_filters('blackbar/enabled', current_user_can('administrator'))) { |
57
|
|
|
return; |
58
|
|
|
} |
59
|
|
|
add_action('admin_enqueue_scripts', [$controller, 'enqueueAssets']); |
60
|
|
|
add_action('wp_enqueue_scripts', [$controller, 'enqueueAssets']); |
61
|
|
|
add_action('admin_footer', [$controller, 'renderBar']); |
62
|
|
|
add_action('wp_footer', [$controller, 'renderBar']); |
63
|
|
|
add_filter('admin_body_class', [$controller, 'filterBodyClasses']); |
64
|
|
|
}); |
65
|
|
|
apply_filters('debug', 'Profiler Started'); |
66
|
|
|
apply_filters('debug', 'blackbar/profiler/noise'); |
67
|
|
|
set_error_handler([$this, 'errorHandler'], E_ALL | E_STRICT); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return static |
72
|
|
|
*/ |
73
|
|
|
public static function load() |
74
|
|
|
{ |
75
|
|
|
if (empty(static::$instance)) { |
76
|
|
|
static::$instance = new static(); |
77
|
|
|
} |
78
|
|
|
return static::$instance; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function path(string $file = '', bool $realpath = true): string |
82
|
|
|
{ |
83
|
|
|
$path = $realpath |
84
|
|
|
? plugin_dir_path($this->file) |
85
|
|
|
: trailingslashit(WP_PLUGIN_DIR).basename(dirname($this->file)); |
86
|
|
|
return trailingslashit($path).ltrim(trim($file), '/'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function render(string $view, array $data = []): void |
90
|
|
|
{ |
91
|
|
|
$file = $this->path(sprintf('views/%s.php', str_replace('.php', '', $view))); |
92
|
|
|
if (!file_exists($file)) { |
93
|
|
|
return; |
94
|
|
|
} |
95
|
|
|
extract($data); |
96
|
|
|
include $file; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function url(string $path = ''): string |
100
|
|
|
{ |
101
|
|
|
return esc_url(plugin_dir_url($this->file).ltrim(trim($path), '/')); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.