working on cleaning up graphical nav

This commit is contained in:
John Bintz 2009-10-21 20:11:22 -04:00
parent 92eec3b5b4
commit 10447b3d6c
5 changed files with 203 additions and 24 deletions

View File

@ -0,0 +1,92 @@
<?php
class ComicPressDBInterface {
var $_non_comic_categories, $_all_categories;
function ComicPressDBInterface() {}
function get_instance() {
static $instance;
if (!isset($instance)) { $instance = new ComicPressDBInterface(); }
return $instance;
}
function _get_categories() { return get_categories("hide_empty=0"); }
/**
* Set the comic categories for the current run of ComicPress.
*/
function set_comic_categories($categories) {
$this->_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); }
}
?>

View File

@ -37,6 +37,8 @@ class ComicPressNavigation {
}
}
}
return $nav;
}
}

View File

@ -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);
}
}
?>

View File

@ -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" => "<b>test</b>",
"next_title" => "test",
"archive_path" => "test",
), $w->update(array(
function testUpdateWidget() {
$result = $this->w->update(array(
"next" => "<b>test</b>",
"next_title" => "<b>test</b>",
"archive_path" => "<b>test</b>",
), 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));
}
}

View File

@ -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 {
<strong><?php echo $label; ?><strong>
</label>
<div class="comicpress-field">
<?php if (isset($title_defaults[$title_field])) { ?>
<input class="widefat comicpress-field"
<input class="widefat"
id="<?php echo $this->get_field_id($title_field); ?>"
name="<?php echo $this->get_field_name($title_field); ?>"
type="text"
value="<?php echo attribute_escape($instance[$title_field]); ?>" />
<?php } ?>
<?php if ($field == "archives") { ?>
<div>
<?php _e('Archive URL:', 'comicpress') ?>
<br />
<input class="widefat"
id="<?php echo $this->get_field_id('archive_path'); ?>"
name="<?php echo $this->get_field_name('archive_path'); ?>"
type="text"
value="<?php echo attribute_escape($instance['archive_path']); ?>" />
</div>
<?php } ?>
</label>
<?php
switch($field) {
case "archives": ?>
<div>
<?php _e('Archive URL:', 'comicpress') ?>
<br />
<input class="widefat"
id="<?php echo $this->get_field_id('archive_path'); ?>"
name="<?php echo $this->get_field_name('archive_path'); ?>"
type="text"
value="<?php echo attribute_escape($instance['archive_path']); ?>" />
</div>
<?php break;
}
?>
</div>
</div>
<?php }
@ -209,7 +259,7 @@ class widget_comicpress_graphical_navigation extends WP_Widget {
return function(e) {
var checkbox = jQuery('.comicpress-field[type=checkbox]', container).get(0);
if (checkbox) {
jQuery('.comicpress-field[type=text]', container)[checkbox.checked ? 'show' : 'hide'](immediate ? null : 'fast');
jQuery('div.comicpress-field', container)[checkbox.checked ? 'show' : 'hide'](immediate ? null : 'fast');
}
}
};