From a4750dc38db188eb9050185c42004796686f11cf Mon Sep 17 00:00:00 2001 From: John Bintz Date: Wed, 4 Nov 2009 07:17:45 -0500 Subject: [PATCH] add additional grouping wrapping --- test/widgets/GraphicalNavigationTest.php | 45 +++++++++++++++++++++- widgets/graphical-navigation.php | 48 +++++++++++++++++++++++- 2 files changed, 90 insertions(+), 3 deletions(-) diff --git a/test/widgets/GraphicalNavigationTest.php b/test/widgets/GraphicalNavigationTest.php index 082a420..a9ce770 100644 --- a/test/widgets/GraphicalNavigationTest.php +++ b/test/widgets/GraphicalNavigationTest.php @@ -1,6 +1,7 @@ w = new WidgetComicPressGraphicalStorylineNavigation(); } + /** + * @covers WidgetComicPressGraphicalStorylineNavigation::update + */ function testUpdateWidget() { $result = $this->w->update(array( "next" => "test", @@ -38,6 +42,7 @@ class GraphicalNavigationTest extends PHPUnit_Framework_TestCase { /** * @dataProvider providerTestIsNavLinkVisible + * @covers WidgetComicPressGraphicalStorylineNavigation::_will_display_nav_link */ function testIsNavLinkVisible($which, $current_id, $target_id, $expected_result) { $current = (object)array('ID' => $current_id); @@ -45,6 +50,44 @@ class GraphicalNavigationTest extends PHPUnit_Framework_TestCase { $this->assertEquals($expected_result, $this->w->_will_display_nav_link($which, $current, $target)); } + + function providerTestGroupNavigationButtons() { + return array( + array(array(), array()), + array( + array('one' => 'will be left'), + array( + 'left' => array('one' => 'will be left') + ) + ), + array( + array('four' => 'will be right'), + array( + 'right' => array('four' => 'will be right') + ) + ), + array( + array('seven' => 'will be center'), + array( + 'center' => array('seven' => 'will be center') + ) + ), + ); + } + + /** + * @dataProvider providerTestGroupNavigationButtons + * @covers WidgetComicPressGraphicalStorylineNavigation::_group_navigation_buttons + */ + function testGroupNavigationButtons($buttons, $expected_grouping) { + _set_filter_expectation('comicpress_navigation_grouping_details', array( + 'left' => array('one', 'two', 'three'), + 'center' => true, + 'right' => array('four', 'five', 'six'), + )); + + $this->assertEquals($expected_grouping, $this->w->_group_navigation_buttons($buttons, array())); + } } ?> \ No newline at end of file diff --git a/widgets/graphical-navigation.php b/widgets/graphical-navigation.php index 78bb5e6..ce2f1e8 100644 --- a/widgets/graphical-navigation.php +++ b/widgets/graphical-navigation.php @@ -23,6 +23,7 @@ class WidgetComicPressGraphicalStorylineNavigation extends WP_Widget { add_filter('comicpress_display_navigation_order', array(&$this, 'comicpress_display_navigation_order')); add_filter('comicpress_display_navigation_link', array(&$this, 'comicpress_display_navigation_link'), 10, 5); add_filter('comicpress_wrap_navigation_buttons', array(&$this, 'comicpress_wrap_navigation_buttons'), 10, 2); + add_filter('comicpress_navigation_grouping_details', array(&$this, 'comicpress_navigation_grouping_details')); // these two need to be moved one level up add_filter('comicpress_get_random_link_url', array(&$this, 'comicpress_get_random_link_url')); @@ -162,6 +163,42 @@ class WidgetComicPressGraphicalStorylineNavigation extends WP_Widget { ); } + function comicpress_navigation_grouping_details($details = array()) { + return array( + 'comic_navi_left' => array('first', 'previous', 'story_prev_in', 'story_prev'), + 'comic_navi_center' => true, + 'comic_navi_right' => array('story_next', 'story_next_in', 'next', 'last') + ); + } + + function _group_navigation_buttons($buttons = array(), $grouped_buttons = array()) { + $grouping_hash = array(); + + $default_group = null; + foreach (apply_filters('comicpress_navigation_grouping_details', array()) as $group => $members) { + if ($members === true) { + $default_group = $group; + } else { + foreach ($members as $member) { $grouping_hash[$member] = $group; } + } + } + + if (is_null($default_group)) { + trigger_error('No default group defined for filter comicpress_navigation_grouping_details', E_USER_WARNING); + } + + $groups = array(); + foreach ($buttons as $key => $button) { + $group = isset($grouping_hash[$key]) ? $grouping_hash[$key] : $default_group; + if (!empty($group)) { + if (!isset($groups[$group])) { $groups[$group] = array(); } + $groups[$group][$key] = $button; + } + } + + return $groups; + } + /** * Wrap navigation buttons in a holder. * @param string|array $buttons The buttons to wrap. @@ -169,7 +206,14 @@ class WidgetComicPressGraphicalStorylineNavigation extends WP_Widget { */ function comicpress_wrap_navigation_buttons($buttons = '', $content = '') { $buttons_text = $buttons; - if (is_array($buttons)) { $buttons_text = implode('', $buttons); } + if (is_array($buttons)) { + $output = array(); + foreach ($this->_group_navigation_buttons($buttons) as $group => $grouped_buttons) { + $output[] = '
' . implode('', array_values($grouped_buttons)) . '
'; + } + + $buttons_text = implode('', $output); + } ob_start(); ?>
@@ -214,7 +258,7 @@ class WidgetComicPressGraphicalStorylineNavigation extends WP_Widget { $target_post = null; if (isset($post_nav[$target_post_nav])) { $target_post = $post_nav[$target_post_nav]; } - $nav_links[] = end(apply_filters('comicpress_display_navigation_link', $order, $post, $target_post, $instance, '')); + $nav_links[$order] = end(apply_filters('comicpress_display_navigation_link', $order, $post, $target_post, $instance, '')); } }