From 10447b3d6cd8d98197b9ae14363e370626458315 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Wed, 21 Oct 2009 20:11:22 -0400 Subject: [PATCH] working on cleaning up graphical nav --- classes/ComicPressDBInterface.inc | 92 ++++++++++++++++++++++++ classes/ComicPressNavigation.inc | 2 + classes/ComicPressStoryline.inc | 10 ++- test/widgets/GraphicalNavigationTest.php | 43 ++++++++--- widgets/graphical-navigation.php | 80 +++++++++++++++++---- 5 files changed, 203 insertions(+), 24 deletions(-) create mode 100644 classes/ComicPressDBInterface.inc diff --git a/classes/ComicPressDBInterface.inc b/classes/ComicPressDBInterface.inc new file mode 100644 index 0000000..0e42de9 --- /dev/null +++ b/classes/ComicPressDBInterface.inc @@ -0,0 +1,92 @@ +_all_categories = get_all_category_ids(); + $this->_non_comic_categories = array_values(array_diff($this->_all_categories, $categories)); + } + + function _get_categories_to_exclude($category = null) { + return (is_null($category)) ? $this->_non_comic_categories : array_values(array_diff($this->_all_categories, array($category))); + } + + /** + * 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_comic($category_id) { + return $this->get_terminal_post_in_category($category_id); + } + + /** + * Get the last comic in a category. + */ + function get_last_comic($category_id) { + return $this->get_terminal_post_in_category($category_id, false); + } + + /** + * Get the comic post adjacent to the current comic. + * Wrapper around get_adjacent_post(). Don't unit test this method. + */ + function get_adjacent_comic($category, $next = false, $override_post = null) { + global $wp_query, $post; + $temp_single = $wp_query->is_single; + $wp_query->is_single = true; + + if (!is_null($override_post)) { + $temp_post = $post; + $post = $override_post; + } + + $result = get_adjacent_post(false, implode(" and ", $this->_get_categories_to_exclude($category)), !$next); + + $wp_query->is_single = $temp_single; + + if (!is_null($override_post)) { + $post = $temp_post; + } + + return empty($result) ? false : $result; + } + + /** + * Get the previous comic from the current one. + */ + function get_previous_comic($category = null, $override_post = null) { return $this->get_adjacent_comic($category, false, $override_post); } + + /** + * Get the next comic from the current one. + */ + function get_next_comic($category = null, $override_post = null) { return $this->get_adjacent_comic($category, true, $override_post); } +} + +?> \ No newline at end of file diff --git a/classes/ComicPressNavigation.inc b/classes/ComicPressNavigation.inc index fa37995..bc83178 100644 --- a/classes/ComicPressNavigation.inc +++ b/classes/ComicPressNavigation.inc @@ -37,6 +37,8 @@ class ComicPressNavigation { } } } + + return $nav; } } diff --git a/classes/ComicPressStoryline.inc b/classes/ComicPressStoryline.inc index 4acc4db..aa8896b 100644 --- a/classes/ComicPressStoryline.inc +++ b/classes/ComicPressStoryline.inc @@ -18,6 +18,10 @@ class ComicPressStoryline { $adjacents_by_parent = array(); + if (is_string($structure)) { + $structure = explode(',', $structure); + } + if (is_array($structure)) { $is_valid = true; foreach ($structure as $branch) { @@ -61,7 +65,7 @@ class ComicPressStoryline { } } } - + $this->_structure = $new_structure; } } @@ -114,6 +118,10 @@ class ComicPressStoryline { } return $result; } + + function get_comic_categories() { + return array_keys($this->_structure); + } } ?> \ No newline at end of file diff --git a/test/widgets/GraphicalNavigationTest.php b/test/widgets/GraphicalNavigationTest.php index a9ec67c..8c5ba14 100644 --- a/test/widgets/GraphicalNavigationTest.php +++ b/test/widgets/GraphicalNavigationTest.php @@ -5,18 +5,45 @@ require_once('PHPUnit/Framework.php'); require_once(dirname(__FILE__) . '/../../widgets/graphical-navigation.php'); class GraphicalNavigationTest extends PHPUnit_Framework_TestCase { - function testUpdateWidget() { - $w = new widget_comicpress_graphical_navigation(); + function setUp() { + _reset_wp(); + $this->w = new widget_comicpress_graphical_navigation(); + } - $this->assertEquals(array( - "next" => "test", - "next_title" => "test", - "archive_path" => "test", - ), $w->update(array( + function testUpdateWidget() { + $result = $this->w->update(array( "next" => "test", "next_title" => "test", "archive_path" => "test", - ), array())); + ), array()); + + foreach (array( + "next" => "on", + "next_title" => "test", + "archive_path" => "test", + ) as $field => $expected_value) { + $this->assertEquals($expected_value, $result[$field]); + } + } + + function providerTestIsNavLinkVisible() { + return array( + array('first', 1, 2, true), + array('first', 1, 1, false), + array('last', 1, 2, true), + array('last', 1, 1, false), + array('prev', 1, 2, true), + ); + } + + /** + * @dataProvider providerTestIsNavLinkVisible + */ + function testIsNavLinkVIsible($which, $current_id, $target_id, $expected_result) { + $current = (object)array('ID' => $current_id); + $target = (object)array('ID' => $target_id); + + $this->assertEquals($expected_result, $this->w->_will_display_nav_link($which, $current, $target)); } } diff --git a/widgets/graphical-navigation.php b/widgets/graphical-navigation.php index 503cf60..112893c 100644 --- a/widgets/graphical-navigation.php +++ b/widgets/graphical-navigation.php @@ -9,16 +9,61 @@ Author URI: http://webcomicplanet.com/ */ +require_once(dirname(__FILE__) . '/../classes/ComicPressNavigation.inc'); + class widget_comicpress_graphical_navigation extends WP_Widget { function widget_comicpress_graphical_navigation() { $widget_ops = array('classname' => 'widget_comicpress_graphical_navigation', 'description' => __('Displays Graphical Navigation Buttons. (used in comic sidebars)','comicpress') ); $this->WP_Widget('graphicalnavigation', __('Comic Navigation','comicpress'), $widget_ops); } + function comicpress_display_navigation_link($which, $current, $target, $title, $content = '') { + switch ($which) { + case 'first': + + } + } + + /** + * Returns true if the combination of target and current post will show or hide this nav link. + * Different from whether or not a user explicitly hid this link. + * @param string $which The link to test. + * @param object $current The currently visible post. + * @param object $target The target post to comare to. + * @return boolean True if this link should be visible. + */ + function _will_display_nav_link($which, $current, $target) { + switch ($which) { + case 'first': + case 'last': + return ($target->ID != $current->ID); + default: + return true; + } + } + + function comicpress_display_navigation_order($order = array()) { + return array( + 'first', 'storyline-previous', 'previous', 'archives', 'random', 'comictitle', 'comments', 'buyprint', 'next', 'storynext', 'last' + ); + } + function widget($args, $instance) { - global $wp_query, $post; + global $post; if (is_home() || is_single()) { + $storyline = new ComicPressStoryline(); + $storyline->create_structure(get_option('comicpress-storyline-category-order')); + + $dbi = ComicPressDBInterface::get_instance(); + $dbi->set_comic_categories($storyline->get_comic_categories()); + + $navigation = new ComicPressNavigation(); + $navigation->init($storyline); + + $storyline_to_nav_mapping = array( + + ); $this_permalink = get_permalink(); @@ -179,26 +224,31 @@ class widget_comicpress_graphical_navigation extends WP_Widget { +
- - -
- -
- -
- - + +
+ +
+ +
+ +