working on improved storyline structure widget

This commit is contained in:
John Bintz 2009-10-31 16:04:29 -04:00
parent 8fa6ba4517
commit 72fc0d356b
7 changed files with 101 additions and 37 deletions

View File

@ -292,6 +292,8 @@ class ComicPressAddonCore extends ComicPressAddon {
function render_admin() { function render_admin() {
$nonce = wp_create_nonce('comicpress'); $nonce = wp_create_nonce('comicpress');
$root_categories = $this->get_root_categories(); $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'); include(dirname(__FILE__) . '/partials/options-admin.inc');
} }

View File

@ -30,9 +30,8 @@ class ComicPressDBInterface {
* Find the terminal post in a specific category. * Find the terminal post in a specific category.
*/ */
function get_terminal_post_in_category($category_id, $first = true) { function get_terminal_post_in_category($category_id, $first = true) {
global $wp_query; $this->_prepare_wp_query();
$temp = $wp_query; $wp_query = null;
$sort_order = $first ? "asc" : "desc"; $sort_order = $first ? "asc" : "desc";
$terminal_comic_query = new WP_Query(); $terminal_comic_query = new WP_Query();
$terminal_comic_query->query("showposts=1&order=${sort_order}&cat=${category_id}&status=publish"); $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()) { if ($terminal_comic_query->have_posts()) {
$post = reset($terminal_comic_query->posts); $post = reset($terminal_comic_query->posts);
} }
$wp_query = null; $wp_query = $temp;
$this->_reset_wp_query();
return $post; return $post;
} }
@ -63,26 +63,35 @@ class ComicPressDBInterface {
* Wrapper around get_adjacent_post(). Don't unit test this method. * Wrapper around get_adjacent_post(). Don't unit test this method.
*/ */
function get_adjacent_comic($category, $next = false, $override_post = null) { function get_adjacent_comic($category, $next = false, $override_post = null) {
global $wp_query, $post; global $post;
$temp_single = $wp_query->is_single;
$wp_query->is_single = true;
if (!is_null($override_post)) { $this->_prepare_wp_query();
$temp_post = $post; if (!is_null($override_post)) { $temp_post = $post; $post = $override_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 ", $this->_get_categories_to_exclude($category)), !$next);
$wp_query->is_single = $temp_single; $this->_reset_wp_query();
if (!is_null($override_post)) { $post = $temp_post; }
if (!is_null($override_post)) {
$post = $temp_post;
}
return empty($result) ? false : $result; 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. * Get the previous comic from the current one.
*/ */

View File

@ -35,10 +35,6 @@ class ComicPressNavigation {
foreach ($valid as $field) { foreach ($valid as $field) {
$nav["storyline-chapter-${field}"] = $this->_dbi->get_first_comic($this->_storyline->{$field}($category)); $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'];
}
} }
} }

View File

@ -83,13 +83,12 @@ class ComicPressStoryline {
return false; return false;
} }
function current($id) { return $id; }
function parent($id) { return $this->_get_field('parent', $id); } function parent($id) { return $this->_get_field('parent', $id); }
function previous($id) { return $this->_get_field('previous', $id); } function previous($id) { return $this->_get_field('previous', $id); }
function next($id) { return $this->_get_field('next', $id); } function next($id) { return $this->_get_field('next', $id); }
function valid($id) { function valid($id) {
if (isset($this->_structure[$id])) { if (isset($this->_structure[$id])) {
return array_merge(array_keys($this->_structure[$id]), array('current')); return array_keys($this->_structure[$id]);
} }
return false; return false;
} }
@ -123,6 +122,47 @@ class ComicPressStoryline {
function get_comic_categories() { function get_comic_categories() {
return array_keys($this->_structure); 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;
}
} }
?> ?>

View File

@ -1,11 +1,6 @@
<?php <?php
global $comicpress, $post, $nav_comics; global $comicpress, $post, $nav_comics;
$dbi = ComicPressDBInterface::get_instance();
var_dump($dbi->get_terminal_post_in_category(3, true));
var_dump($dbi->get_terminal_post_in_category(3, false));
exit(0);
comicpress_init(); comicpress_init();
$nav_comics = array(); $nav_comics = array();

View File

@ -96,7 +96,7 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase {
array('parent', 2, 1), array('parent', 2, 1),
array('next', 2, 3), array('next', 2, 3),
array('next', 3, 4), array('next', 3, 4),
array('valid', 1, array('next', 'current')), array('valid', 1, array('next')),
array('valid', 6, false), array('valid', 6, false),
); );
} }
@ -117,11 +117,11 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase {
function providerTestGetValidNav() { function providerTestGetValidNav() {
return array( return array(
array(array(1), array('next', 'current')), array(array(1), array('next')),
array(array(1,2), false), array(array(1,2), false),
array(array(1,4), array('next', 'current')), array(array(1,4), array('next')),
array(array(2), array('previous', 'next', 'current')), array(array(2), array('previous', 'next')),
array(array(3), array('previous', 'current')), 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)); $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());
}
} }
?> ?>

View File

@ -1,9 +1,9 @@
<?php <?php
require_once('PHPUnit/Framework.php'); require_once('PHPUnit/Framework.php');
require_once(dirname(__FILE__) . '/../../../../mockpress/mockpress.php'); require_once('MockPress/mockpress.php');
require_once(dirname(__FILE__) . '/../../../classes/ComicPressAddon.inc'); require_once(dirname(__FILE__) . '/../../classes/ComicPressAddon.inc');
require_once(dirname(__FILE__) . '/../Core.inc'); require_once(dirname(__FILE__) . '/../../addons/Core/Core.inc');
class CoreTest extends PHPUnit_Framework_TestCase { class CoreTest extends PHPUnit_Framework_TestCase {
function setUp() { function setUp() {