2009-11-13 16:00:25 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class ComicPressDBInterface {
|
2009-11-26 05:27:29 +00:00
|
|
|
var $is_single, $in_the_loop;
|
2009-11-13 16:00:25 +00:00
|
|
|
function ComicPressDBInterface() {}
|
|
|
|
|
|
|
|
function get_instance() {
|
|
|
|
static $instance;
|
|
|
|
|
|
|
|
if (!isset($instance)) { $instance = new ComicPressDBInterface(); }
|
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
|
2009-11-21 15:10:33 +00:00
|
|
|
function _get_categories_to_exclude($categories = null) {
|
2009-11-23 04:07:44 +00:00
|
|
|
if (is_numeric($categories)) { $categories = array($categories); }
|
2009-11-21 15:10:33 +00:00
|
|
|
if (is_array($categories)) {
|
|
|
|
return array_values(array_diff(get_all_category_ids(), $categories));
|
|
|
|
} else {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
}
|
2009-11-13 16:00:25 +00:00
|
|
|
|
2009-11-21 15:10:33 +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;
|
2009-11-13 16:00:25 +00:00
|
|
|
}
|
|
|
|
|
2009-11-21 15:10:33 +00:00
|
|
|
function _reset_wp_query() {
|
|
|
|
global $wp_query;
|
|
|
|
|
|
|
|
$wp_query->is_single = $this->is_single;
|
|
|
|
$wp_query->in_the_loop = $this->in_the_loop;
|
2009-11-13 16:00:25 +00:00
|
|
|
}
|
|
|
|
|
2009-11-21 15:10:33 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
|
2009-11-13 16:00:25 +00:00
|
|
|
/**
|
|
|
|
* Find the terminal post in a specific category.
|
|
|
|
*/
|
2009-11-21 15:10:33 +00:00
|
|
|
function get_terminal_post_in_categories($categories, $first = true) {
|
2009-11-13 16:00:25 +00:00
|
|
|
$this->_prepare_wp_query();
|
|
|
|
|
2009-11-23 13:34:36 +00:00
|
|
|
if (!is_array($categories)) { $categories = array($categories); }
|
|
|
|
|
2009-11-13 16:00:25 +00:00
|
|
|
$sort_order = $first ? "asc" : "desc";
|
|
|
|
$terminal_comic_query = new WP_Query();
|
2009-11-21 15:10:33 +00:00
|
|
|
$terminal_comic_query->query(array(
|
|
|
|
'showposts' => 1,
|
|
|
|
'order' => $sort_order,
|
|
|
|
'category__in' => $categories,
|
|
|
|
'status' => 'publish'
|
|
|
|
));
|
2009-11-13 16:00:25 +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-21 15:10:33 +00:00
|
|
|
function get_first_post($categories) {
|
|
|
|
return $this->get_terminal_post_in_categories($categories);
|
2009-11-13 16:00:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the last comic in a category.
|
|
|
|
*/
|
2009-11-21 15:10:33 +00:00
|
|
|
function get_last_post($categories) {
|
|
|
|
return $this->get_terminal_post_in_categories($categories, false);
|
2009-11-13 16:00:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the comic post adjacent to the current comic.
|
|
|
|
* Wrapper around get_adjacent_post(). Don't unit test this method.
|
|
|
|
*/
|
2009-11-21 15:10:33 +00:00
|
|
|
function get_adjacent_post($categories, $next = false, $override_post = null) {
|
2009-11-13 16:00:25 +00:00
|
|
|
global $post;
|
|
|
|
|
|
|
|
$this->_prepare_wp_query();
|
|
|
|
if (!is_null($override_post)) { $temp_post = $post; $post = $override_post; }
|
|
|
|
|
2009-11-21 15:10:33 +00:00
|
|
|
$result = get_adjacent_post(false, implode(" and ", $this->_get_categories_to_exclude($categories)), !$next);
|
2009-11-13 16:00:25 +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-21 15:10:33 +00:00
|
|
|
function get_previous_post($categories = null, $override_post = null) { return $this->get_adjacent_post($categories, false, $override_post); }
|
2009-11-13 16:00:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the next comic from the current one.
|
|
|
|
*/
|
2009-11-21 15:10:33 +00:00
|
|
|
function get_next_post($categories = null, $override_post = null) { return $this->get_adjacent_post($categories, true, $override_post); }
|
|
|
|
|
|
|
|
// @codeCoverageIgnoreEnd
|
2009-11-13 16:00:25 +00:00
|
|
|
|
2010-01-15 01:54:50 +00:00
|
|
|
function get_parent_child_category_ids() {
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
$parent_child_categories = array();
|
|
|
|
|
|
|
|
$result = $wpdb->get_results("SELECT term_id, parent FROM $wpdb->term_taxonomy", ARRAY_A);
|
|
|
|
if (!empty($result)) {
|
|
|
|
foreach ($result as $row) {
|
|
|
|
$parent_child_categories[$row['term_id']] = $row['parent'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $parent_child_categories;
|
|
|
|
}
|
|
|
|
}
|