refactor storyline code some more

This commit is contained in:
John Bintz 2009-10-19 22:41:37 -04:00
parent 2517034936
commit 8b63f3a1fd
4 changed files with 45 additions and 41 deletions

View File

@ -187,9 +187,9 @@ class ComicPressComicPost {
sort($ids);
$requested_order = array_merge($requested_order, $ids);
}
$requested_order = array_merge($requested_order, array_diff($current_order, $requested_order));
foreach ($requested_order as $requested_comic) {
if (in_array($requested_comic, $current_order)) {
$new_order[$type][] = $requested_comic;
@ -199,7 +199,6 @@ class ComicPressComicPost {
update_post_meta($this->post->ID, 'comic_ordering', $new_order);
}
}
?>

View File

@ -1,37 +1,7 @@
<?php
class ComicPressNavigation {
function init($post, $storyline) {
$this->post = $post;
$this->storyline = $storyline;
}
/**
* 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_in_category($category_id) {
return $this->get_terminal_post_in_category($category_id);
}
/**
* Get the last comic in a category.
*/
function get_last_in_category($category_id) {
return $this->get_terminal_post_in_category($category_id, false);
}
}
?>

View File

@ -1,5 +1,7 @@
<?php
require_once('ComicPressDBInterface.inc');
class ComicPressStoryline {
var $_structure;
@ -72,7 +74,7 @@ class ComicPressStoryline {
}
return is_array($this->_structure);
}
function _get_field($field, $id) {
if (isset($this->_structure)) {
if (isset($this->_structure[$id])) {
@ -96,18 +98,31 @@ class ComicPressStoryline {
return false;
}
/**
* Get the valid navigation directions for a particular post.
*/
function get_valid_nav($post_id) {
$data = false;
if (($category = $this->get_valid_post_category($post_id)) !== false) {
return $this->valid($category);
}
return false;
}
/**
* Get the valid comic category for this post.
*/
function get_valid_post_category($post_id) {
$result = false;
foreach (wp_get_post_categories($post_id) as $category) {
if ($result = $this->valid($category)) {
if ($data) { return false; }
if ($this->valid($category)) {
if ($result) { return false; }
$data = $result;
$result = $category;
}
}
return $data;
}
return $result;
}
}
?>

View File

@ -134,6 +134,26 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase {
$this->assertEquals($expected_navigation, $this->css->get_valid_nav(1));
}
function providerTestGetValidPostCategory() {
return array(
array(array(1,2), false),
array(array(1,3), false),
array(array(1), 1),
);
}
/**
* @dataProvider providerTestGetValidPostCategory
*/
function testGetValidPostCategory($post_categories, $expected_result) {
$css = $this->getMock('ComicPressStoryline', array('valid'));
$css->expects($this->any())->method('valid')->will($this->returnValue(true));
wp_set_post_categories(1, $post_categories);
$this->assertEquals($expected_result, $css->get_valid_post_category(1));
}
}
?>