diff --git a/classes/ComicPress.inc b/classes/ComicPress.inc index 67262da..d3e6747 100644 --- a/classes/ComicPress.inc +++ b/classes/ComicPress.inc @@ -290,34 +290,6 @@ class ComicPress { return false; } - /** - * Find the terminal post in a specific category. - */ - function get_terminal_post_in_category($category_id = null, $first = true) { - $category_id = is_null($category_id) ? $this->comicpress_options['comic_category_id'] : $category_id; - $sort_order = $first ? "asc" : "desc"; - $terminal_comic_query =$this->_new_wp_query(); - $terminal_comic_query->query("showposts=1&order=${sort_order}&cat=${category_id}&status=publish"); - if ($terminal_comic_query->have_posts()) { - return reset($terminal_comic_query->posts); - } - return false; - } - - /** - * Get the first comic in a category. - */ - function get_first_comic($category_id = null) { - return $this->get_terminal_post_in_category($category_id); - } - - /** - * Get the last comic in a category. - */ - function get_last_comic($category_id = null) { - return $this->get_terminal_post_in_category($category_id, false); - } - /** * Get the comics necessary to build the navigation. * @tested diff --git a/classes/ComicPressNavigation.inc b/classes/ComicPressNavigation.inc new file mode 100644 index 0000000..34a6c91 --- /dev/null +++ b/classes/ComicPressNavigation.inc @@ -0,0 +1,37 @@ +post = $post; + $this->storyline = $storyline; + } + + /** + * Find the terminal post in a specific category. + */ + function get_terminal_post_in_category($category_id, $first = true) { + $sort_order = $first ? "asc" : "desc"; + $terminal_comic_query = new WP_Query(); + $terminal_comic_query->query("showposts=1&order=${sort_order}&cat=${category_id}&status=publish"); + if ($terminal_comic_query->have_posts()) { + return reset($terminal_comic_query->posts); + } + return false; + } + + /** + * Get the first comic in a category. + */ + function get_first_in_category($category_id) { + return $this->get_terminal_post_in_category($category_id); + } + + /** + * Get the last comic in a category. + */ + function get_last_in_category($category_id) { + return $this->get_terminal_post_in_category($category_id, false); + } +} + +?> \ No newline at end of file diff --git a/classes/ComicPressStoryline.inc b/classes/ComicPressStoryline.inc index a9dcfe1..3057353 100644 --- a/classes/ComicPressStoryline.inc +++ b/classes/ComicPressStoryline.inc @@ -3,10 +3,15 @@ class ComicPressStoryline { var $_structure; + /** + * Create a searchable structure from a node list. + * @param array $structure The structure to process. + * @return boolean True if the structure was valid. + */ function create_structure($structure) { $new_structure = array(); $parent = null; - $previous = null; + $all_leaves = array(); $adjacents_by_parent = array(); @@ -24,6 +29,7 @@ class ComicPressStoryline { } else { $data = array(); $leaf = end($parts); + $all_leaves[] = $leaf; if (count($parts) > 2) { $parent = $parts[count($parts) - 2]; @@ -52,12 +58,56 @@ class ComicPressStoryline { } } } + + for ($i = 0; $i < count($all_leaves); ++$i) { + foreach (array('prior' => -1, 'upcoming' => 1) as $type => $dir) { + if (isset($all_leaves[$i + $dir])) { + $new_structure[$all_leaves[$i]][$type] = $all_leaves[$i + $dir]; + } + } + } $this->_structure = $new_structure; } } return is_array($this->_structure); } + + function _get_field($field, $id) { + if (isset($this->_structure)) { + if (isset($this->_structure[$id])) { + if (isset($this->_structure[$id][$field])) { + return $this->_structure[$id][$field]; + } + } + } + return false; + } + + function parent($id) { return $this->_get_field('parent', $id); } + function previous($id) { return $this->_get_field('previous', $id); } + function next($id) { return $this->_get_field('next', $id); } + function upcoming($id) { return $this->_get_field('upcoming', $id); } + function prior($id) { return $this->_get_field('prior', $id); } + function valid($id) { + if (isset($this->_structure[$id])) { + return array_keys($this->_structure[$id]); + } + return false; + } + + function get_valid_storyline_nav($post_id) { + $data = false; + + foreach (wp_get_post_categories($post_id) as $category) { + if ($result = $this->valid($category)) { + if ($data) { return false; } + + $data = $result; + } + } + return $data; + } } ?> \ No newline at end of file diff --git a/test/ComicPressNavigationTest.php b/test/ComicPressNavigationTest.php new file mode 100644 index 0000000..50e6996 --- /dev/null +++ b/test/ComicPressNavigationTest.php @@ -0,0 +1,18 @@ +nav = new ComicPressNavigation(); + } + + function testSomething() { + $this->markTestIncomplete(); + } +} + +?> \ No newline at end of file diff --git a/test/ComicPressStorylineTest.php b/test/ComicPressStorylineTest.php index 0d3e5ac..d0461ea 100644 --- a/test/ComicPressStorylineTest.php +++ b/test/ComicPressStorylineTest.php @@ -35,38 +35,38 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { ), array( array('0/1', '0/1/2'), - array('1' => array(), '2' => array('parent' => 1)) + array('1' => array('upcoming' => 2), '2' => array('parent' => 1, 'prior' => 1)) ), array( array('0/1', '0/1/2', '0/1/3'), array( - '1' => array(), - '2' => array('parent' => 1, 'next' => 3), - '3' => array('parent' => 1, 'previous' => 2), + '1' => array('upcoming' => 2), + '2' => array('parent' => 1, 'next' => 3, 'prior' => 1, 'upcoming' => 3), + '3' => array('parent' => 1, 'previous' => 2, 'prior' => 2), ) ), array( array('0/1', '0/1/2', '0/1/2/3', '0/1/2/4', '0/1/5'), array( - '1' => array(), - '2' => array('parent' => 1, 'next' => 5), - '3' => array('parent' => 2, 'next' => 4), - '4' => array('parent' => 2, 'previous' => 3), - '5' => array('parent' => 1, 'previous' => 2), + '1' => array('upcoming' => 2), + '2' => array('parent' => 1, 'next' => 5, 'upcoming' => 3, 'prior' => 1), + '3' => array('parent' => 2, 'next' => 4, 'upcoming' => 4, 'prior' => 2), + '4' => array('parent' => 2, 'previous' => 3, 'upcoming' => 5, 'prior' => 3), + '5' => array('parent' => 1, 'previous' => 2, 'prior' => 4), ) ), array( array('0/1', '0/1/2', '0/1/2/3', '0/1/2/4', '0/1/5', '0/1/5/6', '0/1/5/7', '0/1/5/8', '0/1/9'), array( - '1' => array(), - '2' => array('parent' => 1, 'next' => 5), - '3' => array('parent' => 2, 'next' => 4), - '4' => array('parent' => 2, 'previous' => 3), - '5' => array('parent' => 1, 'previous' => 2, 'next' => 9), - '6' => array('parent' => 5, 'next' => 7), - '7' => array('parent' => 5, 'previous' => 6, 'next' => 8), - '8' => array('parent' => 5, 'previous' => 7), - '9' => array('parent' => 1, 'previous' => 5), + '1' => array('upcoming' => 2), + '2' => array('parent' => 1, 'next' => 5, 'upcoming' => 3, 'prior' => 1), + '3' => array('parent' => 2, 'next' => 4, 'upcoming' => 4, 'prior' => 2), + '4' => array('parent' => 2, 'previous' => 3, 'upcoming' => 5, 'prior' => 3), + '5' => array('parent' => 1, 'previous' => 2, 'next' => 9, 'upcoming' => 6, 'prior' => 4), + '6' => array('parent' => 5, 'next' => 7, 'upcoming' => 7, 'prior' => 5), + '7' => array('parent' => 5, 'previous' => 6, 'next' => 8, 'upcoming' => 8, 'prior' => 6), + '8' => array('parent' => 5, 'previous' => 7, 'upcoming' => 9, 'prior' => 7), + '9' => array('parent' => 1, 'previous' => 5, 'prior' => 8), ) ), ); @@ -79,6 +79,61 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { $this->assertEquals(is_array($expected_structure), $this->css->create_structure($input)); $this->assertEquals($expected_structure, $this->css->_structure); } + + function providerTestGetFields() { + return array( + array('parent', 1, false), + array('parent', 2, 1), + array('next', 3, 4), + array('next', 4, false), + array('previous', 4, 3), + array('previous', 3, false), + array('previous', 2, false), + array('upcoming', 2, 3), + array('upcoming', 3, 4), + array('valid', 1, array('upcoming')), + array('valid', 6, false), + ); + } + + /** + * @dataProvider providerTestGetFields + */ + function testGetFields($field, $category, $expected_value) { + $this->css->_structure = array( + '1' => array('upcoming' => 2), + '2' => array('parent' => 1, 'prior' => 1, 'upcoming' => 3), + '3' => array('parent' => 2, 'next' => 4, 'upcoming' => 4, 'prior' => 2), + '4' => array('parent' => 2, 'previous' => 3, 'prior' => 3) + ); + + $this->assertEquals($expected_value, $this->css->{$field}($category)); + } + + function providerTestGetValidStorylineNav() { + return array( + array(array(1), array('upcoming')), + array(array(1,2), false), + array(array(1,4), array('upcoming')), + array(array(2), array('prior', 'upcoming', 'next')), + array(array(3), array('prior', 'previous')), + ); + } + + /** + * @dataProvider providerTestGetValidStorylineNav + */ + function testGetValidStorylineNav($post_categories, $expected_navigation) { + wp_set_post_categories(1, $post_categories); + + $this->css->_structure = array( + '1' => array('upcoming' => 2), + '2' => array('prior' => 1, 'upcoming' => 3, 'next' => 3), + '3' => array('prior' => 2, 'previous' => 2) + ); + + $this->assertEquals($expected_navigation, $this->css->get_valid_storyline_nav(1)); + } } ?> \ No newline at end of file