diff --git a/classes/ComicPress.inc b/classes/ComicPress.inc index fa8ff80..4672e65 100644 --- a/classes/ComicPress.inc +++ b/classes/ComicPress.inc @@ -142,6 +142,35 @@ class ComicPress { } return $this->categories_by_id; } + + /** + * Turn the tree of comics categories into a string to be fed into wp_query functions. + */ + function get_all_comic_categories_as_cat_string() { + if (empty($this->all_comic_categories_as_string)) { + $categories = array(); + foreach ($this->category_tree as $node) { + $categories[] = end(explode("/", $node)); + } + $this->all_comic_categories_as_string = implode(",", $categories); + } + return $this->all_comic_categories_as_string; + } + + /** + * Return true if the current post is in the comics category or a child category. + */ + function in_comic_category($post_id) { + $categories = wp_get_post_categories($post_id); + if (is_array($categories)) { + foreach ($this->category_tree as $node) { + if (in_array(end(explode("/", $node)), $categories)) { + return true; + } + } + } + return false; + } } ?> \ No newline at end of file diff --git a/functions.php b/functions.php index 25872d5..c2592a8 100644 --- a/functions.php +++ b/functions.php @@ -230,37 +230,8 @@ function get_comic_url($folder = 'comic', $override_post = null, $filter = 'defa return false; } -/** - * Turn the tree of comics categories into a string to be fed into wp_query functions. - */ -function get_all_comic_categories_as_cat_string() { - global $all_comic_categories_as_string, $category_tree; - if (empty($all_comic_categories_as_string)) { - $categories = array(); - foreach ($category_tree as $node) { - $parts = explode("/", $node); - $categories[] = end($parts); - } - $all_comic_categories_as_string = implode(",", $categories); - } - return $all_comic_categories_as_string; -} -/** - * Return true if the current post is in the comics category or a child category. - */ -function in_comic_category() { - global $post, $category_tree; - - $comic_categories = array(); - foreach ($category_tree as $node) { - $comic_categories[] = end(explode("/", $node)); - } - - return (count(array_intersect($comic_categories, wp_get_post_categories($post->ID))) > 0); -} - // ComicPress Template Functions function the_comic($filter = 'default') { echo get_comic_url('comic', null, $filter); } diff --git a/test/ComicPressTest.php b/test/ComicPressTest.php index ccd4c53..2449123 100644 --- a/test/ComicPressTest.php +++ b/test/ComicPressTest.php @@ -93,6 +93,28 @@ class ComicPressTest extends PHPUnit_Framework_TestCase { $this->assertEquals($expected_tree, $this->cp->sort_comic_categories()); } + + function testGetAllComicCategoriesAsCatString() { + $this->cp->category_tree = array("0/1", "0/2", "0/3"); + $this->assertEquals("1,2,3", $this->cp->get_all_comic_categories_as_cat_string()); + } + + function providerTestInComicCategory() { + return array( + array(array(1), false), + array(array(2), true) + ); + } + + /** + * @dataProvider providerTestInComicCategory + */ + function testInComicCategory($post_categories, $is_in_category) { + $this->cp->category_tree = array('0/2'); + wp_set_post_categories(1, $post_categories); + + $this->assertEquals($is_in_category, $this->cp->in_comic_category(1)); + } } ?> \ No newline at end of file