admin-bar.php ➔ wp_admin_bar_new_content_menu()   F
last analyzed

Complexity

Conditions 15
Paths 576

Size

Total Lines 56
Code Lines 35

Duplication

Lines 6
Ratio 10.71 %

Importance

Changes 0
Metric Value
cc 15
eloc 35
nc 576
nop 1
dl 6
loc 56
rs 3.7857
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Toolbar API: Top-level Toolbar functionality
4
 *
5
 * @package WordPress
6
 * @subpackage Toolbar
7
 * @since 3.1.0
8
 */
9
10
/**
11
 * Instantiate the admin bar object and set it up as a global for access elsewhere.
12
 *
13
 * UNHOOKING THIS FUNCTION WILL NOT PROPERLY REMOVE THE ADMIN BAR.
14
 * For that, use show_admin_bar(false) or the {@see 'show_admin_bar'} filter.
15
 *
16
 * @since 3.1.0
17
 * @access private
18
 *
19
 * @global WP_Admin_Bar $wp_admin_bar
20
 *
21
 * @return bool Whether the admin bar was successfully initialized.
22
 */
23
function _wp_admin_bar_init() {
24
	global $wp_admin_bar;
25
26
	if ( ! is_admin_bar_showing() )
27
		return false;
28
29
	/* Load the admin bar class code ready for instantiation */
30
	require_once( ABSPATH . WPINC . '/class-wp-admin-bar.php' );
31
32
	/* Instantiate the admin bar */
33
34
	/**
35
	 * Filters the admin bar class to instantiate.
36
	 *
37
	 * @since 3.1.0
38
	 *
39
	 * @param string $wp_admin_bar_class Admin bar class to use. Default 'WP_Admin_Bar'.
40
	 */
41
	$admin_bar_class = apply_filters( 'wp_admin_bar_class', 'WP_Admin_Bar' );
42
	if ( class_exists( $admin_bar_class ) )
43
		$wp_admin_bar = new $admin_bar_class;
44
	else
45
		return false;
46
47
	$wp_admin_bar->initialize();
48
	$wp_admin_bar->add_menus();
49
50
	return true;
51
}
52
53
/**
54
 * Renders the admin bar to the page based on the $wp_admin_bar->menu member var.
55
 *
56
 * This is called very late on the footer actions so that it will render after
57
 * anything else being added to the footer.
58
 *
59
 * It includes the {@see 'admin_bar_menu'} action which should be used to hook in and
60
 * add new menus to the admin bar. That way you can be sure that you are adding at most
61
 * optimal point, right before the admin bar is rendered. This also gives you access to
62
 * the `$post` global, among others.
63
 *
64
 * @since 3.1.0
65
 *
66
 * @global WP_Admin_Bar $wp_admin_bar
67
 */
68
function wp_admin_bar_render() {
69
	global $wp_admin_bar;
70
71
	if ( ! is_admin_bar_showing() || ! is_object( $wp_admin_bar ) )
72
		return;
73
74
	/**
75
	 * Load all necessary admin bar items.
76
	 *
77
	 * This is the hook used to add, remove, or manipulate admin bar items.
78
	 *
79
	 * @since 3.1.0
80
	 *
81
	 * @param WP_Admin_Bar $wp_admin_bar WP_Admin_Bar instance, passed by reference
82
	 */
83
	do_action_ref_array( 'admin_bar_menu', array( &$wp_admin_bar ) );
84
85
	/**
86
	 * Fires before the admin bar is rendered.
87
	 *
88
	 * @since 3.1.0
89
	 */
90
	do_action( 'wp_before_admin_bar_render' );
91
92
	$wp_admin_bar->render();
93
94
	/**
95
	 * Fires after the admin bar is rendered.
96
	 *
97
	 * @since 3.1.0
98
	 */
99
	do_action( 'wp_after_admin_bar_render' );
100
}
101
102
/**
103
 * Add the WordPress logo menu.
104
 *
105
 * @since 3.3.0
106
 *
107
 * @param WP_Admin_Bar $wp_admin_bar
108
 */
