abstract out wp_query munging

This commit is contained in:
John Bintz 2009-10-22 22:26:13 -04:00
parent fa842f05af
commit 37df3e0db4
1 changed files with 26 additions and 17 deletions

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,25 +63,34 @@ 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; $this->_prepare_wp_query();
if (!is_null($override_post)) { $temp_post = $post; $post = $override_post; }
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); $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.