Exemplo n.º 1
0
/**
 * Displays a primary navigation bar
 *
 * @return void
 */
function bu_navigation_display_primary($args = '')
{
    $defaults = array('post_types' => array('page'), 'include_links' => true, 'depth' => BU_NAVIGATION_PRIMARY_DEPTH, 'max_items' => BU_NAVIGATION_PRIMARY_MAX, 'dive' => true, 'container_tag' => 'ul', 'container_id' => 'nav', 'container_class' => '', 'item_tag' => 'li', 'identify_top' => false, 'whitelist_top' => null, 'echo' => 1);
    $r = wp_parse_args($args, apply_filters('bu_filter_primarynav_defaults', $defaults));
    // Gather all sections
    $section_args = array('direction' => 'down', 'depth' => $r['depth'], 'post_types' => $r['post_types'], 'include_links' => $r['include_links']);
    $sections = bu_navigation_gather_sections(0, $section_args);
    // Fetch only posts in sections that we need
    $post_args = array('sections' => $sections, 'post_types' => $r['post_types'], 'include_links' => $r['include_links']);
    $pages = bu_navigation_get_pages($post_args);
    $pages_by_parent = bu_navigation_pages_by_parent($pages);
    $top_level_pages = array();
    $html = '';
    // Start displaying top level posts
    if (is_array($pages_by_parent) && isset($pages_by_parent[0]) && count($pages_by_parent[0]) > 0) {
        $top_level_pages = $pages_by_parent[0];
    }
    if (!empty($top_level_pages)) {
        $nItems = 0;
        $whitelist = null;
        // Optionally restrict top level posts to white list of post names
        if ($r['whitelist_top']) {
            if (is_string($r['whitelist_top'])) {
                $whitelist = explode(',', $r['whitelist_top']);
            }
            if (is_array($r['whitelist_top'])) {
                $whitelist = $r['whitelist_top'];
            }
        }
        // Start list
        $html = sprintf('<%s id="%s" class="%s %s">', $r['container_tag'], $r['container_id'], $r['container_class'], $r['dive'] ? '' : 'no-dive');
        // Section arguments
        $sargs = array('container_tag' => $r['container_tag'], 'item_tag' => $r['item_tag'], 'depth' => 2);
        foreach ($top_level_pages as $page) {
            // Check whitelist if it's being used
            if (is_array($whitelist) && !in_array($page->post_name, $whitelist)) {
                continue;
            }
            $child_html = '';
            // List children if we're diving
            if ($r['dive']) {
                $child_html = bu_navigation_list_section($page->ID, $pages_by_parent, $sargs);
            }
            // Display formatted page (optionally with post name as ID)
            if ($r['identify_top']) {
                $html .= bu_navigation_format_page($page, array('html' => $child_html, 'depth' => 1, 'item_tag' => $r['item_tag'], 'item_id' => $page->post_name));
            } else {
                $html .= bu_navigation_format_page($page, array('html' => $child_html, 'depth' => 1, 'item_tag' => $r['item_tag']));
            }
            $nItems++;
            // Limit to max number of posts
            if ($nItems >= $r['max_items']) {
                break;
            }
        }
        // Close list
        $html .= sprintf("\n</%s>\n", $r['container_tag']);
    }
    if ($r['echo']) {
        echo $html;
    }
    return $html;
}
Exemplo n.º 2
0
 /**
  *  Covers bu_navigation_format_page()
  */
 public function test_bu_navigation_format_page()
 {
     /*
      *	Test Base Functionality
      */
     // Get parent page details
     $parent = $this->posts['parent'];
     $pages = bu_navigation_get_pages();
     $page = $pages[$parent];
     // Expected formatted results
     $title = $page->post_title;
     $url = $page->url;
     $expected_formatted_page = '<li class="page_item page-item-' . $parent . '">' . "\n" . '<a title="' . $title . '" href="' . $url . '">' . $title . '</a>' . "\n" . ' </li>' . "\n";
     // Format $parent Page
     $formatted_page = bu_navigation_format_page($page);
     // Test the page formating worked
     $this->assertEquals($expected_formatted_page, $formatted_page);
     /*
      *	Test Item Tag Functionaltiy
      */
     $args = array('item_tag' => 'td');
     $formatted_page = bu_navigation_format_page($page, $args);
     $expected_formatted_page = sprintf("<td class=\"page_item page-item-%s\">\n<a title=\"%s\" href=\"%s\">%s</a>\n </td>\n", $parent, $title, $url, $title);
     $this->assertEquals($expected_formatted_page, $formatted_page);
     /*
      *	Test Anchor Class Functionaltiy
      */
     $args = array('anchor_class' => 'test_class');
     $formatted_page = bu_navigation_format_page($page, $args);
     $expected_formatted_page = sprintf("<li class=\"page_item page-item-%s\">\n<a title=\"%s\" class=\"test_class\" href=\"%s\">%s</a>\n </li>\n", $parent, $title, $url, $title);
     $this->assertEquals($expected_formatted_page, $formatted_page);
     /*
      *	Test Depth Class Functionaltiy
      */
     $args = array('depth' => 5);
     $formatted_page = bu_navigation_format_page($page, $args);
     $expected_formatted_page = sprintf("<li class=\"page_item page-item-%s\">\n<a title=\"%s\" class=\"level_5\" href=\"%s\">%s</a>\n </li>\n", $parent, $title, $url, $title);
     $this->assertEquals($expected_formatted_page, $formatted_page);
     /*
      *	Test Section ID Functionaltiy
      */
     $child = $this->posts['child'];
     $page_child = $pages[$child];
     $title_child = $page_child->post_title;
     $url_child = $page_child->url;
     $args = array('section_ids' => array($parent, $child));
     $formatted_page = bu_navigation_format_page($page_child, $args);
     $expected_formatted_page = '<li class="page_item page-item-' . $child . ' has_children">' . "\n" . '<a title="' . $title_child . '" href="' . $url_child . '">' . $title_child . '</a>' . "\n" . ' </li>' . "\n";
     $this->assertEquals($expected_formatted_page, $formatted_page);
     /*
      *	Test Position Functionaltiy
      */
     // First Item
     $args = array('position' => 1, 'siblings' => 3);
     $formatted_page = bu_navigation_format_page($page, $args);
     $expected_formatted_page = '<li class="page_item page-item-' . $parent . ' first_item">' . "\n" . '<a title="' . $title . '" href="' . $url . '">' . $title . '</a>' . "\n" . ' </li>' . "\n";
     $this->assertEquals($expected_formatted_page, $formatted_page);
     // Last Item
     $args = array('position' => 3, 'siblings' => 3);
     $formatted_page = bu_navigation_format_page($page, $args);
     $expected_formatted_page = '<li class="page_item page-item-' . $parent . ' last_item">' . "\n" . '<a title="' . $title . '" href="' . $url . '">' . $title . '</a>' . "\n" . ' </li>' . "\n";
     $this->assertEquals($expected_formatted_page, $formatted_page);
     /*
      *	Test HTML Functionaltiy
      */
     $args = array('html' => 'some html');
     $formatted_page = bu_navigation_format_page($page, $args);
     $expected_formatted_page = '<li class="page_item page-item-' . $parent . '">' . "\n" . '<a title="' . $title . '" href="' . $url . '">' . $title . '</a>' . "\n" . ' some html</li>' . "\n";
     $this->assertEquals($expected_formatted_page, $formatted_page);
     /*
      *	Test ID Functionaltiy
      */
     $args = array('item_id' => 'test_item_id');
     $formatted_page = bu_navigation_format_page($page, $args);
     $expected_formatted_page = '<li id="test_item_id" class="page_item page-item-' . $parent . '">' . "\n" . '<a title="' . $title . '" href="' . $url . '">' . $title . '</a>' . "\n" . ' </li>' . "\n";
     $this->assertEquals($expected_formatted_page, $formatted_page);
 }