109
function wp_admin_bar_wp_menu( $wp_admin_bar ) {
110
	$wp_admin_bar->add_menu( array(
111
		'id'    => 'wp-logo',
112
		'title' => '<span class="ab-icon"></span><span class="screen-reader-text">' . __( 'About WordPress' ) . '</span>',
113
		'href'  => self_admin_url( 'about.php' ),
114
	) );
115
116
	if ( is_user_logged_in() ) {
117
		// Add "About WordPress" link
118
		$wp_admin_bar->add_menu( array(
119
			'parent' => 'wp-logo',
120
			'id'     => 'about',
121
			'title'  => __('About WordPress'),
122
			'href'   => self_admin_url( 'about.php' ),
123
		) );
124
	}
125
126
	// Add WordPress.org link
127
	$wp_admin_bar->add_menu( array(
128
		'parent'    => 'wp-logo-external',
129
		'id'        => 'wporg',
130
		'title'     => __('WordPress.org'),
131
		'href'      => __('https://wordpress.org/'),
132
	) );
133
134
	// Add codex link
135
	$wp_admin_bar->add_menu( array(
136
		'parent'    => 'wp-logo-external',
137
		'id'        => 'documentation',
138
		'title'     => __('Documentation'),
139
		'href'      => __('https://codex.wordpress.org/'),
140
	) );
141
142
	// Add forums link
143
	$wp_admin_bar->add_menu( array(
144
		'parent'    => 'wp-logo-external',
145
		'id'        => 'support-forums',
146
		'title'     => __('Support Forums'),
147
		'href'      => __('https://wordpress.org/support/'),
148
	) );
149
150
	// Add feedback link
151
	$wp_admin_bar->add_menu( array(
152
		'parent'    => 'wp-logo-external',
153
		'id'        => 'feedback',
154
		'title'     => __('Feedback'),
155
		'href'      => __('https://wordpress.org/support/forum/requests-and-feedback'),
156
	) );
157
}
158
159
/**
160
 * Add the sidebar toggle button.
161
 *
162
 * @since 3.8.0
163
 *
164
 * @param WP_Admin_Bar $wp_admin_bar
165
 */
166
function wp_admin_bar_sidebar_toggle( $wp_admin_bar ) {
167
	if ( is_admin() ) {
168
		$wp_admin_bar->add_menu( array(
169
			'id'    => 'menu-toggle',
170
			'title' => '<span class="ab-icon"></span><span class="screen-reader-text">' . __( 'Menu' ) . '</span>',
171
			'href'  => '#',
172
		) );
173
	}
174
}
175
176
/**
177
 * Add the "My Account" item.
178
 *
179
 * @since 3.3.0
180
 *
181
 * @param WP_Admin_Bar $wp_admin_bar
182
 */
183
function wp_admin_bar_my_account_item( $wp_admin_bar ) {
184
	$user_id      = get_current_user_id();
185
	$current_user = wp_get_current_user();
186
187
	if ( ! $user_id )
188
		return;
189
190 View Code Duplication
	if ( current_user_can( 'read' ) ) {
191
		$profile_url = get_edit_profile_url( $user_id );
192
	} elseif ( is_multisite() ) {
193
		$profile_url = get_dashboard_url( $user_id, 'profile.php' );
194
	} else {
195
		$profile_url = false;
196
	}
197
198
	$avatar = get_avatar( $user_id, 26 );
199
	$howdy  = sprintf( __('Howdy, %1$s'), $current_user->display_name );
200
	$class  = empty( $avatar ) ? '' : 'with-avatar';
201
202
	$wp_admin_bar->add_menu( array(
203
		'id'        => 'my-account',
204
		'parent'    => 'top-secondary',
205
		'title'     => $howdy . $avatar,
206
		'href'      => $profile_url,
207
		'meta'      => array(
208
			'class'     => $class,
209
		),
210
	) );
211
}
212
213
/**
214
 * Add the "My Account" submenu items.
215
 *
216
 * @since 3.1.0
217
 *
218
 * @param WP_Admin_Bar $wp_admin_bar
219
 */
220
function wp_admin_bar_my_account_menu( $wp_admin_bar ) {
221
	$user_id      = get_current_user_id();
222
	$current_user = wp_get_current_user();
223
224
	if ( ! $user_id )
225
		return;
226
227 View Code Duplication
	if ( current_user_can( 'read' ) ) {
228
		$profile_url = get_edit_profile_url( $user_id );
229
	} elseif ( is_multisite() ) {
230
		$profile_url = get_dashboard_url( $user_id, 'profile.php' );
231
	} else {
232
		$profile_url = false;
233
	}
234
235
	$wp_admin_bar->add_group( array(
236
		'parent' => 'my-account',
237
		'id'     => 'user-actions',
238
	) );
239
240
	$user_info  = get_avatar( $user_id, 64 );
241
	$user_info .= "<span class='display-name'>{$current_user->display_name}</span>";
242
243
	if ( $current_user->display_name !== $current_user->user_login )
244
		$user_info .= "<span class='username'>{$current_user->user_login}</span>";
245
246
	$wp_admin_bar->add_menu( array(
247
		'parent' => 'user-actions',
248
		'id'     => 'user-info',
249
		'title'  => $user_info,
250
		'href'   => $profile_url,
251
		'meta'   => array(
252
			'tabindex' => -1,
253
		),
254
	) );
255
256
	if ( false !== $profile_url ) {
257
		$wp_admin_bar->add_menu( array(
258
			'parent' => 'user-actions',
259
			'id'     => 'edit-profile',
260
			'title'  => __( 'Edit My Profile' ),
261
			'href'   => $profile_url,
262
		) );
263
	}
264
265
	$wp_admin_bar->add_menu( array(
266
		'parent' => 'user-actions',
267
		'id'     => 'logout',
268
		'title'  => __( 'Log Out' ),
269
		'href'   => wp_logout_url(),
270
	) );
271
}
272
273
/**
274
 * Add the "Site Name" menu.
275
 *
276
 * @since 3.3.0
277
 *
278
 * @param WP_Admin_Bar $wp_admin_bar
279
 */
