Skip to content

Commit 325f378

Browse files
Add admin bar debug indicator.
1 parent b3ccb1c commit 325f378

File tree

4 files changed

+171
-0
lines changed

4 files changed

+171
-0
lines changed

assets/css/admin-bar.html

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<style type="text/css">
2+
#wpadminbar {
3+
background-color: %s !important;
4+
}
5+
6+
#wp-admin-bar-environment-notice {
7+
display: none;
8+
}
9+
10+
#wpadminbar .ab-item,
11+
#wpadminbar a.ab-item,
12+
#wpadminbar > #wp-toolbar span.ab-label,
13+
#wpadminbar > #wp-toolbar span.noticon,
14+
.adminbar--environment-notice {
15+
color: #fff;
16+
}
17+
18+
@media only screen and ( min-width: 800px ) {
19+
#wp-admin-bar-environment-notice {
20+
display: block;
21+
}
22+
23+
#wp-admin-bar-environment-notice .ab-item {
24+
background-color: %s !important;
25+
}
26+
27+
#wp-admin-bar-environment-notice:hover .ab-item {
28+
background-color: %s !important;
29+
color: #fff;
30+
}
31+
}
32+
</style>

config/admin-bar.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* Admin Bar Runtime Configuration Parameters.
4+
*
5+
* @package KnowTheCode\DebugToolkit
6+
* @since 1.0.0
7+
* @author hellofromTonya
8+
* @link https://github.com/KnowTheCode/debug-toolkit
9+
* @license GNU-2.0+
10+
*/
11+
12+
namespace KnowTheCode\DebugToolkit;
13+
14+
return [
15+
'message' => 'DEBUG ACTIVE',
16+
'color_scheme' => 'coffee',
17+
'colors' => [
18+
'admin_bar_background_color' => '#627f00',
19+
'message_background_color' => '#cb4b14',
20+
'message_hover_color' => '#1b202d',
21+
],
22+
];

debug-toolkit.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,17 @@ function _get_plugin_root_dir() {
5151
return __DIR__;
5252
}
5353

54+
/**
55+
* Load the Admin Bar.
56+
*
57+
* @since 1.0.0
58+
*/
59+
function load_admin_bar() {
60+
require_once __DIR__ . '/src/class-admin-bar.php';
61+
$config = require __DIR__ . '/config/admin-bar.php';
62+
63+
( new Admin_Bar( $config ) )->init();
64+
}
65+
5466
require_once __DIR__ . '/vendor/autoload.php';
67+
load_admin_bar();

src/class-admin-bar.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
/**
3+
* Admin functions to change the look of the admin bar when this plugin
4+
* is activated, i.e. to differentiate that we are in development mode.
5+
*
6+
* @package KnowTheCode\DebugToolkit
7+
* @since 1.0.0
8+
* @author hellofromTonya
9+
* @link https://github.com/KnowTheCode/debug-toolkit
10+
* @license GNU-2.0+
11+
*/
12+
13+
namespace KnowTheCode\DebugToolkit;
14+
15+
/**
16+
* Class Admin_Bar
17+
* @package KnowTheCode\DebugToolkit
18+
*/
19+
class Admin_Bar {
20+
21+
/**
22+
* Configuration parameters.
23+
*
24+
* @var array
25+
*/
26+
protected $config;
27+
28+
/**
29+
* Admin_Bar constructor.
30+
*
31+
* @param array $config Configuration parameters.
32+
*/
33+
public function __construct( array $config ) {
34+
$this->config = $config;
35+
}
36+
37+
/**
38+
* Initializes by registering each hook's callback to the object.
39+
*
40+
* @since 1.0.0
41+
*/
42+
public function init() {
43+
add_filter( 'get_user_option_admin_color', [ $this, 'set_local_development_admin_color_scheme' ], 5 );
44+
add_action( 'admin_bar_menu', [ $this, 'add_admin_bar_notice' ], 9999 );
45+
add_action( 'admin_head', [ $this, 'render_admin_bar_css' ], 9999 );
46+
add_action( 'wp_head', [ $this, 'render_admin_bar_css' ], 9999 );
47+
}
48+
49+
/**
50+
* Force different admin color scheme when this plugin is active.
51+
*
52+
* @since 1.0.0
53+
*
54+
* @return string
55+
*/
56+
public function set_local_development_admin_color_scheme() {
57+
return $this->config['color_scheme'];
58+
}
59+
60+
/**
61+
* Add an admin bar notice to alert user that they are in local development
62+
* and this plugin is activated.
63+
*
64+
* @since 1.0.0
65+
*
66+
* @return void
67+
*/
68+
public function add_admin_bar_notice() {
69+
if ( ! is_admin_bar_showing() ) {
70+
return;
71+
}
72+
global $wp_admin_bar;
73+
74+
$admin_notice = [
75+
'parent' => 'top-secondary',
76+
'id' => 'environment-notice',
77+
'title' => sprintf( '<span class="adminbar--environment-notice">%s</span>', $this->config['message'] ),
78+
];
79+
80+
$wp_admin_bar->add_menu( $admin_notice );
81+
}
82+
83+
84+
/**
85+
* Render the admin bar CSS.
86+
*
87+
* @since 1.0.0
88+
*
89+
* @return void
90+
*/
91+
public function render_admin_bar_css() {
92+
if ( ! is_admin_bar_showing() ) {
93+
return;
94+
}
95+
96+
ob_start();
97+
98+
include _get_plugin_root_dir() . '/assets/css/admin-bar.html';
99+
100+
$css_pattern = ob_get_clean();
101+
102+
vprintf( $css_pattern, $this->config['colors'] );
103+
}
104+
}

0 commit comments

Comments
 (0)