more category work

This commit is contained in:
John Bintz 2009-07-08 23:01:11 -04:00
parent b415c4d0e8
commit 1ff6d12398
3 changed files with 51 additions and 29 deletions

View File

@ -142,6 +142,35 @@ class ComicPress {
} }
return $this->categories_by_id; 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;
}
} }
?> ?>

View File

@ -230,37 +230,8 @@ function get_comic_url($folder = 'comic', $override_post = null, $filter = 'defa
return false; 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 // ComicPress Template Functions
function the_comic($filter = 'default') { echo get_comic_url('comic', null, $filter); } function the_comic($filter = 'default') { echo get_comic_url('comic', null, $filter); }

View File

@ -93,6 +93,28 @@ class ComicPressTest extends PHPUnit_Framework_TestCase {
$this->assertEquals($expected_tree, $this->cp->sort_comic_categories()); $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));
}
} }
?> ?>