280
function wp_admin_bar_site_menu( $wp_admin_bar ) {
281
	// Don't show for logged out users.
282
	if ( ! is_user_logged_in() )
283
		return;
284
285
	// Show only when the user is a member of this site, or they're a super admin.
286
	if ( ! is_user_member_of_blog() && ! is_super_admin() )
287
		return;
288
289
	$blogname = get_bloginfo('name');
290
291
	if ( ! $blogname ) {
292
		$blogname = preg_replace( '#^(https?://)?(www.)?#', '', get_home_url() );
293
	}
294
295 View Code Duplication
	if ( is_network_admin() ) {
296
		$blogname = sprintf( __('Network Admin: %s'), esc_html( get_current_site()->site_name ) );
297
	} elseif ( is_user_admin() ) {
298
		$blogname = sprintf( __('User Dashboard: %s'), esc_html( get_current_site()->site_name ) );
299
	}
300
301
	$title = wp_html_excerpt( $blogname, 40, '&hellip;' );
302
303
	$wp_admin_bar->add_menu( array(
304
		'id'    => 'site-name',
305
		'title' => $title,
306
		'href'  => ( is_admin() || ! current_user_can( 'read' ) ) ? home_url( '/' ) : admin_url(),
307
	) );
308
309
	// Create submenu items.
310
311
	if ( is_admin() ) {
312
		// Add an option to visit the site.
313
		$wp_admin_bar->add_menu( array(
314
			'parent' => 'site-name',
315
			'id'     => 'view-site',
316
			'title'  => __( 'Visit Site' ),
317
			'href'   => home_url( '/' ),
318
		) );
319
320
		if ( is_blog_admin() && is_multisite() && current_user_can( 'manage_sites' ) ) {
321
			$wp_admin_bar->add_menu( array(
322
				'parent' => 'site-name',
323
				'id'     => 'edit-site',
324
				'title'  => __( 'Edit Site' ),
325
				'href'   => network_admin_url( 'site-info.php?id=' . get_current_blog_id() ),
326
			) );
327
		}
328
329 View Code Duplication
	} else if ( current_user_can( 'read' ) ) {
330
		// We're on the front end, link to the Dashboard.
331
		$wp_admin_bar->add_menu( array(
332
			'parent' => 'site-name',
333
			'id'     => 'dashboard',
334
			'title'  => __( 'Dashboard' ),
335
			'href'   => admin_url(),
336
		) );
337
338
		// Add the appearance submenu items.
339
		wp_admin_bar_appearance_menu( $wp_admin_bar );
340
	}
341
}
342
343
/**
344
 * Adds the "Customize" link to the Toolbar.
345
 *
346
 * @since 4.3.0
347
 *
348
 * @param WP_Admin_Bar $wp_admin_bar WP_Admin_Bar instance.
349
 */
350
function wp_admin_bar_customize_menu( $wp_admin_bar ) {
351
	// Don't show for users who can't access the customizer or when in the admin.
352
	if ( ! current_user_can( 'customize' ) || is_admin() ) {
353
		return;
354
	}
355
356
	$current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
357
	$customize_url = add_query_arg( 'url', urlencode( $current_url ), wp_customize_url() );
358
359
	$wp_admin_bar->add_menu( array(
360
		'id'     => 'customize',
361
		'title'  => __( 'Customize' ),
362
		'href'   => $customize_url,
363
		'meta'   => array(
364
			'class' => 'hide-if-no-customize',
365
		),
366
	) );
367
	add_action( 'wp_before_admin_bar_render', 'wp_customize_support_script' );
368
}
369
370
/**
371
 * Add the "My Sites/[Site Name]" menu and all submenus.
372
 *
373
 * @since 3.1.0
374
 *
375
 * @param WP_Admin_Bar $wp_admin_bar
376
 */
