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