author | ymh <ymh.work@gmail.com> |
Tue, 27 Sep 2022 16:37:53 +0200 | |
changeset 19 | 3d72ae0968f4 |
parent 18 | be944660c56a |
child 21 | 48c4eec2b7e6 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3 |
* Toolbar API: Top-level Toolbar functionality |
0 | 4 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5 |
* @package WordPress |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6 |
* @subpackage Toolbar |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7 |
* @since 3.1.0 |
0 | 8 |
*/ |
9 |
||
10 |
/** |
|
19 | 11 |
* Instantiates the admin bar object and set it up as a global for access elsewhere. |
0 | 12 |
* |
13 |
* UNHOOKING THIS FUNCTION WILL NOT PROPERLY REMOVE THE ADMIN BAR. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
14 |
* For that, use show_admin_bar(false) or the {@see 'show_admin_bar'} filter. |
0 | 15 |
* |
16 |
* @since 3.1.0 |
|
17 |
* @access private |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
18 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
19 |
* @global WP_Admin_Bar $wp_admin_bar |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
20 |
* |
0 | 21 |
* @return bool Whether the admin bar was successfully initialized. |
22 |
*/ |
|
23 |
function _wp_admin_bar_init() { |
|
24 |
global $wp_admin_bar; |
|
25 |
||
9 | 26 |
if ( ! is_admin_bar_showing() ) { |
0 | 27 |
return false; |
9 | 28 |
} |
0 | 29 |
|
30 |
/* Load the admin bar class code ready for instantiation */ |
|
16 | 31 |
require_once ABSPATH . WPINC . '/class-wp-admin-bar.php'; |
0 | 32 |
|
33 |
/* Instantiate the admin bar */ |
|
5 | 34 |
|
35 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
36 |
* Filters the admin bar class to instantiate. |
5 | 37 |
* |
38 |
* @since 3.1.0 |
|
39 |
* |
|
40 |
* @param string $wp_admin_bar_class Admin bar class to use. Default 'WP_Admin_Bar'. |
|
41 |
*/ |
|
0 | 42 |
$admin_bar_class = apply_filters( 'wp_admin_bar_class', 'WP_Admin_Bar' ); |
9 | 43 |
if ( class_exists( $admin_bar_class ) ) { |
0 | 44 |
$wp_admin_bar = new $admin_bar_class; |
9 | 45 |
} else { |
0 | 46 |
return false; |
9 | 47 |
} |
0 | 48 |
|
49 |
$wp_admin_bar->initialize(); |
|
50 |
$wp_admin_bar->add_menus(); |
|
51 |
||
52 |
return true; |
|
53 |
} |
|
54 |
||
55 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
56 |
* Renders the admin bar to the page based on the $wp_admin_bar->menu member var. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
57 |
* |
16 | 58 |
* This is called very early on the {@see 'wp_body_open'} action so that it will render |
59 |
* before anything else being added to the page body. |
|
60 |
* |
|
61 |
* For backward compatibility with themes not using the 'wp_body_open' action, |
|
62 |
* the function is also called late on {@see 'wp_footer'}. |
|
0 | 63 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
64 |
* It includes the {@see 'admin_bar_menu'} action which should be used to hook in and |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
65 |
* add new menus to the admin bar. That way you can be sure that you are adding at most |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
66 |
* optimal point, right before the admin bar is rendered. This also gives you access to |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
67 |
* the `$post` global, among others. |
0 | 68 |
* |
69 |
* @since 3.1.0 |
|
16 | 70 |
* @since 5.4.0 Called on 'wp_body_open' action first, with 'wp_footer' as a fallback. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
71 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
72 |
* @global WP_Admin_Bar $wp_admin_bar |
0 | 73 |
*/ |
74 |
function wp_admin_bar_render() { |
|
75 |
global $wp_admin_bar; |
|
16 | 76 |
static $rendered = false; |
77 |
||
78 |
if ( $rendered ) { |
|
79 |
return; |
|
80 |
} |
|
0 | 81 |
|
9 | 82 |
if ( ! is_admin_bar_showing() || ! is_object( $wp_admin_bar ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
83 |
return; |
9 | 84 |
} |
0 | 85 |
|
5 | 86 |
/** |
19 | 87 |
* Loads all necessary admin bar items. |
5 | 88 |
* |
89 |
* This is the hook used to add, remove, or manipulate admin bar items. |
|
90 |
* |
|
91 |
* @since 3.1.0 |
|
92 |
* |
|
19 | 93 |
* @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance, passed by reference. |
5 | 94 |
*/ |
0 | 95 |
do_action_ref_array( 'admin_bar_menu', array( &$wp_admin_bar ) ); |
96 |
||
5 | 97 |
/** |
98 |
* Fires before the admin bar is rendered. |
|
99 |
* |
|
100 |
* @since 3.1.0 |
|
101 |
*/ |
|
0 | 102 |
do_action( 'wp_before_admin_bar_render' ); |
103 |
||
104 |
$wp_admin_bar->render(); |
|
105 |
||
5 | 106 |
/** |
107 |
* Fires after the admin bar is rendered. |
|
108 |
* |
|
109 |
* @since 3.1.0 |
|
110 |
*/ |
|
0 | 111 |
do_action( 'wp_after_admin_bar_render' ); |
16 | 112 |
|
113 |
$rendered = true; |
|
0 | 114 |
} |
115 |
||
116 |
/** |
|
19 | 117 |
* Adds the WordPress logo menu. |
0 | 118 |
* |
119 |
* @since 3.3.0 |
|
120 |
* |
|
19 | 121 |
* @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance. |
0 | 122 |
*/ |
123 |
function wp_admin_bar_wp_menu( $wp_admin_bar ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
124 |
if ( current_user_can( 'read' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
125 |
$about_url = self_admin_url( 'about.php' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
126 |
} elseif ( is_multisite() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
127 |
$about_url = get_dashboard_url( get_current_user_id(), 'about.php' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
128 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
129 |
$about_url = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
130 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
131 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
132 |
$wp_logo_menu_args = array( |
0 | 133 |
'id' => 'wp-logo', |
18 | 134 |
'title' => '<span class="ab-icon" aria-hidden="true"></span><span class="screen-reader-text">' . __( 'About WordPress' ) . '</span>', |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
135 |
'href' => $about_url, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
136 |
); |
0 | 137 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
138 |
// Set tabindex="0" to make sub menus accessible when no URL is available. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
139 |
if ( ! $about_url ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
140 |
$wp_logo_menu_args['meta'] = array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
141 |
'tabindex' => 0, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
142 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
143 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
144 |
|
16 | 145 |
$wp_admin_bar->add_node( $wp_logo_menu_args ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
146 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
147 |
if ( $about_url ) { |
16 | 148 |
// Add "About WordPress" link. |
149 |
$wp_admin_bar->add_node( |
|
9 | 150 |
array( |
151 |
'parent' => 'wp-logo', |
|
152 |
'id' => 'about', |
|
153 |
'title' => __( 'About WordPress' ), |
|
154 |
'href' => $about_url, |
|
155 |
) |
|
156 |
); |
|
0 | 157 |
} |
158 |
||
16 | 159 |
// Add WordPress.org link. |
160 |
$wp_admin_bar->add_node( |
|
9 | 161 |
array( |
162 |
'parent' => 'wp-logo-external', |
|
163 |
'id' => 'wporg', |
|
164 |
'title' => __( 'WordPress.org' ), |
|
165 |
'href' => __( 'https://wordpress.org/' ), |
|
166 |
) |
|
167 |
); |
|
0 | 168 |
|
18 | 169 |
// Add documentation link. |
16 | 170 |
$wp_admin_bar->add_node( |
9 | 171 |
array( |
172 |
'parent' => 'wp-logo-external', |
|
173 |
'id' => 'documentation', |
|
174 |
'title' => __( 'Documentation' ), |
|
18 | 175 |
'href' => __( 'https://wordpress.org/support/' ), |
9 | 176 |
) |
177 |
); |
|
0 | 178 |
|
16 | 179 |
// Add forums link. |
180 |
$wp_admin_bar->add_node( |
|
9 | 181 |
array( |
182 |
'parent' => 'wp-logo-external', |
|
183 |
'id' => 'support-forums', |
|
184 |
'title' => __( 'Support' ), |
|
18 | 185 |
'href' => __( 'https://wordpress.org/support/forums/' ), |
9 | 186 |
) |
187 |
); |
|
0 | 188 |
|
16 | 189 |
// Add feedback link. |
190 |
$wp_admin_bar->add_node( |
|
9 | 191 |
array( |
192 |
'parent' => 'wp-logo-external', |
|
193 |
'id' => 'feedback', |
|
194 |
'title' => __( 'Feedback' ), |
|
195 |
'href' => __( 'https://wordpress.org/support/forum/requests-and-feedback' ), |
|
196 |
) |
|
197 |
); |
|
0 | 198 |
} |
199 |
||
200 |
/** |
|
19 | 201 |
* Adds the sidebar toggle button. |
5 | 202 |
* |
203 |
* @since 3.8.0 |
|
204 |
* |
|
19 | 205 |
* @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance. |
5 | 206 |
*/ |
207 |
function wp_admin_bar_sidebar_toggle( $wp_admin_bar ) { |
|
208 |
if ( is_admin() ) { |
|
16 | 209 |
$wp_admin_bar->add_node( |
9 | 210 |
array( |
211 |
'id' => 'menu-toggle', |
|
18 | 212 |
'title' => '<span class="ab-icon" aria-hidden="true"></span><span class="screen-reader-text">' . __( 'Menu' ) . '</span>', |
9 | 213 |
'href' => '#', |
214 |
) |
|
215 |
); |
|
5 | 216 |
} |
217 |
} |
|
218 |
||
219 |
/** |
|
19 | 220 |
* Adds the "My Account" item. |
0 | 221 |
* |
222 |
* @since 3.3.0 |
|
223 |
* |
|
19 | 224 |
* @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance. |
0 | 225 |
*/ |
226 |
function wp_admin_bar_my_account_item( $wp_admin_bar ) { |
|
227 |
$user_id = get_current_user_id(); |
|
228 |
$current_user = wp_get_current_user(); |
|
229 |
||
9 | 230 |
if ( ! $user_id ) { |
0 | 231 |
return; |
9 | 232 |
} |
0 | 233 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
234 |
if ( current_user_can( 'read' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
235 |
$profile_url = get_edit_profile_url( $user_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
236 |
} elseif ( is_multisite() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
237 |
$profile_url = get_dashboard_url( $user_id, 'profile.php' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
238 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
239 |
$profile_url = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
240 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
241 |
|
5 | 242 |
$avatar = get_avatar( $user_id, 26 ); |
16 | 243 |
/* translators: %s: Current user's display name. */ |
9 | 244 |
$howdy = sprintf( __( 'Howdy, %s' ), '<span class="display-name">' . $current_user->display_name . '</span>' ); |
245 |
$class = empty( $avatar ) ? '' : 'with-avatar'; |
|
0 | 246 |
|
16 | 247 |
$wp_admin_bar->add_node( |
9 | 248 |
array( |
249 |
'id' => 'my-account', |
|
250 |
'parent' => 'top-secondary', |
|
251 |
'title' => $howdy . $avatar, |
|
252 |
'href' => $profile_url, |
|
253 |
'meta' => array( |
|
254 |
'class' => $class, |
|
255 |
), |
|
256 |
) |
|
257 |
); |
|
0 | 258 |
} |
259 |
||
260 |
/** |
|
19 | 261 |
* Adds the "My Account" submenu items. |
0 | 262 |
* |
263 |
* @since 3.1.0 |
|
264 |
* |
|
19 | 265 |
* @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance. |
0 | 266 |
*/ |
267 |
function wp_admin_bar_my_account_menu( $wp_admin_bar ) { |
|
268 |
$user_id = get_current_user_id(); |
|
269 |
$current_user = wp_get_current_user(); |
|
270 |
||
9 | 271 |
if ( ! $user_id ) { |
0 | 272 |
return; |
9 | 273 |
} |
0 | 274 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
275 |
if ( current_user_can( 'read' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
276 |
$profile_url = get_edit_profile_url( $user_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
277 |
} elseif ( is_multisite() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
278 |
$profile_url = get_dashboard_url( $user_id, 'profile.php' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
279 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
280 |
$profile_url = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
281 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
282 |
|
9 | 283 |
$wp_admin_bar->add_group( |
284 |
array( |
|
285 |
'parent' => 'my-account', |
|
286 |
'id' => 'user-actions', |
|
287 |
) |
|
288 |
); |
|
0 | 289 |
|
290 |
$user_info = get_avatar( $user_id, 64 ); |
|
291 |
$user_info .= "<span class='display-name'>{$current_user->display_name}</span>"; |
|
292 |
||
9 | 293 |
if ( $current_user->display_name !== $current_user->user_login ) { |
0 | 294 |
$user_info .= "<span class='username'>{$current_user->user_login}</span>"; |
9 | 295 |
} |
0 | 296 |
|
16 | 297 |
$wp_admin_bar->add_node( |
9 | 298 |
array( |
299 |
'parent' => 'user-actions', |
|
300 |
'id' => 'user-info', |
|
301 |
'title' => $user_info, |
|
302 |
'href' => $profile_url, |
|
303 |
'meta' => array( |
|
304 |
'tabindex' => -1, |
|
305 |
), |
|
306 |
) |
|
307 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
308 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
309 |
if ( false !== $profile_url ) { |
16 | 310 |
$wp_admin_bar->add_node( |
9 | 311 |
array( |
312 |
'parent' => 'user-actions', |
|
313 |
'id' => 'edit-profile', |
|
16 | 314 |
'title' => __( 'Edit Profile' ), |
9 | 315 |
'href' => $profile_url, |
316 |
) |
|
317 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
318 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
319 |
|
16 | 320 |
$wp_admin_bar->add_node( |
9 | 321 |
array( |
322 |
'parent' => 'user-actions', |
|
323 |
'id' => 'logout', |
|
324 |
'title' => __( 'Log Out' ), |
|
325 |
'href' => wp_logout_url(), |
|
326 |
) |
|
327 |
); |
|
0 | 328 |
} |
329 |
||
330 |
/** |
|
19 | 331 |
* Adds the "Site Name" menu. |
0 | 332 |
* |
333 |
* @since 3.3.0 |
|
334 |
* |
|
19 | 335 |
* @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance. |
0 | 336 |
*/ |
337 |
function wp_admin_bar_site_menu( $wp_admin_bar ) { |
|
338 |
// Don't show for logged out users. |
|
9 | 339 |
if ( ! is_user_logged_in() ) { |
0 | 340 |
return; |
9 | 341 |
} |
0 | 342 |
|
343 |
// Show only when the user is a member of this site, or they're a super admin. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
344 |
if ( ! is_user_member_of_blog() && ! current_user_can( 'manage_network' ) ) { |
0 | 345 |
return; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
346 |
} |
0 | 347 |
|
9 | 348 |
$blogname = get_bloginfo( 'name' ); |
0 | 349 |
|
5 | 350 |
if ( ! $blogname ) { |
0 | 351 |
$blogname = preg_replace( '#^(https?://)?(www.)?#', '', get_home_url() ); |
5 | 352 |
} |
0 | 353 |
|
354 |
if ( is_network_admin() ) { |
|
16 | 355 |
/* translators: %s: Site title. */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
356 |
$blogname = sprintf( __( 'Network Admin: %s' ), esc_html( get_network()->site_name ) ); |
0 | 357 |
} elseif ( is_user_admin() ) { |
16 | 358 |
/* translators: %s: Site title. */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
359 |
$blogname = sprintf( __( 'User Dashboard: %s' ), esc_html( get_network()->site_name ) ); |
0 | 360 |
} |
361 |
||
362 |
$title = wp_html_excerpt( $blogname, 40, '…' ); |
|
363 |
||
16 | 364 |
$wp_admin_bar->add_node( |
9 | 365 |
array( |
366 |
'id' => 'site-name', |
|
367 |
'title' => $title, |
|
368 |
'href' => ( is_admin() || ! current_user_can( 'read' ) ) ? home_url( '/' ) : admin_url(), |
|
369 |
) |
|
370 |
); |
|
0 | 371 |
|
372 |
// Create submenu items. |
|
373 |
||
374 |
if ( is_admin() ) { |
|
375 |
// Add an option to visit the site. |
|
16 | 376 |
$wp_admin_bar->add_node( |
9 | 377 |
array( |
378 |
'parent' => 'site-name', |
|
379 |
'id' => 'view-site', |
|
380 |
'title' => __( 'Visit Site' ), |
|
381 |
'href' => home_url( '/' ), |
|
382 |
) |
|
383 |
); |
|
0 | 384 |
|
385 |
if ( is_blog_admin() && is_multisite() && current_user_can( 'manage_sites' ) ) { |
|
16 | 386 |
$wp_admin_bar->add_node( |
9 | 387 |
array( |
388 |
'parent' => 'site-name', |
|
389 |
'id' => 'edit-site', |
|
390 |
'title' => __( 'Edit Site' ), |
|
391 |
'href' => network_admin_url( 'site-info.php?id=' . get_current_blog_id() ), |
|
392 |
) |
|
393 |
); |
|
0 | 394 |
} |
9 | 395 |
} elseif ( current_user_can( 'read' ) ) { |
0 | 396 |
// We're on the front end, link to the Dashboard. |
16 | 397 |
$wp_admin_bar->add_node( |
9 | 398 |
array( |
399 |
'parent' => 'site-name', |
|
400 |
'id' => 'dashboard', |
|
401 |
'title' => __( 'Dashboard' ), |
|
402 |
'href' => admin_url(), |
|
403 |
) |
|
404 |
); |
|
0 | 405 |
|
406 |
// Add the appearance submenu items. |
|
407 |
wp_admin_bar_appearance_menu( $wp_admin_bar ); |
|
408 |
} |
|
409 |
} |
|
410 |
||
411 |
/** |
|
19 | 412 |
* Adds the "Edit site" link to the Toolbar. |
413 |
* |
|
414 |
* @since 5.9.0 |
|
415 |
* |
|
416 |
* @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance. |
|
417 |
*/ |
|
418 |
function wp_admin_bar_edit_site_menu( $wp_admin_bar ) { |
|
419 |
// Don't show if a block theme is not activated. |
|
420 |
if ( ! wp_is_block_theme() ) { |
|
421 |
return; |
|
422 |
} |
|
423 |
||
424 |
// Don't show for users who can't edit theme options or when in the admin. |
|
425 |
if ( ! current_user_can( 'edit_theme_options' ) || is_admin() ) { |
|
426 |
return; |
|
427 |
} |
|
428 |
||
429 |
$wp_admin_bar->add_node( |
|
430 |
array( |
|
431 |
'id' => 'site-editor', |
|
432 |
'title' => __( 'Edit site' ), |
|
433 |
'href' => admin_url( 'site-editor.php' ), |
|
434 |
) |
|
435 |
); |
|
436 |
} |
|
437 |
||
438 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
439 |
* Adds the "Customize" link to the Toolbar. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
440 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
441 |
* @since 4.3.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
442 |
* |
19 | 443 |
* @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
444 |
* @global WP_Customize_Manager $wp_customize |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
445 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
446 |
function wp_admin_bar_customize_menu( $wp_admin_bar ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
447 |
global $wp_customize; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
448 |
|
19 | 449 |
// Don't show if a block theme is activated and no plugins use the customizer. |
450 |
if ( wp_is_block_theme() && ! has_action( 'customize_register' ) ) { |
|
451 |
return; |
|
452 |
} |
|
453 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
454 |
// Don't show for users who can't access the customizer or when in the admin. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
455 |
if ( ! current_user_can( 'customize' ) || is_admin() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
456 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
457 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
458 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
459 |
// Don't show if the user cannot edit a given customize_changeset post currently being previewed. |
16 | 460 |
if ( is_customize_preview() && $wp_customize->changeset_post_id() |
461 |
&& ! current_user_can( get_post_type_object( 'customize_changeset' )->cap->edit_post, $wp_customize->changeset_post_id() ) |
|
462 |
) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
463 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
464 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
465 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
466 |
$current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
467 |
if ( is_customize_preview() && $wp_customize->changeset_uuid() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
468 |
$current_url = remove_query_arg( 'customize_changeset_uuid', $current_url ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
469 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
470 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
471 |
$customize_url = add_query_arg( 'url', urlencode( $current_url ), wp_customize_url() ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
472 |
if ( is_customize_preview() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
473 |
$customize_url = add_query_arg( array( 'changeset_uuid' => $wp_customize->changeset_uuid() ), $customize_url ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
474 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
475 |
|
16 | 476 |
$wp_admin_bar->add_node( |
9 | 477 |
array( |
478 |
'id' => 'customize', |
|
479 |
'title' => __( 'Customize' ), |
|
480 |
'href' => $customize_url, |
|
481 |
'meta' => array( |
|
482 |
'class' => 'hide-if-no-customize', |
|
483 |
), |
|
484 |
) |
|
485 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
486 |
add_action( 'wp_before_admin_bar_render', 'wp_customize_support_script' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
487 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
488 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
489 |
/** |
19 | 490 |
* Adds the "My Sites/[Site Name]" menu and all submenus. |
0 | 491 |
* |
492 |
* @since 3.1.0 |
|
493 |
* |
|
19 | 494 |
* @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance. |
0 | 495 |
*/ |
496 |
function wp_admin_bar_my_sites_menu( $wp_admin_bar ) { |
|
497 |
// Don't show for logged out users or single site mode. |
|
9 | 498 |
if ( ! is_user_logged_in() || ! is_multisite() ) { |
0 | 499 |
return; |
9 | 500 |
} |
0 | 501 |
|
502 |
// Show only when the user has at least one site, or they're a super admin. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
503 |
if ( count( $wp_admin_bar->user->blogs ) < 1 && ! current_user_can( 'manage_network' ) ) { |
0 | 504 |
return; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
505 |
} |
0 | 506 |
|
5 | 507 |
if ( $wp_admin_bar->user->active_blog ) { |
508 |
$my_sites_url = get_admin_url( $wp_admin_bar->user->active_blog->blog_id, 'my-sites.php' ); |
|
509 |
} else { |
|
510 |
$my_sites_url = admin_url( 'my-sites.php' ); |
|
511 |
} |
|
512 |
||
16 | 513 |
$wp_admin_bar->add_node( |
9 | 514 |
array( |
515 |
'id' => 'my-sites', |
|
516 |
'title' => __( 'My Sites' ), |
|
517 |
'href' => $my_sites_url, |
|
518 |
) |
|
519 |
); |
|
0 | 520 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
521 |
if ( current_user_can( 'manage_network' ) ) { |
9 | 522 |
$wp_admin_bar->add_group( |
523 |
array( |
|
524 |
'parent' => 'my-sites', |
|
525 |
'id' => 'my-sites-super-admin', |
|
526 |
) |
|
527 |
); |
|
0 | 528 |
|
16 | 529 |
$wp_admin_bar->add_node( |
9 | 530 |
array( |
531 |
'parent' => 'my-sites-super-admin', |
|
532 |
'id' => 'network-admin', |
|
533 |
'title' => __( 'Network Admin' ), |
|
534 |
'href' => network_admin_url(), |
|
535 |
) |
|
536 |
); |
|
0 | 537 |
|
16 | 538 |
$wp_admin_bar->add_node( |
9 | 539 |
array( |
540 |
'parent' => 'network-admin', |
|
541 |
'id' => 'network-admin-d', |
|
542 |
'title' => __( 'Dashboard' ), |
|
543 |
'href' => network_admin_url(), |
|
544 |
) |
|
545 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
546 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
547 |
if ( current_user_can( 'manage_sites' ) ) { |
16 | 548 |
$wp_admin_bar->add_node( |
9 | 549 |
array( |
550 |
'parent' => 'network-admin', |
|
551 |
'id' => 'network-admin-s', |
|
552 |
'title' => __( 'Sites' ), |
|
553 |
'href' => network_admin_url( 'sites.php' ), |
|
554 |
) |
|
555 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
556 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
557 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
558 |
if ( current_user_can( 'manage_network_users' ) ) { |
16 | 559 |
$wp_admin_bar->add_node( |
9 | 560 |
array( |
561 |
'parent' => 'network-admin', |
|
562 |
'id' => 'network-admin-u', |
|
563 |
'title' => __( 'Users' ), |
|
564 |
'href' => network_admin_url( 'users.php' ), |
|
565 |
) |
|
566 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
567 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
568 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
569 |
if ( current_user_can( 'manage_network_themes' ) ) { |
16 | 570 |
$wp_admin_bar->add_node( |
9 | 571 |
array( |
572 |
'parent' => 'network-admin', |
|
573 |
'id' => 'network-admin-t', |
|
574 |
'title' => __( 'Themes' ), |
|
575 |
'href' => network_admin_url( 'themes.php' ), |
|
576 |
) |
|
577 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
578 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
579 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
580 |
if ( current_user_can( 'manage_network_plugins' ) ) { |
16 | 581 |
$wp_admin_bar->add_node( |
9 | 582 |
array( |
583 |
'parent' => 'network-admin', |
|
584 |
'id' => 'network-admin-p', |
|
585 |
'title' => __( 'Plugins' ), |
|
586 |
'href' => network_admin_url( 'plugins.php' ), |
|
587 |
) |
|
588 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
589 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
590 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
591 |
if ( current_user_can( 'manage_network_options' ) ) { |
16 | 592 |
$wp_admin_bar->add_node( |
9 | 593 |
array( |
594 |
'parent' => 'network-admin', |
|
595 |
'id' => 'network-admin-o', |
|
596 |
'title' => __( 'Settings' ), |
|
597 |
'href' => network_admin_url( 'settings.php' ), |
|
598 |
) |
|
599 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
600 |
} |
0 | 601 |
} |
602 |
||
16 | 603 |
// Add site links. |
9 | 604 |
$wp_admin_bar->add_group( |
605 |
array( |
|
606 |
'parent' => 'my-sites', |
|
607 |
'id' => 'my-sites-list', |
|
608 |
'meta' => array( |
|
609 |
'class' => current_user_can( 'manage_network' ) ? 'ab-sub-secondary' : '', |
|
610 |
), |
|
611 |
) |
|
612 |
); |
|
0 | 613 |
|
19 | 614 |
/** |
615 |
* Filters whether to show the site icons in toolbar. |
|
616 |
* |
|
617 |
* Returning false to this hook is the recommended way to hide site icons in the toolbar. |
|
618 |
* A truthy return may have negative performance impact on large multisites. |
|
619 |
* |
|
620 |
* @since 6.0.0 |
|
621 |
* |
|
622 |
* @param bool $show_site_icons Whether site icons should be shown in the toolbar. Default true. |
|
623 |
*/ |
|
624 |
$show_site_icons = apply_filters( 'wp_admin_bar_show_site_icons', true ); |
|
625 |
||
0 | 626 |
foreach ( (array) $wp_admin_bar->user->blogs as $blog ) { |
627 |
switch_to_blog( $blog->userblog_id ); |
|
628 |
||
19 | 629 |
if ( true === $show_site_icons && has_site_icon() ) { |
18 | 630 |
$blavatar = sprintf( |
19 | 631 |
'<img class="blavatar" src="%s" srcset="%s 2x" alt="" width="16" height="16"%s />', |
18 | 632 |
esc_url( get_site_icon_url( 16 ) ), |
19 | 633 |
esc_url( get_site_icon_url( 32 ) ), |
634 |
( wp_lazy_loading_enabled( 'img', 'site_icon_in_toolbar' ) ? ' loading="lazy"' : '' ) |
|
18 | 635 |
); |
636 |
} else { |
|
637 |
$blavatar = '<div class="blavatar"></div>'; |
|
638 |
} |
|
0 | 639 |
|
5 | 640 |
$blogname = $blog->blogname; |
641 |
||
642 |
if ( ! $blogname ) { |
|
643 |
$blogname = preg_replace( '#^(https?://)?(www.)?#', '', get_home_url() ); |
|
644 |
} |
|
645 |
||
9 | 646 |
$menu_id = 'blog-' . $blog->userblog_id; |
0 | 647 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
648 |
if ( current_user_can( 'read' ) ) { |
16 | 649 |
$wp_admin_bar->add_node( |
9 | 650 |
array( |
651 |
'parent' => 'my-sites-list', |
|
652 |
'id' => $menu_id, |
|
653 |
'title' => $blavatar . $blogname, |
|
654 |
'href' => admin_url(), |
|
655 |
) |
|
656 |
); |
|
0 | 657 |
|
16 | 658 |
$wp_admin_bar->add_node( |
9 | 659 |
array( |
660 |
'parent' => $menu_id, |
|
661 |
'id' => $menu_id . '-d', |
|
662 |
'title' => __( 'Dashboard' ), |
|
663 |
'href' => admin_url(), |
|
664 |
) |
|
665 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
666 |
} else { |
16 | 667 |
$wp_admin_bar->add_node( |
9 | 668 |
array( |
669 |
'parent' => 'my-sites-list', |
|
670 |
'id' => $menu_id, |
|
671 |
'title' => $blavatar . $blogname, |
|
672 |
'href' => home_url(), |
|
673 |
) |
|
674 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
675 |
} |
0 | 676 |
|
677 |
if ( current_user_can( get_post_type_object( 'post' )->cap->create_posts ) ) { |
|
16 | 678 |
$wp_admin_bar->add_node( |
9 | 679 |
array( |
680 |
'parent' => $menu_id, |
|
681 |
'id' => $menu_id . '-n', |
|
682 |
'title' => get_post_type_object( 'post' )->labels->new_item, |
|
683 |
'href' => admin_url( 'post-new.php' ), |
|
684 |
) |
|
685 |
); |
|
0 | 686 |
} |
687 |
||
688 |
if ( current_user_can( 'edit_posts' ) ) { |
|
16 | 689 |
$wp_admin_bar->add_node( |
9 | 690 |
array( |
691 |
'parent' => $menu_id, |
|
692 |
'id' => $menu_id . '-c', |
|
693 |
'title' => __( 'Manage Comments' ), |
|
694 |
'href' => admin_url( 'edit-comments.php' ), |
|
695 |
) |
|
696 |
); |
|
0 | 697 |
} |
698 |
||
16 | 699 |
$wp_admin_bar->add_node( |
9 | 700 |
array( |
701 |
'parent' => $menu_id, |
|
702 |
'id' => $menu_id . '-v', |
|
703 |
'title' => __( 'Visit Site' ), |
|
704 |
'href' => home_url( '/' ), |
|
705 |
) |
|
706 |
); |
|
0 | 707 |
|
708 |
restore_current_blog(); |
|
709 |
} |
|
710 |
} |
|
711 |
||
712 |
/** |
|
19 | 713 |
* Provides a shortlink. |
0 | 714 |
* |
715 |
* @since 3.1.0 |
|
716 |
* |
|
19 | 717 |
* @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance. |
0 | 718 |
*/ |
719 |
function wp_admin_bar_shortlink_menu( $wp_admin_bar ) { |
|
720 |
$short = wp_get_shortlink( 0, 'query' ); |
|
9 | 721 |
$id = 'get-shortlink'; |
0 | 722 |
|
9 | 723 |
if ( empty( $short ) ) { |
0 | 724 |
return; |
9 | 725 |
} |
0 | 726 |
|
727 |
$html = '<input class="shortlink-input" type="text" readonly="readonly" value="' . esc_attr( $short ) . '" />'; |
|
728 |
||
16 | 729 |
$wp_admin_bar->add_node( |
9 | 730 |
array( |
731 |
'id' => $id, |
|
732 |
'title' => __( 'Shortlink' ), |
|
733 |
'href' => $short, |
|
734 |
'meta' => array( 'html' => $html ), |
|
735 |
) |
|
736 |
); |
|
0 | 737 |
} |
738 |
||
739 |
/** |
|
19 | 740 |
* Provides an edit link for posts and terms. |
0 | 741 |
* |
742 |
* @since 3.1.0 |
|
18 | 743 |
* @since 5.5.0 Added a "View Post" link on Comments screen for a single post. |
0 | 744 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
745 |
* @global WP_Term $tag |
16 | 746 |
* @global WP_Query $wp_the_query WordPress Query object. |
9 | 747 |
* @global int $user_id The ID of the user being edited. Not to be confused with the |
748 |
* global $user_ID, which contains the ID of the current user. |
|
16 | 749 |
* @global int $post_id The ID of the post when editing comments for a single post. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
750 |
* |
19 | 751 |
* @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance. |
0 | 752 |
*/ |
753 |
function wp_admin_bar_edit_menu( $wp_admin_bar ) { |
|
16 | 754 |
global $tag, $wp_the_query, $user_id, $post_id; |
0 | 755 |
|
756 |
if ( is_admin() ) { |
|
16 | 757 |
$current_screen = get_current_screen(); |
758 |
$post = get_post(); |
|
759 |
$post_type_object = null; |
|
0 | 760 |
|
16 | 761 |
if ( 'post' === $current_screen->base ) { |
762 |
$post_type_object = get_post_type_object( $post->post_type ); |
|
763 |
} elseif ( 'edit' === $current_screen->base ) { |
|
764 |
$post_type_object = get_post_type_object( $current_screen->post_type ); |
|
765 |
} elseif ( 'edit-comments' === $current_screen->base && $post_id ) { |
|
766 |
$post = get_post( $post_id ); |
|
767 |
if ( $post ) { |
|
768 |
$post_type_object = get_post_type_object( $post->post_type ); |
|
769 |
} |
|
770 |
} |
|
771 |
||
772 |
if ( ( 'post' === $current_screen->base || 'edit-comments' === $current_screen->base ) |
|
773 |
&& 'add' !== $current_screen->action |
|
774 |
&& ( $post_type_object ) |
|
0 | 775 |
&& current_user_can( 'read_post', $post->ID ) |
776 |
&& ( $post_type_object->public ) |
|
9 | 777 |
&& ( $post_type_object->show_in_admin_bar ) ) { |
16 | 778 |
if ( 'draft' === $post->post_status ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
779 |
$preview_link = get_preview_post_link( $post ); |
16 | 780 |
$wp_admin_bar->add_node( |
9 | 781 |
array( |
782 |
'id' => 'preview', |
|
783 |
'title' => $post_type_object->labels->view_item, |
|
784 |
'href' => esc_url( $preview_link ), |
|
785 |
'meta' => array( 'target' => 'wp-preview-' . $post->ID ), |
|
786 |
) |
|
787 |
); |
|
5 | 788 |
} else { |
16 | 789 |
$wp_admin_bar->add_node( |
9 | 790 |
array( |
791 |
'id' => 'view', |
|
792 |
'title' => $post_type_object->labels->view_item, |
|
793 |
'href' => get_permalink( $post->ID ), |
|
794 |
) |
|
795 |
); |
|
5 | 796 |
} |
16 | 797 |
} elseif ( 'edit' === $current_screen->base |
798 |
&& ( $post_type_object ) |
|
9 | 799 |
&& ( $post_type_object->public ) |
800 |
&& ( $post_type_object->show_in_admin_bar ) |
|
801 |
&& ( get_post_type_archive_link( $post_type_object->name ) ) |
|
802 |
&& ! ( 'post' === $post_type_object->name && 'posts' === get_option( 'show_on_front' ) ) ) { |
|
803 |
$wp_admin_bar->add_node( |
|
804 |
array( |
|
805 |
'id' => 'archive', |
|
806 |
'title' => $post_type_object->labels->view_items, |
|
807 |
'href' => get_post_type_archive_link( $current_screen->post_type ), |
|
808 |
) |
|
809 |
); |
|
16 | 810 |
} elseif ( 'term' === $current_screen->base && isset( $tag ) && is_object( $tag ) && ! is_wp_error( $tag ) ) { |
811 |
$tax = get_taxonomy( $tag->taxonomy ); |
|
812 |
if ( is_taxonomy_viewable( $tax ) ) { |
|
813 |
$wp_admin_bar->add_node( |
|
814 |
array( |
|
815 |
'id' => 'view', |
|
816 |
'title' => $tax->labels->view_item, |
|
817 |
'href' => get_term_link( $tag ), |
|
818 |
) |
|
819 |
); |
|
820 |
} |
|
821 |
} elseif ( 'user-edit' === $current_screen->base && isset( $user_id ) ) { |
|
822 |
$user_object = get_userdata( $user_id ); |
|
823 |
$view_link = get_author_posts_url( $user_object->ID ); |
|
824 |
if ( $user_object->exists() && $view_link ) { |
|
825 |
$wp_admin_bar->add_node( |
|
826 |
array( |
|
827 |
'id' => 'view', |
|
828 |
'title' => __( 'View User' ), |
|
829 |
'href' => $view_link, |
|
830 |
) |
|
831 |
); |
|
832 |
} |
|
0 | 833 |
} |
834 |
} else { |
|
835 |
$current_object = $wp_the_query->get_queried_object(); |
|
836 |
||
9 | 837 |
if ( empty( $current_object ) ) { |
0 | 838 |
return; |
9 | 839 |
} |
0 | 840 |
|
16 | 841 |
if ( ! empty( $current_object->post_type ) ) { |
842 |
$post_type_object = get_post_type_object( $current_object->post_type ); |
|
843 |
$edit_post_link = get_edit_post_link( $current_object->ID ); |
|
844 |
if ( $post_type_object |
|
845 |
&& $edit_post_link |
|
846 |
&& current_user_can( 'edit_post', $current_object->ID ) |
|
847 |
&& $post_type_object->show_in_admin_bar ) { |
|
848 |
$wp_admin_bar->add_node( |
|
849 |
array( |
|
850 |
'id' => 'edit', |
|
851 |
'title' => $post_type_object->labels->edit_item, |
|
852 |
'href' => $edit_post_link, |
|
853 |
) |
|
854 |
); |
|
855 |
} |
|
856 |
} elseif ( ! empty( $current_object->taxonomy ) ) { |
|
857 |
$tax = get_taxonomy( $current_object->taxonomy ); |
|
858 |
$edit_term_link = get_edit_term_link( $current_object->term_id, $current_object->taxonomy ); |
|
859 |
if ( $tax && $edit_term_link && current_user_can( 'edit_term', $current_object->term_id ) ) { |
|
860 |
$wp_admin_bar->add_node( |
|
861 |
array( |
|
862 |
'id' => 'edit', |
|
863 |
'title' => $tax->labels->edit_item, |
|
864 |
'href' => $edit_term_link, |
|
865 |
) |
|
866 |
); |
|
867 |
} |
|
868 |
} elseif ( is_a( $current_object, 'WP_User' ) && current_user_can( 'edit_user', $current_object->ID ) ) { |
|
869 |
$edit_user_link = get_edit_user_link( $current_object->ID ); |
|
870 |
if ( $edit_user_link ) { |
|
871 |
$wp_admin_bar->add_node( |
|
872 |
array( |
|
873 |
'id' => 'edit', |
|
874 |
'title' => __( 'Edit User' ), |
|
875 |
'href' => $edit_user_link, |
|
876 |
) |
|
877 |
); |
|
878 |
} |
|
0 | 879 |
} |
880 |
} |
|
881 |
} |
|
882 |
||
883 |
/** |
|
19 | 884 |
* Adds "Add New" menu. |
0 | 885 |
* |
886 |
* @since 3.1.0 |
|
887 |
* |
|
19 | 888 |
* @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance. |
0 | 889 |
*/ |
890 |
function wp_admin_bar_new_content_menu( $wp_admin_bar ) { |
|
891 |
$actions = array(); |
|
892 |
||
893 |
$cpts = (array) get_post_types( array( 'show_in_admin_bar' => true ), 'objects' ); |
|
894 |
||
9 | 895 |
if ( isset( $cpts['post'] ) && current_user_can( $cpts['post']->cap->create_posts ) ) { |
896 |
$actions['post-new.php'] = array( $cpts['post']->labels->name_admin_bar, 'new-post' ); |
|
897 |
} |
|
0 | 898 |
|
9 | 899 |
if ( isset( $cpts['attachment'] ) && current_user_can( 'upload_files' ) ) { |
900 |
$actions['media-new.php'] = array( $cpts['attachment']->labels->name_admin_bar, 'new-media' ); |
|
901 |
} |
|
0 | 902 |
|
9 | 903 |
if ( current_user_can( 'manage_links' ) ) { |
904 |
$actions['link-add.php'] = array( _x( 'Link', 'add new from admin bar' ), 'new-link' ); |
|
905 |
} |
|
0 | 906 |
|
9 | 907 |
if ( isset( $cpts['page'] ) && current_user_can( $cpts['page']->cap->create_posts ) ) { |
908 |
$actions['post-new.php?post_type=page'] = array( $cpts['page']->labels->name_admin_bar, 'new-page' ); |
|
909 |
} |
|
0 | 910 |
|
911 |
unset( $cpts['post'], $cpts['page'], $cpts['attachment'] ); |
|
912 |
||
913 |
// Add any additional custom post types. |
|
914 |
foreach ( $cpts as $cpt ) { |
|
9 | 915 |
if ( ! current_user_can( $cpt->cap->create_posts ) ) { |
0 | 916 |
continue; |
9 | 917 |
} |
0 | 918 |
|
9 | 919 |
$key = 'post-new.php?post_type=' . $cpt->name; |
0 | 920 |
$actions[ $key ] = array( $cpt->labels->name_admin_bar, 'new-' . $cpt->name ); |
921 |
} |
|
922 |
// Avoid clash with parent node and a 'content' post type. |
|
9 | 923 |
if ( isset( $actions['post-new.php?post_type=content'] ) ) { |
0 | 924 |
$actions['post-new.php?post_type=content'][1] = 'add-new-content'; |
9 | 925 |
} |
0 | 926 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
927 |
if ( current_user_can( 'create_users' ) || ( is_multisite() && current_user_can( 'promote_users' ) ) ) { |
9 | 928 |
$actions['user-new.php'] = array( _x( 'User', 'add new from admin bar' ), 'new-user' ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
929 |
} |
0 | 930 |
|
9 | 931 |
if ( ! $actions ) { |
0 | 932 |
return; |
9 | 933 |
} |
0 | 934 |
|
18 | 935 |
$title = '<span class="ab-icon" aria-hidden="true"></span><span class="ab-label">' . _x( 'New', 'admin bar menu group label' ) . '</span>'; |
0 | 936 |
|
16 | 937 |
$wp_admin_bar->add_node( |
9 | 938 |
array( |
939 |
'id' => 'new-content', |
|
940 |
'title' => $title, |
|
941 |
'href' => admin_url( current( array_keys( $actions ) ) ), |
|
942 |
) |
|
943 |
); |
|
0 | 944 |
|
945 |
foreach ( $actions as $link => $action ) { |
|
946 |
list( $title, $id ) = $action; |
|
947 |
||
16 | 948 |
$wp_admin_bar->add_node( |
9 | 949 |
array( |
950 |
'parent' => 'new-content', |
|
951 |
'id' => $id, |
|
952 |
'title' => $title, |
|
953 |
'href' => admin_url( $link ), |
|
954 |
) |
|
955 |
); |
|
0 | 956 |
} |
957 |
} |
|
958 |
||
959 |
/** |
|
19 | 960 |
* Adds edit comments link with awaiting moderation count bubble. |
0 | 961 |
* |
962 |
* @since 3.1.0 |
|
963 |
* |
|
19 | 964 |
* @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance. |
0 | 965 |
*/ |
966 |
function wp_admin_bar_comments_menu( $wp_admin_bar ) { |
|
9 | 967 |
if ( ! current_user_can( 'edit_posts' ) ) { |
0 | 968 |
return; |
9 | 969 |
} |
0 | 970 |
|
9 | 971 |
$awaiting_mod = wp_count_comments(); |
972 |
$awaiting_mod = $awaiting_mod->moderated; |
|
973 |
$awaiting_text = sprintf( |
|
16 | 974 |
/* translators: %s: Number of comments. */ |
9 | 975 |
_n( '%s Comment in moderation', '%s Comments in moderation', $awaiting_mod ), |
976 |
number_format_i18n( $awaiting_mod ) |
|
977 |
); |
|
0 | 978 |
|
18 | 979 |
$icon = '<span class="ab-icon" aria-hidden="true"></span>'; |
9 | 980 |
$title = '<span class="ab-label awaiting-mod pending-count count-' . $awaiting_mod . '" aria-hidden="true">' . number_format_i18n( $awaiting_mod ) . '</span>'; |
981 |
$title .= '<span class="screen-reader-text comments-in-moderation-text">' . $awaiting_text . '</span>'; |
|
0 | 982 |
|
16 | 983 |
$wp_admin_bar->add_node( |
9 | 984 |
array( |
985 |
'id' => 'comments', |
|
986 |
'title' => $icon . $title, |
|
987 |
'href' => admin_url( 'edit-comments.php' ), |
|
988 |
) |
|
989 |
); |
|
0 | 990 |
} |
991 |
||
992 |
/** |
|
19 | 993 |
* Adds appearance submenu items to the "Site Name" menu. |
0 | 994 |
* |
995 |
* @since 3.1.0 |
|
996 |
* |
|
19 | 997 |
* @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance. |
0 | 998 |
*/ |
999 |
function wp_admin_bar_appearance_menu( $wp_admin_bar ) { |
|
9 | 1000 |
$wp_admin_bar->add_group( |
1001 |
array( |
|
1002 |
'parent' => 'site-name', |
|
1003 |
'id' => 'appearance', |
|
1004 |
) |
|
1005 |
); |
|
0 | 1006 |
|
5 | 1007 |
if ( current_user_can( 'switch_themes' ) ) { |
16 | 1008 |
$wp_admin_bar->add_node( |
9 | 1009 |
array( |
1010 |
'parent' => 'appearance', |
|
1011 |
'id' => 'themes', |
|
1012 |
'title' => __( 'Themes' ), |
|
1013 |
'href' => admin_url( 'themes.php' ), |
|
1014 |
) |
|
1015 |
); |
|
5 | 1016 |
} |
1017 |
||
1018 |
if ( ! current_user_can( 'edit_theme_options' ) ) { |
|
1019 |
return; |
|
1020 |
} |
|
0 | 1021 |
|
9 | 1022 |
if ( current_theme_supports( 'widgets' ) ) { |
16 | 1023 |
$wp_admin_bar->add_node( |
9 | 1024 |
array( |
1025 |
'parent' => 'appearance', |
|
1026 |
'id' => 'widgets', |
|
1027 |
'title' => __( 'Widgets' ), |
|
1028 |
'href' => admin_url( 'widgets.php' ), |
|
1029 |
) |
|
1030 |
); |
|
5 | 1031 |
} |
1032 |
||
9 | 1033 |
if ( current_theme_supports( 'menus' ) || current_theme_supports( 'widgets' ) ) { |
16 | 1034 |
$wp_admin_bar->add_node( |
9 | 1035 |
array( |
1036 |
'parent' => 'appearance', |
|
1037 |
'id' => 'menus', |
|
1038 |
'title' => __( 'Menus' ), |
|
1039 |
'href' => admin_url( 'nav-menus.php' ), |
|
1040 |
) |
|
1041 |
); |
|
1042 |
} |
|
0 | 1043 |
|
5 | 1044 |
if ( current_theme_supports( 'custom-background' ) ) { |
16 | 1045 |
$wp_admin_bar->add_node( |
9 | 1046 |
array( |
1047 |
'parent' => 'appearance', |
|
1048 |
'id' => 'background', |
|
1049 |
'title' => __( 'Background' ), |
|
1050 |
'href' => admin_url( 'themes.php?page=custom-background' ), |
|
1051 |
'meta' => array( |
|
1052 |
'class' => 'hide-if-customize', |
|
1053 |
), |
|
1054 |
) |
|
1055 |
); |
|
5 | 1056 |
} |
0 | 1057 |
|
5 | 1058 |
if ( current_theme_supports( 'custom-header' ) ) { |
16 | 1059 |
$wp_admin_bar->add_node( |
9 | 1060 |
array( |
1061 |
'parent' => 'appearance', |
|
1062 |
'id' => 'header', |
|
1063 |
'title' => __( 'Header' ), |
|
1064 |
'href' => admin_url( 'themes.php?page=custom-header' ), |
|
1065 |
'meta' => array( |
|
1066 |
'class' => 'hide-if-customize', |
|
1067 |
), |
|
1068 |
) |
|
1069 |
); |
|
5 | 1070 |
} |
1071 |
||
0 | 1072 |
} |
1073 |
||
1074 |
/** |
|
19 | 1075 |
* Provides an update link if theme/plugin/core updates are available. |
0 | 1076 |
* |
1077 |
* @since 3.1.0 |
|
1078 |
* |
|
19 | 1079 |
* @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance. |
0 | 1080 |
*/ |
1081 |
function wp_admin_bar_updates_menu( $wp_admin_bar ) { |
|
1082 |
||
1083 |
$update_data = wp_get_update_data(); |
|
1084 |
||
9 | 1085 |
if ( ! $update_data['counts']['total'] ) { |
0 | 1086 |
return; |
9 | 1087 |
} |
0 | 1088 |
|
18 | 1089 |
$updates_text = sprintf( |
1090 |
/* translators: %s: Total number of updates available. */ |
|
1091 |
_n( '%s update available', '%s updates available', $update_data['counts']['total'] ), |
|
1092 |
number_format_i18n( $update_data['counts']['total'] ) |
|
1093 |
); |
|
1094 |
||
1095 |
$icon = '<span class="ab-icon" aria-hidden="true"></span>'; |
|
1096 |
$title = '<span class="ab-label" aria-hidden="true">' . number_format_i18n( $update_data['counts']['total'] ) . '</span>'; |
|
1097 |
$title .= '<span class="screen-reader-text updates-available-text">' . $updates_text . '</span>'; |
|
0 | 1098 |
|
16 | 1099 |
$wp_admin_bar->add_node( |
9 | 1100 |
array( |
1101 |
'id' => 'updates', |
|
18 | 1102 |
'title' => $icon . $title, |
9 | 1103 |
'href' => network_admin_url( 'update-core.php' ), |
1104 |
) |
|
1105 |
); |
|
0 | 1106 |
} |
1107 |
||
1108 |
/** |
|
19 | 1109 |
* Adds search form. |
0 | 1110 |
* |
1111 |
* @since 3.3.0 |
|
1112 |
* |
|
19 | 1113 |
* @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance. |
0 | 1114 |
*/ |
1115 |
function wp_admin_bar_search_menu( $wp_admin_bar ) { |
|
9 | 1116 |
if ( is_admin() ) { |
0 | 1117 |
return; |
9 | 1118 |
} |
0 | 1119 |
|
1120 |
$form = '<form action="' . esc_url( home_url( '/' ) ) . '" method="get" id="adminbarsearch">'; |
|
1121 |
$form .= '<input class="adminbar-input" name="s" id="adminbar-search" type="text" value="" maxlength="150" />'; |
|
5 | 1122 |
$form .= '<label for="adminbar-search" class="screen-reader-text">' . __( 'Search' ) . '</label>'; |
18 | 1123 |
$form .= '<input type="submit" class="adminbar-button" value="' . __( 'Search' ) . '" />'; |
0 | 1124 |
$form .= '</form>'; |
1125 |
||
16 | 1126 |
$wp_admin_bar->add_node( |
9 | 1127 |
array( |
1128 |
'parent' => 'top-secondary', |
|
1129 |
'id' => 'search', |
|
1130 |
'title' => $form, |
|
1131 |
'meta' => array( |
|
1132 |
'class' => 'admin-bar-search', |
|
1133 |
'tabindex' => -1, |
|
1134 |
), |
|
0 | 1135 |
) |
9 | 1136 |
); |
1137 |
} |
|
1138 |
||
1139 |
/** |
|
19 | 1140 |
* Adds a link to exit recovery mode when Recovery Mode is active. |
9 | 1141 |
* |
1142 |
* @since 5.2.0 |
|
1143 |
* |
|
19 | 1144 |
* @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance. |
9 | 1145 |
*/ |
1146 |
function wp_admin_bar_recovery_mode_menu( $wp_admin_bar ) { |
|
1147 |
if ( ! wp_is_recovery_mode() ) { |
|
1148 |
return; |
|
1149 |
} |
|
1150 |
||
1151 |
$url = wp_login_url(); |
|
1152 |
$url = add_query_arg( 'action', WP_Recovery_Mode::EXIT_ACTION, $url ); |
|
1153 |
$url = wp_nonce_url( $url, WP_Recovery_Mode::EXIT_ACTION ); |
|
1154 |
||
16 | 1155 |
$wp_admin_bar->add_node( |
9 | 1156 |
array( |
1157 |
'parent' => 'top-secondary', |
|
1158 |
'id' => 'recovery-mode', |
|
1159 |
'title' => __( 'Exit Recovery Mode' ), |
|
1160 |
'href' => $url, |
|
1161 |
) |
|
1162 |
); |
|
0 | 1163 |
} |
1164 |
||
1165 |
/** |
|
19 | 1166 |
* Adds secondary menus. |
0 | 1167 |
* |
1168 |
* @since 3.3.0 |
|
1169 |
* |
|
19 | 1170 |
* @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance. |
0 | 1171 |
*/ |
1172 |
function wp_admin_bar_add_secondary_groups( $wp_admin_bar ) { |
|
9 | 1173 |
$wp_admin_bar->add_group( |
1174 |
array( |
|
1175 |
'id' => 'top-secondary', |
|
1176 |
'meta' => array( |
|
1177 |
'class' => 'ab-top-secondary', |
|
1178 |
), |
|
1179 |
) |
|
1180 |
); |
|
0 | 1181 |
|
9 | 1182 |
$wp_admin_bar->add_group( |
1183 |
array( |
|
1184 |
'parent' => 'wp-logo', |
|
1185 |
'id' => 'wp-logo-external', |
|
1186 |
'meta' => array( |
|
1187 |
'class' => 'ab-sub-secondary', |
|
1188 |
), |
|
1189 |
) |
|
1190 |
); |
|
0 | 1191 |
} |
1192 |
||
1193 |
/** |
|
19 | 1194 |
* Prints style and scripts for the admin bar. |
0 | 1195 |
* |
1196 |
* @since 3.1.0 |
|
1197 |
*/ |
|
9 | 1198 |
function wp_admin_bar_header() { |
16 | 1199 |
$type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"'; |
9 | 1200 |
?> |
16 | 1201 |
<style<?php echo $type_attr; ?> media="print">#wpadminbar { display:none; }</style> |
9 | 1202 |
<?php |
0 | 1203 |
} |
1204 |
||
1205 |
/** |
|
19 | 1206 |
* Prints default admin bar callback. |
0 | 1207 |
* |
1208 |
* @since 3.1.0 |
|
1209 |
*/ |
|
9 | 1210 |
function _admin_bar_bump_cb() { |
16 | 1211 |
$type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"'; |
9 | 1212 |
?> |
16 | 1213 |
<style<?php echo $type_attr; ?> media="screen"> |
5 | 1214 |
html { margin-top: 32px !important; } |
1215 |
@media screen and ( max-width: 782px ) { |
|
1216 |
html { margin-top: 46px !important; } |
|
1217 |
} |
|
0 | 1218 |
</style> |
9 | 1219 |
<?php |
0 | 1220 |
} |
1221 |
||
1222 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1223 |
* Sets the display status of the admin bar. |
0 | 1224 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1225 |
* This can be called immediately upon plugin load. It does not need to be called |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1226 |
* from a function hooked to the {@see 'init'} action. |
0 | 1227 |
* |
1228 |
* @since 3.1.0 |
|
1229 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1230 |
* @global bool $show_admin_bar |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1231 |
* |
0 | 1232 |
* @param bool $show Whether to allow the admin bar to show. |
1233 |
*/ |
|
1234 |
function show_admin_bar( $show ) { |
|
1235 |
global $show_admin_bar; |
|
1236 |
$show_admin_bar = (bool) $show; |
|
1237 |
} |
|
1238 |
||
1239 |
/** |
|
9 | 1240 |
* Determines whether the admin bar should be showing. |
1241 |
* |
|
1242 |
* For more information on this and similar theme functions, check out |
|
1243 |
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|
1244 |
* Conditional Tags} article in the Theme Developer Handbook. |
|
0 | 1245 |
* |
1246 |
* @since 3.1.0 |
|
1247 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1248 |
* @global bool $show_admin_bar |
19 | 1249 |
* @global string $pagenow The filename of the current screen. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1250 |
* |
0 | 1251 |
* @return bool Whether the admin bar should be showing. |
1252 |
*/ |
|
1253 |
function is_admin_bar_showing() { |
|
1254 |
global $show_admin_bar, $pagenow; |
|
1255 |
||
1256 |
// For all these types of requests, we never want an admin bar. |
|
9 | 1257 |
if ( defined( 'XMLRPC_REQUEST' ) || defined( 'DOING_AJAX' ) || defined( 'IFRAME_REQUEST' ) || wp_is_json_request() ) { |
0 | 1258 |
return false; |
9 | 1259 |
} |
0 | 1260 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1261 |
if ( is_embed() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1262 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1263 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1264 |
|
0 | 1265 |
// Integrated into the admin. |
9 | 1266 |
if ( is_admin() ) { |
0 | 1267 |
return true; |
9 | 1268 |
} |
0 | 1269 |
|
1270 |
if ( ! isset( $show_admin_bar ) ) { |
|
16 | 1271 |
if ( ! is_user_logged_in() || 'wp-login.php' === $pagenow ) { |
0 | 1272 |
$show_admin_bar = false; |
1273 |
} else { |
|
1274 |
$show_admin_bar = _get_admin_bar_pref(); |
|
1275 |
} |
|
1276 |
} |
|
1277 |
||
5 | 1278 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1279 |
* Filters whether to show the admin bar. |
5 | 1280 |
* |
1281 |
* Returning false to this hook is the recommended way to hide the admin bar. |
|
1282 |
* The user's display preference is used for logged in users. |
|
1283 |
* |
|
1284 |
* @since 3.1.0 |
|
1285 |
* |
|
1286 |
* @param bool $show_admin_bar Whether the admin bar should be shown. Default false. |
|
1287 |
*/ |
|
0 | 1288 |
$show_admin_bar = apply_filters( 'show_admin_bar', $show_admin_bar ); |
1289 |
||
1290 |
return $show_admin_bar; |
|
1291 |
} |
|
1292 |
||
1293 |
/** |
|
19 | 1294 |
* Retrieves the admin bar display preference of a user. |
0 | 1295 |
* |
1296 |
* @since 3.1.0 |
|
1297 |
* @access private |
|
1298 |
* |
|
1299 |
* @param string $context Context of this preference check. Defaults to 'front'. The 'admin' |
|
16 | 1300 |
* preference is no longer used. |
1301 |
* @param int $user Optional. ID of the user to check, defaults to 0 for current user. |
|
0 | 1302 |
* @return bool Whether the admin bar should be showing for this user. |
1303 |
*/ |
|
1304 |
function _get_admin_bar_pref( $context = 'front', $user = 0 ) { |
|
1305 |
$pref = get_user_option( "show_admin_bar_{$context}", $user ); |
|
9 | 1306 |
if ( false === $pref ) { |
0 | 1307 |
return true; |
9 | 1308 |
} |
0 | 1309 |
|
1310 |
return 'true' === $pref; |
|
1311 |
} |