377
function wp_admin_bar_my_sites_menu( $wp_admin_bar ) {
378
	// Don't show for logged out users or single site mode.
379
	if ( ! is_user_logged_in() || ! is_multisite() )
380
		return;
381
382
	// Show only when the user has at least one site, or they're a super admin.
383
	if ( count( $wp_admin_bar->user->blogs ) < 1 && ! is_super_admin() )
384
		return;
385
386
	if ( $wp_admin_bar->user->active_blog ) {
387
		$my_sites_url = get_admin_url( $wp_admin_bar->user->active_blog->blog_id, 'my-sites.php' );
388
	} else {
389
		$my_sites_url = admin_url( 'my-sites.php' );
390
	}
391
392
	$wp_admin_bar->add_menu( array(
393
		'id'    => 'my-sites',
394
		'title' => __( 'My Sites' ),
395
		'href'  => $my_sites_url,
396
	) );
397
398
	if ( is_super_admin() ) {
399
		$wp_admin_bar->add_group( array(
400
			'parent' => 'my-sites',
401
			'id'     => 'my-sites-super-admin',
402
		) );
403
404
		$wp_admin_bar->add_menu( array(
405
			'parent' => 'my-sites-super-admin',
406
			'id'     => 'network-admin',
407
			'title'  => __('Network Admin'),
408
			'href'   => network_admin_url(),
409
		) );
410
411
		$wp_admin_bar->add_menu( array(
412
			'parent' => 'network-admin',
413
			'id'     => 'network-admin-d',
414
			'title'  => __( 'Dashboard' ),
415
			'href'   => network_admin_url(),
416
		) );
417
		$wp_admin_bar->add_menu( array(
418
			'parent' => 'network-admin',
419
			'id'     => 'network-admin-s',
420
			'title'  => __( 'Sites' ),
421
			'href'   => network_admin_url( 'sites.php' ),
422
		) );
423
		$wp_admin_bar->add_menu( array(
424
			'parent' => 'network-admin',
425
			'id'     => 'network-admin-u',
426
			'title'  => __( 'Users' ),
427
			'href'   => network_admin_url( 'users.php' ),
428
		) );
429
		$wp_admin_bar->add_menu( array(
430
			'parent' => 'network-admin',
431
			'id'     => 'network-admin-t',
432
			'title'  => __( 'Themes' ),
433
			'href'   => network_admin_url( 'themes.php' ),
434
		) );
435
		$wp_admin_bar->add_menu( array(
436
			'parent' => 'network-admin',
437
			'id'     => 'network-admin-p',
438
			'title'  => __( 'Plugins' ),
439
			'href'   => network_admin_url( 'plugins.php' ),
440
		) );
441
		$wp_admin_bar->add_menu( array(
442
			'parent' => 'network-admin',
443
			'id'     => 'network-admin-o',
444
			'title'  => __( 'Settings' ),
445
			'href'   => network_admin_url( 'settings.php' ),
446
		) );
447
	}
448
449
	// Add site links
450
	$wp_admin_bar->add_group( array(
451
		'parent' => 'my-sites',
452
		'id'     => 'my-sites-list',
453
		'meta'   => array(
454
			'class' => is_super_admin() ? 'ab-sub-secondary' : '',
455
		),
456
	) );
457
458
	foreach ( (array) $wp_admin_bar->user->blogs as $blog ) {
459
		switch_to_blog( $blog->userblog_id );
460
461
		$blavatar = '<div class="blavatar"></div>';
462
463
		$blogname = $blog->blogname;
464
465
		if ( ! $blogname ) {
466
			$blogname = preg_replace( '#^(https?://)?(www.)?#', '', get_home_url() );
467
		}
468
469
		$menu_id  = 'blog-' . $blog->userblog_id;
470
471
		$wp_admin_bar->add_menu( array(
472
			'parent'    => 'my-sites-list',
473
			'id'        => $menu_id,
474
			'title'     => $blavatar . $blogname,
475
			'href'      => admin_url(),
476
		) );
477
478
		$wp_admin_bar->add_menu( array(
479
			'parent' => $menu_id,
480
			'id'     => $menu_id . '-d',
481
			'title'  => __( 'Dashboard' ),
482
			'href'   => admin_url(),
483
		) );
484
485 View Code Duplication
		if ( current_user_can( get_post_type_object( 'post' )->cap->create_posts ) ) {
486
			$wp_admin_bar->add_menu( array(
487
				'parent' => $menu_id,
488
				'id'     => $menu_id . '-n',
489
				'title'  => __( 'New Post' ),
490
				'href'   => admin_url( 'post-new.php' ),
491
			) );
492
		}
493
494
		if ( current_user_can( 'edit_posts' ) ) {
495
			$wp_admin_bar->add_menu( array(
496
				'parent' => $menu_id,
497
				'id'     => $menu_id . '-c',
498
				'title'  => __( 'Manage Comments' ),
499
				'href'   => admin_url( 'edit-comments.php' ),
500
			) );
501
		}
502
503
		$wp_admin_bar->add_menu( array(
504
			'parent' => $menu_id,
505
			'id'     => $menu_id . '-v',
506
			'title'  => __( 'Visit Site' ),
507
			'href'   => home_url( '/' ),
508
		) );
509
510
		restore_current_blog();
511
	}
512
}
513
514
/**
515
 * Provide a shortlink.
516
 *
517
 * @since 3.1.0
518
 *
519
 * @param WP_Admin_Bar $wp_admin_bar
520
 */
