home *** CD-ROM | disk | FTP | other *** search
/ HTML Examples / WP.iso / wordpress / wp-includes / admin-bar.php next >
Encoding:
PHP Script  |  2017-10-09  |  28.0 KB  |  1,053 lines

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