Пример #1
0
/**
 * Displays a select box containing page parents, used to filter page list by parent
 *
 * Relocated from the navigation plugin (bu-filter-pages.php) to remove dependency on plugin.
 *
 * @return boolean TRUE if the box was displayed, FALSE otherwise.
 */
function bu_filter_pages_parent_dropdown($pages_by_parent, $default = 0, $parent = 0, $level = 0)
{
    $post_types = bu_navigation_supported_post_types();
    if (is_array($pages_by_parent) && array_key_exists($parent, $pages_by_parent) && count($pages_by_parent) > 0) {
        foreach ($pages_by_parent[$parent] as $p) {
            if (!in_array($p->post_type, $post_types)) {
                continue;
            }
            // only show valid post types
            if (!array_key_exists($p->ID, $pages_by_parent)) {
                continue;
            }
            // don't show pages with no children
            $padding = str_repeat(' ', $level * 3);
            $selected = $p->ID == $default ? 'selected="selected"' : '';
            printf("\n\t<option value=\"%d\" %s>%s%s</option>\r", $p->ID, $selected, $padding, esc_html($p->post_title));
            bu_filter_pages_parent_dropdown($pages_by_parent, $default, $p->ID, $level + 1);
        }
        return TRUE;
    }
    return FALSE;
}
Пример #2
0
 /**
  * Covers bu_filter_pages_parent_dropdown()
  */
 public function test_bu_filter_pages_parent_dropdown()
 {
     $parent = $this->posts['parent'];
     $child = $this->posts['child'];
     $grandchild_one = $this->posts['grandchild_one'];
     $pages = bu_navigation_get_pages();
     $pages_by_parent = bu_navigation_pages_by_parent($pages);
     $page_options_expected = "\n\t" . '<option value="' . $child . '" >Child Page</option>' . "\r" . "\n\t" . '<option value="' . $grandchild_one . '" >&nbsp;&nbsp;&nbsp;Grand Child Page 1</option>' . "\r";
     ob_start();
     bu_filter_pages_parent_dropdown($pages_by_parent, 0, $parent);
     $page_options_results = ob_get_contents();
     ob_end_clean();
     $this->assertEquals($page_options_results, $page_options_expected);
     /**
      * 	Test Default Function
      */
     $page_options_default_expected = "\n\t" . '<option value="' . $child . '" selected="selected">Child Page</option>' . "\r" . "\n\t" . '<option value="' . $grandchild_one . '" >&nbsp;&nbsp;&nbsp;Grand Child Page 1</option>' . "\r";
     ob_start();
     bu_filter_pages_parent_dropdown($pages_by_parent, $child, $parent);
     $page_options_default_results = ob_get_contents();
     ob_end_clean();
     $this->assertEquals($page_options_default_expected, $page_options_default_results);
     /**
      * 	Test Parent Function
      */
     $page_options_parent_expected = "\n\t" . '<option value="' . $grandchild_one . '" >Grand Child Page 1</option>' . "\r";
     ob_start();
     bu_filter_pages_parent_dropdown($pages_by_parent, 0, $child);
     $page_options_parent_results = ob_get_contents();
     ob_end_clean();
     $this->assertEquals($page_options_parent_expected, $page_options_parent_results);
     /**
      * 	Test Level Function
      */
     $page_options_level_expected = "\n\t" . '<option value="' . $child . '" selected="selected">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Child Page</option>' . "\r" . "\n\t" . '<option value="' . $grandchild_one . '" >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Grand Child Page 1</option>' . "\r";
     ob_start();
     bu_filter_pages_parent_dropdown($pages_by_parent, $child, $parent, 2);
     $page_options_level_results = ob_get_contents();
     ob_end_clean();
     $this->assertEquals($page_options_level_expected, $page_options_level_results);
 }