521
function wp_admin_bar_shortlink_menu( $wp_admin_bar ) {
522
	$short = wp_get_shortlink( 0, 'query' );
523
	$id = 'get-shortlink';
524
525
	if ( empty( $short ) )
526
		return;
527
528
	$html = '<input class="shortlink-input" type="text" readonly="readonly" value="' . esc_attr( $short ) . '" />';
529
530
	$wp_admin_bar->add_menu( array(
531
		'id' => $id,
532
		'title' => __( 'Shortlink' ),
533
		'href' => $short,
534
		'meta' => array( 'html' => $html ),
535
	) );
536
}
537
538
/**
539
 * Provide an edit link for posts and terms.
540
 *
541
 * @since 3.1.0
542
 *
543
 * @global WP_Term  $tag
544
 * @global WP_Query $wp_the_query
545
 *
546
 * @param WP_Admin_Bar $wp_admin_bar
547
 */
548
function wp_admin_bar_edit_menu( $wp_admin_bar ) {
549
	global $tag, $wp_the_query;
550
551
	if ( is_admin() ) {
552
		$current_screen = get_current_screen();
553
		$post = get_post();
554
555
		if ( 'post' == $current_screen->base
556
			&& 'add' != $current_screen->action
557
			&& ( $post_type_object = get_post_type_object( $post->post_type ) )
558
			&& current_user_can( 'read_post', $post->ID )
559
			&& ( $post_type_object->public )
560
			&& ( $post_type_object->show_in_admin_bar ) )
561
		{
562
			if ( 'draft' == $post->post_status ) {
563
				$preview_link = get_preview_post_link( $post );
0 ignored issues
show
Bug introduced by
It seems like $post defined by get_post() on line 553 can also be of type array; however, get_preview_post_link() does only seem to accept integer|object<WP_Post>|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
564
				$wp_admin_bar->add_menu( array(
565
					'id' => 'preview',
566
					'title' => $post_type_object->labels->view_item,
567
					'href' => esc_url( $preview_link ),
568
					'meta' => array( 'target' => 'wp-preview-' . $post->ID ),
569
				) );
570
			} else {
571
				$wp_admin_bar->add_menu( array(
572
					'id' => 'view',
573
					'title' => $post_type_object->labels->view_item,
574
					'href' => get_permalink( $post->ID )
575
				) );
576
			}
577
		} elseif ( 'term' == $current_screen->base
578
			&& isset( $tag ) && is_object( $tag ) && ! is_wp_error( $tag )
579
			&& ( $tax = get_taxonomy( $tag->taxonomy ) )
580
			&& $tax->public )
581
		{
582
			$wp_admin_bar->add_menu( array(
583
				'id' => 'view',
584
				'title' => $tax->labels->view_item,
585
				'href' => get_term_link( $tag )
586
			) );
587
		}
588
	} else {
589
		$current_object = $wp_the_query->get_queried_object();
590
591
		if ( empty( $current_object ) )
592
			return;
593
594
		if ( ! empty( $current_object->post_type )
595
			&& ( $post_type_object = get_post_type_object( $current_object->post_type ) )
596
			&& current_user_can( 'edit_post', $current_object->ID )
597
			&& $post_type_object->show_in_admin_bar
598
			&& $edit_post_link = get_edit_post_link( $current_object->ID ) )
599
		{
600
			$wp_admin_bar->add_menu( array(
601
				'id' => 'edit',
602
				'title' => $post_type_object->labels->edit_item,
603
				'href' => $edit_post_link
604
			) );
605
		} elseif ( ! empty( $current_object->taxonomy )
606
			&& ( $tax = get_taxonomy( $current_object->taxonomy ) )
607
			&& current_user_can( $tax->cap->edit_terms )
608
			&& $edit_term_link = get_edit_term_link( $current_object->term_id, $current_object->taxonomy ) )
609
		{
610
			$wp_admin_bar->add_menu( array(
611
				'id' => 'edit',
612
				'title' => $tax->labels->edit_item,
613
				'href' => $edit_term_link
614
			) );
615
		}
616
	}
617
}
618
619
/**
620
 * Add "Add New" menu.
621
 *
622
 * @since 3.1.0
623
 *
624
 * @param WP_Admin_Bar $wp_admin_bar
625
 */
