comicpress-core/classes/ComicPressComicPost.inc

227 lines
7.7 KiB
PHP
Raw Normal View History

<?php
class ComicPressComicPost {
var $post;
2009-07-11 00:27:36 +00:00
var $attachments = null;
var $comicpress;
function ComicPressComicPost($post = null, $comicpress = null) {
if (!is_null($post)) { $this->post = $post; }
if (!is_null($comicpress)) { $this->comicpress = $comicpress; }
}
function get_comic_image_attachments() {
2009-07-11 00:27:36 +00:00
if (is_null($this->attachments)) {
$this->attachments = get_children(array(
'post_parent' => $this->post->ID,
'post_type' => 'attachment',
'post_mime_type' => 'image'
));
}
return $this->attachments;
}
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-07-22 22:18:49 +00:00
foreach ($ordering[$type] as $attachment_id) {
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);
$url = wp_get_attachment_url($attachment->ID, '');
$sizes = image_downsize($attachment->ID, $size_type);
if ($sizes) {
$url = $sizes[0];
}
2009-08-04 01:28:59 +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;
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-08-04 01:28:59 +00:00
echo apply_filters('comicpress_display_attached_images', implode("\n", $output), $this->post->ID);
2009-07-13 02:31:14 +00:00
return $found;
}
2009-08-04 01:28:59 +00:00
function display_comics($format) { $this->display_attached_images('comic', null, 'comic', $format); }
2009-07-13 02:31:14 +00:00
2009-08-04 01:28:59 +00:00
function display_archive($format) {
if (!$this->display_attached_images('archive', 1, 'archive', $format)) {
$this->display_attached_images('comic', 1, 'archive', $format);
2009-07-13 02:31:14 +00:00
}
}
2009-08-04 01:28:59 +00:00
function display_rss($format) {
if (!$this->display_attached_images('rss', null, 'rss', $formar)) {
$this->display_attached_images('comic', null, 'rss', $format);
2009-07-13 02:31:14 +00:00
}
}
function get_comic_img_tag($url, $type, $additional_parameters = array()) {
$dimensions = array();
2009-07-13 02:31:14 +00:00
if (isset($this->comicpress->comicpress_options["${type}_dimensions"])) {
list($width, $height) = explode("x", $this->comicpress->comicpress_options["${type}_dimensions"]);
$dimensions = compact('width', 'height');
}
$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) {
$output .= $parameter . '="' . $value . '" ';
}
}
$output .= "/>";
return $output;
}
2009-07-22 21:59:59 +00:00
function breakdown_comic_ordering_string($string) {
$parts = explode(";", $string);
$orderings = array();
foreach ($parts as $part) {
$type_key_value = explode(":", $part);
if (count($type_key_value) == 2) {
list ($key, $value) = $type_key_value;
if (preg_match('#[^a-z0-9\_\-]#', $key) == 0) {
$orderings[$key] = array();
$values = explode(",", $value);
foreach ($values as $value) {
$value = trim($value);
if (is_numeric($value)) {
$orderings[$key][] = $value;
}
}
}
}
}
return $orderings;
}
2009-08-03 00:32:08 +00:00
function build_comic_ordering_string($order) {
$result_string_parts = array();
foreach ($order as $key => $values) {
$result_string_parts[] = "${key}:" . implode(",", $values);
}
sort($result_string_parts);
return implode(";", $result_string_parts);
}
2009-08-01 15:05:39 +00:00
function normalize_comic_image_ordering() {
2009-07-22 21:59:59 +00:00
if (is_array($this->get_comic_image_attachments())) {
$ordering_by_type = array();
2009-08-03 00:32:08 +00:00
$ordering_types = get_post_meta($this->post->ID, 'comic_ordering', true);
2009-07-22 21:59:59 +00:00
if (!empty($ordering_types)) { $ordering_types = $this->breakdown_comic_ordering_string($ordering_types); }
$comic_image_ordering = array();
$found_post_ids = array();
if (!empty($ordering_types)) {
foreach ($ordering_types as $type => $post_ids) {
$comic_image_ordering[$type] = array();
foreach ($post_ids as $ordering_post_id) {
2009-07-22 21:59:59 +00:00
foreach ($this->get_comic_image_attachments() as $attachment) {
if (!isset($found_post_ids[$attachment->ID])) {
if ($attachment->ID == $ordering_post_id) {
2009-07-22 21:59:59 +00:00
$comic_image_ordering[$type][] = $attachment->ID;
$found_post_ids[$ordering_post_id] = true;
2009-07-22 21:59:59 +00:00
}
}
}
}
}
}
$remaining_posts_to_sort = array();
foreach ($this->get_comic_image_attachments() as $attachment) {
$comic_image_type = get_post_meta($attachment->ID, 'comic_image_type', true);
if (!empty($comic_image_type)) {
if (!isset($found_post_ids[$attachment->ID])) {
if (!isset($remaining_posts_to_sort[$comic_image_type])) {
$remaining_posts_to_sort[$comic_image_type] = array();
}
$remaining_posts_to_sort[$comic_image_type][] = $attachment->ID;
}
}
}
foreach ($remaining_posts_to_sort as $type => $posts) {
usort($remaining_posts_to_sort[$type], array(&$this, 'sort_remaining_comic_images'));
}
2009-08-22 22:31:10 +00:00
foreach ($remaining_posts_to_sort as $type => $posts) {
if (is_array($comic_image_ordering[$type])) {
$comic_image_ordering[$type] = array_merge($comic_image_ordering[$type], $posts);
} else {
$comic_image_ordering[$type] = $posts;
}
}
2009-08-03 00:32:08 +00:00
2009-08-22 22:31:10 +00:00
update_post_meta($this->post->ID, 'comic_ordering', $this->build_comic_ordering_string($comic_image_ordering));
return $comic_image_ordering;
2009-07-22 21:59:59 +00:00
}
}
2009-07-22 21:59:59 +00:00
function sort_remaining_comic_images($a, $b) {
return strtotime($a->post_date) - strtotime($b->post_date);
}
2009-08-03 00:32:08 +00:00
function change_comic_image_ordering($requested_new_order) {
$orderings = array();
2009-08-03 00:32:08 +00:00
if (($order_string = get_post_meta($this->post->ID, 'comic_ordering', true)) !== false) {
$orderings = $this->breakdown_comic_ordering_string($order_string);
}
$new_order = array();
foreach ($orderings as $type => $current_order) {
$new_order[$type] = array();
$sort_by_position = array();
foreach ($requested_new_order[$type] as $id => $position) {
if (!isset($sort_by_position[$position])) {
$sort_by_position[$position] = array();
}
$sort_by_position[$position][] = $id;
}
ksort($sort_by_position);
$requested_order = array();
foreach ($sort_by_position as $position => $ids) {
sort($ids);
$requested_order = array_merge($requested_order, $ids);
}
2009-08-03 00:32:08 +00:00
$requested_order = array_merge($requested_order, array_diff($current_order, $requested_order));
2009-08-03 00:32:08 +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
}
}
}
update_post_meta($this->post->ID, 'comic_ordering', $this->build_comic_ordering_string($new_order));
2009-08-03 00:32:08 +00:00
}
}
?>