From 7b1e8b73489109d55f20c105077e0bf935cd51ca Mon Sep 17 00:00:00 2001 From: John Bintz Date: Wed, 29 Jul 2009 19:13:20 -0400 Subject: [PATCH] more category and storyline modifications --- classes/ComicPress.inc | 59 ++++++++++++++++++++++++ partials/nav.inc | 61 ++++-------------------- test/ComicPressTest.php | 100 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 168 insertions(+), 52 deletions(-) diff --git a/classes/ComicPress.inc b/classes/ComicPress.inc index 88f048f..38b55f0 100644 --- a/classes/ComicPress.inc +++ b/classes/ComicPress.inc @@ -57,6 +57,8 @@ class ComicPress { $this->sort_comic_categories(); add_action('wp_head', array(&$this, 'wp_head')); + add_filter('comicpress_nav', array(&$this, 'comicpress_nav'), 10, 2); + add_filter('comicpress_nav_fields', array(&$this, 'comicpress_nav_fields')); if (current_user_can('edit_themes')) { if (!empty($this->comicpress_options['helpers'])) { @@ -77,6 +79,29 @@ class ComicPress { function is_multicomic() { return $this->comicpress_options['category_usage'] == "multicomic"; } + + function comicpress_nav($type, $content) { + return $type; + } + + function comicpress_nav_fields($nav_fields) { + $nav_fields = array( + 'first' => '‹‹ ' . __('First', 'comicpress'), + 'previous' => '‹ ' . __('Previous', 'comicpress'), + 'next' => __('Next', 'comicpress') . ' ›', + 'last' => __('Last', 'comicpress') . ' ››' + ); + + if ($this->needs_storyline_nav()) { + $nav_fields = array_merge( + array('prior' => '‹‹ ' . __('Prior Storyline', 'comicpress')), + $nav_fields, + array('upcoming' => __('Upcoming Storyline', 'comicpress') . ' ››') + ); + } + + return $nav_fields; + } function wp_head() { foreach ($this->additional_stylesheets as $uri) { ?> @@ -297,6 +322,40 @@ class ComicPress { $comic_posts['show_next'] = (!empty($comic_posts['next']) && (trim($comic_posts['last']->ID) != trim($comic_posts['next']->ID))); $comic_posts['show_last'] = (trim($post->ID) != trim($comic_posts['last']->ID)); + if ($this->needs_storyline_nav()) { + $comic_posts = array_merge($comic_posts, $this->get_storyline_nav_comics()); + } + + return $comic_posts; + } + + function get_storyline_nav_comics() { + $comic_posts = array('prior' => false, 'upcoming' => false); + foreach ($this->get_sorted_post_categories() as $category_id) { + $prev_next_categories = $this->get_previous_next_categories($category_id); + + foreach ($prev_next_categories as $master_id => $cat_list) { + foreach ($cat_list as $which => $id) { + switch ($which) { + case "previous": + $terminal_post = $this->get_last_comic($id); + $which_field = "prior"; + break; + case "next": + $terminal_post = $this->get_first_comic($id); + $which_field = "upcoming"; + break; + } + + if (is_object($terminal_post)) { + $comic_posts[$which_field] = $terminal_post; + $comic_posts["show_${which_field}"] =true; + } + + if (count($terminal_post) == 2) { break; } + } + } + } return $comic_posts; } diff --git a/partials/nav.inc b/partials/nav.inc index f2db2e1..9dec7c0 100644 --- a/partials/nav.inc +++ b/partials/nav.inc @@ -1,53 +1,10 @@ - '‹‹ ' . __('First', 'comicpress'), - 'previous' => '‹ ' . __('Previous', 'comicpress'), - 'next' => __('Next', 'comicpress') . ' ›', - 'last' => __('Last', 'comicpress') . ' ››' - ); - - if ($comicpress->needs_storyline_nav()) { - $nav_fields = array_merge( - array('prior' => '‹‹ ' . __('Prior Storyline', 'comicpress')), - $nav_fields, - array('upcoming' => __('Upcoming Storyline', 'comicpress') . ' ››') - ); - - foreach ($comicpress->get_sorted_post_categories() as $category_id) { - $prev_next_categories = $comicpress->get_previous_next_categories($category_id); - - foreach ($prev_next_categories as $master_id => $cat_list) { - foreach ($cat_list as $which => $id) { - switch ($which) { - case "previous": - $terminal_post = $comicpress->get_last_comic($id); - $which_field = "prior"; - break; - case "next": - $terminal_post = $comicpress->get_first_comic($id); - $which_field = "upcoming"; - break; - } - - if (is_object($terminal_post)) { - $nav_comics[$which_field] = $terminal_post; - $nav_comics["show_${which_field}"] =true; - } - - if (count($terminal_post) == 2) { break; } - } - } - } - } - - ?> - \ No newline at end of file diff --git a/test/ComicPressTest.php b/test/ComicPressTest.php index f328228..6641706 100644 --- a/test/ComicPressTest.php +++ b/test/ComicPressTest.php @@ -172,6 +172,106 @@ class ComicPressTest extends PHPUnit_Framework_TestCase { } } + function providerTestGetNavStorylineEnabled() { + return array( + array( + array('10'), + array( + '10' => array( + '10' => array('previous' => '9', 'next' => '11') + ) + ), + array( + '9' => true, + '11' => true + ), + array( + 'prior' => true, + 'upcoming' => true + ) + ), + array( + array('10', '20'), + array( + '10' => array( + '10' => array('previous' => '7', 'next' => '3') + ), + '20' => array( + '20' => array('previous' => '9', 'next' => '11') + ), + ), + array( + '9' => true, + '11' => true + ), + array( + 'prior' => true, + 'upcoming' => true + ) + ), + array( + array('10'), + array( + '10' => array( + '10' => array('previous' => '7', 'next' => '3') + ), + ), + array( + '15' => true, + '20' => true + ), + array( + 'prior' => false, + 'upcoming' => false + ) + ), + array( + array('10'), + array( + '10' => array( + '7' => array('previous' => '15', 'next' => '20') + ), + ), + array( + '15' => true, + '20' => true + ), + array( + 'prior' => true, + 'upcoming' => true + ) + ), + ); + } + + /** + * @dataProvider providerTestGetNavStorylineEnabled + */ + function testGetNavComicsStorylineEnabled($post_categories, $previous_next_categories, $terminal_comics, $expected_results) { + $cp = $this->getMock('ComicPress', array('get_sorted_post_categories', 'get_previous_next_categories', 'get_last_comic', 'get_first_comic')); + + $cp->expects($this->once())->method('get_sorted_post_categories')->will($this->returnValue($post_categories)); + $cp->expects($this->any())->method('get_previous_next_categories')->will($this->returnCallback(array(&$this, 'callbackGetPreviousNextCategories'))); + $cp->expects($this->any())->method('get_first_comic')->will($this->returnCallback(array(&$this, 'callbackGetTerminalComic'))); + $cp->expects($this->any())->method('get_last_comic')->will($this->returnCallback(array(&$this, 'callbackGetTerminalComic'))); + + $this->_previous_next_categories = $previous_next_categories; + $this->_terminal_comics = $terminal_comics; + + $result = $cp->get_storyline_nav_comics(); + foreach ($expected_results as $field => $value) { + $this->assertEquals($value, $result["show_${field}"]); + } + } + + function callbackGetPreviousNextCategories($id) { + return $this->_previous_next_categories[$id]; + } + + function callbackGetTerminalComic($id) { + return isset($this->_terminal_comics[$id]) ? (object)array() : false; + } + function providerTestGetPreviousNextCategories() { return array( array(