comicpress-core/classes/ComicPressDBInterface.inc

112 lines
2.8 KiB
PHP
Raw Normal View History

2009-10-20 01:22:00 +00:00
<?php
class ComicPressDBInterface {
2009-11-24 01:54:07 +00:00
var $is_single, $in_the_loop;
2009-11-13 03:13:05 +00:00
function ComicPressDBInterface() {}
2009-10-20 01:22:00 +00:00
2009-11-13 03:13:05 +00:00
function get_instance() {
static $instance;
if (!isset($instance)) { $instance = new ComicPressDBInterface(); }
return $instance;
}
2009-11-15 15:37:43 +00:00
function _get_categories_to_exclude($categories = null) {
2009-11-24 01:54:07 +00:00
if (is_numeric($categories)) { $categories = array($categories); }
2009-11-15 15:37:43 +00:00
if (is_array($categories)) {
2009-11-15 15:46:47 +00:00
return array_values(array_diff(get_all_category_ids(), $categories));
2009-11-13 03:13:05 +00:00
} else {
2009-11-15 15:37:43 +00:00
return array();
2009-11-13 03:13:05 +00:00
}
}
2009-11-15 15:46:47 +00:00
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;
}
// @codeCoverageIgnoreStart
2009-11-13 03:13:05 +00:00
/**
* Find the terminal post in a specific category.
*/
2009-11-15 15:37:43 +00:00
function get_terminal_post_in_categories($categories, $first = true) {
2009-11-13 03:13:05 +00:00
$this->_prepare_wp_query();
2009-11-24 01:54:07 +00:00
if (!is_array($categories)) { $categories = array($categories); }
2009-11-13 03:13:05 +00:00
$sort_order = $first ? "asc" : "desc";
$terminal_comic_query = new WP_Query();
2009-11-15 15:37:43 +00:00
$terminal_comic_query->query(array(
'showposts' => 1,
'order' => $sort_order,
'category__in' => $categories,
'status' => 'publish'
));
2009-11-13 03:13:05 +00:00
$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.
*/
2009-11-15 15:37:43 +00:00
function get_first_post($categories) {
return $this->get_terminal_post_in_categories($categories);
2009-11-13 03:13:05 +00:00
}
/**
* Get the last comic in a category.
*/
2009-11-15 15:37:43 +00:00
function get_last_post($categories) {
return $this->get_terminal_post_in_categories($categories, false);
2009-11-13 03:13:05 +00:00
}
/**
* Get the comic post adjacent to the current comic.
* Wrapper around get_adjacent_post(). Don't unit test this method.
*/
2009-11-15 15:37:43 +00:00
function get_adjacent_post($categories, $next = false, $override_post = null) {
2009-11-13 03:13:05 +00:00
global $post;
$this->_prepare_wp_query();
if (!is_null($override_post)) { $temp_post = $post; $post = $override_post; }
2009-11-15 15:37:43 +00:00
$result = get_adjacent_post(false, implode(" and ", $this->_get_categories_to_exclude($categories)), !$next);
2009-11-13 03:13:05 +00:00
$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.
*/
2009-11-15 15:37:43 +00:00
function get_previous_post($categories = null, $override_post = null) { return $this->get_adjacent_post($categories, false, $override_post); }
2009-11-13 03:13:05 +00:00
/**
* Get the next comic from the current one.
*/
2009-11-15 15:37:43 +00:00
function get_next_post($categories = null, $override_post = null) { return $this->get_adjacent_post($categories, true, $override_post); }
2009-11-15 15:46:47 +00:00
// @codeCoverageIgnoreEnd
2009-10-20 01:22:00 +00:00
}
2009-11-13 03:13:05 +00:00
?>