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(); $adjacents_by_parent = array();
if (is_string($structure)) {
$structure = explode(',', $structure);
}
if (is_array($structure)) { if (is_array($structure)) {
$is_valid = true; $is_valid = true;
foreach ($structure as $branch) { foreach ($structure as $branch) {
@ -61,7 +65,7 @@ class ComicPressStoryline {
} }
} }
} }
$this->_structure = $new_structure; $this->_structure = $new_structure;
} }
} }
@ -114,6 +118,10 @@ class ComicPressStoryline {
} }
return $result; 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'); require_once(dirname(__FILE__) . '/../../widgets/graphical-navigation.php');
class GraphicalNavigationTest extends PHPUnit_Framework_TestCase { class GraphicalNavigationTest extends PHPUnit_Framework_TestCase {
function testUpdateWidget() { function setUp() {
$w = new widget_comicpress_graphical_navigation(); _reset_wp();
$this->w = new widget_comicpress_graphical_navigation();
}
$this->assertEquals(array( function testUpdateWidget() {
"next" => "<b>test</b>", $result = $this->w->update(array(
"next_title" => "test",
"archive_path" => "test",
), $w->update(array(
"next" => "<b>test</b>", "next" => "<b>test</b>",
"next_title" => "<b>test</b>", "next_title" => "<b>test</b>",
"archive_path" => "<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 { class widget_comicpress_graphical_navigation extends WP_Widget {
function widget_comicpress_graphical_navigation() { function widget_comicpress_graphical_navigation() {
$widget_ops = array('classname' => 'widget_comicpress_graphical_navigation', 'description' => __('Displays Graphical Navigation Buttons. (used in comic sidebars)','comicpress') ); $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); $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) { function widget($args, $instance) {
global $wp_query, $post; global $post;
if (is_home() || is_single()) { 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(); $this_permalink = get_permalink();
@ -179,26 +224,31 @@ class widget_comicpress_graphical_navigation extends WP_Widget {
<strong><?php echo $label; ?><strong> <strong><?php echo $label; ?><strong>
</label> </label>
<div class="comicpress-field">
<?php if (isset($title_defaults[$title_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); ?>" id="<?php echo $this->get_field_id($title_field); ?>"
name="<?php echo $this->get_field_name($title_field); ?>" name="<?php echo $this->get_field_name($title_field); ?>"
type="text" type="text"
value="<?php echo attribute_escape($instance[$title_field]); ?>" /> value="<?php echo attribute_escape($instance[$title_field]); ?>" />
<?php } ?> <?php } ?>
<?php if ($field == "archives") { ?> <?php
<div> switch($field) {
<?php _e('Archive URL:', 'comicpress') ?> case "archives": ?>
<br /> <div>
<input class="widefat" <?php _e('Archive URL:', 'comicpress') ?>
id="<?php echo $this->get_field_id('archive_path'); ?>" <br />
name="<?php echo $this->get_field_name('archive_path'); ?>" <input class="widefat"
type="text" id="<?php echo $this->get_field_id('archive_path'); ?>"
value="<?php echo attribute_escape($instance['archive_path']); ?>" /> name="<?php echo $this->get_field_name('archive_path'); ?>"
</div> type="text"
<?php } ?> value="<?php echo attribute_escape($instance['archive_path']); ?>" />
</label> </div>
<?php break;
}
?>
</div>
</div> </div>
<?php } <?php }
@ -209,7 +259,7 @@ class widget_comicpress_graphical_navigation extends WP_Widget {
return function(e) { return function(e) {
var checkbox = jQuery('.comicpress-field[type=checkbox]', container).get(0); var checkbox = jQuery('.comicpress-field[type=checkbox]', container).get(0);
if (checkbox) { 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');
} }
} }
}; };