626
function wp_admin_bar_new_content_menu( $wp_admin_bar ) {
627
	$actions = array();
628
629
	$cpts = (array) get_post_types( array( 'show_in_admin_bar' => true ), 'objects' );
630
631 View Code Duplication
	if ( isset( $cpts['post'] ) && current_user_can( $cpts['post']->cap->create_posts ) )
632
		$actions[ 'post-new.php' ] = array( $cpts['post']->labels->name_admin_bar, 'new-post' );
633
634 View Code Duplication
	if ( isset( $cpts['attachment'] ) && current_user_can( 'upload_files' ) )
635
		$actions[ 'media-new.php' ] = array( $cpts['attachment']->labels->name_admin_bar, 'new-media' );
636
637
	if ( current_user_can( 'manage_links' ) )
638
		$actions[ 'link-add.php' ] = array( _x( 'Link', 'add new from admin bar' ), 'new-link' );
639
640 View Code Duplication
	if ( isset( $cpts['page'] ) && current_user_can( $cpts['page']->cap->create_posts ) )
641
		$actions[ 'post-new.php?post_type=page' ] = array( $cpts['page']->labels->name_admin_bar, 'new-page' );
642
643
	unset( $cpts['post'], $cpts['page'], $cpts['attachment'] );
644
645
	// Add any additional custom post types.
646
	foreach ( $cpts as $cpt ) {
647
		if ( ! current_user_can( $cpt->cap->create_posts ) )
648
			continue;
649
650
		$key = 'post-new.php?post_type=' . $cpt->name;
651
		$actions[ $key ] = array( $cpt->labels->name_admin_bar, 'new-' . $cpt->name );
652
	}
653
	// Avoid clash with parent node and a 'content' post type.
654
	if ( isset( $actions['post-new.php?post_type=content'] ) )
655
		$actions['post-new.php?post_type=content'][1] = 'add-new-content';
656
657
	if ( current_user_can( 'create_users' ) || current_user_can( 'promote_users' ) )
658
		$actions[ 'user-new.php' ] = array( _x( 'User', 'add new from admin bar' ), 'new-user' );
659
660
	if ( ! $actions )
661
		return;
662
663
	$title = '<span class="ab-icon"></span><span class="ab-label">' . _x( 'New', 'admin bar menu group label' ) . '</span>';
664
665
	$wp_admin_bar->add_menu( array(
666
		'id'    => 'new-content',
667
		'title' => $title,
668
		'href'  => admin_url( current( array_keys( $actions ) ) ),
0 ignored issues
show
Security Bug introduced by
It seems like current(array_keys($actions)) targeting current() can also be of type false; however, admin_url() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
669
	) );
670
671
	foreach ( $actions as $link => $action ) {
672
		list( $title, $id ) = $action;
673
674
		$wp_admin_bar->add_menu( array(
675
			'parent'    => 'new-content',
676
			'id'        => $id,
677
			'title'     => $title,
678
			'href'      => admin_url( $link )
679
		) );
680
	}
681
}
682
683
/**
684
 * Add edit comments link with awaiting moderation count bubble.
685
 *
686
 * @since 3.1.0
687
 *
688
 * @param WP_Admin_Bar $wp_admin_bar
689
 */
