/** * 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; }
/** * Covers bu_navigation_list_section() */ public function test_bu_navigation_list_section() { $parent = $this->posts['parent']; $child = $this->posts['child']; $grandchild_one = $this->posts['grandchild_one']; $grandchild_two = $this->posts['grandchild_two']; $greatgrandchild = $this->posts['greatgrandchild']; $pages = bu_navigation_get_pages(); $pages_by_parent = bu_navigation_pages_by_parent($pages); // Generate Expected Results - Child Section $child_section_expected = "\n<ul>\n" . '<li class="page_item page-item-' . $grandchild_one . '">' . "\n" . '<a title="' . $pages[$grandchild_one]->post_title . '" class="level_1" href="' . $pages[$grandchild_one]->url . '">' . $pages[$grandchild_one]->post_title . '</a>' . "\n \n" . "<ul>\n" . '<li class="page_item page-item-' . $greatgrandchild . '">' . "\n" . '<a title="' . $pages[$greatgrandchild]->post_title . '" class="level_2" href="' . $pages[$greatgrandchild]->url . '">' . $pages[$greatgrandchild]->post_title . '</a>' . "\n" . " </li>\n\n" . "</ul>\n" . "</li>\n" . '<li class="page_item page-item-' . $grandchild_two . '">' . "\n" . '<a title="' . $pages[$grandchild_two]->post_title . '" class="level_1" href="' . $pages[$grandchild_two]->url . '">' . $pages[$grandchild_two]->post_title . '</a>' . "\n" . " </li>\n\n" . "</ul>\n"; // Generate Expected Results - Parent Section $parent_section_expected = "\n<ul>\n" . '<li class="page_item page-item-' . $child . '">' . "\n" . '<a title="' . $pages[$child]->post_title . '" class="level_1" href="' . $pages[$child]->url . '">' . $pages[$child]->post_title . '</a>' . "\n \n" . "<ul>\n" . '<li class="page_item page-item-' . $grandchild_one . '">' . "\n" . '<a title="' . $pages[$grandchild_one]->post_title . '" class="level_2" href="' . $pages[$grandchild_one]->url . '">' . $pages[$grandchild_one]->post_title . '</a>' . "\n \n" . "<ul>\n" . '<li class="page_item page-item-' . $greatgrandchild . '">' . "\n" . '<a title="' . $pages[$greatgrandchild]->post_title . '" class="level_3" href="' . $pages[$greatgrandchild]->url . '">' . $pages[$greatgrandchild]->post_title . '</a>' . "\n" . " </li>\n\n" . "</ul>\n" . "</li>\n" . '<li class="page_item page-item-' . $grandchild_two . '">' . "\n" . '<a title="' . $pages[$grandchild_two]->post_title . '" class="level_2" href="' . $pages[$grandchild_two]->url . '">' . $pages[$grandchild_two]->post_title . '</a>' . "\n" . " </li>\n\n" . "</ul>\n</li>\n\n" . "</ul>\n"; // Get Results $child_section_results = bu_navigation_list_section($child, $pages_by_parent); $parent_section_results = bu_navigation_list_section($parent, $pages_by_parent); // Test Results $this->assertEquals($child_section_expected, $child_section_results); $this->assertEquals($parent_section_expected, $parent_section_results); /** * Test Depth Functionaltiy */ $parent_section_depth_expected = "\n<ul>\n" . '<li class="page_item page-item-' . $child . '">' . "\n" . '<a title="' . $pages[$child]->post_title . '" class="level_3" href="' . $pages[$child]->url . '">' . $pages[$child]->post_title . '</a>' . "\n \n" . "<ul>\n" . '<li class="page_item page-item-' . $grandchild_one . '">' . "\n" . '<a title="' . $pages[$grandchild_one]->post_title . '" class="level_4" href="' . $pages[$grandchild_one]->url . '">' . $pages[$grandchild_one]->post_title . '</a>' . "\n \n" . "<ul>\n" . '<li class="page_item page-item-' . $greatgrandchild . '">' . "\n" . '<a title="' . $pages[$greatgrandchild]->post_title . '" class="level_5" href="' . $pages[$greatgrandchild]->url . '">' . $pages[$greatgrandchild]->post_title . '</a>' . "\n" . " </li>\n\n" . "</ul>\n" . "</li>\n" . '<li class="page_item page-item-' . $grandchild_two . '">' . "\n" . '<a title="' . $pages[$grandchild_two]->post_title . '" class="level_4" href="' . $pages[$grandchild_two]->url . '">' . $pages[$grandchild_two]->post_title . '</a>' . "\n" . " </li>\n\n" . "</ul>\n</li>\n\n" . "</ul>\n"; $args = array('depth' => 3); $parent_section_depth_results = bu_navigation_list_section($parent, $pages_by_parent, $args); $this->assertEquals($parent_section_depth_expected, $parent_section_depth_results); /** * Test Container Tag Functionaltiy */ $parent_section_container_tag_expected = "\n<ol>\n" . '<li class="page_item page-item-' . $child . '">' . "\n" . '<a title="' . $pages[$child]->post_title . '" class="level_1" href="' . $pages[$child]->url . '">' . $pages[$child]->post_title . '</a>' . "\n \n" . "<ol>\n" . '<li class="page_item page-item-' . $grandchild_one . '">' . "\n" . '<a title="' . $pages[$grandchild_one]->post_title . '" class="level_2" href="' . $pages[$grandchild_one]->url . '">' . $pages[$grandchild_one]->post_title . '</a>' . "\n \n" . "<ol>\n" . '<li class="page_item page-item-' . $greatgrandchild . '">' . "\n" . '<a title="' . $pages[$greatgrandchild]->post_title . '" class="level_3" href="' . $pages[$greatgrandchild]->url . '">' . $pages[$greatgrandchild]->post_title . '</a>' . "\n" . " </li>\n\n" . "</ol>\n" . "</li>\n" . '<li class="page_item page-item-' . $grandchild_two . '">' . "\n" . '<a title="' . $pages[$grandchild_two]->post_title . '" class="level_2" href="' . $pages[$grandchild_two]->url . '">' . $pages[$grandchild_two]->post_title . '</a>' . "\n" . " </li>\n\n" . "</ol>\n</li>\n\n" . "</ol>\n"; $args = array('container_tag' => 'ol'); $parent_section_container_tag_results = bu_navigation_list_section($parent, $pages_by_parent, $args); $this->assertEquals($parent_section_container_tag_expected, $parent_section_container_tag_results); /** * Test Item Tag Functionaltiy */ $parent_section_item_tag_expected = "\n<ul>\n" . '<test class="page_item page-item-' . $child . '">' . "\n" . '<a title="' . $pages[$child]->post_title . '" class="level_1" href="' . $pages[$child]->url . '">' . $pages[$child]->post_title . '</a>' . "\n \n" . "<ul>\n" . '<test class="page_item page-item-' . $grandchild_one . '">' . "\n" . '<a title="' . $pages[$grandchild_one]->post_title . '" class="level_2" href="' . $pages[$grandchild_one]->url . '">' . $pages[$grandchild_one]->post_title . '</a>' . "\n \n" . "<ul>\n" . '<test class="page_item page-item-' . $greatgrandchild . '">' . "\n" . '<a title="' . $pages[$greatgrandchild]->post_title . '" class="level_3" href="' . $pages[$greatgrandchild]->url . '">' . $pages[$greatgrandchild]->post_title . '</a>' . "\n" . " </test>\n\n" . "</ul>\n" . "</test>\n" . '<test class="page_item page-item-' . $grandchild_two . '">' . "\n" . '<a title="' . $pages[$grandchild_two]->post_title . '" class="level_2" href="' . $pages[$grandchild_two]->url . '">' . $pages[$grandchild_two]->post_title . '</a>' . "\n" . " </test>\n\n" . "</ul>\n</test>\n\n" . "</ul>\n"; $args = array('item_tag' => 'test'); $parent_section_item_tag_results = bu_navigation_list_section($parent, $pages_by_parent, $args); $this->assertEquals($parent_section_item_tag_expected, $parent_section_item_tag_results); /** * Test Section Ids Functionaltiy */ $parent_section_section_id_expected = "\n<ul>\n" . '<li class="page_item page-item-' . $child . ' has_children">' . "\n" . '<a title="' . $pages[$child]->post_title . '" class="level_1" href="' . $pages[$child]->url . '">' . $pages[$child]->post_title . '</a>' . "\n \n" . "<ul>\n" . '<li class="page_item page-item-' . $grandchild_one . ' has_children">' . "\n" . '<a title="' . $pages[$grandchild_one]->post_title . '" class="level_2" href="' . $pages[$grandchild_one]->url . '">' . $pages[$grandchild_one]->post_title . '</a>' . "\n \n" . "<ul>\n" . '<li class="page_item page-item-' . $greatgrandchild . '">' . "\n" . '<a title="' . $pages[$greatgrandchild]->post_title . '" class="level_3" href="' . $pages[$greatgrandchild]->url . '">' . $pages[$greatgrandchild]->post_title . '</a>' . "\n" . " </li>\n\n" . "</ul>\n" . "</li>\n" . '<li class="page_item page-item-' . $grandchild_two . '">' . "\n" . '<a title="' . $pages[$grandchild_two]->post_title . '" class="level_2" href="' . $pages[$grandchild_two]->url . '">' . $pages[$grandchild_two]->post_title . '</a>' . "\n" . " </li>\n\n" . "</ul>\n</li>\n\n" . "</ul>\n"; $args = array('section_ids' => array($child, $grandchild_one)); $parent_section_section_id_results = bu_navigation_list_section($parent, $pages_by_parent, $args); $this->assertEquals($parent_section_section_id_expected, $parent_section_section_id_results); }