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); } function get_next_post($categories = null, $override_post = null) { return $this->get_adjacent_post($categories, true, $override_post); }
// @codeCoverageIgnoreEnd // @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); return $this->_merge_simple_storyline($simple_storyline);
} }
function get_comicpress_dbi() {
return ComicPressDBInterface::get_instance();
}
/** /**
* Get a simple structure. * Get a simple structure.
*/ */
function get_category_simple_structure($parent = null) { function get_category_simple_structure($parent = null) {
$structure = array(); $structure = array();
foreach (get_all_category_ids() as $category_id) { $dbi = $this->get_comicpress_dbi();
$category = get_category($category_id);
if (!isset($structure[$category->parent])) { $result = $dbi->get_parent_child_category_ids();
$structure[$category->parent] = array();
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); $structure = $this->_merge_simple_storyline($structure);
if (!empty($parent)) { if (!empty($parent)) {
if (isset($structure[0])) { if (isset($structure[0])) {
foreach ($structure[0] as $key => $children) { foreach ($structure[0] as $key => $children) {
@ -267,6 +276,7 @@ class ComicPressStoryline {
} }
} }
} }
return $structure; return $structure;
} }

View File

@ -283,11 +283,15 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase {
} }
function testGetCategorySimpleStructure() { function testGetCategorySimpleStructure() {
add_category(1, (object)array('parent' => 0)); $css = $this->getMock('ComicPressStoryline', array('get_comicpress_dbi'));
add_category(2, (object)array('parent' => 1));
add_category(3, (object)array('parent' => 2)); $dbi = $this->getMock('ComicPressDBInterface', array('get_parent_child_category_ids'));
add_category(4, (object)array('parent' => 2));
add_category(5, (object)array('parent' => 0)); $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( $this->assertEquals(array(
'0' => 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( $this->assertEquals(array(
'0' => array( '0' => array(
@ -310,7 +314,7 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase {
), ),
'5' => true '5' => true
) )
), $this->css->get_category_simple_structure()); ), $css->get_category_simple_structure());
} }
function providerTestNormalizeFlattenedStoryline() { function providerTestNormalizeFlattenedStoryline() {