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