> Menus screen. */ function admin_notice_menus() { // Only display the notice if on the Appearance >> Menus screen. if ( \ski\question::is_admin_screen( 'nav-menus' ) ) { echo '
'. '

'. __( 'BluePrint-Q supports a full Bootstrap two-level menu system in the main Navigation area. It '. 'also supports single-level menus within any other widgetized areas. To assign these menus, '. 'please use the Theme Customizer.', 'bpq' ). '

'. '
'; } } /** * Responsible for altering menus in the administration area. */ function admin_menus() { // Add a link to the BluePrint-Q landing page. add_theme_page( __( 'Welcome to BluePrint-Q', 'bpq' ), __( 'BluePrint-Q', 'bpq' ), 'edit_theme_options', 'bpq', 'bpq\admin_display_landing' ); } /** * Acts as a clearfix for cases where the traditional CSS solution does not * necessarily work. The driving case for this is when the navbar is hooked * to the end of the floater area or main zone. */ function force_clear() { echo '
'; } /** * In navigation menus, typically, a theme would differentiate the current * page item from all other items in the menu - to do this, we add a special * class to the
  • element desired by using this filter. * * Note: Do not highlight if the link includes an anchor - only solo URLs. * * @link http://codex.wordpress.org/Function_Reference/wp_nav_menu * @param type $classes The current list of classes already attached to this item * @param type $item The nav item being analyzed * @return The new list of classes to be attached to this item */ function highlight_current_nav_item( $classes, $item ) { if ( in_array( 'current-menu-item', $classes ) && empty( $item->anchor ) ) { $classes[] = 'active'; } return $classes; } /** * To add custom fields to the menu elements, the special Walker for * editing menus must be changed. There are really no usable hooks * in the default Walker to add fields, but there is a filter to * override the Walker that is used. * * @return string The name of the walker that will construct the element markup */ function menu_edit_walker() { return 'bpq\walker_nav_edit'; } /** * An $item object is typically passed into the Walker that displays the * menu on the frontend. That $item object is useful for analysis. When * extending the custom fields in the backend Appearance >> Menu screen, * the $item object should be extended as well. * * @param type $menu_item The instance of the menu item * @return type The new item instance */ function menu_edit_setup( $menu_item ) { $menu_item->anchor = get_post_meta( $menu_item->ID, '_menu_item_anchor', true ); $menu_item->divider = get_post_meta( $menu_item->ID, '_menu_item_divider', true ); $menu_item->header = get_post_meta( $menu_item->ID, '_menu_item_header', true ); $menu_item->icon = get_post_meta( $menu_item->ID, '_menu_item_icon', true ); return $menu_item; } /** * In the Appearance >> Menu administration screen, when the user clicks * "Save Menu", this function is responsible for saving the updates of * new fields added to the Walker. * * @param type $menu_id The unique ID assigned to the active menu * @param type $menu_item_db_id The unqiue ID assigned to the active menu item * @param type $args Context */ function menu_edit_update( $menu_id, $menu_item_db_id, $args ) { if ( isset( $_REQUEST['menu-item-anchor'][$menu_item_db_id] ) ) { update_post_meta( $menu_item_db_id, '_menu_item_anchor', $_REQUEST['menu-item-anchor'][$menu_item_db_id] ); } update_post_meta( $menu_item_db_id, '_menu_item_divider', ( isset( $_REQUEST['menu-item-divider'][$menu_item_db_id] ) ) ); update_post_meta( $menu_item_db_id, '_menu_item_header', ( isset( $_REQUEST['menu-item-header'][$menu_item_db_id] ) ) ); if ( isset( $_REQUEST['menu-item-icon'][$menu_item_db_id] ) ) { update_post_meta( $menu_item_db_id, '_menu_item_icon', $_REQUEST['menu-item-icon'][$menu_item_db_id] ); } } /** * Registers theme locations for menus. */ function register_menu_theme_locations() { register_nav_menu( 'navbar', 'Main Navigation Bar' ); } /** * Generates the markup for the main navigation area. This assumes * Bootstrap 3's navbar capabilities, which only supports a depth * of 2 - that is, 1 parent and 1 child only. Any menu items that * are grandchildren or younger will have unexpected results. * * Note: This class was originally copied from Walker_Nav_Menu, * then modified to accomodate Bootstrap classes. This * was done to ensure that the same WP hooks are in-place. * * Note: Bootstrap can only support a single parent/child relation. * There are no grandchildren support as of Bootstrap 3.0 - * apparently, there is a thread where Mark Otto states * something along the lines of: "grandchildren not being * mobile-friendly". Therefore, at the top of each member * function, you will see a chack to ensure that we do not * process any items that are at the grandchild level or lower. * * Example Bootstrap 3 navbar markup: * * * @link http://getbootstrap.com/components/#navbar * @uses \Walker_Nav_Menu */ class walker_nav extends \Walker_Nav_Menu { /** * Starts the list before the elements are added. * * @see Walker::start_lvl() * * @since 3.0.0 * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu() */ function start_lvl( &$output, $depth = 0, $args = array() ) { if ( $depth >= 2 ) return; $output .= ''; } /** * Start the element output. * * @see Walker::start_el() * * @since 3.0.0 * * @param string $output Passed by reference. Used to append additional content. * @param object $item Menu item data object. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu() * @param int $id Current item ID. */ function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) { if ( $depth >= 2 ) return; // // AREA: Figure the classes on the
  • . // // An array to store the classes that go on the
  • . $classes = empty( $item->classes ) ? array() : (array)$item->classes; $classes[] = 'menu-item-'.$item->ID; // Parent elements must have the 'dropdown' class applied for Bootstrap. if ( $item->has_children && !$item->header ) $classes[] = 'dropdown'; // Descriptions can normally run along the entire width of the navbar; // applying the 'desc' class will wrap the text. if ( ( $depth >= 1 ) && ( !empty( $item->description ) ) ) $classes[] = 'desc'; // Some menu items can be marked as headers, which get such an effect: // http://getbootstrap.com/components/#dropdowns-headers if ( $item->header ) $classes[] = 'dropdown-header'; /** * Filter the CSS class(es) applied to a menu item's
  • . * * @since 3.0.0 * * @param array $classes The CSS classes that are applied to the menu item's
  • . * @param object $item The current menu item. * @param array $args An array of arguments. @see wp_nav_menu() */ $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) ); $class_names = $class_names ? 'class="'.esc_attr( $class_names ).'"' : ''; // // AREA: Figure the id on the
  • . // /** * Filter the ID applied to a menu item's
  • . * * @since 3.0.1 * * @param string The ID that is applied to the menu item's
  • . * @param object $item The current menu item. * @param array $args An array of arguments. @see wp_nav_menu() */ $id = apply_filters( 'nav_menu_item_id', 'menu-item-'.$item->ID, $item, $args ); $id = $id ? 'id="'.esc_attr( $id ).'"' : ''; $output_li = '
  • '; // // AREA: Figure the attributes on the that will be a direct child of the
  • . // $attrs_a = array(); $attrs_a['title'] = empty( $item->attr_title ) ? '' : $item->attr_title; $attrs_a['target'] = empty( $item->target ) ? '' : $item->target; $attrs_a['rel'] = empty( $item->xfn ) ? '' : $item->xfn; $attrs_a['href'] = empty( $item->url ) ? '' : $item->url; // Parent elements must have class and data-toggle set for Bootstrap. if ( ( $item->has_children ) && ( $args->depth > 1 ) ) { $attrs_a['class'] = 'dropdown-toggle'; $attrs_a['data-toggle'] = 'dropdown'; } /** * Filter the HTML attributes applied to a menu item's . * * @since 3.6.0 * * @param array $attrs_a { * The HTML attributes applied to the menu item's , empty strings are ignored. * * @type string $title The title attribute. * @type string $target The target attribute. * @type string $rel The rel attribute. * @type string $href The href attribute. * } * @param object $item The current menu item. * @param array $args An array of arguments. @see wp_nav_menu() */ $attrs_a = apply_filters( 'nav_menu_link_attributes', $attrs_a, $item, $args ); $attributes = ''; foreach ( $attrs_a as $key => $value ) { if ( !empty( $value ) ) { // If an achor was specified, append it to the end of the href value - // the admin may have prepended the # or maybe not, cover both cases. if ( ( 'href' == $key ) && ( !empty( $item->anchor ) ) ) { if ( !\ski\string::starts_with( $item->anchor, '#' ) ) $value .= '#'; $value .= $item->anchor; } $value = ( 'href' === $key ) ? esc_url( $value ) : esc_attr( $value ); $attributes .= ' '.$key.'="'.$value.'"'; } } // Headers do not get the link decorations. $output_a = ''; if ( $item->header ) { $output_a .= ( !empty( $item->icon ) ) ? $item->icon : ''; $output_a .= apply_filters( 'the_title', $item->title, $item->ID ); $output_a .= ( ( $item->has_children ) && ( $depth == 0 ) && ( $args->depth > 1 ) ) ? '' : ''; $output_a .= ( ( $depth >= 1 ) && ( !empty( $item->description ) ) ) ? '
    '.$item->description.'' : ''; } // But regular menu items do get the link decorations. else { $output_a .= $args->before; $output_a .= '
    '; /** This filter is documented in wp-includes/post-template.php */ $output_a .= $args->link_before; $output_a .= ( !empty( $item->icon ) ) ? $item->icon : ''; $output_a .= apply_filters( 'the_title', $item->title, $item->ID ); $output_a .= ( ( $item->has_children ) && ( $depth == 0 ) && ( $args->depth > 1 ) ) ? '' : ''; $output_a .= ( ( $depth >= 1 ) && ( !empty( $item->description ) ) ) ? '
    '.$item->description.'' : ''; $output_a .= $args->link_after; $output_a .= '
    '; $output_a .= $args->after; } /** * Filter a menu item's starting output. * * The menu item's starting output only includes $args->before, the opening , * the menu item's title, the closing , and $args->after. Currently, there is * no filter for modifying the opening and closing
  • for a menu item. * * @since 3.0.0 * * @param string $output_a The menu item's starting HTML output. * @param object $item Menu item data object. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu() */ $output .= $output_li.apply_filters( 'walker_nav_menu_start_el', $output_a, $item, $depth, $args ); } /** * Ends the element output, if needed. * * @see Walker::end_el() * * @since 3.0.0 * * @param string $output Passed by reference. Used to append additional content. * @param object $item Page data object. Not used. * @param int $depth Depth of page. Not Used. * @param array $args An array of arguments. @see wp_nav_menu() */ function end_el( &$output, $item, $depth = 0, $args = array() ) { if ( $depth >= 2 ) return; $output .= '
  • '; // The user can choose to append a divider after the menu item - // if so, then add some extra Bootstrap markup to get the divider. if ( $item->divider ) $output .= '
  • '; } /* Add a 'hasChildren' property to the item * Code from: http://wordpress.org/support/topic/how-do-i-know-if-a-menu-item-has-children-or-is-a-leaf#post-3139633 */ /** * Traverse elements to create list from elements. * * Display one element if the element doesn't have any children otherwise, * display the element and its children. Will only traverse up to the max * depth and no ignore elements under that depth. It is possible to set the * max depth to include all depths, see walk() method. * * This method should not be called directly, use the walk() method instead. * * @since 2.5.0 * * @param object $element Data object. * @param array $children_elements List of elements to continue traversing. * @param int $max_depth Max depth to traverse. * @param int $depth Depth of current element. * @param array $args An array of arguments. * @param string $output Passed by reference. Used to append additional content. * @return null Null on failure with no changes to parameters. */ function display_element ($element, &$children_elements, $max_depth, $depth = 0, $args, &$output) { if ( $depth >= 2 ) return; // Only overrode this function to write a shortcut that indicates // whether the current item is going to be a parent. // @link http://wordpress.org/support/topic/how-do-i-know-if-a-menu-item-has-children-or-is-a-leaf $element->has_children = isset( $children_elements[$element->ID] ) && !empty( $children_elements[$element->ID] ); return parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output ); } } /** * Generates the markup for the backend menu-editing elements. * Specifically, this is the way to add/remove fields like * 'description', 'XFN', etc from each menu item's custom fields. * * Note: This class was originally copied from Walker_Nav_Menu_Edit, * then modified to accomodate the new custom fields. Other * than formatting, code is mostly still the same as the WP * original version. Really, this was done largely because * there is incredibly little documentation and the meat of * this class (start_el) is like one, long, run-on sentence. * By keeping things the same, we can help ensure the same * WP coverage is there. * * Create HTML list of nav menu input items. * * @since 3.0.0 * @uses \Walker_Nav_Menu */ class walker_nav_edit extends \Walker_Nav_Menu { /** * Starts the list before the elements are added. * * @see Walker_Nav_Menu::start_lvl() * * @since 3.0.0 * * @param string $output Passed by reference. * @param int $depth Depth of menu item. Used for padding. * @param array $args Not used. */ function start_lvl( &$output, $depth = 0, $args = array() ) { ; // Intentionally blank. } /** * Ends the list of after the elements are added. * * @see Walker_Nav_Menu::end_lvl() * * @since 3.0.0 * * @param string $output Passed by reference. * @param int $depth Depth of menu item. Used for padding. * @param array $args Not used. */ function end_lvl( &$output, $depth = 0, $args = array() ) { ; // Intentionally blank. } /** * Start the element output. * * @see Walker_Nav_Menu::start_el() * @since 3.0.0 * * @param string $output Passed by reference. Used to append additional content. * @param object $item Menu item data object. * @param int $depth Depth of menu item. Used for padding. * @param array $args Not used. * @param int $id Not used. */ function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) { global $_wp_nav_menu_max_depth; $_wp_nav_menu_max_depth = $depth > $_wp_nav_menu_max_depth ? $depth : $_wp_nav_menu_max_depth; ob_start(); $item_id = esc_attr( $item->ID ); $removed_args = array( 'action', 'customlink-tab', 'edit-menu-item', 'menu-item', 'page-tab', '_wpnonce' ); $original_title = ''; if ( 'taxonomy' == $item->type ) { $original_title = get_term_field( 'name', $item->object_id, $item->object, 'raw' ); if ( is_wp_error( $original_title ) ) $original_title = false; } elseif ( 'post_type' == $item->type ) { $original_object = get_post( $item->object_id ); $original_title = get_the_title( $original_object->ID ); } $classes = array( 'menu-item menu-item-depth-'.$depth, 'menu-item-'.esc_attr( $item->object ), 'menu-item-edit-'.( ( isset( $_GET['edit-menu-item'] ) && $item_id == $_GET['edit-menu-item'] ) ? 'active' : 'inactive' ), ); $title = $item->title; if ( ! empty( $item->_invalid ) ) { $classes[] = 'menu-item-invalid'; /* translators: %s: title of menu item which is invalid */ $title = sprintf( __( '%s (Invalid)', 'bpq' ), $item->title ); } elseif ( isset( $item->post_status ) && ( 'draft' == $item->post_status ) ) { $classes[] = 'pending'; /* translators: %s: title of menu item in draft status */ $title = sprintf( __('%s (Pending)', 'bpq' ), $item->title ); } $title = ( !isset( $item->label ) || ( '' == $item->label ) ) ? $title : $item->label; $submenu_text = ''; if ( 0 == $depth ) $submenu_text = 'style="display: none;"'; $markup = '