690
function wp_admin_bar_comments_menu( $wp_admin_bar ) {
691
	if ( !current_user_can('edit_posts') )
692
		return;
693
694
	$awaiting_mod = wp_count_comments();
695
	$awaiting_mod = $awaiting_mod->moderated;
696
	$awaiting_text = sprintf( _n( '%s comment awaiting moderation', '%s comments awaiting moderation', $awaiting_mod ), number_format_i18n( $awaiting_mod ) );
697
698
	$icon  = '<span class="ab-icon"></span>';
699
	$title = '<span id="ab-awaiting-mod" class="ab-label awaiting-mod pending-count count-' . $awaiting_mod . '" aria-hidden="true">' . number_format_i18n( $awaiting_mod ) . '</span>';
700
	$title .= '<span class="screen-reader-text">' . $awaiting_text . '</span>';
701
702
	$wp_admin_bar->add_menu( array(
703
		'id'    => 'comments',
704
		'title' => $icon . $title,
705
		'href'  => admin_url('edit-comments.php'),
706
	) );
707
}
708
709
/**
710
 * Add appearance submenu items to the "Site Name" menu.
711
 *
712
 * @since 3.1.0
713
 *
714
 * @param WP_Admin_Bar $wp_admin_bar
715
 */
716
function wp_admin_bar_appearance_menu( $wp_admin_bar ) {
717
	$wp_admin_bar->add_group( array( 'parent' => 'site-name', 'id' => 'appearance' ) );
718
719 View Code Duplication
	if ( current_user_can( 'switch_themes' ) ) {
720
		$wp_admin_bar->add_menu( array(
721
			'parent' => 'appearance',
722
			'id'     => 'themes',
723
			'title'  => __( 'Themes' ),
724
			'href'   => admin_url( 'themes.php' ),
725
		) );
726
	}
727
728
	if ( ! current_user_can( 'edit_theme_options' ) ) {
729
		return;
730
	}
731
732 View Code Duplication
	if ( current_theme_supports( 'widgets' )  ) {
733
		$wp_admin_bar->add_menu( array(
734
			'parent' => 'appearance',
735
			'id'     => 'widgets',
736
			'title'  => __( 'Widgets' ),
737
			'href'   => admin_url( 'widgets.php' ),
738
		) );
739
	}
740
741 View Code Duplication
	if ( current_theme_supports( 'menus' ) || current_theme_supports( 'widgets' ) )
742
		$wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'menus', 'title' => __('Menus'), 'href' => admin_url('nav-menus.php') ) );
743
744 View Code Duplication
	if ( current_theme_supports( 'custom-background' ) ) {
745
		$wp_admin_bar->add_menu( array(
746
			'parent' => 'appearance',
747
			'id'     => 'background',
748
			'title'  => __( 'Background' ),
749
			'href'   => admin_url( 'themes.php?page=custom-background' ),
750
			'meta'   => array(
751
				'class' => 'hide-if-customize',
752
			),
753
		) );
754
	}
755
756 View Code Duplication
	if ( current_theme_supports( 'custom-header' ) ) {
757
		$wp_admin_bar->add_menu( array(
758
			'parent' => 'appearance',
759
			'id'     => 'header',
760
			'title'  => __( 'Header' ),
761
			'href'   => admin_url( 'themes.php?page=custom-header' ),
762
			'meta'   => array(
763
				'class' => 'hide-if-customize',
764
			),
765
		) );
766
	}
767
768
}
769
770
/**
771
 * Provide an update link if theme/plugin/core updates are available.
772
 *
773
 * @since 3.1.0
774
 *
775
 * @param WP_Admin_Bar $wp_admin_bar
776
 */
777
function wp_admin_bar_updates_menu( $wp_admin_bar ) {
778
779
	$update_data = wp_get_update_data();
780
781
	if ( !$update_data['counts']['total'] )
782
		return;
783
784
	$title = '<span class="ab-icon"></span><span class="ab-label">' . number_format_i18n( $update_data['counts']['total'] ) . '</span>';
785
	$title .= '<span class="screen-reader-text">' . $update_data['title'] . '</span>';
786
787
	$wp_admin_bar->add_menu( array(
788
		'id'    => 'updates',
789
		'title' => $title,
790
		'href'  => network_admin_url( 'update-core.php' ),
791
		'meta'  => array(
792
			'title' => $update_data['title'],
793
		),
794
	) );
795
}
796
797
/**
798
 * Add search form.
799
 *
800
 * @since 3.3.0
801
 *
802
 * @param WP_Admin_Bar $wp_admin_bar
803
 */
804
function wp_admin_bar_search_menu( $wp_admin_bar ) {
805
	if ( is_admin() )
806
		return;
807
808
	$form  = '<form action="' . esc_url( home_url( '/' ) ) . '" method="get" id="adminbarsearch">';
809
	$form .= '<input class="adminbar-input" name="s" id="adminbar-search" type="text" value="" maxlength="150" />';
810
	$form .= '<label for="adminbar-search" class="screen-reader-text">' . __( 'Search' ) . '</label>';
811
	$form .= '<input type="submit" class="adminbar-button" value="' . __('Search') . '"/>';
812
	$form .= '</form>';
813
814
	$wp_admin_bar->add_menu( array(
815
		'parent' => 'top-secondary',
816
		'id'     => 'search',
817
		'title'  => $form,
818
		'meta'   => array(
819
			'class'    => 'admin-bar-search',
820
			'tabindex' => -1,
821
		)
822
	) );
823
}
824
825
/**
826
 * Add secondary menus.
827
 *
828
 * @since 3.3.0
829
 *
830
 * @param WP_Admin_Bar $wp_admin_bar
831
 */
