comicpress-core/classes/ComicPressDBInterface.inc

304 lines
8.4 KiB
PHP
Raw Permalink 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-03-18 03:28:52 +00:00
function get_terminal_post_in_categories($categories, $first = true, $count = false, $additional_parameters = array()) {
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();
2010-03-18 03:28:52 +00:00
if (isset($additional_parameters['post_status'])) {
if (is_array($additional_parameters['post_status'])) {
$additional_parameters['post_status'] = implode(',', $additional_parameters['post_status']);
}
}
$terminal_comic_query->query(array_merge(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'
2010-03-18 03:28:52 +00:00
), $additional_parameters));
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-03-18 03:28:52 +00:00
function get_first_post($categories, $reference_post = null, $count = false, $additional = array()) {
return $this->get_terminal_post_in_categories($categories, true, $count, $additional);
2009-11-13 03:13:05 +00:00
}
/**
* Get the last comic in a category.
*/
2010-03-18 03:28:52 +00:00
function get_last_post($categories, $reference_post = null, $count = false, $additional = array()) {
return $this->get_terminal_post_in_categories($categories, false, $count, $additional);
}
function setup_clauses($additional) {
$clauses = array();
foreach (array_merge(array('post_type' => 'post', 'post_status' => 'publish'), $additional) as $field => $value) {
if (is_array($value)) {
foreach ($value as &$v) {
$v = "'${v}'";
}
$clauses[] = "AND p.${field} IN (" . join(", ", $value) . ")";
} else {
$clauses[] = "AND p.${field} = '${value}'";
}
}
return $clauses;
2009-11-13 03:13:05 +00:00
}
/**
* Get the comic post adjacent to the current comic.
*/
2010-03-18 03:28:52 +00:00
function get_adjacent_post($categories, $next = false, $override_post = null, $count = false, $additional = array()) {
2010-02-16 03:06:59 +00:00
global $wpdb, $post;
$count = $this->ensure_count($count);
$post_to_use = (!is_null($override_post)) ? $override_post : $post;
2010-03-18 03:28:52 +00:00
$clauses = $this->setup_clauses($additional);
2010-02-16 03:06:59 +00:00
$op = ($next ? '>' : '<');
$order = ($next ? 'ASC' : 'DESC');
$cats = implode(',', $categories);
2010-02-16 03:06:59 +00:00
$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
2010-03-18 03:28:52 +00:00
WHERE p.post_date ${op} %s"
. implode(" ", $clauses) . "
2010-02-16 03:06:59 +00:00
AND tt.taxonomy = 'category'
AND tt.term_id IN (${cats})
2010-02-16 03:06:59 +00:00
ORDER BY p.post_date ${order} LIMIT %d",
$post_to_use->post_date,
$count);
2010-03-18 03:28:52 +00:00
return $this->do_query($query, 'get_results', function($result, $query_key) use ($count) {
if (!empty($result)) {
if ($count == 1) { $result = $result[0]; }
2009-11-13 03:13:05 +00:00
2010-03-18 03:28:52 +00:00
wp_cache_set($query_key, $result, 'counts');
2009-11-13 03:13:05 +00:00
2010-03-18 03:28:52 +00:00
return $result;
} else {
return ($count == 1) ? false : array();
}
});
}
function do_query($query, $retrieval_method, $callback) {
global $wpdb;
2009-11-13 03:13:05 +00:00
2010-03-18 03:28:52 +00:00
$query_key = 'comicpress-query' . md5($query);
2009-11-13 03:13:05 +00:00
2010-03-18 03:28:52 +00:00
$result = wp_cache_get($query_key, 'comicpress');
if ($result !== false) {
2010-02-16 03:06:59 +00:00
return $result;
}
2010-03-18 03:28:52 +00:00
$result = $wpdb->{$retrieval_method}($query);
return $callback($result, $query_key);
2009-11-13 03:13:05 +00:00
}
/**
* Get the previous comic from the current one.
*/
2010-03-18 03:28:52 +00:00
function get_previous_post($categories = null, $override_post = null, $count = false, $additional = array()) {
return $this->get_adjacent_post($categories, false, $override_post, $count, $additional);
2010-02-16 03:06:59 +00:00
}
2009-11-13 03:13:05 +00:00
/**
* Get the next comic from the current one.
*/
2010-03-18 03:28:52 +00:00
function get_next_post($categories = null, $override_post = null, $count = false, $additional = array()) {
return $this->get_adjacent_post($categories, true, $override_post, $count, $additional);
}
/**
* Get all posts in a particular category.
*/
function get_all_posts($categories = null, $additional = array()) {
if (!is_array($categories)) { $categories = array($categories); }
$sort_order = $first ? "asc" : "desc";
$all_comic_query = new WP_Query();
$all_comic_query->query(array_merge(array(
'nopaging' => true,
'order' => $sort_order,
'category__in' => $categories,
'post_status' => 'publish'
), $additional));
return $all_comic_query->posts;
}
/**
* Get a count of all available posts.
*/
// TODO Make the additional merges work for SQL queries
function count_all_posts($categories = null, $additional = array()) {
global $wpdb;
if (!is_array($categories)) { $categories = array($categories); }
$cats = implode(',', $categories);
$clauses = $this->setup_clauses($additional);
$query = $wpdb->prepare("SELECT count(p.ID) 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 tt.taxonomy = 'category'
AND tt.term_id IN (${cats})" .
implode(" ", $clauses));
return $this->do_query($query, 'get_var', function($result, $query_key) {
if (!empty($result)) {
wp_cache_set($query_key, $result, 'counts');
return $result;
} else {
return is_numeric($result) ? $result : false;
}
});
}
function get_post_by_index($categories = null, $index = 0, $additional = array()) {
global $wpdb;
if (!is_array($categories)) { $categories = array($categories); }
$cats = implode(',', $categories);
$clauses = $this->setup_clauses($additional);
$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 tt.taxonomy = 'category'
AND tt.term_id IN (${cats})" .
implode(" ", $clauses) . "
ORDER BY p.post_date ASC
LIMIT ${index}, 1");
return $this->do_query($query, 'get_row', function($result, $query_key) {
if (!empty($result)) {
wp_cache_set($query_key, $result, 'counts');
return $result;
} else {
return false;
}
});
2010-02-16 03:06:59 +00:00
}
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;
}
2010-03-18 03:28:52 +00:00
function clear_annotations() {
global $wpdb;
$wpdb->query($wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE meta_key = %s", 'comicpress-annotation'));
}
function get_annotations($category) {
global $wpdb;
$query = $wpdb->prepare("SELECT pm.post_id, pm.meta_value
FROM $wpdb->postmeta pm
WHERE pm.meta_key = %s", 'comicpress-annotation');
$result = $wpdb->get_results($query);
$matching_annotations = array();
if (is_array($result)) {
foreach ($result as $row) {
if ($annotation = maybe_unserialize($row->meta_value)) {
$matching_annotations[$row->post_id] = $annotation[$category];
}
}
}
return $matching_annotations;
}
// @codeCoverageIgnoreEnd
2010-01-15 00:37:05 +00:00
}