bring in latest comicpress core storyline code

This commit is contained in:
John Bintz 2009-11-21 10:10:33 -05:00
parent 0cb36de9a3
commit e8cb5a6af3
3 changed files with 182 additions and 145 deletions

View File

@ -1,8 +1,6 @@
<?php <?php
class ComicPressDBInterface { class ComicPressDBInterface {
var $_non_comic_categories = array(), $_all_categories = array();
function ComicPressDBInterface() {} function ComicPressDBInterface() {}
function get_instance() { function get_instance() {
@ -12,75 +10,14 @@ class ComicPressDBInterface {
return $instance; return $instance;
} }
function _get_categories() { return get_categories("hide_empty=0"); } function _get_categories_to_exclude($categories = null) {
if (is_array($categories)) {
/** return array_values(array_diff(get_all_category_ids(), $categories));
* 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) {
$result = array_diff($this->_all_categories, array($category));
if (is_array($result)) {
return (is_null($category)) ? $this->_non_comic_categories : array_values($result);
} else { } else {
return $this->_non_comic_categories; return array();
} }
} }
/**
* Find the terminal post in a specific category.
*/
function get_terminal_post_in_category($category_id, $first = true) {
$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");
$post = false;
if ($terminal_comic_query->have_posts()) {
$post = reset($terminal_comic_query->posts);
}
$this->_reset_wp_query();
return $post;
}
/**
* 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 $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);
$this->_reset_wp_query();
if (!is_null($override_post)) { $post = $temp_post; }
return empty($result) ? false : $result;
}
function _prepare_wp_query() { function _prepare_wp_query() {
global $wp_query; global $wp_query;
@ -97,15 +34,74 @@ class ComicPressDBInterface {
$wp_query->in_the_loop = $this->in_the_loop; $wp_query->in_the_loop = $this->in_the_loop;
} }
// @codeCoverageIgnoreStart
/**
* Find the terminal post in a specific category.
*/
function get_terminal_post_in_categories($categories, $first = true) {
$this->_prepare_wp_query();
$sort_order = $first ? "asc" : "desc";
$terminal_comic_query = new WP_Query();
$terminal_comic_query->query(array(
'showposts' => 1,
'order' => $sort_order,
'category__in' => $categories,
'status' => 'publish'
));
$post = false;
if ($terminal_comic_query->have_posts()) {
$post = reset($terminal_comic_query->posts);
}
$this->_reset_wp_query();
return $post;
}
/**
* Get the first comic in a category.
*/
function get_first_post($categories) {
return $this->get_terminal_post_in_categories($categories);
}
/**
* Get the last comic in a category.
*/
function get_last_post($categories) {
return $this->get_terminal_post_in_categories($categories, false);
}
/**
* Get the comic post adjacent to the current comic.
* Wrapper around get_adjacent_post(). Don't unit test this method.
*/
function get_adjacent_post($categories, $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($categories)), !$next);
$this->_reset_wp_query();
if (!is_null($override_post)) { $post = $temp_post; }
return empty($result) ? false : $result;
}
/** /**
* Get the previous comic from the current one. * 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 = null, $override_post = null) { return $this->get_adjacent_post($categories, false, $override_post); }
/** /**
* Get the next comic from the current one. * 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 = null, $override_post = null) { return $this->get_adjacent_post($categories, true, $override_post); }
// @codeCoverageIgnoreEnd
} }
?> ?>

View File

@ -4,12 +4,14 @@ require_once('ComicPressStoryline.inc');
require_once('ComicPressDBInterface.inc'); require_once('ComicPressDBInterface.inc');
class ComicPressNavigation { class ComicPressNavigation {
// @codeCoverageIgnoreStart
function init($storyline) { function init($storyline) {
$this->_storyline = $storyline; $this->_storyline = $storyline;
$this->_dbi = ComicPressDBInterface::get_instance(); $this->_dbi = ComicPressDBInterface::get_instance();
} }
// @codeCoverageIgnoreEnd
function get_post_nav($post) { function get_post_nav($post, $restrictions = null) {
$nav = array(); $nav = array();
if (is_object($post)) { if (is_object($post)) {
if (isset($post->ID)) { if (isset($post->ID)) {
@ -17,18 +19,21 @@ class ComicPressNavigation {
if (($result = wp_cache_get($cache_key, 'comicpress')) !== false) { if (($result = wp_cache_get($cache_key, 'comicpress')) !== false) {
foreach ($result as $key => $post_id) { foreach ($result as $key => $post_id) {
$nev[$key] = get_post($post_id); $nav[$key] = get_post($post_id);
} }
return $nav;
} }
$categories = $this->_storyline->build_from_restrictions($restrictions);
// global previous/next // global previous/next
foreach (array('previous', 'next') as $field) { foreach (array('previous', 'next') as $field) {
$nav[$field] = $this->_dbi->{"get_${field}_comic"}(null, $post); $nav[$field] = $this->_dbi->{"get_${field}_comic"}($categories, $post);
} }
// global first/last // global first/last
foreach (array('first', 'last') as $field) { foreach (array('first', 'last') as $field) {
$nav[$field] = $this->_dbi->{"get_${field}_comic"}(null); $nav[$field] = $this->_dbi->{"get_${field}_comic"}($categories);
} }
if ($category = $this->_storyline->get_valid_post_category($post->ID)) { if ($category = $this->_storyline->get_valid_post_category($post->ID)) {
@ -54,7 +59,6 @@ class ComicPressNavigation {
$cache_data = array(); $cache_data = array();
foreach ($nav as $key => $output_post) { foreach ($nav as $key => $output_post) {
if (!empty($output_post)) { $cache_data[$key] = $output_post->ID; } if (!empty($output_post)) { $cache_data[$key] = $output_post->ID; }
if ($output_post->ID == $post->ID) { $nav[$key] = false; }
} }
wp_cache_set($cache_key, $cache_data, 'comicpress'); wp_cache_set($cache_key, $cache_data, 'comicpress');
@ -62,6 +66,7 @@ class ComicPressNavigation {
return $nav; return $nav;
} }
} }
return false;
} }
} }

View File

@ -16,8 +16,11 @@ class ComicPressStoryline {
*/ */
function get_flattened_storyline() { function get_flattened_storyline() {
$comicpress = &ComicPress::get_instance(); $comicpress = &ComicPress::get_instance();
if (isset($comicpress->comicpress_options['storyline_order'])) {
return $comicpress->comicpress_options['storyline_order']; return $comicpress->comicpress_options['storyline_order'];
} }
return false;
}
/** /**
* Set the global storyline as a flattened storyline. * Set the global storyline as a flattened storyline.
@ -46,15 +49,27 @@ class ComicPressStoryline {
} }
} }
function _create_structure_key($input) {
$key = 'storyline-structure-';
if (is_string($input)) { return $key . $input; }
if (is_array($input)) {
$fixed_parts = array();
foreach ($input as $i) { if (is_string($i)) { $fixed_parts[] = $i; } }
if (!empty($fixed_parts)) { return $key . implode(',', $fixed_parts); }
}
return false;
}
/** /**
* Create a searchable structure from a node list. * Create a searchable structure from a node list.
* @param array $structure The structure to process. * @param array $structure The structure to process.
* @return boolean True if the structure was valid. * @return boolean True if the structure was valid.
*/ */
function create_structure($structure) { function create_structure($structure) {
$key = null; $key = $this->_create_structure_key($structure);
if ($key !== false) {
if (is_string($structure)) { if (is_string($structure)) {
$key = $structure;
$structure = explode(',', $structure); $structure = explode(',', $structure);
} else { } else {
if (is_array($structure)) { if (is_array($structure)) {
@ -62,13 +77,10 @@ class ComicPressStoryline {
foreach ($structure as $s) { foreach ($structure as $s) {
if (!is_array($s)) { $fixed_structure[] = $s; } if (!is_array($s)) { $fixed_structure[] = $s; }
} }
$key = implode(',', $fixed_structure); $structure = $fixed_structure;
} }
} }
if (!is_null($key)) {
$key = "storyline-structure-${key}";
if (($result = wp_cache_get($key, 'comicpress')) !== false) { if (($result = wp_cache_get($key, 'comicpress')) !== false) {
$this->_structure = $result; $this->_structure = $result;
} else { } else {
@ -133,6 +145,7 @@ class ComicPressStoryline {
function _get_field($field, $id) { function _get_field($field, $id) {
if (isset($this->_structure)) { if (isset($this->_structure)) {
$id = $this->_ensure_numeric_category($id);
if (isset($this->_structure[$id])) { if (isset($this->_structure[$id])) {
if (isset($this->_structure[$id][$field])) { if (isset($this->_structure[$id][$field])) {
return $this->_structure[$id][$field]; return $this->_structure[$id][$field];
@ -142,10 +155,14 @@ class ComicPressStoryline {
return false; return false;
} }
// @codeCoverageIgnoreStart
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); }
// @codeCoverageIgnoreEnd
function valid($id) { function valid($id) {
$id = $this->_ensure_numeric_category($id);
if (isset($this->_structure[$id])) { if (isset($this->_structure[$id])) {
return array_keys($this->_structure[$id]); return array_keys($this->_structure[$id]);
} }
@ -156,16 +173,21 @@ class ComicPressStoryline {
if (isset($this->_structure[$id])) { if (isset($this->_structure[$id])) {
$all_adjacent = array(); $all_adjacent = array();
do { do {
$has_adjacent = false;
if (isset($this->_structure[$id][$direction])) {
$new_id = $this->_structure[$id][$direction]; $new_id = $this->_structure[$id][$direction];
$has_adjacent = false;
if (!in_array($new_id, $all_adjacent)) { if (!in_array($new_id, $all_adjacent)) {
if ($has_adjacent = isset($this->_structure[$id][$direction])) { if ($has_adjacent = isset($this->_structure[$id][$direction])) {
$all_adjacent[] = $new_id; $all_adjacent[] = $new_id;
$id = $new_id; $id = $new_id;
} }
} }
}
// @codeCoverageIgnoreStart
} while ($has_adjacent); } while ($has_adjacent);
// @codeCoverageIgnoreEnd
return $all_adjacent; return $all_adjacent;
} }
return false; return false;
@ -197,14 +219,6 @@ class ComicPressStoryline {
return $result; return $result;
} }
/**
* Get all comic categories.
* @deprecated
*/
function get_comic_categories() {
return array_keys($this->_structure);
}
/** /**
* Get a simple storyline. * Get a simple storyline.
*/ */
@ -248,9 +262,11 @@ class ComicPressStoryline {
/** /**
* Get a flattened category node list. * Get a flattened category node list.
*/ */
// @codeCoverageIgnoreStart
function get_category_flattened($parent = null) { function get_category_flattened($parent = null) {
return $this->flatten_simple_storyline($this->get_category_simple_structure($parent)); return $this->flatten_simple_storyline($this->get_category_simple_structure($parent));
} }
// @codeCoverageIgnoreEnd
/** /**
* Merge a flat simple storyline into a tree. * Merge a flat simple storyline into a tree.
@ -290,7 +306,6 @@ class ComicPressStoryline {
* Integrates a bunch of other things. * Integrates a bunch of other things.
*/ */
function normalize($flattened_storyline = null, $set = true) { function normalize($flattened_storyline = null, $set = true) {
$comicpress = ComicPress::get_instance();
if (is_null($flattened_storyline)) { if (is_null($flattened_storyline)) {
$flattened_storyline = $this->get_flattened_storyline(); $flattened_storyline = $this->get_flattened_storyline();
} }
@ -400,7 +415,7 @@ class ComicPressStoryline {
return $this; return $this;
} }
function _find_children($parent) { function _ensure_numeric_category($parent) {
if (!is_numeric($parent)) { if (!is_numeric($parent)) {
foreach (get_all_category_ids() as $id) { foreach (get_all_category_ids() as $id) {
$category = get_category($id); $category = get_category($id);
@ -409,10 +424,16 @@ class ComicPressStoryline {
} }
} }
} }
return $parent;
}
function _find_children($parent) {
$parent = $this->_ensure_numeric_category($parent);
if (is_numeric($parent)) { if (is_numeric($parent)) {
$children = array($parent); $children = array($parent);
do { do {
$found_children = false; $found_children = false;
if (is_array($this->_structure)) {
foreach ($this->_structure as $category_id => $info) { foreach ($this->_structure as $category_id => $info) {
if (!in_array($category_id, $children)) { if (!in_array($category_id, $children)) {
if (isset($info['parent'])) { if (isset($info['parent'])) {
@ -423,7 +444,10 @@ class ComicPressStoryline {
} }
} }
} }
}
// @codeCoverageIgnoreStart
} while ($found_children); } while ($found_children);
// @codeCoverageIgnoreEnd
return $children; return $children;
} }
@ -545,7 +569,18 @@ class ComicPressStoryline {
} }
if (!$include_all) { if (!$include_all) {
foreach ($restrictions as $type => $list) { foreach ($restrictions as $_type => $_list) {
if (!is_string($_type) && is_array($_list)) {
$all_checks = array($_list);
} else {
$all_checks = array(
array($_type, $_list)
);
}
foreach ($all_checks as $info) {
list($type, $list) = $info;
if (substr($type, 0, 1) == "!") { if (substr($type, 0, 1) == "!") {
$method_root = 'exclude'; $method_root = 'exclude';
$method_type = substr($type, 1); $method_type = substr($type, 1);
@ -580,6 +615,7 @@ class ComicPressStoryline {
} }
} }
} }
}
} else { } else {
$this->include_all(); $this->include_all();
} }