comicpress-core/classes/ComicPressDBInterface.inc

168 lines
4.4 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;
}
2010-02-16 03:06:59 +00:00
function ensure_count($count) {
return max((int)$count, 1);
}
2009-11-15 15:46:47 +00:00
// @codeCoverageIgnoreStart
2009-11-13 03:13:05 +00:00
/**
* Find the terminal post in a specific category.
*/
2010-02-16 03:06:59 +00:00
function get_terminal_post_in_categories($categories, $first = true, $count = false) {
2009-11-13 03:13:05 +00:00
$this->_prepare_wp_query();
2010-02-16 03:06:59 +00:00
$count = $this->ensure_count($count);
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(
2010-02-16 03:06:59 +00:00
'showposts' => $count,
2009-11-15 15:37:43 +00:00
'order' => $sort_order,
2010-02-16 03:06:59 +00:00
'category__in' => $categories,
'post_status' => 'publish'
2009-11-15 15:37:43 +00:00
));
2010-02-16 03:06:59 +00:00
$result = false;
2009-11-13 03:13:05 +00:00
if ($terminal_comic_query->have_posts()) {
2010-02-16 03:06:59 +00:00
if ($count == 1) {
$result = reset($terminal_comic_query->posts);
} else {
$result = $terminal_comic_query->posts;
}
2009-11-13 03:13:05 +00:00
}
$this->_reset_wp_query();
2010-02-16 03:06:59 +00:00
return $result;
2009-11-13 03:13:05 +00:00
}
/**
* Get the first comic in a category.
*/
2010-02-16 03:06:59 +00:00
function get_first_post($categories, $reference_post = null, $count = false) {
return $this->get_terminal_post_in_categories($categories, true, $count);
2009-11-13 03:13:05 +00:00
}
/**
* Get the last comic in a category.
*/
2010-02-16 03:06:59 +00:00
function get_last_post($categories, $reference_post = null, $count = false) {
2009-11-15 15:37:43 +00:00
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.
*/
2010-02-16 03:06:59 +00:00
function get_adjacent_post($categories, $next = false, $override_post = null, $count = false) {
global $wpdb, $post;
$count = $this->ensure_count($count);
$post_to_use = (!is_null($override_post)) ? $override_post : $post;
$op = ($next ? '>' : '<');
$order = ($next ? 'ASC' : 'DESC');
$query = $wpdb->prepare("SELECT p.* FROM $wpdb->posts AS p
INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id
INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
WHERE p.post_date ${op} %s
AND p.post_type = 'post'
AND p.post_status = 'publish'
AND tt.taxonomy = 'category'
AND tt.term_id IN (%s)
ORDER BY p.post_date ${order} LIMIT %d",
$post_to_use->post_date,
implode(',', $categories),
$count);
$query_key = 'comicpress_adjacent_post_' . md5($query);
$result = wp_cache_get($query_key, 'counts');
if ($result !== false) {
return $result;
}
2009-11-13 03:13:05 +00:00
2010-02-16 03:06:59 +00:00
$result = $wpdb->get_results($query);
2009-11-13 03:13:05 +00:00
2010-02-16 03:06:59 +00:00
if (!empty($result)) {
if ($count == 1) { $result = $result[0]; }
2009-11-13 03:13:05 +00:00
2010-02-16 03:06:59 +00:00
wp_cache_set($query_key, $result, 'counts');
2009-11-13 03:13:05 +00:00
2010-02-16 03:06:59 +00:00
return $result;
} else {
return ($count == 1) ? false : array();
}
2009-11-13 03:13:05 +00:00
}
/**
* Get the previous comic from the current one.
*/
2010-02-16 03:06:59 +00:00
function get_previous_post($categories = null, $override_post = null, $count = false) {
return $this->get_adjacent_post($categories, false, $override_post, $count);
}
2009-11-13 03:13:05 +00:00
/**
* Get the next comic from the current one.
*/
2010-02-16 03:06:59 +00:00
function get_next_post($categories = null, $override_post = null, $count = false) {
return $this->get_adjacent_post($categories, true, $override_post, $count);
}
2009-11-15 15:46:47 +00:00
2010-01-15 00:37:05 +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 WHERE `taxonomy` = 'category'", ARRAY_A);
2010-01-15 00:37:05 +00:00
if (!empty($result)) {
foreach ($result as $row) {
$parent_child_categories[$row['term_id']] = $row['parent'];
}
}
return $parent_child_categories;
}
// @codeCoverageIgnoreEnd
2010-01-15 00:37:05 +00:00
}