[ 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  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       * Load 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 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   * Add the WordPress logo menu.
 118   *
 119   * @since 3.3.0
 120   *
 121   * @param WP_Admin_Bar $wp_admin_bar
 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"></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 Codex link.
 170      $wp_admin_bar->add_node(
 171          array(
 172              'parent' => 'wp-logo-external',
 173              'id'     => 'documentation',
 174              'title'  => __( 'Documentation' ),
 175              'href'   => __( 'https://codex.wordpress.org/' ),
 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/' ),
 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   * Add the sidebar toggle button.
 202   *
 203   * @since 3.8.0
 204   *
 205   * @param WP_Admin_Bar $wp_admin_bar
 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"></span><span class="screen-reader-text">' . __( 'Menu' ) . '</span>',
 213                  'href'  => '#',
 214              )
 215          );
 216      }
 217  }
 218  
 219  /**
 220   * Add the "My Account" item.
 221   *
 222   * @since 3.3.0
 223   *
 224   * @param WP_Admin_Bar $wp_admin_bar
 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   * Add the "My Account" submenu items.
 262   *
 263   * @since 3.1.0
 264   *
 265   * @param WP_Admin_Bar $wp_admin_bar
 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   * Add the "Site Name" menu.
 332   *
 333   * @since 3.3.0
 334   *
 335   * @param WP_Admin_Bar $wp_admin_bar
 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 "Customize" link to the Toolbar.
 413   *
 414   * @since 4.3.0
 415   *
 416   * @param WP_Admin_Bar $wp_admin_bar WP_Admin_Bar instance.
 417   * @global WP_Customize_Manager $wp_customize
 418   */
 419  function wp_admin_bar_customize_menu( $wp_admin_bar ) {
 420      global $wp_customize;
 421  
 422      // Don't show for users who can't access the customizer or when in the admin.
 423      if ( ! current_user_can( 'customize' ) || is_admin() ) {
 424          return;
 425      }
 426  
 427      // Don't show if the user cannot edit a given customize_changeset post currently being previewed.
 428      if ( is_customize_preview() && $wp_customize->changeset_post_id()
 429          && ! current_user_can( get_post_type_object( 'customize_changeset' )->cap->edit_post, $wp_customize->changeset_post_id() )
 430      ) {
 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   * @global int      $post_id      The ID of the post when editing comments for a single post.
 696   *
 697   * @param WP_Admin_Bar $wp_admin_bar
 698   */
 699  function wp_admin_bar_edit_menu( $wp_admin_bar ) {
 700      global $tag, $wp_the_query, $user_id, $post_id;
 701  
 702      if ( is_admin() ) {
 703          $current_screen   = get_current_screen();
 704          $post             = get_post();
 705          $post_type_object = null;
 706  
 707          if ( 'post' === $current_screen->base ) {
 708              $post_type_object = get_post_type_object( $post->post_type );
 709          } elseif ( 'edit' === $current_screen->base ) {
 710              $post_type_object = get_post_type_object( $current_screen->post_type );
 711          } elseif ( 'edit-comments' === $current_screen->base && $post_id ) {
 712              $post = get_post( $post_id );
 713              if ( $post ) {
 714                  $post_type_object = get_post_type_object( $post->post_type );
 715              }
 716          }
 717  
 718          if ( ( 'post' === $current_screen->base || 'edit-comments' === $current_screen->base )
 719              && 'add' !== $current_screen->action
 720              && ( $post_type_object )
 721              && current_user_can( 'read_post', $post->ID )
 722              && ( $post_type_object->public )
 723              && ( $post_type_object->show_in_admin_bar ) ) {
 724              if ( 'draft' === $post->post_status ) {
 725                  $preview_link = get_preview_post_link( $post );
 726                  $wp_admin_bar->add_node(
 727                      array(
 728                          'id'    => 'preview',
 729                          'title' => $post_type_object->labels->view_item,
 730                          'href'  => esc_url( $preview_link ),
 731                          'meta'  => array( 'target' => 'wp-preview-' . $post->ID ),
 732                      )
 733                  );
 734              } else {
 735                  $wp_admin_bar->add_node(
 736                      array(
 737                          'id'    => 'view',
 738                          'title' => $post_type_object->labels->view_item,
 739                          'href'  => get_permalink( $post->ID ),
 740                      )
 741                  );
 742              }
 743          } elseif ( 'edit' === $current_screen->base
 744              && ( $post_type_object )
 745              && ( $post_type_object->public )
 746              && ( $post_type_object->show_in_admin_bar )
 747              && ( get_post_type_archive_link( $post_type_object->name ) )
 748              && ! ( 'post' === $post_type_object->name && 'posts' === get_option( 'show_on_front' ) ) ) {
 749              $wp_admin_bar->add_node(
 750                  array(
 751                      'id'    => 'archive',
 752                      'title' => $post_type_object->labels->view_items,
 753                      'href'  => get_post_type_archive_link( $current_screen->post_type ),
 754                  )
 755              );
 756          } elseif ( 'term' === $current_screen->base && isset( $tag ) && is_object( $tag ) && ! is_wp_error( $tag ) ) {
 757              $tax = get_taxonomy( $tag->taxonomy );
 758              if ( is_taxonomy_viewable( $tax ) ) {
 759                  $wp_admin_bar->add_node(
 760                      array(
 761                          'id'    => 'view',
 762                          'title' => $tax->labels->view_item,
 763                          'href'  => get_term_link( $tag ),
 764                      )
 765                  );
 766              }
 767          } elseif ( 'user-edit' === $current_screen->base && isset( $user_id ) ) {
 768              $user_object = get_userdata( $user_id );
 769              $view_link   = get_author_posts_url( $user_object->ID );
 770              if ( $user_object->exists() && $view_link ) {
 771                  $wp_admin_bar->add_node(
 772                      array(
 773                          'id'    => 'view',
 774                          'title' => __( 'View User' ),
 775                          'href'  => $view_link,
 776                      )
 777                  );
 778              }
 779          }
 780      } else {
 781          $current_object = $wp_the_query->get_queried_object();
 782  
 783          if ( empty( $current_object ) ) {
 784              return;
 785          }
 786  
 787          if ( ! empty( $current_object->post_type ) ) {
 788              $post_type_object = get_post_type_object( $current_object->post_type );
 789              $edit_post_link   = get_edit_post_link( $current_object->ID );
 790              if ( $post_type_object
 791                  && $edit_post_link
 792                  && current_user_can( 'edit_post', $current_object->ID )
 793                  && $post_type_object->show_in_admin_bar ) {
 794                  $wp_admin_bar->add_node(
 795                      array(
 796                          'id'    => 'edit',
 797                          'title' => $post_type_object->labels->edit_item,
 798                          'href'  => $edit_post_link,
 799                      )
 800                  );
 801              }
 802          } elseif ( ! empty( $current_object->taxonomy ) ) {
 803              $tax            = get_taxonomy( $current_object->taxonomy );
 804              $edit_term_link = get_edit_term_link( $current_object->term_id, $current_object->taxonomy );
 805              if ( $tax && $edit_term_link && current_user_can( 'edit_term', $current_object->term_id ) ) {
 806                  $wp_admin_bar->add_node(
 807                      array(
 808                          'id'    => 'edit',
 809                          'title' => $tax->labels->edit_item,
 810                          'href'  => $edit_term_link,
 811                      )
 812                  );
 813              }
 814          } elseif ( is_a( $current_object, 'WP_User' ) && current_user_can( 'edit_user', $current_object->ID ) ) {
 815              $edit_user_link = get_edit_user_link( $current_object->ID );
 816              if ( $edit_user_link ) {
 817                  $wp_admin_bar->add_node(
 818                      array(
 819                          'id'    => 'edit',
 820                          'title' => __( 'Edit User' ),
 821                          'href'  => $edit_user_link,
 822                      )
 823                  );
 824              }
 825          }
 826      }
 827  }
 828  
 829  /**
 830   * Add "Add New" menu.
 831   *
 832   * @since 3.1.0
 833   *
 834   * @param WP_Admin_Bar $wp_admin_bar
 835   */
 836  function wp_admin_bar_new_content_menu( $wp_admin_bar ) {
 837      $actions = array();
 838  
 839      $cpts = (array) get_post_types( array( 'show_in_admin_bar' => true ), 'objects' );
 840  
 841      if ( isset( $cpts['post'] ) && current_user_can( $cpts['post']->cap->create_posts ) ) {
 842          $actions['post-new.php'] = array( $cpts['post']->labels->name_admin_bar, 'new-post' );
 843      }
 844  
 845      if ( isset( $cpts['attachment'] ) && current_user_can( 'upload_files' ) ) {
 846          $actions['media-new.php'] = array( $cpts['attachment']->labels->name_admin_bar, 'new-media' );
 847      }
 848  
 849      if ( current_user_can( 'manage_links' ) ) {
 850          $actions['link-add.php'] = array( _x( 'Link', 'add new from admin bar' ), 'new-link' );
 851      }
 852  
 853      if ( isset( $cpts['page'] ) && current_user_can( $cpts['page']->cap->create_posts ) ) {
 854          $actions['post-new.php?post_type=page'] = array( $cpts['page']->labels->name_admin_bar, 'new-page' );
 855      }
 856  
 857      unset( $cpts['post'], $cpts['page'], $cpts['attachment'] );
 858  
 859      // Add any additional custom post types.
 860      foreach ( $cpts as $cpt ) {
 861          if ( ! current_user_can( $cpt->cap->create_posts ) ) {
 862              continue;
 863          }
 864  
 865          $key             = 'post-new.php?post_type=' . $cpt->name;
 866          $actions[ $key ] = array( $cpt->labels->name_admin_bar, 'new-' . $cpt->name );
 867      }
 868      // Avoid clash with parent node and a 'content' post type.
 869      if ( isset( $actions['post-new.php?post_type=content'] ) ) {
 870          $actions['post-new.php?post_type=content'][1] = 'add-new-content';
 871      }
 872  
 873      if ( current_user_can( 'create_users' ) || ( is_multisite() && current_user_can( 'promote_users' ) ) ) {
 874          $actions['user-new.php'] = array( _x( 'User', 'add new from admin bar' ), 'new-user' );
 875      }
 876  
 877      if ( ! $actions ) {
 878          return;
 879      }
 880  
 881      $title = '<span class="ab-icon"></span><span class="ab-label">' . _x( 'New', 'admin bar menu group label' ) . '</span>';
 882  
 883      $wp_admin_bar->add_node(
 884          array(
 885              'id'    => 'new-content',
 886              'title' => $title,
 887              'href'  => admin_url( current( array_keys( $actions ) ) ),
 888          )
 889      );
 890  
 891      foreach ( $actions as $link => $action ) {
 892          list( $title, $id ) = $action;
 893  
 894          $wp_admin_bar->add_node(
 895              array(
 896                  'parent' => 'new-content',
 897                  'id'     => $id,
 898                  'title'  => $title,
 899                  'href'   => admin_url( $link ),
 900              )
 901          );
 902      }
 903  }
 904  
 905  /**
 906   * Add edit comments link with awaiting moderation count bubble.
 907   *
 908   * @since 3.1.0
 909   *
 910   * @param WP_Admin_Bar $wp_admin_bar
 911   */
 912  function wp_admin_bar_comments_menu( $wp_admin_bar ) {
 913      if ( ! current_user_can( 'edit_posts' ) ) {
 914          return;
 915      }
 916  
 917      $awaiting_mod  = wp_count_comments();
 918      $awaiting_mod  = $awaiting_mod->moderated;
 919      $awaiting_text = sprintf(
 920          /* translators: %s: Number of comments. */
 921          _n( '%s Comment in moderation', '%s Comments in moderation', $awaiting_mod ),
 922          number_format_i18n( $awaiting_mod )
 923      );
 924  
 925      $icon   = '<span class="ab-icon"></span>';
 926      $title  = '<span class="ab-label awaiting-mod pending-count count-' . $awaiting_mod . '" aria-hidden="true">' . number_format_i18n( $awaiting_mod ) . '</span>';
 927      $title .= '<span class="screen-reader-text comments-in-moderation-text">' . $awaiting_text . '</span>';
 928  
 929      $wp_admin_bar->add_node(
 930          array(
 931              'id'    => 'comments',
 932              'title' => $icon . $title,
 933              'href'  => admin_url( 'edit-comments.php' ),
 934          )
 935      );
 936  }
 937  
 938  /**
 939   * Add appearance submenu items to the "Site Name" menu.
 940   *
 941   * @since 3.1.0
 942   *
 943   * @param WP_Admin_Bar $wp_admin_bar
 944   */
 945  function wp_admin_bar_appearance_menu( $wp_admin_bar ) {
 946      $wp_admin_bar->add_group(
 947          array(
 948              'parent' => 'site-name',
 949              'id'     => 'appearance',
 950          )
 951      );
 952  
 953      if ( current_user_can( 'switch_themes' ) ) {
 954          $wp_admin_bar->add_node(
 955              array(
 956                  'parent' => 'appearance',
 957                  'id'     => 'themes',
 958                  'title'  => __( 'Themes' ),
 959                  'href'   => admin_url( 'themes.php' ),
 960              )
 961          );
 962      }
 963  
 964      if ( ! current_user_can( 'edit_theme_options' ) ) {
 965          return;
 966      }
 967  
 968      if ( current_theme_supports( 'widgets' ) ) {
 969          $wp_admin_bar->add_node(
 970              array(
 971                  'parent' => 'appearance',
 972                  'id'     => 'widgets',
 973                  'title'  => __( 'Widgets' ),
 974                  'href'   => admin_url( 'widgets.php' ),
 975              )
 976          );
 977      }
 978  
 979      if ( current_theme_supports( 'menus' ) || current_theme_supports( 'widgets' ) ) {
 980          $wp_admin_bar->add_node(
 981              array(
 982                  'parent' => 'appearance',
 983                  'id'     => 'menus',
 984                  'title'  => __( 'Menus' ),
 985                  'href'   => admin_url( 'nav-menus.php' ),
 986              )
 987          );
 988      }
 989  
 990      if ( current_theme_supports( 'custom-background' ) ) {
 991          $wp_admin_bar->add_node(
 992              array(
 993                  'parent' => 'appearance',
 994                  'id'     => 'background',
 995                  'title'  => __( 'Background' ),
 996                  'href'   => admin_url( 'themes.php?page=custom-background' ),
 997                  'meta'   => array(
 998                      'class' => 'hide-if-customize',
 999                  ),
1000              )
1001          );
1002      }
1003  
1004      if ( current_theme_supports( 'custom-header' ) ) {
1005          $wp_admin_bar->add_node(
1006              array(
1007                  'parent' => 'appearance',
1008                  'id'     => 'header',
1009                  'title'  => __( 'Header' ),
1010                  'href'   => admin_url( 'themes.php?page=custom-header' ),
1011                  'meta'   => array(
1012                      'class' => 'hide-if-customize',
1013                  ),
1014              )
1015          );
1016      }
1017  
1018  }
1019  
1020  /**
1021   * Provide an update link if theme/plugin/core updates are available.
1022   *
1023   * @since 3.1.0
1024   *
1025   * @param WP_Admin_Bar $wp_admin_bar
1026   */
1027  function wp_admin_bar_updates_menu( $wp_admin_bar ) {
1028  
1029      $update_data = wp_get_update_data();
1030  
1031      if ( ! $update_data['counts']['total'] ) {
1032          return;
1033      }
1034  
1035      $title  = '<span class="ab-icon"></span><span class="ab-label">' . number_format_i18n( $update_data['counts']['total'] ) . '</span>';
1036      $title .= '<span class="screen-reader-text">' . $update_data['title'] . '</span>';
1037  
1038      $wp_admin_bar->add_node(
1039          array(
1040              'id'    => 'updates',
1041              'title' => $title,
1042              'href'  => network_admin_url( 'update-core.php' ),
1043              'meta'  => array(
1044                  'title' => $update_data['title'],
1045              ),
1046          )
1047      );
1048  }
1049  
1050  /**
1051   * Add search form.
1052   *
1053   * @since 3.3.0
1054   *
1055   * @param WP_Admin_Bar $wp_admin_bar
1056   */
1057  function wp_admin_bar_search_menu( $wp_admin_bar ) {
1058      if ( is_admin() ) {
1059          return;
1060      }
1061  
1062      $form  = '<form action="' . esc_url( home_url( '/' ) ) . '" method="get" id="adminbarsearch">';
1063      $form .= '<input class="adminbar-input" name="s" id="adminbar-search" type="text" value="" maxlength="150" />';
1064      $form .= '<label for="adminbar-search" class="screen-reader-text">' . __( 'Search' ) . '</label>';
1065      $form .= '<input type="submit" class="adminbar-button" value="' . __( 'Search' ) . '"/>';
1066      $form .= '</form>';
1067  
1068      $wp_admin_bar->add_node(
1069          array(
1070              'parent' => 'top-secondary',
1071              'id'     => 'search',
1072              'title'  => $form,
1073              'meta'   => array(
1074                  'class'    => 'admin-bar-search',
1075                  'tabindex' => -1,
1076              ),
1077          )
1078      );
1079  }
1080  
1081  /**
1082   * Add a link to exit recovery mode when Recovery Mode is active.
1083   *
1084   * @since 5.2.0
1085   *
1086   * @param WP_Admin_Bar $wp_admin_bar
1087   */
1088  function wp_admin_bar_recovery_mode_menu( $wp_admin_bar ) {
1089      if ( ! wp_is_recovery_mode() ) {
1090          return;
1091      }
1092  
1093      $url = wp_login_url();
1094      $url = add_query_arg( 'action', WP_Recovery_Mode::EXIT_ACTION, $url );
1095      $url = wp_nonce_url( $url, WP_Recovery_Mode::EXIT_ACTION );
1096  
1097      $wp_admin_bar->add_node(
1098          array(
1099              'parent' => 'top-secondary',
1100              'id'     => 'recovery-mode',
1101              'title'  => __( 'Exit Recovery Mode' ),
1102              'href'   => $url,
1103          )
1104      );
1105  }
1106  
1107  /**
1108   * Add secondary menus.
1109   *
1110   * @since 3.3.0
1111   *
1112   * @param WP_Admin_Bar $wp_admin_bar
1113   */
1114  function wp_admin_bar_add_secondary_groups( $wp_admin_bar ) {
1115      $wp_admin_bar->add_group(
1116          array(
1117              'id'   => 'top-secondary',
1118              'meta' => array(
1119                  'class' => 'ab-top-secondary',
1120              ),
1121          )
1122      );
1123  
1124      $wp_admin_bar->add_group(
1125          array(
1126              'parent' => 'wp-logo',
1127              'id'     => 'wp-logo-external',
1128              'meta'   => array(
1129                  'class' => 'ab-sub-secondary',
1130              ),
1131          )
1132      );
1133  }
1134  
1135  /**
1136   * Style and scripts for the admin bar.
1137   *
1138   * @since 3.1.0
1139   */
1140  function wp_admin_bar_header() {
1141      $type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"';
1142      ?>
1143  <style<?php echo $type_attr; ?> media="print">#wpadminbar { display:none; }</style>
1144      <?php
1145  }
1146  
1147  /**
1148   * Default admin bar callback.
1149   *
1150   * @since 3.1.0
1151   */
1152  function _admin_bar_bump_cb() {
1153      $type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"';
1154      ?>
1155  <style<?php echo $type_attr; ?> media="screen">
1156      html { margin-top: 32px !important; }
1157      * html body { margin-top: 32px !important; }
1158      @media screen and ( max-width: 782px ) {
1159          html { margin-top: 46px !important; }
1160          * html body { margin-top: 46px !important; }
1161      }
1162  </style>
1163      <?php
1164  }
1165  
1166  /**
1167   * Sets the display status of the admin bar.
1168   *
1169   * This can be called immediately upon plugin load. It does not need to be called
1170   * from a function hooked to the {@see 'init'} action.
1171   *
1172   * @since 3.1.0
1173   *
1174   * @global bool $show_admin_bar
1175   *
1176   * @param bool $show Whether to allow the admin bar to show.
1177   */
1178  function show_admin_bar( $show ) {
1179      global $show_admin_bar;
1180      $show_admin_bar = (bool) $show;
1181  }
1182  
1183  /**
1184   * Determines whether the admin bar should be showing.
1185   *
1186   * For more information on this and similar theme functions, check out
1187   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
1188   * Conditional Tags} article in the Theme Developer Handbook.
1189   *
1190   * @since 3.1.0
1191   *
1192   * @global bool   $show_admin_bar
1193   * @global string $pagenow
1194   *
1195   * @return bool Whether the admin bar should be showing.
1196   */
1197  function is_admin_bar_showing() {
1198      global $show_admin_bar, $pagenow;
1199  
1200      // For all these types of requests, we never want an admin bar.
1201      if ( defined( 'XMLRPC_REQUEST' ) || defined( 'DOING_AJAX' ) || defined( 'IFRAME_REQUEST' ) || wp_is_json_request() ) {
1202          return false;
1203      }
1204  
1205      if ( is_embed() ) {
1206          return false;
1207      }
1208  
1209      // Integrated into the admin.
1210      if ( is_admin() ) {
1211          return true;
1212      }
1213  
1214      if ( ! isset( $show_admin_bar ) ) {
1215          if ( ! is_user_logged_in() || 'wp-login.php' === $pagenow ) {
1216              $show_admin_bar = false;
1217          } else {
1218              $show_admin_bar = _get_admin_bar_pref();
1219          }
1220      }
1221  
1222      /**
1223       * Filters whether to show the admin bar.
1224       *
1225       * Returning false to this hook is the recommended way to hide the admin bar.
1226       * The user's display preference is used for logged in users.
1227       *
1228       * @since 3.1.0
1229       *
1230       * @param bool $show_admin_bar Whether the admin bar should be shown. Default false.
1231       */
1232      $show_admin_bar = apply_filters( 'show_admin_bar', $show_admin_bar );
1233  
1234      return $show_admin_bar;
1235  }
1236  
1237  /**
1238   * Retrieve the admin bar display preference of a user.
1239   *
1240   * @since 3.1.0
1241   * @access private
1242   *
1243   * @param string $context Context of this preference check. Defaults to 'front'. The 'admin'
1244   *  preference is no longer used.
1245   * @param int $user Optional. ID of the user to check, defaults to 0 for current user.
1246   * @return bool Whether the admin bar should be showing for this user.
1247   */
1248  function _get_admin_bar_pref( $context = 'front', $user = 0 ) {
1249      $pref = get_user_option( "show_admin_bar_{$context}", $user );
1250      if ( false === $pref ) {
1251          return true;
1252      }
1253  
1254      return 'true' === $pref;
1255  }


Generated: Thu Jul 16 01:00:03 2020 Cross-referenced by PHPXref 0.7.1