fix infinite loop bug

This commit is contained in:
John Bintz 2009-11-10 21:34:57 -05:00
parent e32f66ba48
commit d61cc14008
1 changed files with 189 additions and 37 deletions

View File

@ -1,14 +1,14 @@
<?php
require_once('ComicPress.inc');
require_once('ComicPressDBInterface.inc');
class ComicPressStoryline {
var $_structure;
var $_category_search;
function read_from_options() {
function &read_from_options() {
$this->create_structure($this->get_flattened_storyline());
return $this;
}
/**
@ -58,7 +58,11 @@ class ComicPressStoryline {
$structure = explode(',', $structure);
} else {
if (is_array($structure)) {
$key = implode(',', $structure);
$fixed_structure = array();
foreach ($structure as $s) {
if (!is_array($s)) { $fixed_structure[] = $s; }
}
$key = implode(',', $fixed_structure);
}
}
@ -148,6 +152,23 @@ class ComicPressStoryline {
return false;
}
function all_adjacent($id, $direction) {
if (isset($this->_structure[$id])) {
$all_adjacent = array();
do {
$has_adjacent = false;
if (!in_array($id, $all_adjacent)) {
if ($has_adjacent = isset($this->_structure[$id][$direction])) {
$all_adjacent[] = $this->_structure[$id][$direction];
$id = $this->_structure[$id][$direction];
}
}
} while ($has_adjacent);
return $all_adjacent;
}
return false;
}
/**
* Get the valid navigation directions for a particular post.
*/
@ -378,30 +399,47 @@ class ComicPressStoryline {
}
function _find_children($parent) {
if (!is_numeric($parent)) {
foreach (get_all_category_ids() as $id) {
$category = get_category($id);
if ($category->slug == $parent) {
$parent = $id; break;
}
}
}
if (is_numeric($parent)) {
$children = array($parent);
do {
$found_children = false;
foreach ($this->_structure as $category_id => $info) {
if (!in_array($category_id, $children)) {
if (isset($info['parent'])) {
if (in_array($info['parent'], $children)) {
$children[] = $category_id;
$found_children = true;
}
}
}
}
} while ($found_children);
return $children;
}
return false;
}
function &include_children($parent = null) {
$this->_category_search = array_unique(array_merge($this->_category_search, $this->_find_children($parent)));
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_children($parent = null) {
$this->_category_search = array_diff($this->_category_search, $this->_find_children($parent));
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;
}
@ -414,16 +452,75 @@ class ComicPressStoryline {
return $found;
}
function &include_level_or_above($level = null) {
$this->_category_search = array_unique(array_merge($this->_category_search, $this->_find_level_or_above($level)));
sort($this->_category_search);
return $this;
function _find_only($id = null) {
if (isset($this->_structure[$id])) {
return array($id);
}
return array();
}
function &exclude_level_or_above($level = null) {
$this->_category_search = array_diff($this->_category_search, $this->_find_level_or_above($level));
sort($this->_category_search);
return $this;
function _find_level($level = null) {
$found = array();
foreach ($this->_structure as $category_id => $info) {
if ($info['level'] == $level) { $found[] = $category_id; }
}
return $found;
}
function _ensure_post_id($thing) {
$id = null;
if (is_object($thing)) {
if (isset($thing->ID)) { $id = $thing->ID; }
} else {
if (is_numeric($thing)) { $id = $thing; }
}
return $id;
}
function _find_post_category($post = null) {
$found = array();
$id = $this->_ensure_post_id($post);
if (!is_null($id)) {
if (count($categories = wp_get_post_categories($id)) == 1) {
$found = $categories;
}
}
return $found;
}
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 _find_post_root($post = null) {
$found = array();
$id = $this->_ensure_post_id($post);
if (!is_null($id)) {
if (count($categories = wp_get_post_categories($id)) == 1) {
$comic_post = new ComicPressComicPost(get_post($id));
$parents = $comic_post->find_parents();
if (!empty($parents)) {
$parents = array_keys($parents); $found = $this->_find_children(end($parents));
}
}
}
return $found;
}
function end_search() {
@ -431,6 +528,61 @@ class ComicPressStoryline {
$this->_category_search = array();
return $result;
}
function build_from_restrictions($restrictions = null) {
global $post;
$this->read_from_options()->exclude_all();
$include_all = true;
if (is_array($restrictions)) {
if (!empty($restrictions)) {
$include_all = false;
}
}
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;
}
if (!is_array($list)) { $list = array($list); }
foreach ($list as $restriction) {
$method = '';
$args = array($restriction);
switch ($method_type) {
case 'child_of': $method = 'children'; break;
case 'root_of': $method = 'post_root'; break;
case 'from_post': $method = 'post_category'; break;
case 'previous':
$method = 'adjacent';
$args[] = false;
break;
case 'next':
$method = 'adjacent';
$args[] = true;
break;
default:
$method = $method_type; break;
}
if (!empty($method)) {
array_unshift($args, "_find_${method}");
call_user_func_array(array($this, "_${method_root}"), $args);
}
}
}
} else {
$this->include_all();
}
return $this->end_search();
}
}
?>