-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvironment-debug-admin-toolbar.php
324 lines (282 loc) · 7.36 KB
/
environment-debug-admin-toolbar.php
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
<?php
/**
* Environment & Debug Bar
*
* Display your environment and debug info in the toolbar.
*
* @package EnvDebugToolbar
* @author Medium Rare
* @copyright 2023 Medium Rare
* @license GPL-3.0
* @link https://mediumrare.dev/
*
* @wordpress-plugin
* Plugin Name: Environment & Debug Bar
* Description: Display your environment and debug info in the toolbar.
* Version: 1.3.3
* Requires at least: 5.5
* Requires PHP: 7.4
* Author: Medium Rare
* Author URI: https://mediumrare.dev/
* License: GPL v3
* License URI: https://www.gnu.org/licenses/gpl-3.0.txt
* Text Domain: environment-debug-toolbar
* Domain Path: /languages
*/
defined( 'ABSPATH' ) || exit;
define( 'EDT_FILE', __FILE__ );
define( 'EDT_BASENAME', plugin_basename( EDT_FILE ) );
define( 'EDT_VERSION', '1.3.3' );
/**
* EDT
*/
class EDT {
// > Unsorted Helpers.
// > Misc Helpers.
// > Toolbar Helpers.
/**
* Get_env
*
* @return mixed Name of the environment, or false.
*/
private function get_env() {
if ( function_exists( 'getenv' ) ) {
if ( getenv( 'WP_ENVIRONMENT_TYPE' ) !== false ) {
return getenv( 'WP_ENVIRONMENT_TYPE' );
}
if ( getenv( 'WP_ENV' ) !== false ) {
return getenv( 'WP_ENV' );
}
}
if ( defined( 'WP_ENVIRONMENT_TYPE' ) ) {
return WP_ENVIRONMENT_TYPE;
}
if ( defined( 'WP_ENV' ) ) {
return WP_ENV;
}
return false;
}
/**
* Env_type
*
* @param string $env Name of the environment.
* @return integer Environment type number.
*/
private function env_type( $env ) {
if ( ! $env ) {
return 0;
}
$env = strtolower( $env );
$env_development = apply_filters(
'edt_env_development',
[
'development',
'develop',
'dev',
'local',
]
);
if ( in_array( $env, $env_development ) ) {
return 1;
}
$env_staging = apply_filters(
'edt_env_staging',
[
'staging',
'stage',
'testing',
'test',
'acceptance',
'accept',
'integration',
]
);
if ( in_array( $env, $env_staging ) ) {
return 2;
}
$env_production = apply_filters(
'edt_env_production',
[
'production',
'prod',
'live',
]
);
if ( in_array( $env, $env_production ) ) {
return 6;
}
return 9;
}
/**
* Env_type_label
*
* @param string $type_id Environment type number.
* @return string Name of the environment type.
*/
private function env_type_label( $type_id ) {
switch ( $type_id ) {
case 0:
return __( 'No Environment Found', 'environment-debug-toolbar' );
case 1:
return __( 'Development', 'environment-debug-toolbar' );
case 2:
return __( 'Staging', 'environment-debug-toolbar' );
case 6:
return __( 'Production', 'environment-debug-toolbar' );
case 9:
return __( 'Unknown Environment', 'environment-debug-toolbar' );
default:
return '';
}
}
/**
* Html_label_value
*
* @param string $label Text to display as label.
* @param string $value Text to display as value.
* @return string HTML to display label and value.
*/
private function html_label_value( $label, $value ) {
$html = '';
$html .= '<span class="ei-label">' . $label . '</span>';
$html .= '<span class="ei-value">' . $value . '</span>';
return $html;
}
// > Hooks.
/**
* Register_backend_styles
*
* @return void
*/
public function register_backend_styles() {
// Register.
wp_register_style(
'edt-backend-css', // Name.
plugin_dir_url( EDT_FILE ) . 'assets/edt.min.css', // URL.
[], // Deps.
EDT_VERSION, // Version.
'all' // Media.
);
// Enqueue.
wp_enqueue_style( 'edt-backend-css' );
}
/**
* Register_frontend_styles
*
* @return void
*/
public function register_frontend_styles() {
// Abort unless filter returns true.
if ( ! apply_filters( 'edt_show_on_frontend', false ) ) {
return;
}
// Register.
wp_register_style(
'edt-frontend-css', // Name.
plugin_dir_url( EDT_FILE ) . 'assets/edt.min.css', // URL.
[], // Deps.
EDT_VERSION, // Version.
'all' // Media.
);
// Enqueue.
wp_enqueue_style( 'edt-frontend-css' );
}
/**
* Register_translations
*
* @return void
*/
public function register_translations() {
load_plugin_textdomain( 'environment-debug-toolbar', false, dirname( EDT_BASENAME ) . '/languages' );
}
// > Register Hooks.
/**
* Register_toolbar
*
* @param object $wp_admin_bar Details about toolbar nodes.
* @return void
*/
public function register_toolbar( $wp_admin_bar ) {
// Abort on the frontend unless filter returns true.
if ( ! is_admin() && ! apply_filters( 'edt_show_on_frontend', false ) ) {
return;
}
// Abort if the current user does not meet the capability.
if ( ! current_user_can( apply_filters( 'edt_capability_check', 'manage_options' ) ) ) {
return;
}
$env = $this->get_env();
$type_id = $this->env_type( $env );
$type_name = $this->env_type_label( $type_id );
$wp_admin_bar->add_group([
'id' => 'edt-group',
]);
$wp_admin_bar->add_node([
'id' => 'edt-node',
'title' => $type_name,
'parent' => 'edt-group',
'meta' => [
'title' => ( $this->get_env() ? __( 'Your env is set to:', 'environment-debug-toolbar' ) . ' ' . $this->get_env() : '' ),
'class' => 'env-type-' . $type_id,
],
]);
$wp_admin_bar->add_node([
'id' => 'edt-wp-debug',
'title' => $this->html_label_value( 'WP_DEBUG', ( WP_DEBUG ? 'true' : 'false' ) ),
'parent' => 'edt-node',
]);
if ( WP_DEBUG ) {
$wp_admin_bar->add_node([
'id' => 'edt-wp-debug-log',
'title' => $this->html_label_value( 'WP_DEBUG_LOG', ( WP_DEBUG_LOG ? 'true' : 'false' ) ),
'parent' => 'edt-node',
'meta' => [
'title' => WP_DEBUG_LOG,
],
]);
$wp_admin_bar->add_node([
'id' => 'edt-wp-debug-display',
'title' => $this->html_label_value( 'WP_DEBUG_DISPLAY', ( WP_DEBUG_DISPLAY ? 'true' : 'false' ) ),
'parent' => 'edt-node',
]);
$wp_development_mode = ( function_exists('wp_get_development_mode') ? wp_get_development_mode() : '' );
$wp_admin_bar->add_node([
'id' => 'edt-wp-development-mode',
'title' => $this->html_label_value( 'WP_DEVELOPMENT_MODE', $wp_development_mode ),
'parent' => 'edt-node',
]);
$wp_admin_bar->add_node([
'id' => 'edt-script-display',
'title' => $this->html_label_value( 'SCRIPT_DEBUG', ( SCRIPT_DEBUG ? 'true' : 'false' ) ),
'parent' => 'edt-node',
]);
$wp_admin_bar->add_node([
'id' => 'edt-savequeries',
'title' => $this->html_label_value( 'SAVEQUERIES', ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ? 'true' : 'false' ) ),
'parent' => 'edt-node',
]);
}
$wp_admin_bar->add_node([
'id' => 'edt-php',
'title' => $this->html_label_value( 'PHP', phpversion() ),
'parent' => 'edt-node',
]);
}
/**
* Register_hooks
*
* @return void
*/
public function register_hooks() {
// Register backend styles.
add_action( 'admin_enqueue_scripts', [ $this, 'register_backend_styles' ] );
// Register frontend styles.
add_action( 'wp_enqueue_scripts', [ $this, 'register_frontend_styles' ] );
// Register tranlations.
add_action( 'init', [ $this, 'register_translations' ] );
// Register toolbar.
add_action( 'admin_bar_menu', [ $this, 'register_toolbar' ], 300, 1 );
}
}
$edt = new EDT();
$edt->register_hooks();