improve category collection

This commit is contained in:
John Bintz 2010-01-14 19:37:05 -05:00
parent ff497f14ad
commit 6160058777
3 changed files with 41 additions and 14 deletions

View File

@ -106,6 +106,19 @@ class ComicPressDBInterface {
function get_next_post($categories = null, $override_post = null) { return $this->get_adjacent_post($categories, true, $override_post); }
// @codeCoverageIgnoreEnd
function get_parent_child_category_ids() {
global $wpdb;
$parent_child_categories = array();
$result = $wpdb->get_results("SELECT term_id, parent FROM $wpdb->term_taxonomy", ARRAY_A);
if (!empty($result)) {
foreach ($result as $row) {
$parent_child_categories[$row['term_id']] = $row['parent'];
}
}
?>
return $parent_child_categories;
}
}

View File

@ -247,19 +247,28 @@ class ComicPressStoryline {
return $this->_merge_simple_storyline($simple_storyline);
}
function get_comicpress_dbi() {
return ComicPressDBInterface::get_instance();
}
/**
* Get a simple structure.
*/
function get_category_simple_structure($parent = null) {
$structure = array();
foreach (get_all_category_ids() as $category_id) {
$category = get_category($category_id);
if (!isset($structure[$category->parent])) {
$structure[$category->parent] = array();
$dbi = $this->get_comicpress_dbi();
$result = $dbi->get_parent_child_category_ids();
foreach ($result as $cat_id => $cat_parent) {
if (!isset($structure[$cat_parent])) {
$structure[$cat_parent] = array();
}
$structure[$category->parent][$category_id] = true;
$structure[$cat_parent][$cat_id] = true;
}
$structure = $this->_merge_simple_storyline($structure);
if (!empty($parent)) {
if (isset($structure[0])) {
foreach ($structure[0] as $key => $children) {
@ -267,6 +276,7 @@ class ComicPressStoryline {
}
}
}
return $structure;
}

View File

@ -283,11 +283,15 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase {
}
function testGetCategorySimpleStructure() {
add_category(1, (object)array('parent' => 0));
add_category(2, (object)array('parent' => 1));
add_category(3, (object)array('parent' => 2));
add_category(4, (object)array('parent' => 2));
add_category(5, (object)array('parent' => 0));
$css = $this->getMock('ComicPressStoryline', array('get_comicpress_dbi'));
$dbi = $this->getMock('ComicPressDBInterface', array('get_parent_child_category_ids'));
$dbi->expects($this->any())
->method('get_parent_child_category_ids')
->will($this->returnValue(array(1 => 0, 2 => 1, 3 => 2, 4 => 2, 5 => 0)));
$css->expects($this->any())->method('get_comicpress_dbi')->will($this->returnValue($dbi));
$this->assertEquals(array(
'0' => array(
@ -298,7 +302,7 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase {
)
)
)
), $this->css->get_category_simple_structure(1));
), $css->get_category_simple_structure(1));
$this->assertEquals(array(
'0' => array(
@ -310,7 +314,7 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase {
),
'5' => true
)
), $this->css->get_category_simple_structure());
), $css->get_category_simple_structure());
}
function providerTestNormalizeFlattenedStoryline() {