Repository navigation
Expand file tree
/
Copy pathdev-debug-tools.php
More file actions
355 lines (295 loc) · 9.34 KB
/
Copy pathdev-debug-tools.php
File metadata and controls
355 lines (295 loc) · 9.34 KB
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<?php
/**
* Plugin Name: Developer Debug Tools
* Plugin URI: https://pluginrx.com/plugin/dev-debug-tools/
* Description: WordPress debugging and testing tools for developers
* Version: 3.0.7
* Requires at least: 6.0
* Requires PHP: 8.0
* Author: PluginRx
* Author URI: https://pluginrx.com/
* Discord URI: https://discord.gg/3HnzNEJVnR
* Text Domain: dev-debug-tools
* License: GPLv2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Created on: May 13, 2022
*/
namespace Apos37\DevDebugTools;
if ( ! defined( 'ABSPATH' ) ) exit;
/**
* BOOTSTRAP
*
* Loads plugin metadata, performs environment checks, and initializes the plugin.
*/
final class Bootstrap {
/**
* Plugin files to load.
*
* This array contains the paths to all plugin files that need to be included.
*/
public const FILES = [
'hub/pages/resources/links.php' => 'dev',
'admin-area/class-admin-area.php' => 'admin',
'hub/menu.php' => 'dev',
'functions.php' => 'all',
'helpers/help-map.php' => 'dev',
'helpers/error-messages.php' => 'all',
'helpers/jokes.php' => 'dev',
'helpers/discord.php' => 'all',
'admin-area/security/class-security.php' => 'all',
'shortcodes.php' => 'dev',
'site-wide/heartbeat/class-heartbeat.php' => 'dev',
'admin-area/admin-bar/class-admin-bar.php' => 'all',
'admin-area/online-users/class-online-users.php' => 'all',
'admin-area/plugins/class-plugins.php' => 'admin',
'site-wide/class-site-wide.php' => 'all',
'cleanup.php' => 'dev',
'deprecated.php' => 'dev',
'backdoor.php' => 'all',
'hub/pages/tools/security-checks/class-security-cron.php' => 'all'
];
/**
* Plugin header keys for get_file_data()
*/
public const HEADER_KEYS = [
'name' => 'Plugin Name',
'description' => 'Description',
'version' => 'Version',
'plugin_uri' => 'Plugin URI',
'requires_php' => 'Requires PHP',
'textdomain' => 'Text Domain',
'author' => 'Author',
'author_uri' => 'Author URI',
'discord_uri' => 'Discord URI'
];
/**
* @var array Plugin metadata from file header
*/
private array $meta;
/**
* @var Bootstrap|null Singleton instance
*/
private static ?Bootstrap $instance = null;
/**
* Get instance
*
* @return self
*/
public static function instance() : self {
return self::$instance ??= new self();
} // End instance()
/**
* Constructor
*/
private function __construct() {
$this->meta = $this->load_meta();
$this->check_environment();
add_action( 'plugins_loaded', [ $this, 'load_files' ] );
} // End __construct()
/**
* Check if test mode is enabled
*
* @return bool
*/
public static function is_test_mode() : bool {
return filter_var( get_option( 'ddtt_test_mode' ), FILTER_VALIDATE_BOOLEAN );
} // End is_test_mode()
/**
* Load plugin metadata
*
* @return array
*/
private function load_meta() : array {
return get_file_data( __FILE__, self::HEADER_KEYS );
} // End load_meta()
/**
* Check environment requirements
*
* @return void
*/
private function check_environment() : void {
if ( version_compare( PHP_VERSION, $this->meta[ 'requires_php' ], '<' ) ) {
deactivate_plugins( plugin_basename( __FILE__ ) );
wp_die( sprintf(
/* translators: %1$s is plugin name, %2$s is required PHP version */
esc_html( __( '%1$s requires PHP %2$s or higher.', 'dev-debug-tools' ) ),
esc_html( $this->meta[ 'name' ] ),
esc_html( $this->meta[ 'requires_php' ] )
) );
}
} // End check_environment()
/**
* Load all required plugin files
*
* @return void
*/
public function load_files() : void {
$helpers_file = __DIR__ . '/inc/helpers/helpers.php';
if ( file_exists( $helpers_file ) ) {
require_once $helpers_file;
} else {
return;
}
$is_dev = Helpers::has_access();
$is_administrator = current_user_can( 'administrator' );
foreach ( self::FILES as $file => $env ) {
if (
( $env === 'dev' && $is_dev ) ||
( $env === 'admin' && ( $is_administrator || $is_dev ) ) ||
( $env === 'all' )
) {
$file_path = __DIR__ . '/inc/' . $file;
if ( file_exists( $file_path ) ) {
require_once $file_path;
} else {
_doing_it_wrong(
__METHOD__,
sprintf( 'File not found: %s', esc_html( $file_path ) ),
esc_html( $this->version() )
);
}
}
}
} // End load_files()
/**
* Get admin URL
*
* @param string $path
* @param string $scheme
* @return string
*/
public static function admin_url( $path = '', $scheme = 'admin' ) {
return is_network_admin() ? network_admin_url( $path, $scheme ) : admin_url( $path, $scheme );
} // End admin_url()
/**
* Get metadata value
*
* @param string $key
* @return string
*/
public static function meta( string $key ) : string {
return self::$instance->meta[ $key ] ?? '';
} // End meta()
/**
* Get plugin URL
*
* @param string $append
* @return string
*/
public static function url( string $append = '' ) : string {
return plugin_dir_url( __FILE__ ) . ltrim( $append, '/' );
} // End url()
/**
* Get plugin path
*
* @param string $append
* @return string
*/
public static function path( string $append = '' ) : string {
return plugin_dir_path( __FILE__ ) . ltrim( $append, '/' );
} // End path()
/**
* Get a page URL
*
* @param string $append
* @return string
*/
public static function page_url( $slug ) : string {
return self::admin_url( 'admin.php?page=dev-debug-' . $slug );
} // End page_url()
/**
* Get a tool URL
*
* @param string $append
* @return string
*/
public static function tool_url( $slug ) : string {
if ( is_multisite() && is_network_admin() ) {
$primary_site_id = get_main_site_id();
$base_admin_url = get_admin_url( $primary_site_id, 'admin.php' );
} else {
$base_admin_url = self::admin_url( 'admin.php' );
}
return add_query_arg(
[
'page' => 'dev-debug-tools',
'tool' => $slug,
],
$base_admin_url
);
} // End tool_url()
/**
* Get plugin name
*
* @return string
*/
public static function name() : string {
return self::meta( 'name' );
} // End name()
/**
* Get plugin version
*
* @return string
*/
public static function version() : string {
return self::meta( 'version' );
} // End version()
/**
* Get script/style version for cache busting.
* Returns timestamp if TEST_MODE is enabled, otherwise plugin version.
*
* @return string
*/
public static function script_version() : string {
if ( self::is_test_mode() ) {
return 'TEST-' . time();
}
return self::version();
} // End script_version()
/**
* Get plugin text domain
*
* @return string
*/
public static function textdomain() : string {
return self::meta( 'textdomain' );
} // End textdomain()
/**
* Get plugin author
*
* @return string
*/
public static function author() : string {
return self::meta( 'author' );
} // End author()
/**
* Get plugin URI
*
* @return string
*/
public static function plugin_uri() : string {
return self::meta( 'plugin_uri' );
} // End plugin_uri()
/**
* Get author URI
*
* @return string
*/
public static function author_uri() : string {
return self::meta( 'author_uri' );
} // End author_uri()
/**
* Get Discord URI
*
* @return string
*/
public static function discord_uri() : string {
return self::meta( 'discord_uri' );
} // End discord_uri()
/**
* Prevent cloning and unserializing
*/
public function __clone() {}
public function __wakeup() {}
} // End Bootstrap
Bootstrap::instance();