diff --git a/classes/ComicPressDBInterface.inc b/classes/ComicPressDBInterface.inc
index 4f5a323..41af87a 100644
--- a/classes/ComicPressDBInterface.inc
+++ b/classes/ComicPressDBInterface.inc
@@ -1,7 +1,7 @@
get_terminal_post(true, $include_categories);
}
/**
* Get the last comic in a category.
*/
- function get_last_comic($include_categories = null) {
+ function get_last_post($include_categories = null) {
return $this->get_terminal_post(false, $include_categories);
}
@@ -55,13 +55,13 @@ class ComicPressDBInterface {
* 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) {
+ function get_adjacent_post($categories = array(), $next = false, $override_post = null) {
global $post;
$this->_prepare_wp_query();
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);
+ $result = get_adjacent_post(false, implode(" and ", array_diff(get_all_category_ids(), $categories)), !$next);
$this->_reset_wp_query();
if (!is_null($override_post)) { $post = $temp_post; }
@@ -88,12 +88,12 @@ class ComicPressDBInterface {
/**
* 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); }
+ function get_previous_post($categories = array(), $override_post = null) { return $this->get_adjacent_post($categories, 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); }
+ function get_next_post($categories = array(), $override_post = null) { return $this->get_adjacent_post($categories, true, $override_post); }
}
?>
\ No newline at end of file
diff --git a/classes/ComicPressStoryline.inc b/classes/ComicPressStoryline.inc
index bac9b8a..d59255d 100644
--- a/classes/ComicPressStoryline.inc
+++ b/classes/ComicPressStoryline.inc
@@ -412,14 +412,18 @@ class ComicPressStoryline {
return false;
}
- function &_include($method, $parameter) {
- $this->_category_search = array_unique(array_merge($this->_category_search, $this->{$method}($parameter)));
+ function &_include() {
+ $args = func_get_args();
+ $method = array_shift($args);
+ $this->_category_search = array_unique(array_merge($this->_category_search, call_user_func_array(array($this, $method), $args)));
sort($this->_category_search);
return $this;
}
- function &_exclude($method, $parameter) {
- $this->_category_search = array_diff($this->_category_search, $this->{$method}($parameter));
+ function &_exclude() {
+ $args = func_get_args();
+ $method = array_shift($args);
+ $this->_category_search = array_diff($this->_category_search, call_user_func_array(array($this, $method), $args));
sort($this->_category_search);
return $this;
}
@@ -479,6 +483,56 @@ class ComicPressStoryline {
return $this->_exclude('_find_level', $id);
}
+ function _find_post_category($post = null) {
+ $found = array();
+
+ $id = null;
+ if (is_object($post)) {
+ if (isset($post->ID)) { $id = $post->ID; }
+ } else {
+ if (is_numeric($post)) { $id = $post; }
+ }
+
+ if (!is_null($id)) {
+ if (count($categories = wp_get_post_categories($id)) == 1) {
+ $found = $categories;
+ }
+ }
+ return $found;
+ }
+
+ function &include_post_category($post = null) {
+ return $this->_include('_find_post_category', $post);
+ }
+
+ function &exclude_post_category($post = null) {
+ return $this->_exclude('_find_post_category', $post);
+ }
+
+ function _find_adjacent($category = null, $next = false) {
+ $found = array();
+
+ if (!is_null($category)) {
+ if (isset($this->_structure[$category])) {
+ $field = $next ? 'next' : 'previous';
+ if (isset($this->_structure[$category][$field])) {
+ $found = array($this->_structure[$category][$field]);
+ }
+ }
+ }
+
+ return $found;
+ }
+
+ function &include_adjacent($category = null, $next = false) {
+ return $this->_include('_find_adjacent', $category, $next);
+ }
+
+ function &exclude_adjacent($category = null, $next = false) {
+ return $this->_exclude('_find_adjacent', $category, $next);
+ }
+
+
function end_search() {
$result = $this->_category_search;
$this->_category_search = array();
@@ -486,6 +540,8 @@ class ComicPressStoryline {
}
function build_from_restrictions($restrictions = null) {
+ global $post;
+
$this->read_from_options()->exclude_all();
$include_all = true;
@@ -497,17 +553,33 @@ class ComicPressStoryline {
if (!$include_all) {
foreach ($restrictions as $type => $list) {
+ if (substr($type, 0, 1) == "!") {
+ $method_root = 'exclude';
+ $method_type = substr($type, 1);
+ } else {
+ $method_root = 'include';
+ $method_type = $type;
+ }
+
foreach ((array)$list as $restriction) {
- switch ($type) {
- case 'child_of':
- $this->include_children($restriction);
+ $method = '';
+ $args = array($restriction);
+ switch ($method_type) {
+ case 'child_of': $method = 'children'; break;
+ case 'from_post': $method = 'post_category'; break;
+ case 'previous':
+ $method = 'adjacent';
+ $args[] = false;
break;
- case 'only':
- $this->include_only($restriction);
- break;
- case 'level':
- $this->include_level($restriction);
+ case 'next':
+ $method = 'adjacent';
+ $args[] = true;
break;
+ default:
+ $method = $method_type; break;
+ }
+ if (!empty($method)) {
+ call_user_func_array(array($this, "${method_root}_${method}"), $args);
}
}
}
diff --git a/functions.php b/functions.php
index 801ee01..240ce5e 100644
--- a/functions.php
+++ b/functions.php
@@ -54,17 +54,27 @@ function finish_comicpress() {
/**
* Protect global $post and $wp_query.
*/
-function protect_query() {
+function Protect() {
global $post, $wp_query, $__post, $__wp_query;
$__post = $post;
$__wp_query = $wp_query;
}
+/**
+ * Temporarily restore the global $post variable and set it up for use.
+ */
+function Restore() {
+ global $post, $__post;
+
+ $post = $__post;
+ setup_postdata($post);
+}
+
/**
* Restore global $post and $wp_query.
*/
-function unprotect_query() {
+function Unprotect() {
global $post, $wp_query, $__post, $__wp_query;
$post = $__post;
@@ -73,49 +83,51 @@ function unprotect_query() {
$__post = $__wp_query = null;
}
-function retrieve_storyline_post($which, $restrictions = null, $override_post = null) {
+function R($which, $restrictions = null, $override_post = null) {
global $post;
+ $post_to_use = !is_null($override_post) ? $override_post : $post;
$storyline = new ComicPressStoryline();
- $storyline->read_from_options()->exclude_all();
- if (!is_null($restrictions)) {
- if (is_array($restrictions)) {
- foreach ($restrictions as $type => $list) {
- switch ($type) {
- case 'child_of':
- foreach ($list as $restriction) { $storyline->include_children($restriction); }
- break;
- }
- }
+ if (is_string($restrictions)) {
+ switch ($restrictions) {
+ case 'from_post':
+ $restrictions = array('from_post' => $post_to_use);
+ break;
}
- } else {
- $storyline->include_all();
}
- $categories = $storyline->end_search();
-
- var_dump($categories);
+ $categories = $storyline->build_from_restrictions($restrictions);
$dbi = ComicPressDBInterface::get_instance();
$new_post = false;
switch ($which) {
- case 'first':
- case 'last':
- case 'next':
- case 'previous':
- $method = "get_${which}_post";
- if (method_exists($dbi, $method)) {
- $new_post = $dbi->{$method}($categories);
- }
- break;
+ case 'first': $new_post = $dbi->get_first_post($categories); break;
+ case 'last': $new_post = $dbi->get_last_post($categories); break;
+ case 'next': $new_post = $dbi->get_next_post($categories, $post_to_use); break;
+ case 'previous': $new_post = $dbi->get_previous_post($categories, $post_to_use); break;
}
return $new_post;
}
+function RT($which, $restrictions = null, $override_post = null) {
+ global $post, $__post;
+ if (!empty($override_post)) {
+ $post_to_use = $override_post;
+ } else {
+ $post_to_use = (!empty($__post)) ? $__post : $post;
+ }
+
+ if (($new_post = R($which, $restrictions, $post_to_use)) !== false) {
+ $post = $new_post;
+ setup_postdata($post);
+ }
+ return $post;
+}
+
/**
* Display the list of Storyline categories.
*/
diff --git a/index.php b/index.php
index f93915d..2a899a6 100644
--- a/index.php
+++ b/index.php
@@ -1,7 +1,9 @@
array('amoc', 'dawns-dictionary-drama')));
+ $previous_post = R('previous');
+ $next_post = R('next');
- var_dump($current_post);
+ var_dump($previous_post);
+ var_dump($next_post);
finish_comicpress();
?>
\ No newline at end of file
diff --git a/single.php b/single.php
index df5040d..b320837 100644
--- a/single.php
+++ b/single.php
@@ -1,37 +1,19 @@
- ';
- comicpress_init();
+ Protect();
- ob_start();
+ RT('first', 'from_post'); the_title(); echo '
';
+ RT('previous', 'from_post'); the_title(); echo '
';
+ RT('previous'); the_title(); echo '
';
+ Restore(); the_title(); echo '
';
+ RT('next'); the_title(); echo '
';
+ RT('next', 'from_post'); the_title(); echo '
';
+ RT('last', 'from_post'); the_title(); echo '
';
- if (have_posts()) {
- the_post();
- if (in_comic_category()) { include_partial('single-display-comic'); }
- }
- rewind_posts();
-
- $comic = ob_get_clean();
+ Unprotect();
- ob_start();
+ the_title(); echo '
';
- $nav_comics = $comicpress->get_nav_comics();
-
- if (have_posts()) {
- while (have_posts()) { the_post();
- if (in_comic_category()) {
- if ($comicpress->comicpress_options['comic_space'] == "comic_only") {
- include_partial('single-comic-post');
- }
- } else {
- include_partial('single-blog-post');
- }
- }
- } else {
- include_partial('single-no-matches');
- }
-
- $content = ob_get_clean();
-
- include(get_template_directory() . '/layouts/' . $comicpress->comicpress_options['layout']);
+ finish_comicpress();
?>
\ No newline at end of file
diff --git a/test/ComicPressStorylineTest.php b/test/ComicPressStorylineTest.php
index 43a0d0d..0310634 100644
--- a/test/ComicPressStorylineTest.php
+++ b/test/ComicPressStorylineTest.php
@@ -468,6 +468,52 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase {
$this->assertEquals($expected_return, $this->css->_find_level($id));
}
+ function providerTestFindPostCategory() {
+ return array(
+ array(array(1), array(1)),
+ array(array(1,2), array())
+ );
+ }
+
+ /**
+ * @dataProvider providerTestFindPostCategory
+ */
+ function testFindPostCategory($post_categories, $expected_return) {
+ $this->css->_structure = array(
+ '1' => array('next' => 2, 'level' => 1),
+ '2' => array('parent' => 1, 'previous' => 1, 'next' => 3, 'level' => 2),
+ '3' => array('parent' => 2, 'next' => 4, 'previous' => 2, 'level' => 3),
+ '4' => array('parent' => 2, 'previous' => 3, 'level' => 3)
+ );
+
+ wp_set_post_categories(1, $post_categories);
+
+ $this->assertEquals($expected_return, $this->css->_find_post_category(1));
+ }
+
+ function providerTestFindAdjacentCategory() {
+ return array(
+ array(3, false, array(2)),
+ array(3, true, array(4)),
+ array(1, false, array()),
+ array(4, true, array()),
+ );
+ }
+
+ /**
+ * @dataProvider providerTestFindAdjacentCategory
+ */
+ function testFindAdjacentCategory($category, $which, $expected_return) {
+ $this->css->_structure = array(
+ '1' => array('next' => 2, 'level' => 1),
+ '2' => array('parent' => 1, 'previous' => 1, 'next' => 3, 'level' => 2),
+ '3' => array('parent' => 2, 'next' => 4, 'previous' => 2, 'level' => 3),
+ '4' => array('parent' => 2, 'previous' => 3, 'level' => 3)
+ );
+
+ $this->assertEquals($expected_return, $this->css->_find_adjacent($category, $which));
+ }
+
function providerTestBuildFromRestrictions() {
return array(
array(
@@ -493,7 +539,19 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase {
array(
array('level' => 1),
array(1,4,7)
- )
+ ),
+ array(
+ array('from_post' => 1),
+ array(3)
+ ),
+ array(
+ array('previous' => 3),
+ array(2)
+ ),
+ array(
+ array('next' => 3),
+ array(4)
+ )
);
}
@@ -501,8 +559,13 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase {
* @dataProvider providerTestBuildFromRestrictions
*/
function testBuildFromRestrictions($restrictions, $expected_categories) {
+ global $post;
+
$this->css->set_flattened_storyline('0/1,0/1/2,0/1/3,0/4,0/4/5,0/4/6,0/7');
+ wp_set_post_categories(1, array(3));
+ $post = (object)array('ID' => 1);
+
$this->assertEquals($expected_categories, $this->css->build_from_restrictions($restrictions));
}
}