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


Generated: Tue Apr 7 01:00:07 2020 Cross-referenced by PHPXref 0.7.1