diff --git a/addons/Core/Core.inc b/addons/Core/Core.inc index 9ea6e85..490efb0 100644 --- a/addons/Core/Core.inc +++ b/addons/Core/Core.inc @@ -292,6 +292,8 @@ class ComicPressAddonCore extends ComicPressAddon { function render_admin() { $nonce = wp_create_nonce('comicpress'); $root_categories = $this->get_root_categories(); + $storyline = new ComicPressStoryline(); + $storyline->create_structure(get_option('comicpress-storyline-category-order')); include(dirname(__FILE__) . '/partials/options-admin.inc'); } @@ -397,7 +399,7 @@ class ComicPressAddonCore extends ComicPressAddon { */ function get_storyline_move_statuses() { $nodes_with_statuses = array(); - for ($i = 0, $il = count($this->comicpress->category_tree); $i < $il; ++$i) { + for ($i = 0, $il = count($this->comicpress->category_tree); $i < $il; ++$i) { $node = $this->comicpress->category_tree[$i]; $nodes_with_statuses[$node] = array(); $parts_count = count(explode("/", $node)); diff --git a/classes/ComicPressDBInterface.inc b/classes/ComicPressDBInterface.inc index 2019493..736bfbf 100644 --- a/classes/ComicPressDBInterface.inc +++ b/classes/ComicPressDBInterface.inc @@ -30,9 +30,8 @@ class ComicPressDBInterface { * Find the terminal post in a specific category. */ function get_terminal_post_in_category($category_id, $first = true) { - global $wp_query; - - $temp = $wp_query; $wp_query = null; + $this->_prepare_wp_query(); + $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"); @@ -40,7 +39,8 @@ class ComicPressDBInterface { if ($terminal_comic_query->have_posts()) { $post = reset($terminal_comic_query->posts); } - $wp_query = null; $wp_query = $temp; + + $this->_reset_wp_query(); return $post; } @@ -63,25 +63,34 @@ class ComicPressDBInterface { * 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; - } + 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); - $wp_query->is_single = $temp_single; - - if (!is_null($override_post)) { - $post = $temp_post; - } + $this->_reset_wp_query(); + if (!is_null($override_post)) { $post = $temp_post; } return empty($result) ? false : $result; } + + function _prepare_wp_query() { + global $wp_query; + + $this->is_single = $wp_query->is_single; + $this->in_the_loop = $wp_query->in_the_loop; + + $wp_query->is_single = $wp_query->in_the_loop = true; + } + + function _reset_wp_query() { + global $wp_query; + + $wp_query->is_single = $this->is_single; + $wp_query->in_the_loop = $this->in_the_loop; + } /** * Get the previous comic from the current one. diff --git a/classes/ComicPressNavigation.inc b/classes/ComicPressNavigation.inc index 859e92e..bc83178 100644 --- a/classes/ComicPressNavigation.inc +++ b/classes/ComicPressNavigation.inc @@ -35,10 +35,6 @@ class ComicPressNavigation { foreach ($valid as $field) { $nav["storyline-chapter-${field}"] = $this->_dbi->get_first_comic($this->_storyline->{$field}($category)); } - - if ($post->ID != $nav['storyline-chapter-current']->ID) { - $nav['storyline-chapter-previous'] = $nav['storyline-chapter-current']; - } } } diff --git a/classes/ComicPressStoryline.inc b/classes/ComicPressStoryline.inc index 6b782b5..8fe0b09 100644 --- a/classes/ComicPressStoryline.inc +++ b/classes/ComicPressStoryline.inc @@ -83,13 +83,12 @@ class ComicPressStoryline { return false; } - function current($id) { return $id; } function parent($id) { return $this->_get_field('parent', $id); } function previous($id) { return $this->_get_field('previous', $id); } function next($id) { return $this->_get_field('next', $id); } function valid($id) { if (isset($this->_structure[$id])) { - return array_merge(array_keys($this->_structure[$id]), array('current')); + return array_keys($this->_structure[$id]); } return false; } @@ -123,6 +122,47 @@ class ComicPressStoryline { function get_comic_categories() { return array_keys($this->_structure); } + + function get_simple_storyline() { + $simple_storyline = array('0' => array()); + foreach ($this->_structure as $category_id => $adjacents) { + $parent = 0; + if (isset($adjacents['parent'])) { $parent = $adjacents['parent']; } + if (!isset($simple_storyline[$parent])) { + $simple_storyline[$parent] = array(); + } + $simple_storyline[$parent][$category_id] = true; + } + + while (count($simple_storyline) > 0) { + $merge_found = false; + foreach ($simple_storyline as $parent => $children) { + $has_no_descendents = true; + foreach (array_keys($children) as $child) { + if (is_numeric($child)) { + if (isset($simple_storyline[$child])) { + $has_no_descendents = false; + break; + } + } + } + if ($has_no_descendents) { + $merge_found = $parent; break; + } + } + if ($merge_found !== false) { + foreach ($simple_storyline as $parent => $children) { + if (isset($children[$merge_found])) { + $simple_storyline[$parent][$merge_found] = $simple_storyline[$merge_found]; + unset($simple_storyline[$merge_found]); + break; + } + } + } + if (!$merge_found) { break; } + } + return $simple_storyline; + } } ?> \ No newline at end of file diff --git a/index.php b/index.php index 708c416..0a85f81 100644 --- a/index.php +++ b/index.php @@ -1,11 +1,6 @@ get_terminal_post_in_category(3, true)); - var_dump($dbi->get_terminal_post_in_category(3, false)); - exit(0); - comicpress_init(); $nav_comics = array(); diff --git a/test/ComicPressStorylineTest.php b/test/ComicPressStorylineTest.php index d97b641..c11ecf0 100644 --- a/test/ComicPressStorylineTest.php +++ b/test/ComicPressStorylineTest.php @@ -96,7 +96,7 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { array('parent', 2, 1), array('next', 2, 3), array('next', 3, 4), - array('valid', 1, array('next', 'current')), + array('valid', 1, array('next')), array('valid', 6, false), ); } @@ -117,11 +117,11 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { function providerTestGetValidNav() { return array( - array(array(1), array('next', 'current')), + array(array(1), array('next')), array(array(1,2), false), - array(array(1,4), array('next', 'current')), - array(array(2), array('previous', 'next', 'current')), - array(array(3), array('previous', 'current')), + array(array(1,4), array('next')), + array(array(2), array('previous', 'next')), + array(array(3), array('previous')), ); } @@ -159,6 +159,28 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { $this->assertEquals($expected_result, $css->get_valid_post_category(1)); } + + function testGetSimpleStoryline() { + $this->css->_structure = array( + '1' => array('next' => 2), + '2' => array('parent' => 1, 'previous' => 1, 'next' => 3), + '3' => array('parent' => 2, 'next' => 4, 'previous' => 2), + '4' => array('parent' => 2, 'previous' => 3) + ); + + $expected_result = array( + array( + '1' => array( + '2' => array( + '3' => true, + '4' => true + ) + ) + ) + ); + + $this->assertEquals($expected_result, $this->css->get_simple_storyline()); + } } ?> \ No newline at end of file diff --git a/addons/Core/test/CoreTest.php b/test/addons/CoreTest.php similarity index 98% rename from addons/Core/test/CoreTest.php rename to test/addons/CoreTest.php index ad7c0e6..4965499 100644 --- a/addons/Core/test/CoreTest.php +++ b/test/addons/CoreTest.php @@ -1,9 +1,9 @@