PluginProbe
Black Bar / trunk
Black Bar vtrunk
trunk 1.0.0 1.1.0 1.2.0 2.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.2.0 2.2.1 3.0.0 3.1.0 4.0.0 4.0.1 4.0.2 4.0.3 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0
blackbar / plugin / Application.php

Application.php in Black Bar trunk, at plugin/Application.php

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