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


Generated: Wed Oct 23 08:20:01 2019 Cross-referenced by PHPXref 0.7