[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/ -> admin-bar.php (source)

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


Generated: Sat Jul 2 01:00:03 2022 Cross-referenced by PHPXref 0.7.1