-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommons-admin.php
211 lines (180 loc) · 4.89 KB
/
commons-admin.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
<?php
/**
* Plugin Name: Commons Admin Bar
* Plugin URI: http://commons.hwdsb.on.ca
* Description: A customized version of the WP Admin bar
* Author: r-a-y
* Version: 1.0
* Author URI: https://commons.hwdsb.on.ca
* License:
*/
add_action( 'plugins_loaded', array( 'HWDSB_Adminbar', 'init' ) );
/**
* HWDSB Adminbar mods.
*/
class HWDSB_Adminbar {
/**
* Static initializer.
*/
public static function init() {
return new self;
}
/**
* Constructor.
*/
public function __construct() {
add_filter( 'allowed_redirect_hosts', array( $this, 'whitelist_subdomains' ), 10, 2 );
add_action( 'admin_bar_menu', array( $this, 'add_custom_parent_menu' ), 11 );
add_action( 'admin_bar_menu', array( $this, 'add_random_site' ), 5 );
add_action( 'wp_head', array( $this, 'add_random_site_css' ), 999 );
}
/**
* Whitelists HWDSB subdomains so users are able to be redirected to them.
*
* By default, only the root domain is whitelisted to be redirected. However,
* we want subdomains from HWDSB to be recognized and redirected as well.
*
* @param array $retval By default, only the root domain is whitelisted
* @param string $subdomain The subdomain being tested for redirection.
* @return array
*/
public function whitelist_subdomains( $retval, $subdomain ) {
if ( strpos( $subdomain, 'hwdsb.on.ca' ) !== false ) {
$retval[] = $subdomain;
}
return $retval;
}
/**
* Adds custom "Home" menu to WP Adminbar.
*
* Also removes the "WP logo" menu.
*
* @param object $wp_admin_bar The WP Admin Bar object
*/
public function add_custom_parent_menu( $wp_admin_bar ) {
/**
* Removing the "W" menu
*/
$wp_admin_bar->remove_menu( 'wp-logo' );
/**
* Create a "Home" menu.
*
* First, just create the parent menu item.
*/
$wp_admin_bar->add_menu( array(
'id' => 'commonlinks',
'parent' => '0', //puts it on the left-hand side
'title' => 'Home',
'href' => ('http://commons.hwdsb.on.ca/')
) );
/**
* Add submenu items to "Home" menu.
*/
// Only show the following for logged-in users
if ( current_user_can( 'read' ) ) {
// Support link
$wp_admin_bar->add_menu( array(
'id' => 'support',
'parent' => 'commonlinks',
'title' => 'Support',
'href' => ('http://support.commons.hwdsb.on.ca/')
) );
// Blog request form
$wp_admin_bar->add_menu( array(
'id' => 'blogrequest',
'parent' => 'commonlinks',
'title' => 'Blog Request Form',
'href' => ('http://support.commons.hwdsb.on.ca/blog-request-form/' )
) );
// Developers blog
$wp_admin_bar->add_menu( array(
'id' => 'developments',
'parent' => 'commonlinks',
'title' => 'Developments',
'href' => ('http://dev.commons.hwdsb.on.ca/' )
) );
}
}
/**
* Inserts a "Random Site" icon in the top-right corner of the WP Admin Bar.
*
* @param object $wp_admin_bar The WP Admin Bar object
*/
public function add_random_site( $wp_admin_bar ) {
if ( is_admin() ) {
return;
}
if ( ! function_exists( 'bp_is_active' ) ) {
return;
}
if ( ! is_multisite() && ! bp_is_active( 'blogs' ) ) {
return;
}
$title = '<span class="ab-icon"></span>';
$wp_admin_bar->add_node( array(
'parent' => 'top-secondary',
'id' => 'random-site',
'title' => $title,
'href' => bp_get_blogs_directory_permalink() . '?random-blog',
'meta' => array(
'title' => __( 'Random Site', 'hwdsb' ),
'rel' => 'nofollow',
)
) );
}
/**
* Inline CSS for the "Random Site" dashicon for the WP adminbar menu item.
*/
public function add_random_site_css() {
if ( ! function_exists( 'bp_is_active' ) ) {
return;
}
if ( ! is_multisite() && ! bp_is_active( 'blogs' ) ) {
return;
}
?>
<style type="text/css">
#wpadminbar #wp-admin-bar-random-site > .ab-item:before {
content: "\f463";
top: 2px;
width: 8px;
}
#wpadminbar #wp-admin-bar-commonlinks > .ab-item:before {
content: "\f319";
}
@media screen and ( max-width: 782px ) {
#wpadminbar #wp-admin-bar-random-site a.ab-item,
#wpadminbar #wp-admin-bar-commonlinks a.ab-item {
text-overflow: clip;
}
#wpadminbar #wp-admin-bar-random-site > .ab-item,
#wpadminbar #wp-admin-bar-commonlinks > .ab-item {
text-indent: 100%;
white-space: nowrap;
overflow: hidden;
width: 52px;
padding: 0;
color: #999;
position: relative;
}
#wpadminbar #wp-admin-bar-random-site > .ab-item:before,
#wpadminbar #wp-admin-bar-commonlinks > .ab-item:before {
display: block;
text-indent: 0;
font: normal 32px/1 'dashicons';
speak: none;
top: 7px;
width: 52px;
text-align: center;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
#wpadminbar li#wp-admin-bar-random-site,
#wpadminbar li#wp-admin-bar-commonlinks {
display: block;
}
}
</style>
<?php
}
}