diff --git a/classes/ComicPressDBInterface.inc b/classes/ComicPressDBInterface.inc index 71b07d0..b47b3c5 100644 --- a/classes/ComicPressDBInterface.inc +++ b/classes/ComicPressDBInterface.inc @@ -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; + } +} diff --git a/classes/ComicPressStoryline.inc b/classes/ComicPressStoryline.inc index cf64314..e39c0c4 100644 --- a/classes/ComicPressStoryline.inc +++ b/classes/ComicPressStoryline.inc @@ -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; } diff --git a/test/ComicPressStorylineTest.php b/test/ComicPressStorylineTest.php index e254440..9449c0b 100644 --- a/test/ComicPressStorylineTest.php +++ b/test/ComicPressStorylineTest.php @@ -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() {