comicpress-core/classes/ComicPressComicPost.inc

239 lines
7.5 KiB
PHP
Raw Normal View History

<?php
class ComicPressComicPost {
var $post;
2009-07-11 00:27:36 +00:00
var $attachments = null;
2009-10-20 00:56:32 +00:00
function ComicPressComicPost($post = null) {
if (!is_null($post)) { $this->post = $post; }
}
2009-11-09 03:11:26 +00:00
function get_attachments() {
$comicpress = ComicPress::get_instance();
$attachments = array();
foreach ($comicpress->backends as $backend) {
$attachments = array_merge($attachments, call_user_func(array($backend, 'generate_from_post'), $this->post));
}
return $attachments;
}
2009-11-09 03:11:26 +00:00
2009-10-20 00:56:32 +00:00
/**
* Display all the attached images.
*/
2009-08-04 01:28:59 +00:00
function display_attached_images($type = "comic", $limit = null, $size_type = null, $format = "%s") {
2009-07-13 02:31:14 +00:00
if (is_null($size_type)) { $size_type = $type; }
$found = false;
2009-07-22 22:18:49 +00:00
$ordering = $this->normalize_comic_image_ordering($this->post->ID);
2009-08-04 01:28:59 +00:00
$output = array();
2009-07-22 22:18:49 +00:00
if (is_array($ordering[$type])) {
2009-08-04 01:28:59 +00:00
$i = 1;
2009-10-20 00:56:32 +00:00
foreach ($ordering[$type] as $attachment_id) {
2009-07-22 22:18:49 +00:00
if (get_post_meta($attachment_id, "comic_image_type", true) == $type) {
$attachment = get_post($attachment_id);
2009-07-13 08:22:50 +00:00
$title = (!empty($attachment->post_excerpt) ? $attachment->post_excerpt : $attachment->post_title);
2009-11-09 03:11:26 +00:00
$url = wp_get_attachment_url($attachment->ID, '');
$sizes = image_downsize($attachment->ID, $size_type);
if ($sizes) {
$url = $sizes[0];
}
2009-11-09 03:11:26 +00:00
$output[] = apply_filters('comicpress_attached_image',
sprintf($format, $this->get_comic_img_tag($url, $size_type, array('title' => $title))),
2009-08-04 01:28:59 +00:00
$attachment_id,
$i++);
2009-07-13 02:31:14 +00:00
$found = true;
2009-11-09 03:11:26 +00:00
2009-07-13 02:31:14 +00:00
if (!is_null($limit)) {
if (--$limit == 0) { break; }
}
}
2009-07-11 00:27:36 +00:00
}
2009-07-13 02:31:14 +00:00
}
2009-10-20 00:56:32 +00:00
if (!empty($output)) {
echo apply_filters('comicpress_display_attached_images', $output, $this->post->ID, '');
}
2009-07-13 02:31:14 +00:00
return $found;
}
2009-11-09 03:11:26 +00:00
2009-10-18 23:21:00 +00:00
function _display_type($types, $format, $single = false) {
$target_type = reset($types);
foreach ($types as $type) {
if ($this->display_attached_images($type, ($single ? 1 : null), $target_type, $format)) {
return;
}
2009-07-13 02:31:14 +00:00
}
}
2009-11-09 03:11:26 +00:00
2009-10-18 23:21:00 +00:00
function display_comics($format) { $this->_display_type(array('comic'), $format); }
function display_archive($format) { $this->_display_type(array('archive'. 'comic'), $format, true); }
function display_rss($format) { $this->_display_type(array('rss'. 'comic'), $format); }
2009-07-13 02:31:14 +00:00
2009-10-20 00:56:32 +00:00
/**
* Build an &lt;img /&gt; tag for a comic.
*/
function get_comic_img_tag($url, $type, $additional_parameters = array()) {
$dimensions = array();
2009-11-09 03:11:26 +00:00
if (isset($this->comicpress->comicpress_options["${type}_dimensions"])) {
$parts = explode("x", $this->comicpress->comicpress_options["${type}_dimensions"]);
switch (count($parts)) {
case 1: list($width) = $parts; break;
case 2: list($width, $height) = $parts; break;
}
$dimensions = compact('width', 'height');
}
2009-11-09 03:11:26 +00:00
$output = '<img src="' . $url . '" ';
foreach (array('width', 'height') as $field) {
if (!empty($dimensions[$field])) {
$output .= $field . '="' . $dimensions[$field] . '" ';
}
}
if (is_array($additional_parameters)) {
foreach ($additional_parameters as $parameter => $value) {
2009-11-09 03:11:26 +00:00
$output .= $parameter . '="' . $value . '" ';
}
}
$output .= "/>";
2009-11-09 03:11:26 +00:00
return $output;
}
2009-10-18 23:21:00 +00:00
/**
* Normalize the ordering of comic images in this post.
* If images have beed added or removed, intelligently update the metadata.
*/
2009-11-09 03:11:26 +00:00
function normalize_ordering() {
$attachments = $this->get_attachments();
if (is_array($attachments)) {
$new_ordering = array();
$current_ordering = get_post_meta($this->post->ID, 'image-ordering', true);
if (!is_array($current_ordering)) { $current_ordering = array(); }
$all_current_ids = array();
foreach ($current_ordering as $key => $children) {
$all_current_ids[$key] = true;
if (is_array($children)) {
foreach ($children as $type => $kids) {
$all_current_ids = array_merge($all_current_ids, $kids);
}
}
2009-07-22 21:59:59 +00:00
}
2009-11-09 03:11:26 +00:00
$all_current_ids = array_keys($all_current_ids);
$attachment_ids = array();
foreach ($attachments as $attachment) { $attachment_ids[] = $attachment->id; }
$new_attachments = array_diff($attachment_ids, $all_current_ids);
$missing_attachments = array_diff($all_current_ids, $attachment_ids);
foreach ($new_attachments as $attachment_id) {
$current_ordering[$attachment_id] = true;
2009-07-22 21:59:59 +00:00
}
2009-11-09 03:11:26 +00:00
foreach ($missing_attachments as $attachment_id) {
if (isset($current_ordering[$attachment_id])) {
unset($current_ordering[$attachment_id]);
} else {
foreach ($current_ordering as $key => $children) {
foreach ($children as $type => $kids) {
if (isset($kids[$attachment_id])) { unset($kids[$attachment_id]); }
$children[$type] = $kids;
}
if (empty($children[$type])) {
$current_ordering[$key] = true;
} else {
$current_ordering[$key] = $children;
}
}
}
2009-08-22 22:31:10 +00:00
}
2009-08-03 00:32:08 +00:00
2009-11-09 03:11:26 +00:00
update_post_meta($this->post->ID, 'image-ordering', $current_ordering);
return $current_ordering;
2009-07-22 21:59:59 +00:00
}
2009-10-18 23:21:00 +00:00
return false;
2009-07-22 21:59:59 +00:00
}
2009-11-09 03:11:26 +00:00
/**
* Sort the remaining comic images by file date.
* @param object $a
* @param object $b
* @return int
*/
2009-07-22 21:59:59 +00:00
function sort_remaining_comic_images($a, $b) {
$a_date = isset($a->post_date) ? $a->post_date : 0;
$b_date = isset($b->post_date) ? $b->post_date : 0;
2009-11-09 03:11:26 +00:00
return $a_date - $b_date;
}
2009-10-18 23:21:00 +00:00
/**
* Change the ordering of comic images in the associated post.
* @param array $order The new requested order.
*/
2009-08-03 00:32:08 +00:00
function change_comic_image_ordering($requested_new_order) {
2009-10-18 23:21:00 +00:00
$orderings = get_post_meta($this->post->ID, 'comic_ordering', true);
if (!is_array($orderings)) { $orderings = array(); }
2009-11-09 03:11:26 +00:00
$new_order = array();
2009-11-04 02:45:56 +00:00
$requested_new_order = (array)$requested_new_order;
foreach ($orderings as $type => $current_order) {
$new_order[$type] = array();
$sort_by_position = array();
2009-11-04 02:45:56 +00:00
$position = 0;
foreach ($requested_new_order[$type] as $id) {
if (!isset($sort_by_position[$position])) {
2009-11-09 03:11:26 +00:00
$sort_by_position[$position] = array();
}
$sort_by_position[$position][] = $id;
2009-11-04 02:45:56 +00:00
$position++;
}
ksort($sort_by_position);
$requested_order = array();
foreach ($sort_by_position as $position => $ids) {
sort($ids);
$requested_order = array_merge($requested_order, $ids);
}
2009-10-20 02:41:37 +00:00
$requested_order = array_merge($requested_order, array_diff($current_order, $requested_order));
2009-10-20 02:41:37 +00:00
foreach ($requested_order as $requested_comic) {
if (in_array($requested_comic, $current_order)) {
$new_order[$type][] = $requested_comic;
2009-08-03 00:32:08 +00:00
}
}
}
2009-11-09 03:11:26 +00:00
2009-10-18 23:21:00 +00:00
update_post_meta($this->post->ID, 'comic_ordering', $new_order);
2009-08-03 00:32:08 +00:00
}
2009-11-06 12:33:06 +00:00
2009-11-07 18:47:08 +00:00
function find_parents() {
$parents = array();
$post_categories = wp_get_post_categories($this->post->ID);
if (count($post_categories) == 1) {
do {
$category_parent = 0;
$category = get_category(end($post_categories));
if (!empty($category)) {
$category_parent = $category->parent;
if ($category_parent != 0) {
$post_categories[] = $category_parent;
2009-11-09 03:11:26 +00:00
}
2009-11-07 18:47:08 +00:00
}
} while ($category_parent != 0);
2009-11-09 03:11:26 +00:00
2009-11-07 18:47:08 +00:00
foreach ($post_categories as $category_id) {
$category = get_category($category_id);
$parents[$category_id] = $category->slug;
}
}
2009-11-09 03:11:26 +00:00
2009-11-07 18:47:08 +00:00
return $parents;
2009-11-06 12:33:06 +00:00
}
}
?>