832
function wp_admin_bar_add_secondary_groups( $wp_admin_bar ) {
833
	$wp_admin_bar->add_group( array(
834
		'id'     => 'top-secondary',
835
		'meta'   => array(
836
			'class' => 'ab-top-secondary',
837
		),
838
	) );
839
840
	$wp_admin_bar->add_group( array(
841
		'parent' => 'wp-logo',
842
		'id'     => 'wp-logo-external',
843
		'meta'   => array(
844
			'class' => 'ab-sub-secondary',
845
		),
846
	) );
847
}
848
849
/**
850
 * Style and scripts for the admin bar.
851
 *
852
 * @since 3.1.0
853
 */
854
function wp_admin_bar_header() { ?>
855
<style type="text/css" media="print">#wpadminbar { display:none; }</style>
856
<?php
857
}
858
859
/**
860
 * Default admin bar callback.
861
 *
862
 * @since 3.1.0
863
 */
864
function _admin_bar_bump_cb() { ?>
865
<style type="text/css" media="screen">
866
	html { margin-top: 32px !important; }
867
	* html body { margin-top: 32px !important; }
868
	@media screen and ( max-width: 782px ) {
869
		html { margin-top: 46px !important; }
870
		* html body { margin-top: 46px !important; }
871
	}
872
</style>
873
<?php
874
}
875
876
/**
877
 * Sets the display status of the admin bar.
878
 *
879
 * This can be called immediately upon plugin load. It does not need to be called
880
 * from a function hooked to the {@see 'init'} action.
881
 *
882
 * @since 3.1.0
883
 *
884
 * @global bool $show_admin_bar
885
 *
886
 * @param bool $show Whether to allow the admin bar to show.
887
 */
888
function show_admin_bar( $show ) {
889
	global $show_admin_bar;
890
	$show_admin_bar = (bool) $show;
891
}
892
893
/**
894
 * Determine whether the admin bar should be showing.
895
 *
896
 * @since 3.1.0
897
 *
898
 * @global bool   $show_admin_bar
899
 * @global string $pagenow
900
 *
901
 * @return bool Whether the admin bar should be showing.
902
 */
903
function is_admin_bar_showing() {
904
	global $show_admin_bar, $pagenow;
905
906
	// For all these types of requests, we never want an admin bar.
907
	if ( defined('XMLRPC_REQUEST') || defined('DOING_AJAX') || defined('IFRAME_REQUEST') )
908
		return false;
909
910
	if ( is_embed() ) {
911
		return false;
912
	}
913
914
	// Integrated into the admin.
915
	if ( is_admin() )
916
		return true;
917
918
	if ( ! isset( $show_admin_bar ) ) {
919
		if ( ! is_user_logged_in() || 'wp-login.php' == $pagenow ) {
920
			$show_admin_bar = false;
921
		} else {
922
			$show_admin_bar = _get_admin_bar_pref();
923
		}
924
	}
925
926
	/**
927
	 * Filters whether to show the admin bar.
928
	 *
929
	 * Returning false to this hook is the recommended way to hide the admin bar.
930
	 * The user's display preference is used for logged in users.
931
	 *
932
	 * @since 3.1.0
933
	 *
934
	 * @param bool $show_admin_bar Whether the admin bar should be shown. Default false.
935
	 */
936
	$show_admin_bar = apply_filters( 'show_admin_bar', $show_admin_bar );
937
938
	return $show_admin_bar;
939
}
940
941
/**
942
 * Retrieve the admin bar display preference of a user.
943
 *
944
 * @since 3.1.0
945
 * @access private
946
 *
947
 * @param string $context Context of this preference check. Defaults to 'front'. The 'admin'
948
 * 	preference is no longer used.
949
 * @param int $user Optional. ID of the user to check, defaults to 0 for current user.
950
 * @return bool Whether the admin bar should be showing for this user.
951
 */
952
function _get_admin_bar_pref( $context = 'front', $user = 0 ) {
953
	$pref = get_user_option( "show_admin_bar_{$context}", $user );
954
	if ( false === $pref )
955
		return true;
956
957
	return 'true' === $pref;
958
}
959