factor out category handling

This commit is contained in:
John Bintz 2009-07-08 22:38:43 -04:00
parent cd77f0a83d
commit b415c4d0e8
3 changed files with 147 additions and 78 deletions

View File

@ -5,9 +5,12 @@ class ComicPress {
'comic_category_id' => 1,
'comic_dimensions' => '760x',
'rss_dimensions' => '350x',
'archive_dimensions' => '125x'
'archive_dimensions' => '125x',
'category_order' => false
);
var $category_tree = array();
function load() {
$result = get_option('comicpress-options');
if (is_array($result)) {
@ -23,6 +26,10 @@ class ComicPress {
function init() {
$this->load();
$this->get_all_category_objects_by_id();
$this->flatten_categories();
$this->separate_categories();
$this->sort_comic_categories();
}
function get_comic_img_tag($url, $type, $additional_parameters = array()) {
@ -47,6 +54,94 @@ class ComicPress {
return $output;
}
/**
* Flatten all WP categories into nodes like 0/3/5.
*/
function flatten_categories() {
$this->category_tree = array();
foreach (array_keys($this->categories_by_id) as $category_id) {
$this->category_tree[] = $this->categories_by_id[$category_id]->parent . '/' . $category_id;
}
do {
$all_ok = true;
for ($i = 0; $i < count($this->category_tree); ++$i) {
$current_parts = explode("/", $this->category_tree[$i]);
if (reset($current_parts) != 0) {
$all_ok = false;
for ($j = 0; $j < count($this->category_tree); ++$j) {
$j_parts = explode("/", $this->category_tree[$j]);
if (end($j_parts) == reset($current_parts)) {
$this->category_tree[$i] = implode("/", array_merge($j_parts, array_slice($current_parts, 1)));
break;
}
}
}
}
} while (!$all_ok);
return $this->category_tree;
}
/**
* Separate categories into comics and non-comics categories.
*/
function separate_categories() {
$comic_categories = array();
$non_comic_categories = array();
foreach ($this->category_tree as $node) {
$parts = split("/", $node);
if ($parts[1] == $this->comicpress_options['comic_category_id']) {
$comic_categories[] = $node;
} else {
$non_comic_categories[] = $node;
}
}
$this->category_tree = $comic_categories;
$this->non_comic_categories = $non_comic_categories;
}
/**
* Sort the category tree, adding in new categories in the order as necessary.
*/
function sort_comic_categories() {
if (is_array($this->comicpress_options['category_order'])) {
$new_order = array();
foreach ($this->comicpress_options['category_order'] as $node) {
if (in_array($node, $this->category_tree)) {
$new_order[] = $node;
}
}
foreach ($this->category_tree as $node) {
if (!in_array($node, $this->comicpress_options['category_order'])) {
$new_order[] = $node;
}
}
$this->category_tree = $new_order;;
}
return $this->category_tree;
}
/**
* Turn the list of categories into a hash table of category objects.
*/
function get_all_category_objects_by_id() {
if (empty($this->categories_by_id)) {
$this->categories_by_id = array();
foreach (get_categories("hide_empty=0") as $category_object) {
$this->categories_by_id[$category_object->term_id] = $category_object;
}
}
return $this->categories_by_id;
}
}
?>

View File

@ -246,83 +246,6 @@ function get_all_comic_categories_as_cat_string() {
return $all_comic_categories_as_string;
}
/**
* Turn the list of categories into a hash table of category objects.
*/
function get_all_category_objects_by_id() {
global $categories_by_id;
if (empty($categories_by_id)) {
$categories_by_id = array();
foreach (get_categories("hide_empty=0") as $category_object) {
$categories_by_id[$category_object->term_id] = $category_object;
}
}
return $categories_by_id;
}
/**
* Parse all categories and sort them into comics and non-comics categories.
*/
function get_all_comic_categories() {
global $comiccat, $category_tree, $non_comic_categories;
$categories_by_id = get_all_category_objects_by_id();
foreach (array_keys($categories_by_id) as $category_id) {
$category_tree[] = $categories_by_id[$category_id]->parent . '/' . $category_id;
}
do {
$all_ok = true;
for ($i = 0; $i < count($category_tree); ++$i) {
$current_parts = explode("/", $category_tree[$i]);
if (reset($current_parts) != 0) {
$all_ok = false;
for ($j = 0; $j < count($category_tree); ++$j) {
$j_parts = explode("/", $category_tree[$j]);
if (end($j_parts) == reset($current_parts)) {
$category_tree[$i] = implode("/", array_merge($j_parts, array_slice($current_parts, 1)));
break;
}
}
}
}
} while (!$all_ok);
$non_comic_tree = array();
if (get_option('comicpress-enable-storyline-support') == 1) {
$result = get_option("comicpress-storyline-category-order");
if (!empty($result)) {
$category_tree = explode(",", $result);
}
$non_comic_tree = array_keys($categories_by_id);
foreach ($category_tree as $node) {
$parts = explode("/", $node);
$category_id = end($parts);
if ($parts[1] == $comiccat) {
if (($index = array_search($category_id, $non_comic_tree)) !== false) {
array_splice($non_comic_tree, $index, 1);
}
}
}
} else {
$new_category_tree = array();
foreach ($category_tree as $node) {
$parts = explode("/", $node);
if ($parts[1] == $comiccat) {
$new_category_tree[] = $node;
} else {
$non_comic_tree[] = end($parts);
}
}
$category_tree = $new_category_tree;
}
$non_comic_categories = implode(" and ", $non_comic_tree);
}
/**
* Return true if the current post is in the comics category or a child category.

View File

@ -42,6 +42,57 @@ class ComicPressTest extends PHPUnit_Framework_TestCase {
}
}
}
function testFlattenCategories() {
$cp = $this->getMock('ComicPress', array('get_all_category_objects_by_id'));
$cp->categories_by_id = array(
'1' => (object)array(
'term_id' => 1,
'parent' => 0
),
'2' => (object)array(
'term_id' => 2,
'parent' => 1
),
'3' => (object)array(
'term_id' => 3,
'parent' => 0
)
);
$this->assertEquals(array('0/1', '0/1/2', '0/3'), $cp->flatten_categories());
}
function testSeparateCategories() {
$cp = $this->getMock('ComicPress', array('flatten_categories'));
$cp->category_tree = array('0/1', '0/1/2', '0/3');
$cp->comicpress_options['comic_category_id'] = 1;
$cp->separate_categories();
$this->assertEquals(array('0/1', '0/1/2'), $cp->category_tree);
$this->assertEquals(array('0/3'), $cp->non_comic_categories);
}
function providerSortComicCategories() {
return array(
array(false, array('0/1', '0/2'), array('0/1', '0/2')),
array(array('0/2', '0/1'), array('0/1', '0/2', '0/3'), array('0/2', '0/1', '0/3'))
);
}
/**
* @dataProvider providerSortComicCategories
*/
function testSortComicCategories($category_order, $category_tree, $expected_tree) {
$this->cp->comicpress_options['category_order'] = $category_order;
$this->cp->category_tree = $category_tree;
$this->assertEquals($expected_tree, $this->cp->sort_comic_categories());
}
}
?>