-
Notifications
You must be signed in to change notification settings - Fork 0
/
mai-debugger.php
155 lines (129 loc) · 3.37 KB
/
mai-debugger.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
<?php
/**
* Plugin Name: Mai Debugger
* Plugin URI: https://bizbudding.com/mai-theme/
* Description: An aggressive debugging plugin with Whoops and Symfony var-dumper.
* Version: 0.1.2
*
* Author: BizBudding
* Author URI: https://bizbudding.com
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) exit;
// Must be at the top of the file.
// use Symfony\Component\VarDumper\VarDumper;
use YahnisElsts\PluginUpdateChecker\v5\PucFactory;
// Include vendor libraries.
require_once __DIR__ . '/vendor/autoload.php';
// Register the handler.
if ( ! ( isset( $_GET['maidebugger'] ) && in_array( $_GET['maidebugger'], [ 'off', 'deactivate' ] ) ) ) {
$whoops = new \Whoops\Run;
$whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler);
$whoops->register();
}
/**
* Force deactivate plugin with query parameter.
*
* @return void
*/
add_action( 'template_redirect', function() {
if ( ! ( isset( $_GET['maidebugger'] ) && 'deactivate' === $_GET['maidebugger'] ) ) {
return;
}
// Deactivate plugin.
deactivate_plugins( 'mai-debugger/mai-debugger.php' );
// Redirect to same page without any query parameters.
wp_safe_redirect( home_url( add_query_arg( [] ) ) );
exit();
});
/**
* Setup the updater.
*
* composer require yahnis-elsts/plugin-update-checker
*
* @since 0.1.0
*
* @uses https://github.com/YahnisElsts/plugin-update-checker/
*
* @return void
*/
add_action( 'plugins_loaded', function() {
// Bail if plugin updater is not loaded.
if ( ! class_exists( 'YahnisElsts\PluginUpdateChecker\v5\PucFactory' ) ) {
return;
}
// Setup the updater.
$updater = PucFactory::buildUpdateChecker( 'https://github.com/maithemewp/mai-debugger/', __FILE__, 'mai-debugger' );
// Set the stable branch.
$updater->setBranch( 'main' );
// Maybe set github api token.
if ( defined( 'MAI_GITHUB_API_TOKEN' ) ) {
$updater->setAuthentication( MAI_GITHUB_API_TOKEN );
}
// Add icons for Dashboard > Updates screen.
if ( function_exists( 'mai_get_updater_icons' ) && $icons = mai_get_updater_icons() ) {
$updater->addResultFilter(
function ( $info ) use ( $icons ) {
$info->icons = $icons;
return $info;
}
);
}
});
/**
* Add an admin bar notice to alert user that the debugger is running.
*
* @since 0.1.0
*
* @return void
*/
add_action( 'admin_bar_menu', function() {
if ( ! is_admin_bar_showing() ) {
return;
}
global $wp_admin_bar;
$admin_notice = [
'parent' => 'top-secondary',
'id' => 'environment-notice',
'title' => sprintf( '<span class="adminbar--environment-notice">%s</span>', __( 'Debug On', 'mai-debugger' ) ),
];
$wp_admin_bar->add_menu( $admin_notice );
}, 9999 );
/**
* Render the admin bar CSS.
*
* @since 0.1.0
*
* @return void
*/
add_action( 'admin_bar_init', function() {
if ( ! is_admin_bar_showing() ) {
return;
}
$css ='
.adminbar--environment-notice {
color: var(--color-link, #72aee6) !important;
text-transform: uppercase !important;
}
@media only screen and ( min-width: 800px ) {
#wp-admin-bar-environment-notice {
display: block;
}
#wp-admin-bar-environment-notice .ab-item {
background-color: var(--color-alt, white) !important;
}
}
';
wp_add_inline_style( 'admin-bar', $css );
}, 9999 );
/**
* Run test.
*
* @return void
*/
// add_action( 'wp_head', function() {
// $value = [
// 'test' => 'Okay',
// ];
// dump( $value );
// });