/** * Appends an "active_section" property to every post being returned during bu_navigation_get_pages * * @param array $pages Associative array of pages keyed on page ID * @return array Filtered associative array of pages with active_section member variable set */ function bu_navigation_filter_pages_ancestors($pages) { global $wpdb, $post; // Only useful during single post query if (!$post) { return $pages; } // Only needed for hierarchical post types $post_type_object = get_post_type_object($post->post_type); if (!$post_type_object->hierarchical) { return $pages; } $ancestors = bu_navigation_gather_sections($post->ID, array('post_types' => $post->post_type)); $filtered = array(); if (is_array($pages) && count($pages) > 0) { if (is_array($ancestors) && count($ancestors) > 0) { foreach ($pages as $page) { $page->active_section = false; if (in_array($page->ID, $ancestors) && $page->ID != $post->ID) { $page->active_section = true; } $filtered[$page->ID] = $page; } } else { $filtered = $pages; } } return $filtered; }
/** * Filters arguments passed to bu_navigation_list_pages from widget display * * This is an ugly way of short circuiting the logic within bu_navigation_list_pages to not * display all sections. */ function widget_bu_pages_args_adaptive($args) { remove_filter('widget_bu_pages_args', 'widget_bu_pages_args_adaptive'); if ($args['page_id']) { $section_args = array('post_types' => $args['post_types']); $sections = bu_navigation_gather_sections($args['page_id'], $section_args); $args['sections'] = $sections; $args['page_id'] = NULL; } return $args; }
/** * Returns HTML fragment containing a section title * * @param array $args widget args, as passed to WP_Widget::widget * @param array $instance widget instance args, as passed to WP_Widget::widget * @return string HTML fragment with title */ public function section_title($args, $instance) { global $post; $html = $title = $href = ''; $section_id = 0; // Determine which post to use for the section title if ($instance['navigation_style'] != 'site') { // Gather ancestors $sections = bu_navigation_gather_sections($post->ID, array('post_types' => $post->post_type)); // Adaptive navigation style uses the grandparent of current post if ($instance['navigation_style'] == 'adaptive') { $grandparent_offset = count($sections) - 2; // If the current page is the last item, go up one further if (end($sections) == $post->ID) { $grandparent_offset--; } if (isset($sections[$grandparent_offset])) { $section_id = $sections[$grandparent_offset]; } } else { // Default to top level post (if we have one) if (isset($sections[1])) { $section_id = $sections[1]; } } } // Use section post for title if ($section_id) { $section = get_post($section_id); // Prevent usage of non-published posts as titles if ('publish' === $section->post_status) { // Second argument prevents usage of default (no title) label $title = bu_navigation_get_label($section, ''); $href = get_permalink($section->ID); } } // Fallback to site title if we're still empty if (empty($title)) { $title = get_bloginfo('name'); $href = trailingslashit(get_bloginfo('url')); } if ($title && $href) { $html = sprintf("<a class=\"content_nav_header\" href=\"%s\">%s</a>\n", esc_attr($href), $title); } return $html; }
public function filter_posts($posts) { if ($this->post_parent) { $section_args = array('direction' => 'down', 'depth' => 0, 'post_types' => array($this->post_type)); $sections = bu_navigation_gather_sections($this->post_parent, $section_args); if (is_array($sections) && count($sections) > 0) { $filtered = array(); foreach ($posts as $p) { if (in_array($p->post_parent, $sections) || in_array($p->ID, $sections)) { array_push($filtered, $p); } } $posts = $filtered; } } return $posts; }
/** * Generate page parent select menu * * @uses bu_filter_pages_parent_dropdown(). * * @param string $post_type required -- post type to filter posts for * @param int $selected post ID of the selected post * @param array $args optional configuration object * * @return string the resulting dropdown markup */ function bu_navigation_page_parent_dropdown($post_type, $selected = 0, $args = array()) { $defaults = array('echo' => 1, 'select_id' => 'bu_filter_pages', 'select_name' => 'post_parent', 'select_classes' => '', 'post_status' => array('publish', 'private')); $r = wp_parse_args($args, $defaults); // Grab top level pages for current post type $args = array('direction' => 'down', 'depth' => 1, 'post_types' => (array) $post_type); $sections = bu_navigation_gather_sections(0, $args); $args = array('suppress_filter_pages' => TRUE, 'sections' => $sections, 'post_types' => (array) $post_type, 'post_status' => (array) $r['post_status']); $pages = bu_navigation_get_pages($args); $pages_by_parent = bu_navigation_pages_by_parent($pages); $options = "\n\t<option value=\"0\">" . __('Show all sections') . "</option>\r"; // Get options ob_start(); bu_filter_pages_parent_dropdown($pages_by_parent, $selected); $options .= ob_get_contents(); ob_end_clean(); $classes = !empty($r['select_classes']) ? " class=\"{$r['select_classes']}\"" : ''; $dropdown = sprintf("<select id=\"%s\" name=\"%s\"%s>\r%s\r</select>\r", $r['select_id'], $r['select_name'], $classes, $options); if ($r['echo']) { echo $dropdown; } return $dropdown; }
public function test_bu_navigation_get_post_link() { global $wp_rewrite; // Non-Page Hierarchical Post Type 'Default Permalinks' do not work for child posts prior to 4.0 // @see https://core.trac.wordpress.org/ticket/29615 if ($wp_rewrite->using_permalinks() && version_compare($GLOBALS['wp_version'], '4.4', '>=')) { $grandchild = $this->posts['test_grandchild']; $grandchild = get_post($grandchild); // With ancestors $ancestors = bu_navigation_gather_sections($grandchild->ID, array('direction' => 'up')); $this->assertEquals(get_post_permalink($grandchild), bu_navigation_get_post_link($grandchild, $ancestors)); // Without ancestors $this->assertEquals(get_post_permalink($grandchild), bu_navigation_get_post_link($grandchild)); } }
function bu_navigation_breadcrumbs($args = '') { global $post; $defaults = array('post' => $post, 'glue' => ' » ', 'container_tag' => 'div', 'container_id' => 'breadcrumbs', 'container_class' => '', 'anchor_class' => 'crumb', 'crumb_tag' => '', 'crumb_current' => 1, 'anchor_current' => 0, 'echo' => 0, 'home' => false, 'home_label' => 'Home', 'prefix' => '', 'suffix' => '', 'include_statuses' => 'publish', 'include_hidden' => false, 'show_links' => true); $r = wp_parse_args($args, $defaults); if ($r['post']) { $p = null; if (is_numeric($r['post'])) { $p = get_post($r['post']); } else { if (is_object($r['post'])) { $p = $r['post']; } } if (is_null($p)) { error_log('bu_navigation_breadcrumbs - invalid post argument: ' . $r['post']); return false; } } // Grab ancestors $ancestors = bu_navigation_gather_sections($p->ID, array('post_types' => $p->post_type)); if (!in_array($p->ID, $ancestors)) { array_push($ancestors, $p->ID); } $exclude_filter_removed = false; if ($r['include_hidden'] && has_filter('bu_navigation_filter_pages', 'bu_navigation_filter_pages_exclude')) { $exclude_filter_removed = remove_filter('bu_navigation_filter_pages', 'bu_navigation_filter_pages_exclude'); } $pages = bu_navigation_get_pages(array('pages' => $ancestors, 'post_types' => $p->post_type, 'post_status' => $r['include_statuses'])); if ($r['include_hidden'] && $exclude_filter_removed) { add_filter('bu_navigation_filter_pages', 'bu_navigation_filter_pages_exclude'); } // Container markup $attrs = ''; if ($r['container_id']) { $attrs .= sprintf(' id="%s"', $r['container_id']); } if ($r['container_class']) { $attrs .= sprintf(' class="%s"', $r['container_class']); } $html = sprintf('<%s%s>%s', $r['container_tag'], $attrs, $r['prefix']); // Build crumb markup $crumbs = array(); if (is_array($pages) && count($pages) > 0) { foreach ($ancestors as $page_id) { if (!$page_id && $r['home']) { $anchor_open = sprintf('<a href="%s" class="%s">', get_bloginfo('url'), $r['anchor_class']); $anchor_close = '</a>'; if ($r['show_links']) { $crumb = $anchor_open . $r['home_label'] . $anchor_close; } else { $crumb = $r['home_label']; } array_push($crumbs, $crumb); continue; } else { if (!array_key_exists($page_id, $pages)) { continue; } } $current = $pages[$page_id]; if (!isset($current->navigation_label)) { $current->navigation_label = apply_filters('the_title', $current->post_title); } $title = esc_attr($current->navigation_label); $href = $current->url; $classname = $r['anchor_class']; $crumb = $anchor_open = $anchor_close = ''; if ($current->ID == $p->ID) { $classname .= ' active'; } if ($r['show_links']) { if ($current->ID == $p->ID && !$r['anchor_current']) { $anchor_open = sprintf('<a class="%s">', $classname); } else { $anchor_open = sprintf('<a href="%s" class="%s">', $href, $classname); } $anchor_close = '</a>'; } $before_crumb = $after_crumb = ''; if ($r['crumb_tag']) { $before_crumb = $current->ID == $p->ID ? sprintf('<%s class="current">', $r['crumb_tag']) : sprintf('<%s>', $r['crumb_tag']); $after_crumb = sprintf('</%s>', $r['crumb_tag']); } $crumb = $before_crumb . $anchor_open . $title . $anchor_close . $after_crumb; $crumb = apply_filters('bu_navigation_filter_crumb_html', $crumb, $current, $r); // Only crumb if not current page or if we're crumbing the current page if ($current->ID != $p->ID || $r['crumb_current']) { array_push($crumbs, $crumb); } } $html .= implode($r['glue'], $crumbs); } $html .= sprintf('%s</%s>', $r['suffix'], $r['container_tag']); if ($r['echo']) { echo $html; } return $html; }
/** * Execute query set up during construction */ protected function query() { // Setup filters remove_filter('bu_navigation_filter_pages', 'bu_navigation_filter_pages_exclude'); add_filter('bu_navigation_filter_pages', array($this, 'filter_posts')); // Gather sections $section_args = array('direction' => 'down', 'depth' => $this->args['depth'], 'post_types' => $this->args['post_types']); $sections = bu_navigation_gather_sections($this->args['child_of'], $section_args); // Load pages in sections $this->posts = bu_navigation_get_posts(array('sections' => $sections, 'post_types' => $this->args['post_types'], 'post_status' => $this->args['post_status'], 'include_links' => $this->args['include_links'], 'suppress_urls' => $this->args['suppress_urls'])); $this->post_count = count($this->posts); // Restore filters remove_filter('bu_navigation_filter_pages', array($this, 'filter_posts')); add_filter('bu_navigation_filter_pages', 'bu_navigation_filter_pages_exclude'); }