From e8cb5a6af3e7039c9300364fa4da379b2e0a91f3 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sat, 21 Nov 2009 10:10:33 -0500 Subject: [PATCH] bring in latest comicpress core storyline code --- classes/ComicPressDBInterface.inc | 134 +++++++++++------------ classes/ComicPressNavigation.inc | 17 ++- classes/ComicPressStoryline.inc | 176 ++++++++++++++++++------------ 3 files changed, 182 insertions(+), 145 deletions(-) diff --git a/classes/ComicPressDBInterface.inc b/classes/ComicPressDBInterface.inc index d844749..3fb4789 100644 --- a/classes/ComicPressDBInterface.inc +++ b/classes/ComicPressDBInterface.inc @@ -1,8 +1,6 @@ _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); + function _get_categories_to_exclude($categories = null) { + if (is_array($categories)) { + return array_values(array_diff(get_all_category_ids(), $categories)); } 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() { global $wp_query; @@ -97,15 +34,74 @@ class ComicPressDBInterface { $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. */ - 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. */ - 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 } ?> diff --git a/classes/ComicPressNavigation.inc b/classes/ComicPressNavigation.inc index 2097b0b..938583e 100644 --- a/classes/ComicPressNavigation.inc +++ b/classes/ComicPressNavigation.inc @@ -4,12 +4,14 @@ require_once('ComicPressStoryline.inc'); require_once('ComicPressDBInterface.inc'); class ComicPressNavigation { + // @codeCoverageIgnoreStart function init($storyline) { $this->_storyline = $storyline; $this->_dbi = ComicPressDBInterface::get_instance(); } + // @codeCoverageIgnoreEnd - function get_post_nav($post) { + function get_post_nav($post, $restrictions = null) { $nav = array(); if (is_object($post)) { if (isset($post->ID)) { @@ -17,18 +19,21 @@ class ComicPressNavigation { if (($result = wp_cache_get($cache_key, 'comicpress')) !== false) { 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 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 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)) { @@ -54,7 +59,6 @@ class ComicPressNavigation { $cache_data = array(); foreach ($nav as $key => $output_post) { 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'); @@ -62,7 +66,8 @@ class ComicPressNavigation { return $nav; } } + return false; } } -?> \ No newline at end of file +?> diff --git a/classes/ComicPressStoryline.inc b/classes/ComicPressStoryline.inc index bfaf3f8..b9566e0 100644 --- a/classes/ComicPressStoryline.inc +++ b/classes/ComicPressStoryline.inc @@ -16,7 +16,10 @@ class ComicPressStoryline { */ function get_flattened_storyline() { $comicpress = &ComicPress::get_instance(); - return $comicpress->comicpress_options['storyline_order']; + if (isset($comicpress->comicpress_options['storyline_order'])) { + return $comicpress->comicpress_options['storyline_order']; + } + return false; } /** @@ -46,28 +49,37 @@ 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. * @param array $structure The structure to process. * @return boolean True if the structure was valid. */ function create_structure($structure) { - $key = null; - if (is_string($structure)) { - $key = $structure; - $structure = explode(',', $structure); - } else { - if (is_array($structure)) { - $fixed_structure = array(); - foreach ($structure as $s) { - if (!is_array($s)) { $fixed_structure[] = $s; } - } - $key = implode(',', $fixed_structure); - } - } + $key = $this->_create_structure_key($structure); - if (!is_null($key)) { - $key = "storyline-structure-${key}"; + if ($key !== false) { + if (is_string($structure)) { + $structure = explode(',', $structure); + } else { + if (is_array($structure)) { + $fixed_structure = array(); + foreach ($structure as $s) { + if (!is_array($s)) { $fixed_structure[] = $s; } + } + $structure = $fixed_structure; + } + } if (($result = wp_cache_get($key, 'comicpress')) !== false) { $this->_structure = $result; @@ -133,6 +145,7 @@ class ComicPressStoryline { function _get_field($field, $id) { if (isset($this->_structure)) { + $id = $this->_ensure_numeric_category($id); if (isset($this->_structure[$id])) { if (isset($this->_structure[$id][$field])) { return $this->_structure[$id][$field]; @@ -142,11 +155,15 @@ class ComicPressStoryline { return false; } + // @codeCoverageIgnoreStart 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])) { + // @codeCoverageIgnoreEnd + + function valid($id) { + $id = $this->_ensure_numeric_category($id); + if (isset($this->_structure[$id])) { return array_keys($this->_structure[$id]); } return false; @@ -156,16 +173,21 @@ class ComicPressStoryline { if (isset($this->_structure[$id])) { $all_adjacent = array(); do { - $new_id = $this->_structure[$id][$direction]; - $has_adjacent = false; - if (!in_array($new_id, $all_adjacent)) { - if ($has_adjacent = isset($this->_structure[$id][$direction])) { - $all_adjacent[] = $new_id; - $id = $new_id; + + if (isset($this->_structure[$id][$direction])) { + $new_id = $this->_structure[$id][$direction]; + + if (!in_array($new_id, $all_adjacent)) { + if ($has_adjacent = isset($this->_structure[$id][$direction])) { + $all_adjacent[] = $new_id; + $id = $new_id; + } } } + // @codeCoverageIgnoreStart } while ($has_adjacent); + // @codeCoverageIgnoreEnd return $all_adjacent; } return false; @@ -197,14 +219,6 @@ class ComicPressStoryline { return $result; } - /** - * Get all comic categories. - * @deprecated - */ - function get_comic_categories() { - return array_keys($this->_structure); - } - /** * Get a simple storyline. */ @@ -248,9 +262,11 @@ class ComicPressStoryline { /** * Get a flattened category node list. */ + // @codeCoverageIgnoreStart function get_category_flattened($parent = null) { return $this->flatten_simple_storyline($this->get_category_simple_structure($parent)); } + // @codeCoverageIgnoreEnd /** * Merge a flat simple storyline into a tree. @@ -290,7 +306,6 @@ class ComicPressStoryline { * Integrates a bunch of other things. */ function normalize($flattened_storyline = null, $set = true) { - $comicpress = ComicPress::get_instance(); if (is_null($flattened_storyline)) { $flattened_storyline = $this->get_flattened_storyline(); } @@ -400,7 +415,7 @@ class ComicPressStoryline { return $this; } - function _find_children($parent) { + function _ensure_numeric_category($parent) { if (!is_numeric($parent)) { foreach (get_all_category_ids() as $id) { $category = get_category($id); @@ -409,21 +424,30 @@ class ComicPressStoryline { } } } + return $parent; + } + + function _find_children($parent) { + $parent = $this->_ensure_numeric_category($parent); 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; - } - } - } + if (is_array($this->_structure)) { + 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; + } + } + } + } } + // @codeCoverageIgnoreStart } while ($found_children); + // @codeCoverageIgnoreEnd return $children; } @@ -545,38 +569,50 @@ class ComicPressStoryline { } if (!$include_all) { - foreach ($restrictions as $type => $list) { - if (substr($type, 0, 1) == "!") { - $method_root = 'exclude'; - $method_type = substr($type, 1); + foreach ($restrictions as $_type => $_list) { + if (!is_string($_type) && is_array($_list)) { + $all_checks = array($_list); } else { - $method_root = 'include'; - $method_type = $type; + $all_checks = array( + array($_type, $_list) + ); } - if (!is_array($list)) { $list = array($list); } + foreach ($all_checks as $info) { + list($type, $list) = $info; - 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 (substr($type, 0, 1) == "!") { + $method_root = 'exclude'; + $method_type = substr($type, 1); + } else { + $method_root = 'include'; + $method_type = $type; } - if (!empty($method)) { - array_unshift($args, "_find_${method}"); - call_user_func_array(array($this, "_${method_root}"), $args); + + 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); + } } } }