@@ -6,20 +6,20 @@ |
||
6 | 6 | * PSR-4 autoloader |
7 | 7 | */ |
8 | 8 | spl_autoload_register(function ($class) { |
9 | - $namespaces = [ |
|
10 | - 'GeminiLabs\\BlackBar\\' => __DIR__.'/src/', |
|
11 | - 'GeminiLabs\\BlackBar\\Tests\\' => __DIR__.'/tests/', |
|
12 | - ]; |
|
13 | - foreach ($namespaces as $prefix => $base_dir) { |
|
14 | - $len = strlen($prefix); |
|
15 | - if (0 !== strncmp($prefix, $class, $len)) { |
|
16 | - continue; |
|
17 | - } |
|
18 | - $file = $base_dir.str_replace('\\', '/', substr($class, $len)).'.php'; |
|
19 | - if (!file_exists($file)) { |
|
20 | - continue; |
|
21 | - } |
|
22 | - require $file; |
|
23 | - break; |
|
24 | - } |
|
9 | + $namespaces = [ |
|
10 | + 'GeminiLabs\\BlackBar\\' => __DIR__.'/src/', |
|
11 | + 'GeminiLabs\\BlackBar\\Tests\\' => __DIR__.'/tests/', |
|
12 | + ]; |
|
13 | + foreach ($namespaces as $prefix => $base_dir) { |
|
14 | + $len = strlen($prefix); |
|
15 | + if (0 !== strncmp($prefix, $class, $len)) { |
|
16 | + continue; |
|
17 | + } |
|
18 | + $file = $base_dir.str_replace('\\', '/', substr($class, $len)).'.php'; |
|
19 | + if (!file_exists($file)) { |
|
20 | + continue; |
|
21 | + } |
|
22 | + require $file; |
|
23 | + break; |
|
24 | + } |
|
25 | 25 | }); |
@@ -7,8 +7,8 @@ discard block |
||
7 | 7 | * @see https://docs.gravityforms.com/gform_noconflict_scripts/ |
8 | 8 | */ |
9 | 9 | add_filter('gform_noconflict_scripts', function (array $scripts) { |
10 | - $scripts[] = 'blackbar'; |
|
11 | - return $scripts; |
|
10 | + $scripts[] = 'blackbar'; |
|
11 | + return $scripts; |
|
12 | 12 | }); |
13 | 13 | |
14 | 14 | /* |
@@ -16,7 +16,7 @@ discard block |
||
16 | 16 | * @see https://docs.gravityforms.com/gform_noconflict_styles/ |
17 | 17 | */ |
18 | 18 | add_filter('gform_noconflict_styles', function (array $styles) { |
19 | - $styles[] = 'blackbar'; |
|
20 | - $styles[] = 'blackbar-syntax'; |
|
21 | - return $styles; |
|
19 | + $styles[] = 'blackbar'; |
|
20 | + $styles[] = 'blackbar-syntax'; |
|
21 | + return $styles; |
|
22 | 22 | }); |
@@ -4,106 +4,106 @@ |
||
4 | 4 | |
5 | 5 | final class Application |
6 | 6 | { |
7 | - const CONSOLE_HOOK = 'console'; |
|
8 | - const ID = 'blackbar'; |
|
9 | - const LANG = '/languages/'; |
|
10 | - const PROFILER_HOOK = 'profile'; |
|
7 | + const CONSOLE_HOOK = 'console'; |
|
8 | + const ID = 'blackbar'; |
|
9 | + const LANG = '/languages/'; |
|
10 | + const PROFILER_HOOK = 'profile'; |
|
11 | 11 | |
12 | - public $actions; |
|
13 | - public $console; |
|
14 | - public $errors = array(); |
|
15 | - public $file; |
|
16 | - public $profiler; |
|
12 | + public $actions; |
|
13 | + public $console; |
|
14 | + public $errors = array(); |
|
15 | + public $file; |
|
16 | + public $profiler; |
|
17 | 17 | |
18 | - public function __construct() |
|
19 | - { |
|
20 | - $this->actions = new SlowActions(); |
|
21 | - $this->console = new Console(); |
|
22 | - $this->file = realpath(dirname(__DIR__).'/'.static::ID.'.php'); |
|
23 | - $this->profiler = new Profiler(); |
|
24 | - } |
|
18 | + public function __construct() |
|
19 | + { |
|
20 | + $this->actions = new SlowActions(); |
|
21 | + $this->console = new Console(); |
|
22 | + $this->file = realpath(dirname(__DIR__).'/'.static::ID.'.php'); |
|
23 | + $this->profiler = new Profiler(); |
|
24 | + } |
|
25 | 25 | |
26 | - /** |
|
27 | - * @param int $errno |
|
28 | - * @param string $errstr |
|
29 | - * @param string $errfile |
|
30 | - * @param int $errline |
|
31 | - * @return void |
|
32 | - */ |
|
33 | - public function errorHandler($errno, $errstr, $errfile, $errline) |
|
34 | - { |
|
35 | - $errname = array_key_exists($errno, Console::ERROR_CODES) |
|
36 | - ? Console::ERROR_CODES[$errno] |
|
37 | - : 'Unknown'; |
|
38 | - $hash = md5($errno.$errstr.$errfile.$errline); |
|
39 | - if (array_key_exists($hash, $this->errors)) { |
|
40 | - ++$this->errors[$hash]['count']; |
|
41 | - } else { |
|
42 | - $this->errors[$hash] = array( |
|
43 | - 'errno' => $errno, |
|
44 | - 'message' => $errstr, |
|
45 | - 'file' => $errfile, |
|
46 | - 'name' => $errname, |
|
47 | - 'line' => $errline, |
|
48 | - 'count' => 0, |
|
49 | - ); |
|
50 | - } |
|
51 | - } |
|
26 | + /** |
|
27 | + * @param int $errno |
|
28 | + * @param string $errstr |
|
29 | + * @param string $errfile |
|
30 | + * @param int $errline |
|
31 | + * @return void |
|
32 | + */ |
|
33 | + public function errorHandler($errno, $errstr, $errfile, $errline) |
|
34 | + { |
|
35 | + $errname = array_key_exists($errno, Console::ERROR_CODES) |
|
36 | + ? Console::ERROR_CODES[$errno] |
|
37 | + : 'Unknown'; |
|
38 | + $hash = md5($errno.$errstr.$errfile.$errline); |
|
39 | + if (array_key_exists($hash, $this->errors)) { |
|
40 | + ++$this->errors[$hash]['count']; |
|
41 | + } else { |
|
42 | + $this->errors[$hash] = array( |
|
43 | + 'errno' => $errno, |
|
44 | + 'message' => $errstr, |
|
45 | + 'file' => $errfile, |
|
46 | + 'name' => $errname, |
|
47 | + 'line' => $errline, |
|
48 | + 'count' => 0, |
|
49 | + ); |
|
50 | + } |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * @return void |
|
55 | - */ |
|
56 | - public function init() |
|
57 | - { |
|
58 | - $controller = new Controller($this); |
|
59 | - add_filter('all', array($controller, 'initConsole')); |
|
60 | - add_filter('all', array($controller, 'initProfiler')); |
|
61 | - add_filter('all', array($controller, 'measureSlowActions')); |
|
62 | - add_action('plugins_loaded', array($controller, 'registerLanguages')); |
|
63 | - add_action('init', function () use ($controller) { |
|
64 | - if (!apply_filters('blackbar/enabled', true)) { |
|
65 | - return; |
|
66 | - } |
|
67 | - add_action('admin_enqueue_scripts', array($controller, 'enqueueAssets')); |
|
68 | - add_action('wp_enqueue_scripts', array($controller, 'enqueueAssets')); |
|
69 | - add_action('admin_footer', array($controller, 'renderBar')); |
|
70 | - add_action('wp_footer', array($controller, 'renderBar')); |
|
71 | - add_filter('admin_body_class', array($controller, 'filterBodyClasses')); |
|
72 | - }); |
|
73 | - apply_filters('debug', 'Profiler Started'); |
|
74 | - apply_filters('debug', 'blackbar/profiler/noise'); |
|
75 | - set_error_handler(array($this, 'errorHandler'), E_ALL | E_STRICT); |
|
76 | - } |
|
53 | + /** |
|
54 | + * @return void |
|
55 | + */ |
|
56 | + public function init() |
|
57 | + { |
|
58 | + $controller = new Controller($this); |
|
59 | + add_filter('all', array($controller, 'initConsole')); |
|
60 | + add_filter('all', array($controller, 'initProfiler')); |
|
61 | + add_filter('all', array($controller, 'measureSlowActions')); |
|
62 | + add_action('plugins_loaded', array($controller, 'registerLanguages')); |
|
63 | + add_action('init', function () use ($controller) { |
|
64 | + if (!apply_filters('blackbar/enabled', true)) { |
|
65 | + return; |
|
66 | + } |
|
67 | + add_action('admin_enqueue_scripts', array($controller, 'enqueueAssets')); |
|
68 | + add_action('wp_enqueue_scripts', array($controller, 'enqueueAssets')); |
|
69 | + add_action('admin_footer', array($controller, 'renderBar')); |
|
70 | + add_action('wp_footer', array($controller, 'renderBar')); |
|
71 | + add_filter('admin_body_class', array($controller, 'filterBodyClasses')); |
|
72 | + }); |
|
73 | + apply_filters('debug', 'Profiler Started'); |
|
74 | + apply_filters('debug', 'blackbar/profiler/noise'); |
|
75 | + set_error_handler(array($this, 'errorHandler'), E_ALL | E_STRICT); |
|
76 | + } |
|
77 | 77 | |
78 | - /** |
|
79 | - * @param string $file |
|
80 | - * @return string |
|
81 | - */ |
|
82 | - public function path($file = '') |
|
83 | - { |
|
84 | - return plugin_dir_path($this->file).ltrim(trim($file), '/'); |
|
85 | - } |
|
78 | + /** |
|
79 | + * @param string $file |
|
80 | + * @return string |
|
81 | + */ |
|
82 | + public function path($file = '') |
|
83 | + { |
|
84 | + return plugin_dir_path($this->file).ltrim(trim($file), '/'); |
|
85 | + } |
|
86 | 86 | |
87 | - /** |
|
88 | - * @param string $view |
|
89 | - * @return void |
|
90 | - */ |
|
91 | - public function render($view, array $data = array()) |
|
92 | - { |
|
93 | - $file = $this->path(sprintf('views/%s.php', str_replace('.php', '', $view))); |
|
94 | - if (!file_exists($file)) { |
|
95 | - return; |
|
96 | - } |
|
97 | - extract($data); |
|
98 | - include $file; |
|
99 | - } |
|
87 | + /** |
|
88 | + * @param string $view |
|
89 | + * @return void |
|
90 | + */ |
|
91 | + public function render($view, array $data = array()) |
|
92 | + { |
|
93 | + $file = $this->path(sprintf('views/%s.php', str_replace('.php', '', $view))); |
|
94 | + if (!file_exists($file)) { |
|
95 | + return; |
|
96 | + } |
|
97 | + extract($data); |
|
98 | + include $file; |
|
99 | + } |
|
100 | 100 | |
101 | - /** |
|
102 | - * @param string $path |
|
103 | - * @return string |
|
104 | - */ |
|
105 | - public function url($path = '') |
|
106 | - { |
|
107 | - return esc_url(plugin_dir_url($this->file).ltrim(trim($path), '/')); |
|
108 | - } |
|
101 | + /** |
|
102 | + * @param string $path |
|
103 | + * @return string |
|
104 | + */ |
|
105 | + public function url($path = '') |
|
106 | + { |
|
107 | + return esc_url(plugin_dir_url($this->file).ltrim(trim($path), '/')); |
|
108 | + } |
|
109 | 109 | } |
@@ -4,109 +4,109 @@ |
||
4 | 4 | |
5 | 5 | class Profiler |
6 | 6 | { |
7 | - /** |
|
8 | - * This is the time that WordPress takes to execute the profiler hook. |
|
9 | - * @var int |
|
10 | - */ |
|
11 | - protected $noise = 0; |
|
7 | + /** |
|
8 | + * This is the time that WordPress takes to execute the profiler hook. |
|
9 | + * @var int |
|
10 | + */ |
|
11 | + protected $noise = 0; |
|
12 | 12 | |
13 | - /** |
|
14 | - * @var int |
|
15 | - */ |
|
16 | - protected $start = null; |
|
13 | + /** |
|
14 | + * @var int |
|
15 | + */ |
|
16 | + protected $start = null; |
|
17 | 17 | |
18 | - /** |
|
19 | - * @var int |
|
20 | - */ |
|
21 | - protected $stop = null; |
|
18 | + /** |
|
19 | + * @var int |
|
20 | + */ |
|
21 | + protected $stop = null; |
|
22 | 22 | |
23 | - /** |
|
24 | - * @var array |
|
25 | - */ |
|
26 | - protected $timers = array(); |
|
23 | + /** |
|
24 | + * @var array |
|
25 | + */ |
|
26 | + protected $timers = array(); |
|
27 | 27 | |
28 | - /** |
|
29 | - * @return array |
|
30 | - */ |
|
31 | - public function getMeasure() |
|
32 | - { |
|
33 | - return $this->timers; |
|
34 | - } |
|
28 | + /** |
|
29 | + * @return array |
|
30 | + */ |
|
31 | + public function getMeasure() |
|
32 | + { |
|
33 | + return $this->timers; |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * @param array $timer |
|
38 | - * @return string |
|
39 | - */ |
|
40 | - public function getMemoryString($timer) |
|
41 | - { |
|
42 | - $timer = $this->normalize($timer); |
|
43 | - return sprintf('%s kB', round($timer['memory'] / 1000)); |
|
44 | - } |
|
36 | + /** |
|
37 | + * @param array $timer |
|
38 | + * @return string |
|
39 | + */ |
|
40 | + public function getMemoryString($timer) |
|
41 | + { |
|
42 | + $timer = $this->normalize($timer); |
|
43 | + return sprintf('%s kB', round($timer['memory'] / 1000)); |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * @param array $timer |
|
48 | - * @return string |
|
49 | - */ |
|
50 | - public function getNameString($timer) |
|
51 | - { |
|
52 | - $timer = $this->normalize($timer); |
|
53 | - return $timer['name']; |
|
54 | - } |
|
46 | + /** |
|
47 | + * @param array $timer |
|
48 | + * @return string |
|
49 | + */ |
|
50 | + public function getNameString($timer) |
|
51 | + { |
|
52 | + $timer = $this->normalize($timer); |
|
53 | + return $timer['name']; |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * @param array $timer |
|
58 | - * @return string |
|
59 | - */ |
|
60 | - public function getTimeString($timer) |
|
61 | - { |
|
62 | - $timer = $this->normalize($timer); |
|
63 | - $index = array_search($timer['name'], array_column($this->timers, 'name')); |
|
64 | - $start = $this->start + ($index * $this->noise); |
|
65 | - $time = number_format(round(($timer['time'] - $start) * 1000, 4), 4); |
|
66 | - return sprintf('%s ms', $time); |
|
67 | - } |
|
56 | + /** |
|
57 | + * @param array $timer |
|
58 | + * @return string |
|
59 | + */ |
|
60 | + public function getTimeString($timer) |
|
61 | + { |
|
62 | + $timer = $this->normalize($timer); |
|
63 | + $index = array_search($timer['name'], array_column($this->timers, 'name')); |
|
64 | + $start = $this->start + ($index * $this->noise); |
|
65 | + $time = number_format(round(($timer['time'] - $start) * 1000, 4), 4); |
|
66 | + return sprintf('%s ms', $time); |
|
67 | + } |
|
68 | 68 | |
69 | - /** |
|
70 | - * @return int Microseconds |
|
71 | - */ |
|
72 | - public function getTotalTime() |
|
73 | - { |
|
74 | - $totalNoise = (count($this->timers) - 1) * $this->noise; |
|
75 | - return $this->stop - $this->start - $totalNoise; |
|
76 | - } |
|
69 | + /** |
|
70 | + * @return int Microseconds |
|
71 | + */ |
|
72 | + public function getTotalTime() |
|
73 | + { |
|
74 | + $totalNoise = (count($this->timers) - 1) * $this->noise; |
|
75 | + return $this->stop - $this->start - $totalNoise; |
|
76 | + } |
|
77 | 77 | |
78 | - /** |
|
79 | - * @param string $name |
|
80 | - * @return void |
|
81 | - */ |
|
82 | - public function trace($name) |
|
83 | - { |
|
84 | - $microtime = microtime(true); |
|
85 | - if (!$this->start) { |
|
86 | - $this->start = $microtime; |
|
87 | - } |
|
88 | - if ('blackbar/profiler/noise' === $name) { |
|
89 | - $this->noise = $microtime - $this->start; |
|
90 | - return; |
|
91 | - } |
|
92 | - $this->timers[] = array( |
|
93 | - 'memory' => memory_get_peak_usage(), |
|
94 | - 'name' => $name, |
|
95 | - 'time' => $microtime, |
|
96 | - ); |
|
97 | - $this->stop = $microtime; |
|
98 | - } |
|
78 | + /** |
|
79 | + * @param string $name |
|
80 | + * @return void |
|
81 | + */ |
|
82 | + public function trace($name) |
|
83 | + { |
|
84 | + $microtime = microtime(true); |
|
85 | + if (!$this->start) { |
|
86 | + $this->start = $microtime; |
|
87 | + } |
|
88 | + if ('blackbar/profiler/noise' === $name) { |
|
89 | + $this->noise = $microtime - $this->start; |
|
90 | + return; |
|
91 | + } |
|
92 | + $this->timers[] = array( |
|
93 | + 'memory' => memory_get_peak_usage(), |
|
94 | + 'name' => $name, |
|
95 | + 'time' => $microtime, |
|
96 | + ); |
|
97 | + $this->stop = $microtime; |
|
98 | + } |
|
99 | 99 | |
100 | - /** |
|
101 | - * @param array $timer |
|
102 | - * @return array |
|
103 | - */ |
|
104 | - protected function normalize($timer) |
|
105 | - { |
|
106 | - return wp_parse_args((array) $timer, array( |
|
107 | - 'memory' => 0, |
|
108 | - 'name' => '', |
|
109 | - 'time' => 0, |
|
110 | - )); |
|
111 | - } |
|
100 | + /** |
|
101 | + * @param array $timer |
|
102 | + * @return array |
|
103 | + */ |
|
104 | + protected function normalize($timer) |
|
105 | + { |
|
106 | + return wp_parse_args((array) $timer, array( |
|
107 | + 'memory' => 0, |
|
108 | + 'name' => '', |
|
109 | + 'time' => 0, |
|
110 | + )); |
|
111 | + } |
|
112 | 112 | } |
@@ -4,289 +4,289 @@ |
||
4 | 4 | |
5 | 5 | class Controller |
6 | 6 | { |
7 | - /** |
|
8 | - * @var Application |
|
9 | - */ |
|
10 | - protected $app; |
|
7 | + /** |
|
8 | + * @var Application |
|
9 | + */ |
|
10 | + protected $app; |
|
11 | 11 | |
12 | - public function __construct(Application $app) |
|
13 | - { |
|
14 | - $this->app = $app; |
|
15 | - } |
|
12 | + public function __construct(Application $app) |
|
13 | + { |
|
14 | + $this->app = $app; |
|
15 | + } |
|
16 | 16 | |
17 | - /** |
|
18 | - * @return void |
|
19 | - * @action admin_enqueue_scripts |
|
20 | - * @action wp_enqueue_scripts |
|
21 | - */ |
|
22 | - public function enqueueAssets() |
|
23 | - { |
|
24 | - wp_enqueue_script(Application::ID, $this->app->url('assets/main.js')); |
|
25 | - wp_enqueue_style(Application::ID, $this->app->url('assets/main.css'), array('dashicons')); |
|
26 | - wp_enqueue_style(Application::ID.'-syntax', $this->app->url('assets/syntax.css')); |
|
27 | - } |
|
17 | + /** |
|
18 | + * @return void |
|
19 | + * @action admin_enqueue_scripts |
|
20 | + * @action wp_enqueue_scripts |
|
21 | + */ |
|
22 | + public function enqueueAssets() |
|
23 | + { |
|
24 | + wp_enqueue_script(Application::ID, $this->app->url('assets/main.js')); |
|
25 | + wp_enqueue_style(Application::ID, $this->app->url('assets/main.css'), array('dashicons')); |
|
26 | + wp_enqueue_style(Application::ID.'-syntax', $this->app->url('assets/syntax.css')); |
|
27 | + } |
|
28 | 28 | |
29 | - /** |
|
30 | - * @param string $classes |
|
31 | - * @return string |
|
32 | - * @action admin_body_class |
|
33 | - */ |
|
34 | - public function filterBodyClasses($classes) |
|
35 | - { |
|
36 | - return trim($classes.' '.Application::ID); |
|
37 | - } |
|
29 | + /** |
|
30 | + * @param string $classes |
|
31 | + * @return string |
|
32 | + * @action admin_body_class |
|
33 | + */ |
|
34 | + public function filterBodyClasses($classes) |
|
35 | + { |
|
36 | + return trim($classes.' '.Application::ID); |
|
37 | + } |
|
38 | 38 | |
39 | - /** |
|
40 | - * @return void |
|
41 | - * @filter all |
|
42 | - */ |
|
43 | - public function initConsole() |
|
44 | - { |
|
45 | - if (Application::CONSOLE_HOOK != func_get_arg(0)) { |
|
46 | - return; |
|
47 | - } |
|
48 | - $args = array_pad(func_get_args(), 4, ''); |
|
49 | - $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 4); |
|
50 | - $entry = array_pop($backtrace); |
|
51 | - $location = $args[3]; |
|
52 | - if (empty(trim($location)) && array_key_exists('file', $entry)) { |
|
53 | - $path = explode(ABSPATH, $entry['file']); |
|
54 | - $location = sprintf('%s:%s', array_pop($path), $entry['line']); |
|
55 | - } |
|
56 | - $this->app->console->store($args[1], $args[2], '['.$location.'] '); |
|
57 | - } |
|
39 | + /** |
|
40 | + * @return void |
|
41 | + * @filter all |
|
42 | + */ |
|
43 | + public function initConsole() |
|
44 | + { |
|
45 | + if (Application::CONSOLE_HOOK != func_get_arg(0)) { |
|
46 | + return; |
|
47 | + } |
|
48 | + $args = array_pad(func_get_args(), 4, ''); |
|
49 | + $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 4); |
|
50 | + $entry = array_pop($backtrace); |
|
51 | + $location = $args[3]; |
|
52 | + if (empty(trim($location)) && array_key_exists('file', $entry)) { |
|
53 | + $path = explode(ABSPATH, $entry['file']); |
|
54 | + $location = sprintf('%s:%s', array_pop($path), $entry['line']); |
|
55 | + } |
|
56 | + $this->app->console->store($args[1], $args[2], '['.$location.'] '); |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * @return void |
|
61 | - * @filter all |
|
62 | - */ |
|
63 | - public function initProfiler() |
|
64 | - { |
|
65 | - if (Application::PROFILER_HOOK != func_get_arg(0)) { |
|
66 | - return; |
|
67 | - } |
|
68 | - $this->app->profiler->trace(func_get_arg(1)); |
|
69 | - } |
|
59 | + /** |
|
60 | + * @return void |
|
61 | + * @filter all |
|
62 | + */ |
|
63 | + public function initProfiler() |
|
64 | + { |
|
65 | + if (Application::PROFILER_HOOK != func_get_arg(0)) { |
|
66 | + return; |
|
67 | + } |
|
68 | + $this->app->profiler->trace(func_get_arg(1)); |
|
69 | + } |
|
70 | 70 | |
71 | - /** |
|
72 | - * @return void |
|
73 | - * @filter all |
|
74 | - */ |
|
75 | - public function measureSlowActions() |
|
76 | - { |
|
77 | - $this->app->actions->startTimer(); |
|
78 | - } |
|
71 | + /** |
|
72 | + * @return void |
|
73 | + * @filter all |
|
74 | + */ |
|
75 | + public function measureSlowActions() |
|
76 | + { |
|
77 | + $this->app->actions->startTimer(); |
|
78 | + } |
|
79 | 79 | |
80 | - /** |
|
81 | - * @return void |
|
82 | - * @action plugins_loaded |
|
83 | - */ |
|
84 | - public function registerLanguages() |
|
85 | - { |
|
86 | - load_plugin_textdomain(Application::ID, false, |
|
87 | - plugin_basename($this->app->path()).Application::LANG |
|
88 | - ); |
|
89 | - } |
|
80 | + /** |
|
81 | + * @return void |
|
82 | + * @action plugins_loaded |
|
83 | + */ |
|
84 | + public function registerLanguages() |
|
85 | + { |
|
86 | + load_plugin_textdomain(Application::ID, false, |
|
87 | + plugin_basename($this->app->path()).Application::LANG |
|
88 | + ); |
|
89 | + } |
|
90 | 90 | |
91 | - /** |
|
92 | - * @return void |
|
93 | - * @action admin_footer |
|
94 | - * @action wp_footer |
|
95 | - */ |
|
96 | - public function renderBar() |
|
97 | - { |
|
98 | - apply_filters('debug', 'Profiler Stopped'); |
|
99 | - $this->app->render('debug-bar', array( |
|
100 | - 'blackbar' => $this->app, |
|
101 | - 'actions' => $this->app->actions, |
|
102 | - 'actionsLabel' => $this->getSlowActionsLabel(), |
|
103 | - 'consoleEntries' => $this->getConsoleEntries(), |
|
104 | - 'consoleLabel' => $this->getConsoleLabel(), |
|
105 | - 'profiler' => $this->app->profiler, |
|
106 | - 'profilerLabel' => $this->getProfilerLabel(), |
|
107 | - 'queries' => $this->getQueries(), |
|
108 | - 'queriesLabel' => $this->getQueriesLabel(), |
|
109 | - 'templates' => $this->getTemplates(), |
|
110 | - )); |
|
111 | - } |
|
91 | + /** |
|
92 | + * @return void |
|
93 | + * @action admin_footer |
|
94 | + * @action wp_footer |
|
95 | + */ |
|
96 | + public function renderBar() |
|
97 | + { |
|
98 | + apply_filters('debug', 'Profiler Stopped'); |
|
99 | + $this->app->render('debug-bar', array( |
|
100 | + 'blackbar' => $this->app, |
|
101 | + 'actions' => $this->app->actions, |
|
102 | + 'actionsLabel' => $this->getSlowActionsLabel(), |
|
103 | + 'consoleEntries' => $this->getConsoleEntries(), |
|
104 | + 'consoleLabel' => $this->getConsoleLabel(), |
|
105 | + 'profiler' => $this->app->profiler, |
|
106 | + 'profilerLabel' => $this->getProfilerLabel(), |
|
107 | + 'queries' => $this->getQueries(), |
|
108 | + 'queriesLabel' => $this->getQueriesLabel(), |
|
109 | + 'templates' => $this->getTemplates(), |
|
110 | + )); |
|
111 | + } |
|
112 | 112 | |
113 | - /** |
|
114 | - * @param int $time |
|
115 | - * @param int $decimals |
|
116 | - * @return string |
|
117 | - */ |
|
118 | - protected function convertToMiliseconds($time, $decimals = 2) |
|
119 | - { |
|
120 | - return number_format($time * 1000, $decimals); |
|
121 | - } |
|
113 | + /** |
|
114 | + * @param int $time |
|
115 | + * @param int $decimals |
|
116 | + * @return string |
|
117 | + */ |
|
118 | + protected function convertToMiliseconds($time, $decimals = 2) |
|
119 | + { |
|
120 | + return number_format($time * 1000, $decimals); |
|
121 | + } |
|
122 | 122 | |
123 | - /** |
|
124 | - * @return array |
|
125 | - */ |
|
126 | - protected function getConsoleEntries() |
|
127 | - { |
|
128 | - return array_merge($this->getErrors(), $this->app->console->entries); |
|
129 | - } |
|
123 | + /** |
|
124 | + * @return array |
|
125 | + */ |
|
126 | + protected function getConsoleEntries() |
|
127 | + { |
|
128 | + return array_merge($this->getErrors(), $this->app->console->entries); |
|
129 | + } |
|
130 | 130 | |
131 | - /** |
|
132 | - * @return string |
|
133 | - */ |
|
134 | - protected function getConsoleLabel() |
|
135 | - { |
|
136 | - $class = ''; |
|
137 | - $entries = $this->getConsoleEntries(); |
|
138 | - $entryCount = count($entries); |
|
139 | - $errorCount = 0; |
|
140 | - $label = __('Console', 'blackbar'); |
|
141 | - foreach ($entries as $entry) { |
|
142 | - if (in_array($entry['errno'], [E_NOTICE, E_STRICT, E_DEPRECATED])) { |
|
143 | - $class = 'glbb-warning'; |
|
144 | - } |
|
145 | - if (in_array($entry['errno'], [E_WARNING])) { |
|
146 | - ++$errorCount; |
|
147 | - } |
|
148 | - } |
|
149 | - if ($entryCount > 0) { |
|
150 | - $label .= sprintf(' (%d)', $entryCount); |
|
151 | - } |
|
152 | - if ($errorCount > 0) { |
|
153 | - $class = 'glbb-error'; |
|
154 | - $label .= sprintf(' (%d, %d!)', $entryCount, $errorCount); |
|
155 | - } |
|
156 | - return '<span class="'.$class.'">'.$label.'</span>'; |
|
157 | - } |
|
131 | + /** |
|
132 | + * @return string |
|
133 | + */ |
|
134 | + protected function getConsoleLabel() |
|
135 | + { |
|
136 | + $class = ''; |
|
137 | + $entries = $this->getConsoleEntries(); |
|
138 | + $entryCount = count($entries); |
|
139 | + $errorCount = 0; |
|
140 | + $label = __('Console', 'blackbar'); |
|
141 | + foreach ($entries as $entry) { |
|
142 | + if (in_array($entry['errno'], [E_NOTICE, E_STRICT, E_DEPRECATED])) { |
|
143 | + $class = 'glbb-warning'; |
|
144 | + } |
|
145 | + if (in_array($entry['errno'], [E_WARNING])) { |
|
146 | + ++$errorCount; |
|
147 | + } |
|
148 | + } |
|
149 | + if ($entryCount > 0) { |
|
150 | + $label .= sprintf(' (%d)', $entryCount); |
|
151 | + } |
|
152 | + if ($errorCount > 0) { |
|
153 | + $class = 'glbb-error'; |
|
154 | + $label .= sprintf(' (%d, %d!)', $entryCount, $errorCount); |
|
155 | + } |
|
156 | + return '<span class="'.$class.'">'.$label.'</span>'; |
|
157 | + } |
|
158 | 158 | |
159 | - /** |
|
160 | - * @return array |
|
161 | - */ |
|
162 | - protected function getErrors() |
|
163 | - { |
|
164 | - $errors = array(); |
|
165 | - foreach ($this->app->errors as $error) { |
|
166 | - $class = 'glbb-info'; |
|
167 | - if (in_array($error['errno'], [E_NOTICE, E_STRICT, E_DEPRECATED])) { |
|
168 | - $class = 'glbb-warning'; |
|
169 | - } |
|
170 | - if (E_WARNING == $error['errno']) { |
|
171 | - $class = 'glbb-error'; |
|
172 | - } |
|
173 | - if ($error['count'] > 1) { |
|
174 | - $error['name'] .= ' ('.$error['count'].')'; |
|
175 | - } |
|
176 | - $errors[] = array( |
|
177 | - 'errno' => $error['errno'], |
|
178 | - 'name' => '<span class="'.$class.'">'.$error['name'].'</span>', |
|
179 | - 'message' => sprintf(__('%s on line %s in file %s', 'blackbar'), |
|
180 | - $error['message'], |
|
181 | - $error['line'], |
|
182 | - $error['file'] |
|
183 | - ), |
|
184 | - ); |
|
185 | - } |
|
186 | - return $errors; |
|
187 | - } |
|
159 | + /** |
|
160 | + * @return array |
|
161 | + */ |
|
162 | + protected function getErrors() |
|
163 | + { |
|
164 | + $errors = array(); |
|
165 | + foreach ($this->app->errors as $error) { |
|
166 | + $class = 'glbb-info'; |
|
167 | + if (in_array($error['errno'], [E_NOTICE, E_STRICT, E_DEPRECATED])) { |
|
168 | + $class = 'glbb-warning'; |
|
169 | + } |
|
170 | + if (E_WARNING == $error['errno']) { |
|
171 | + $class = 'glbb-error'; |
|
172 | + } |
|
173 | + if ($error['count'] > 1) { |
|
174 | + $error['name'] .= ' ('.$error['count'].')'; |
|
175 | + } |
|
176 | + $errors[] = array( |
|
177 | + 'errno' => $error['errno'], |
|
178 | + 'name' => '<span class="'.$class.'">'.$error['name'].'</span>', |
|
179 | + 'message' => sprintf(__('%s on line %s in file %s', 'blackbar'), |
|
180 | + $error['message'], |
|
181 | + $error['line'], |
|
182 | + $error['file'] |
|
183 | + ), |
|
184 | + ); |
|
185 | + } |
|
186 | + return $errors; |
|
187 | + } |
|
188 | 188 | |
189 | - /** |
|
190 | - * @return array |
|
191 | - */ |
|
192 | - protected function getIncludedFiles() |
|
193 | - { |
|
194 | - $files = array_values(array_filter(get_included_files(), function ($file) { |
|
195 | - $bool = false !== strpos($file, '/themes/') |
|
196 | - && false === strpos($file, '/functions.php'); |
|
197 | - return (bool) apply_filters('blackbar/templates/file', $bool, $file); |
|
198 | - })); |
|
199 | - return array_map(function ($key, $value) { |
|
200 | - $value = str_replace(trailingslashit(WP_CONTENT_DIR), '', $value); |
|
201 | - return sprintf('[%s] => %s', $key, $value); |
|
202 | - }, array_keys($files), $files); |
|
203 | - } |
|
189 | + /** |
|
190 | + * @return array |
|
191 | + */ |
|
192 | + protected function getIncludedFiles() |
|
193 | + { |
|
194 | + $files = array_values(array_filter(get_included_files(), function ($file) { |
|
195 | + $bool = false !== strpos($file, '/themes/') |
|
196 | + && false === strpos($file, '/functions.php'); |
|
197 | + return (bool) apply_filters('blackbar/templates/file', $bool, $file); |
|
198 | + })); |
|
199 | + return array_map(function ($key, $value) { |
|
200 | + $value = str_replace(trailingslashit(WP_CONTENT_DIR), '', $value); |
|
201 | + return sprintf('[%s] => %s', $key, $value); |
|
202 | + }, array_keys($files), $files); |
|
203 | + } |
|
204 | 204 | |
205 | - /** |
|
206 | - * @return string |
|
207 | - */ |
|
208 | - protected function getProfilerLabel() |
|
209 | - { |
|
210 | - $label = __('Profiler', 'blackbar'); |
|
211 | - $profilerTime = $this->convertToMiliseconds($this->app->profiler->getTotalTime(), 0); |
|
212 | - if ($profilerTime > 0) { |
|
213 | - $label .= sprintf(' (%s %s)', $profilerTime, __('ms', 'blackbar')); |
|
214 | - } |
|
215 | - return $label; |
|
216 | - } |
|
205 | + /** |
|
206 | + * @return string |
|
207 | + */ |
|
208 | + protected function getProfilerLabel() |
|
209 | + { |
|
210 | + $label = __('Profiler', 'blackbar'); |
|
211 | + $profilerTime = $this->convertToMiliseconds($this->app->profiler->getTotalTime(), 0); |
|
212 | + if ($profilerTime > 0) { |
|
213 | + $label .= sprintf(' (%s %s)', $profilerTime, __('ms', 'blackbar')); |
|
214 | + } |
|
215 | + return $label; |
|
216 | + } |
|
217 | 217 | |
218 | - /** |
|
219 | - * @return array |
|
220 | - */ |
|
221 | - protected function getQueries() |
|
222 | - { |
|
223 | - global $wpdb; |
|
224 | - $queries = array(); |
|
225 | - $search = array( |
|
226 | - 'AND', 'FROM', 'GROUP BY', 'INNER JOIN', 'LIMIT', 'ON DUPLICATE KEY UPDATE', |
|
227 | - 'ORDER BY', 'SET', 'WHERE', |
|
228 | - ); |
|
229 | - $replace = array_map(function ($value) { |
|
230 | - return PHP_EOL.$value; |
|
231 | - }, $search); |
|
232 | - foreach ($wpdb->queries as $query) { |
|
233 | - $miliseconds = number_format(round($query[1] * 1000, 4), 4); |
|
234 | - $sql = preg_replace('/\s\s+/', ' ', trim($query[0])); |
|
235 | - $sql = str_replace(PHP_EOL, ' ', $sql); |
|
236 | - $sql = str_replace($search, $replace, $sql); |
|
237 | - $queries[] = array( |
|
238 | - 'ms' => $miliseconds, |
|
239 | - 'sql' => $sql, |
|
240 | - ); |
|
241 | - } |
|
242 | - return $queries; |
|
243 | - } |
|
218 | + /** |
|
219 | + * @return array |
|
220 | + */ |
|
221 | + protected function getQueries() |
|
222 | + { |
|
223 | + global $wpdb; |
|
224 | + $queries = array(); |
|
225 | + $search = array( |
|
226 | + 'AND', 'FROM', 'GROUP BY', 'INNER JOIN', 'LIMIT', 'ON DUPLICATE KEY UPDATE', |
|
227 | + 'ORDER BY', 'SET', 'WHERE', |
|
228 | + ); |
|
229 | + $replace = array_map(function ($value) { |
|
230 | + return PHP_EOL.$value; |
|
231 | + }, $search); |
|
232 | + foreach ($wpdb->queries as $query) { |
|
233 | + $miliseconds = number_format(round($query[1] * 1000, 4), 4); |
|
234 | + $sql = preg_replace('/\s\s+/', ' ', trim($query[0])); |
|
235 | + $sql = str_replace(PHP_EOL, ' ', $sql); |
|
236 | + $sql = str_replace($search, $replace, $sql); |
|
237 | + $queries[] = array( |
|
238 | + 'ms' => $miliseconds, |
|
239 | + 'sql' => $sql, |
|
240 | + ); |
|
241 | + } |
|
242 | + return $queries; |
|
243 | + } |
|
244 | 244 | |
245 | - /** |
|
246 | - * @return string |
|
247 | - */ |
|
248 | - protected function getQueriesLabel() |
|
249 | - { |
|
250 | - $label = __('SQL', 'blackbar'); |
|
251 | - if (!SAVEQUERIES) { |
|
252 | - return $label; |
|
253 | - } |
|
254 | - global $wpdb; |
|
255 | - $queryTime = 0; |
|
256 | - foreach ($wpdb->queries as $query) { |
|
257 | - $queryTime += $query[1]; |
|
258 | - } |
|
259 | - $queriesCount = '<span class="glbb-queries-count">'.count($wpdb->queries).'</span>'; |
|
260 | - $queriesTime = '<span class="glbb-queries-time">'.$this->convertToMiliseconds($queryTime).'</span>'; |
|
261 | - return $label.sprintf(' (%s %s | %s %s)', $queriesCount, __('queries', 'blackbar'), $queriesTime, __('ms', 'blackbar')); |
|
262 | - } |
|
245 | + /** |
|
246 | + * @return string |
|
247 | + */ |
|
248 | + protected function getQueriesLabel() |
|
249 | + { |
|
250 | + $label = __('SQL', 'blackbar'); |
|
251 | + if (!SAVEQUERIES) { |
|
252 | + return $label; |
|
253 | + } |
|
254 | + global $wpdb; |
|
255 | + $queryTime = 0; |
|
256 | + foreach ($wpdb->queries as $query) { |
|
257 | + $queryTime += $query[1]; |
|
258 | + } |
|
259 | + $queriesCount = '<span class="glbb-queries-count">'.count($wpdb->queries).'</span>'; |
|
260 | + $queriesTime = '<span class="glbb-queries-time">'.$this->convertToMiliseconds($queryTime).'</span>'; |
|
261 | + return $label.sprintf(' (%s %s | %s %s)', $queriesCount, __('queries', 'blackbar'), $queriesTime, __('ms', 'blackbar')); |
|
262 | + } |
|
263 | 263 | |
264 | - /** |
|
265 | - * @return string |
|
266 | - */ |
|
267 | - protected function getSlowActionsLabel() |
|
268 | - { |
|
269 | - $label = __('Hooks', 'blackbar'); |
|
270 | - $totalTime = $this->convertToMiliseconds($this->app->actions->getTotalTime(), 0); |
|
271 | - if ($totalTime > 0) { |
|
272 | - $label .= sprintf(' (%s %s)', $totalTime, __('ms', 'blackbar')); |
|
273 | - } |
|
274 | - return $label; |
|
275 | - } |
|
264 | + /** |
|
265 | + * @return string |
|
266 | + */ |
|
267 | + protected function getSlowActionsLabel() |
|
268 | + { |
|
269 | + $label = __('Hooks', 'blackbar'); |
|
270 | + $totalTime = $this->convertToMiliseconds($this->app->actions->getTotalTime(), 0); |
|
271 | + if ($totalTime > 0) { |
|
272 | + $label .= sprintf(' (%s %s)', $totalTime, __('ms', 'blackbar')); |
|
273 | + } |
|
274 | + return $label; |
|
275 | + } |
|
276 | 276 | |
277 | - /** |
|
278 | - * @return void|string |
|
279 | - */ |
|
280 | - protected function getTemplates() |
|
281 | - { |
|
282 | - if (is_admin()) { |
|
283 | - return; |
|
284 | - } |
|
285 | - if (class_exists('\GeminiLabs\Castor\Facades\Development')) { |
|
286 | - ob_start(); |
|
287 | - \GeminiLabs\Castor\Facades\Development::printTemplatePaths(); |
|
288 | - return ob_get_clean(); |
|
289 | - } |
|
290 | - return '<pre>'.implode(PHP_EOL, $this->getIncludedFiles()).'</pre>'; |
|
291 | - } |
|
277 | + /** |
|
278 | + * @return void|string |
|
279 | + */ |
|
280 | + protected function getTemplates() |
|
281 | + { |
|
282 | + if (is_admin()) { |
|
283 | + return; |
|
284 | + } |
|
285 | + if (class_exists('\GeminiLabs\Castor\Facades\Development')) { |
|
286 | + ob_start(); |
|
287 | + \GeminiLabs\Castor\Facades\Development::printTemplatePaths(); |
|
288 | + return ob_get_clean(); |
|
289 | + } |
|
290 | + return '<pre>'.implode(PHP_EOL, $this->getIncludedFiles()).'</pre>'; |
|
291 | + } |
|
292 | 292 | } |
@@ -4,161 +4,161 @@ |
||
4 | 4 | |
5 | 5 | class SlowActions |
6 | 6 | { |
7 | - /** |
|
8 | - * @var array |
|
9 | - */ |
|
10 | - protected $flow; |
|
11 | - |
|
12 | - /** |
|
13 | - * This is the time that WordPress takes to execute the all hook. |
|
14 | - * @var int |
|
15 | - */ |
|
16 | - protected $noise = 0; |
|
17 | - |
|
18 | - /** |
|
19 | - * @var int |
|
20 | - */ |
|
21 | - protected $start = null; |
|
22 | - |
|
23 | - /** |
|
24 | - * @var int |
|
25 | - */ |
|
26 | - protected $stop = null; |
|
27 | - |
|
28 | - /** |
|
29 | - * @var int |
|
30 | - */ |
|
31 | - protected $totalActions = 0; |
|
32 | - |
|
33 | - /** |
|
34 | - * @var int |
|
35 | - */ |
|
36 | - protected $totalTime = 0; |
|
37 | - |
|
38 | - public function __construct() |
|
39 | - { |
|
40 | - $this->flow = []; |
|
41 | - $this->start = microtime(true); |
|
42 | - } |
|
43 | - |
|
44 | - /** |
|
45 | - * @return array |
|
46 | - */ |
|
47 | - public function getTotalTimeForHook(array $data) |
|
48 | - { |
|
49 | - $total = 0; |
|
50 | - foreach ($data['time'] as $time) { |
|
51 | - $total += ($time['stop'] - $time['start']) * 1000; |
|
52 | - } |
|
53 | - return $total; |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * @return array |
|
58 | - */ |
|
59 | - public function addCallbacksForAction($action) |
|
60 | - { |
|
61 | - global $wp_filter; |
|
62 | - if (!array_key_exists($action, $this->flow)) { |
|
63 | - return; |
|
64 | - } |
|
65 | - $this->flow[$action]['callbacks_count'] = 0; |
|
66 | - foreach ($wp_filter[$action] as $priority => $callbacks) { |
|
67 | - if (!array_key_exists($priority, $this->flow[$action]['callbacks'])) { |
|
68 | - $this->flow[$action]['callbacks'][$priority] = []; |
|
69 | - } |
|
70 | - foreach ($callbacks as $callback) { |
|
71 | - if (is_array($callback['function']) && 2 == count($callback['function'])) { |
|
72 | - list($object, $method) = $callback['function']; |
|
73 | - if (is_object($object)) { |
|
74 | - $object = get_class($object); |
|
75 | - } |
|
76 | - $this->flow[$action]['callbacks'][$priority][] = sprintf('%s::%s', $object, $method); |
|
77 | - } elseif (is_object($callback['function'])) { |
|
78 | - $this->flow[$action]['callbacks'][$priority][] = get_class($callback['function']); |
|
79 | - } else { |
|
80 | - $this->flow[$action]['callbacks'][$priority][] = $callback['function']; |
|
81 | - } |
|
82 | - ++$this->flow[$action]['callbacks_count']; |
|
83 | - } |
|
84 | - } |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * @return array |
|
89 | - */ |
|
90 | - public function getMeasure() |
|
91 | - { |
|
92 | - foreach ($this->flow as $action => $data) { |
|
93 | - $total = $this->getTotalTimeForHook($data); |
|
94 | - $this->flow[$action]['total'] = $total; |
|
95 | - $this->totalTime += $total; |
|
96 | - $this->totalActions += $data['count']; |
|
97 | - $this->addCallbacksForAction($action); |
|
98 | - } |
|
99 | - uasort($this->flow, [$this, 'sortByTime']); |
|
100 | - return $this->flow; |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * @return int |
|
105 | - */ |
|
106 | - public function getTotalActions() |
|
107 | - { |
|
108 | - return $this->totalActions; |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * @return int Microseconds |
|
113 | - */ |
|
114 | - public function getTotalTime() |
|
115 | - { |
|
116 | - return $this->totalTime; |
|
117 | - // $totalNoise = (count($this->timers) - 1) * $this->noise; |
|
118 | - // return $this->stop - $this->start - $totalNoise; |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * @param string $name |
|
123 | - * @return void |
|
124 | - */ |
|
125 | - public function startTimer() |
|
126 | - { |
|
127 | - if (!isset($this->flow[current_filter()])) { |
|
128 | - $this->flow[current_filter()] = [ |
|
129 | - 'callbacks' => [], |
|
130 | - 'count' => 0, |
|
131 | - 'stack' => [], |
|
132 | - 'time' => [], |
|
133 | - ]; |
|
134 | - add_action(current_filter(), [$this, 'stopTimer'], 9000); |
|
135 | - } |
|
136 | - $count = ++$this->flow[current_filter()]['count']; |
|
137 | - array_push($this->flow[current_filter()]['stack'], ['start' => microtime(true)]); |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * @param mixed $possibleFilter |
|
142 | - * @return mixed |
|
143 | - */ |
|
144 | - public function stopTimer($possibleFilter = null) |
|
145 | - { |
|
146 | - $time = array_pop($this->flow[current_filter()]['stack']); |
|
147 | - $time['stop'] = microtime(true); |
|
148 | - array_push($this->flow[current_filter()]['time'], $time); |
|
149 | - return $possibleFilter; |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * @param array $a |
|
154 | - * @param array $b |
|
155 | - * @return int |
|
156 | - */ |
|
157 | - protected function sortByTime($a, $b) |
|
158 | - { |
|
159 | - if ($a['total'] == $b['total']) { |
|
160 | - return 0; |
|
161 | - } |
|
162 | - return ($a['total'] > $b['total']) ? -1 : 1; |
|
163 | - } |
|
7 | + /** |
|
8 | + * @var array |
|
9 | + */ |
|
10 | + protected $flow; |
|
11 | + |
|
12 | + /** |
|
13 | + * This is the time that WordPress takes to execute the all hook. |
|
14 | + * @var int |
|
15 | + */ |
|
16 | + protected $noise = 0; |
|
17 | + |
|
18 | + /** |
|
19 | + * @var int |
|
20 | + */ |
|
21 | + protected $start = null; |
|
22 | + |
|
23 | + /** |
|
24 | + * @var int |
|
25 | + */ |
|
26 | + protected $stop = null; |
|
27 | + |
|
28 | + /** |
|
29 | + * @var int |
|
30 | + */ |
|
31 | + protected $totalActions = 0; |
|
32 | + |
|
33 | + /** |
|
34 | + * @var int |
|
35 | + */ |
|
36 | + protected $totalTime = 0; |
|
37 | + |
|
38 | + public function __construct() |
|
39 | + { |
|
40 | + $this->flow = []; |
|
41 | + $this->start = microtime(true); |
|
42 | + } |
|
43 | + |
|
44 | + /** |
|
45 | + * @return array |
|
46 | + */ |
|
47 | + public function getTotalTimeForHook(array $data) |
|
48 | + { |
|
49 | + $total = 0; |
|
50 | + foreach ($data['time'] as $time) { |
|
51 | + $total += ($time['stop'] - $time['start']) * 1000; |
|
52 | + } |
|
53 | + return $total; |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * @return array |
|
58 | + */ |
|
59 | + public function addCallbacksForAction($action) |
|
60 | + { |
|
61 | + global $wp_filter; |
|
62 | + if (!array_key_exists($action, $this->flow)) { |
|
63 | + return; |
|
64 | + } |
|
65 | + $this->flow[$action]['callbacks_count'] = 0; |
|
66 | + foreach ($wp_filter[$action] as $priority => $callbacks) { |
|
67 | + if (!array_key_exists($priority, $this->flow[$action]['callbacks'])) { |
|
68 | + $this->flow[$action]['callbacks'][$priority] = []; |
|
69 | + } |
|
70 | + foreach ($callbacks as $callback) { |
|
71 | + if (is_array($callback['function']) && 2 == count($callback['function'])) { |
|
72 | + list($object, $method) = $callback['function']; |
|
73 | + if (is_object($object)) { |
|
74 | + $object = get_class($object); |
|
75 | + } |
|
76 | + $this->flow[$action]['callbacks'][$priority][] = sprintf('%s::%s', $object, $method); |
|
77 | + } elseif (is_object($callback['function'])) { |
|
78 | + $this->flow[$action]['callbacks'][$priority][] = get_class($callback['function']); |
|
79 | + } else { |
|
80 | + $this->flow[$action]['callbacks'][$priority][] = $callback['function']; |
|
81 | + } |
|
82 | + ++$this->flow[$action]['callbacks_count']; |
|
83 | + } |
|
84 | + } |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * @return array |
|
89 | + */ |
|
90 | + public function getMeasure() |
|
91 | + { |
|
92 | + foreach ($this->flow as $action => $data) { |
|
93 | + $total = $this->getTotalTimeForHook($data); |
|
94 | + $this->flow[$action]['total'] = $total; |
|
95 | + $this->totalTime += $total; |
|
96 | + $this->totalActions += $data['count']; |
|
97 | + $this->addCallbacksForAction($action); |
|
98 | + } |
|
99 | + uasort($this->flow, [$this, 'sortByTime']); |
|
100 | + return $this->flow; |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * @return int |
|
105 | + */ |
|
106 | + public function getTotalActions() |
|
107 | + { |
|
108 | + return $this->totalActions; |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * @return int Microseconds |
|
113 | + */ |
|
114 | + public function getTotalTime() |
|
115 | + { |
|
116 | + return $this->totalTime; |
|
117 | + // $totalNoise = (count($this->timers) - 1) * $this->noise; |
|
118 | + // return $this->stop - $this->start - $totalNoise; |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * @param string $name |
|
123 | + * @return void |
|
124 | + */ |
|
125 | + public function startTimer() |
|
126 | + { |
|
127 | + if (!isset($this->flow[current_filter()])) { |
|
128 | + $this->flow[current_filter()] = [ |
|
129 | + 'callbacks' => [], |
|
130 | + 'count' => 0, |
|
131 | + 'stack' => [], |
|
132 | + 'time' => [], |
|
133 | + ]; |
|
134 | + add_action(current_filter(), [$this, 'stopTimer'], 9000); |
|
135 | + } |
|
136 | + $count = ++$this->flow[current_filter()]['count']; |
|
137 | + array_push($this->flow[current_filter()]['stack'], ['start' => microtime(true)]); |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * @param mixed $possibleFilter |
|
142 | + * @return mixed |
|
143 | + */ |
|
144 | + public function stopTimer($possibleFilter = null) |
|
145 | + { |
|
146 | + $time = array_pop($this->flow[current_filter()]['stack']); |
|
147 | + $time['stop'] = microtime(true); |
|
148 | + array_push($this->flow[current_filter()]['time'], $time); |
|
149 | + return $possibleFilter; |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * @param array $a |
|
154 | + * @param array $b |
|
155 | + * @return int |
|
156 | + */ |
|
157 | + protected function sortByTime($a, $b) |
|
158 | + { |
|
159 | + if ($a['total'] == $b['total']) { |
|
160 | + return 0; |
|
161 | + } |
|
162 | + return ($a['total'] > $b['total']) ? -1 : 1; |
|
163 | + } |
|
164 | 164 | } |
@@ -6,70 +6,70 @@ |
||
6 | 6 | |
7 | 7 | class Console |
8 | 8 | { |
9 | - const ERROR_CODES = array( |
|
10 | - E_ERROR => 'Error', // 1 |
|
11 | - E_WARNING => 'Warning', // 2 |
|
12 | - E_NOTICE => 'Notice', // 8 |
|
13 | - E_STRICT => 'Strict', // 2048 |
|
14 | - E_DEPRECATED => 'Deprecated', // 8192 |
|
15 | - ); |
|
9 | + const ERROR_CODES = array( |
|
10 | + E_ERROR => 'Error', // 1 |
|
11 | + E_WARNING => 'Warning', // 2 |
|
12 | + E_NOTICE => 'Notice', // 8 |
|
13 | + E_STRICT => 'Strict', // 2048 |
|
14 | + E_DEPRECATED => 'Deprecated', // 8192 |
|
15 | + ); |
|
16 | 16 | |
17 | - const MAPPED_ERROR_CODES = array( |
|
18 | - 'debug' => 0, |
|
19 | - 'info' => 0, |
|
20 | - 'notice' => 0, |
|
21 | - 'warning' => E_NOTICE, // 8 |
|
22 | - 'error' => E_WARNING, // 2 |
|
23 | - 'critical' => E_WARNING, // 2 |
|
24 | - 'alert' => E_WARNING, // 2 |
|
25 | - 'emergency' => E_WARNING, // 2 |
|
26 | - ); |
|
17 | + const MAPPED_ERROR_CODES = array( |
|
18 | + 'debug' => 0, |
|
19 | + 'info' => 0, |
|
20 | + 'notice' => 0, |
|
21 | + 'warning' => E_NOTICE, // 8 |
|
22 | + 'error' => E_WARNING, // 2 |
|
23 | + 'critical' => E_WARNING, // 2 |
|
24 | + 'alert' => E_WARNING, // 2 |
|
25 | + 'emergency' => E_WARNING, // 2 |
|
26 | + ); |
|
27 | 27 | |
28 | - public $entries = array(); |
|
28 | + public $entries = array(); |
|
29 | 29 | |
30 | - /** |
|
31 | - * @param string $message |
|
32 | - * @return static |
|
33 | - */ |
|
34 | - public function store($message, $errno = 0, $location = '') |
|
35 | - { |
|
36 | - $errname = 'Debug'; |
|
37 | - if (array_key_exists($errno, static::MAPPED_ERROR_CODES)) { |
|
38 | - $errname = ucfirst($errno); |
|
39 | - $errno = static::MAPPED_ERROR_CODES[$errno]; |
|
40 | - } elseif (array_key_exists($errno, static::ERROR_CODES)) { |
|
41 | - $errname = static::ERROR_CODES[$errno]; |
|
42 | - } |
|
43 | - $this->entries[] = array( |
|
44 | - 'errno' => $errno, |
|
45 | - 'message' => $location.$this->normalizeValue($message), |
|
46 | - 'name' => sprintf('<span class="glbb-info glbb-%s">%s</span>', strtolower($errname), $errname), |
|
47 | - ); |
|
48 | - return $this; |
|
49 | - } |
|
30 | + /** |
|
31 | + * @param string $message |
|
32 | + * @return static |
|
33 | + */ |
|
34 | + public function store($message, $errno = 0, $location = '') |
|
35 | + { |
|
36 | + $errname = 'Debug'; |
|
37 | + if (array_key_exists($errno, static::MAPPED_ERROR_CODES)) { |
|
38 | + $errname = ucfirst($errno); |
|
39 | + $errno = static::MAPPED_ERROR_CODES[$errno]; |
|
40 | + } elseif (array_key_exists($errno, static::ERROR_CODES)) { |
|
41 | + $errname = static::ERROR_CODES[$errno]; |
|
42 | + } |
|
43 | + $this->entries[] = array( |
|
44 | + 'errno' => $errno, |
|
45 | + 'message' => $location.$this->normalizeValue($message), |
|
46 | + 'name' => sprintf('<span class="glbb-info glbb-%s">%s</span>', strtolower($errname), $errname), |
|
47 | + ); |
|
48 | + return $this; |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * @param mixed $value |
|
53 | - * @return bool |
|
54 | - */ |
|
55 | - protected function isObjectOrArray($value) |
|
56 | - { |
|
57 | - return is_object($value) || is_array($value); |
|
58 | - } |
|
51 | + /** |
|
52 | + * @param mixed $value |
|
53 | + * @return bool |
|
54 | + */ |
|
55 | + protected function isObjectOrArray($value) |
|
56 | + { |
|
57 | + return is_object($value) || is_array($value); |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * @param mixed $value |
|
62 | - * @return string |
|
63 | - */ |
|
64 | - protected function normalizeValue($value) |
|
65 | - { |
|
66 | - if ($value instanceof DateTime) { |
|
67 | - $value = $value->format('Y-m-d H:i:s'); |
|
68 | - } elseif ($this->isObjectOrArray($value)) { |
|
69 | - $value = print_r(json_decode(json_encode($value)), true); |
|
70 | - } elseif (is_resource($value)) { |
|
71 | - $value = (string) $value; |
|
72 | - } |
|
73 | - return esc_html($value); |
|
74 | - } |
|
60 | + /** |
|
61 | + * @param mixed $value |
|
62 | + * @return string |
|
63 | + */ |
|
64 | + protected function normalizeValue($value) |
|
65 | + { |
|
66 | + if ($value instanceof DateTime) { |
|
67 | + $value = $value->format('Y-m-d H:i:s'); |
|
68 | + } elseif ($this->isObjectOrArray($value)) { |
|
69 | + $value = print_r(json_decode(json_encode($value)), true); |
|
70 | + } elseif (is_resource($value)) { |
|
71 | + $value = (string) $value; |
|
72 | + } |
|
73 | + return esc_html($value); |
|
74 | + } |
|
75 | 75 | } |
@@ -18,15 +18,15 @@ |
||
18 | 18 | defined('WPINC') || die; |
19 | 19 | |
20 | 20 | if (!class_exists('GL_Plugin_Check_v3')) { |
21 | - require_once __DIR__.'/activate.php'; |
|
21 | + require_once __DIR__.'/activate.php'; |
|
22 | 22 | } |
23 | 23 | if (!(new GL_Plugin_Check_v3(__FILE__, array('php' => '5.6', 'wordpress' => '5.2.0')))->canProceed()) { |
24 | - return; |
|
24 | + return; |
|
25 | 25 | } |
26 | 26 | require_once __DIR__.'/autoload.php'; |
27 | 27 | require_once __DIR__.'/compatibility.php'; |
28 | 28 | |
29 | 29 | if (!defined('SAVEQUERIES')) { |
30 | - define('SAVEQUERIES', 1); |
|
30 | + define('SAVEQUERIES', 1); |
|
31 | 31 | } |
32 | 32 | (new GeminiLabs\BlackBar\Application())->init(); |
@@ -8,137 +8,137 @@ |
||
8 | 8 | */ |
9 | 9 | class GL_Plugin_Check_v3 |
10 | 10 | { |
11 | - const MIN_PHP_VERSION = '5.6.0'; |
|
12 | - const MIN_WORDPRESS_VERSION = '4.7.0'; |
|
11 | + const MIN_PHP_VERSION = '5.6.0'; |
|
12 | + const MIN_WORDPRESS_VERSION = '4.7.0'; |
|
13 | 13 | |
14 | - /** |
|
15 | - * @var string |
|
16 | - */ |
|
17 | - protected $file; |
|
14 | + /** |
|
15 | + * @var string |
|
16 | + */ |
|
17 | + protected $file; |
|
18 | 18 | |
19 | - /** |
|
20 | - * @var array |
|
21 | - */ |
|
22 | - protected $versions; |
|
19 | + /** |
|
20 | + * @var array |
|
21 | + */ |
|
22 | + protected $versions; |
|
23 | 23 | |
24 | - /** |
|
25 | - * @param string $file |
|
26 | - */ |
|
27 | - public function __construct($file, array $versions = array()) |
|
28 | - { |
|
29 | - $this->file = realpath($file); |
|
30 | - $this->versions = wp_parse_args($versions, array( |
|
31 | - 'php' => static::MIN_PHP_VERSION, |
|
32 | - 'wordpress' => static::MIN_WORDPRESS_VERSION, |
|
33 | - )); |
|
34 | - } |
|
24 | + /** |
|
25 | + * @param string $file |
|
26 | + */ |
|
27 | + public function __construct($file, array $versions = array()) |
|
28 | + { |
|
29 | + $this->file = realpath($file); |
|
30 | + $this->versions = wp_parse_args($versions, array( |
|
31 | + 'php' => static::MIN_PHP_VERSION, |
|
32 | + 'wordpress' => static::MIN_WORDPRESS_VERSION, |
|
33 | + )); |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * @return bool |
|
38 | - */ |
|
39 | - public function canProceed() |
|
40 | - { |
|
41 | - if ($this->isValid()) { |
|
42 | - return true; |
|
43 | - } |
|
44 | - add_action('activated_plugin', array($this, 'deactivate')); |
|
45 | - add_action('admin_notices', array($this, 'deactivate')); |
|
46 | - return false; |
|
47 | - } |
|
36 | + /** |
|
37 | + * @return bool |
|
38 | + */ |
|
39 | + public function canProceed() |
|
40 | + { |
|
41 | + if ($this->isValid()) { |
|
42 | + return true; |
|
43 | + } |
|
44 | + add_action('activated_plugin', array($this, 'deactivate')); |
|
45 | + add_action('admin_notices', array($this, 'deactivate')); |
|
46 | + return false; |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * @return bool |
|
51 | - */ |
|
52 | - public function isPhpValid() |
|
53 | - { |
|
54 | - return !version_compare(PHP_VERSION, $this->versions['php'], '<'); |
|
55 | - } |
|
49 | + /** |
|
50 | + * @return bool |
|
51 | + */ |
|
52 | + public function isPhpValid() |
|
53 | + { |
|
54 | + return !version_compare(PHP_VERSION, $this->versions['php'], '<'); |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * @return bool |
|
59 | - */ |
|
60 | - public function isValid() |
|
61 | - { |
|
62 | - return $this->isPhpValid() && $this->isWpValid(); |
|
63 | - } |
|
57 | + /** |
|
58 | + * @return bool |
|
59 | + */ |
|
60 | + public function isValid() |
|
61 | + { |
|
62 | + return $this->isPhpValid() && $this->isWpValid(); |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * @return bool |
|
67 | - */ |
|
68 | - public function isWpValid() |
|
69 | - { |
|
70 | - global $wp_version; |
|
71 | - return !version_compare($wp_version, $this->versions['wordpress'], '<'); |
|
72 | - } |
|
65 | + /** |
|
66 | + * @return bool |
|
67 | + */ |
|
68 | + public function isWpValid() |
|
69 | + { |
|
70 | + global $wp_version; |
|
71 | + return !version_compare($wp_version, $this->versions['wordpress'], '<'); |
|
72 | + } |
|
73 | 73 | |
74 | - /** |
|
75 | - * @param string $plugin |
|
76 | - * @return void |
|
77 | - */ |
|
78 | - public function deactivate($plugin) |
|
79 | - { |
|
80 | - if ($this->isValid()) { |
|
81 | - return; |
|
82 | - } |
|
83 | - $pluginSlug = plugin_basename($this->file); |
|
84 | - if ($plugin == $pluginSlug) { |
|
85 | - $this->redirect(); //exit |
|
86 | - } |
|
87 | - $pluginData = get_file_data($this->file, array('name' => 'Plugin Name'), 'plugin'); |
|
88 | - deactivate_plugins($pluginSlug); |
|
89 | - $this->printNotice($pluginData['name']); |
|
90 | - } |
|
74 | + /** |
|
75 | + * @param string $plugin |
|
76 | + * @return void |
|
77 | + */ |
|
78 | + public function deactivate($plugin) |
|
79 | + { |
|
80 | + if ($this->isValid()) { |
|
81 | + return; |
|
82 | + } |
|
83 | + $pluginSlug = plugin_basename($this->file); |
|
84 | + if ($plugin == $pluginSlug) { |
|
85 | + $this->redirect(); //exit |
|
86 | + } |
|
87 | + $pluginData = get_file_data($this->file, array('name' => 'Plugin Name'), 'plugin'); |
|
88 | + deactivate_plugins($pluginSlug); |
|
89 | + $this->printNotice($pluginData['name']); |
|
90 | + } |
|
91 | 91 | |
92 | - /** |
|
93 | - * @return array |
|
94 | - */ |
|
95 | - protected function getMessages() |
|
96 | - { |
|
97 | - return array( |
|
98 | - __('The %s plugin was deactivated.', 'blackbar'), |
|
99 | - __('This plugin requires %s or greater in order to work properly.', 'blackbar'), |
|
100 | - __('Please contact your hosting provider or server administrator to upgrade the version of PHP on your server (your server is running PHP version %s), or try to find an alternative plugin.', 'blackbar'), |
|
101 | - __('PHP version', 'blackbar'), |
|
102 | - __('WordPress version', 'blackbar'), |
|
103 | - __('Update WordPress', 'blackbar'), |
|
104 | - __('You can use the %s plugin to restore %s to the previous version.', 'blackbar'), |
|
105 | - ); |
|
106 | - } |
|
92 | + /** |
|
93 | + * @return array |
|
94 | + */ |
|
95 | + protected function getMessages() |
|
96 | + { |
|
97 | + return array( |
|
98 | + __('The %s plugin was deactivated.', 'blackbar'), |
|
99 | + __('This plugin requires %s or greater in order to work properly.', 'blackbar'), |
|
100 | + __('Please contact your hosting provider or server administrator to upgrade the version of PHP on your server (your server is running PHP version %s), or try to find an alternative plugin.', 'blackbar'), |
|
101 | + __('PHP version', 'blackbar'), |
|
102 | + __('WordPress version', 'blackbar'), |
|
103 | + __('Update WordPress', 'blackbar'), |
|
104 | + __('You can use the %s plugin to restore %s to the previous version.', 'blackbar'), |
|
105 | + ); |
|
106 | + } |
|
107 | 107 | |
108 | - /** |
|
109 | - * @param string $pluginName |
|
110 | - * @return void |
|
111 | - */ |
|
112 | - protected function printNotice($pluginName) |
|
113 | - { |
|
114 | - $noticeTemplate = '<div id="message" class="notice notice-error error is-dismissible"><p><strong>%s</strong></p><p>%s</p><p>%s</p></div>'; |
|
115 | - $messages = $this->getMessages(); |
|
116 | - $rollbackMessage = sprintf('<strong>'.$messages[6].'</strong>', '<a href="https://wordpress.org/plugins/wp-rollback/">WP Rollback</a>', $pluginName); |
|
117 | - if (!$this->isPhpValid()) { |
|
118 | - printf($noticeTemplate, |
|
119 | - sprintf($messages[0], $pluginName), |
|
120 | - sprintf($messages[1], $messages[3].' '.$this->versions['php']), |
|
121 | - sprintf($messages[2], PHP_VERSION).'</p><p>'.$rollbackMessage |
|
122 | - ); |
|
123 | - } elseif (!$this->isWpValid()) { |
|
124 | - printf($noticeTemplate, |
|
125 | - sprintf($messages[0], $pluginName), |
|
126 | - sprintf($messages[1], $messages[4].' '.$this->versions['wordpress']), |
|
127 | - $rollbackMessage.'</p><p>'.sprintf('<a href="%s">%s</a>', admin_url('update-core.php'), $messages[5]) |
|
128 | - ); |
|
129 | - } |
|
130 | - } |
|
108 | + /** |
|
109 | + * @param string $pluginName |
|
110 | + * @return void |
|
111 | + */ |
|
112 | + protected function printNotice($pluginName) |
|
113 | + { |
|
114 | + $noticeTemplate = '<div id="message" class="notice notice-error error is-dismissible"><p><strong>%s</strong></p><p>%s</p><p>%s</p></div>'; |
|
115 | + $messages = $this->getMessages(); |
|
116 | + $rollbackMessage = sprintf('<strong>'.$messages[6].'</strong>', '<a href="https://wordpress.org/plugins/wp-rollback/">WP Rollback</a>', $pluginName); |
|
117 | + if (!$this->isPhpValid()) { |
|
118 | + printf($noticeTemplate, |
|
119 | + sprintf($messages[0], $pluginName), |
|
120 | + sprintf($messages[1], $messages[3].' '.$this->versions['php']), |
|
121 | + sprintf($messages[2], PHP_VERSION).'</p><p>'.$rollbackMessage |
|
122 | + ); |
|
123 | + } elseif (!$this->isWpValid()) { |
|
124 | + printf($noticeTemplate, |
|
125 | + sprintf($messages[0], $pluginName), |
|
126 | + sprintf($messages[1], $messages[4].' '.$this->versions['wordpress']), |
|
127 | + $rollbackMessage.'</p><p>'.sprintf('<a href="%s">%s</a>', admin_url('update-core.php'), $messages[5]) |
|
128 | + ); |
|
129 | + } |
|
130 | + } |
|
131 | 131 | |
132 | - /** |
|
133 | - * @return void |
|
134 | - */ |
|
135 | - protected function redirect() |
|
136 | - { |
|
137 | - wp_safe_redirect(self_admin_url(sprintf('plugins.php?plugin_status=%s&paged=%s&s=%s', |
|
138 | - filter_input(INPUT_GET, 'plugin_status'), |
|
139 | - filter_input(INPUT_GET, 'paged'), |
|
140 | - filter_input(INPUT_GET, 's') |
|
141 | - ))); |
|
142 | - exit; |
|
143 | - } |
|
132 | + /** |
|
133 | + * @return void |
|
134 | + */ |
|
135 | + protected function redirect() |
|
136 | + { |
|
137 | + wp_safe_redirect(self_admin_url(sprintf('plugins.php?plugin_status=%s&paged=%s&s=%s', |
|
138 | + filter_input(INPUT_GET, 'plugin_status'), |
|
139 | + filter_input(INPUT_GET, 'paged'), |
|
140 | + filter_input(INPUT_GET, 's') |
|
141 | + ))); |
|
142 | + exit; |
|
143 | + } |
|
144 | 144 | } |