[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

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


Generated: Mon Jun 17 08:20:02 2019 Cross-referenced by PHPXref 0.7