comicpress-core/classes/ComicPressComicPost.inc

181 lines
5.6 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-11 00:55:03 +00:00
/**
* TODO normalize the attachments
2009-11-14 17:38:09 +00:00
* @return array The raw list of attached files for this post.
2009-11-11 00:55:03 +00:00
*/
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
/**
* Normalize the ordering of attachments in this post.
2009-10-18 23:21:00 +00:00
* If images have beed added or removed, intelligently update the metadata.
* @param boolean $skip_checks If true, only return the post metadata, assuming that everything is correct. Do not use anywhere in the admin interface!
* @return array The normalized data, which is also written to the post's metadata if $skip_checks is false.
2009-10-18 23:21:00 +00:00
*/
function normalize_ordering($skip_checks = false) {
2009-11-09 03:11:26 +00:00
$attachments = $this->get_attachments();
if (is_array($attachments)) {
$new_ordering = array();
$current_ordering = get_post_meta($this->post->ID, 'image-ordering', true);
if ($skip_checks) { return $current_ordering; }
2009-11-09 03:11:26 +00:00
if (!is_array($current_ordering)) { $current_ordering = array(); }
$all_current_ids = array();
2009-11-09 04:13:34 +00:00
foreach ($current_ordering as $key => $properties) {
2009-11-09 03:11:26 +00:00
$all_current_ids[$key] = true;
2009-11-09 04:13:34 +00:00
if (isset($properties['children'])) {
2009-11-13 21:15:36 +00:00
foreach (array_values($properties['children']) as $kid) { $all_current_ids[$kid] = true; }
2009-11-09 03:11:26 +00:00
}
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) {
2009-11-09 04:13:34 +00:00
$current_ordering[$attachment_id] = array('enabled' => 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 {
2009-11-09 04:13:34 +00:00
foreach ($current_ordering as $key => $properties) {
if (isset($properties['children'])) {
2009-11-13 21:15:36 +00:00
foreach ($properties['children'] as $type => $kid) {
if (!in_array($kid, $attachment_ids)) {
unset($properties['children'][$type]);
2009-11-09 04:13:34 +00:00
}
}
if (empty($properties['children'])) {
unset($current_ordering[$key]['children']);
} else {
$current_ordering[$key] = $properties;
}
2009-11-09 03:11:26 +00:00
}
}
}
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
2009-11-11 12:49:54 +00:00
// @codeCoverageIgnoreStart
/**
* 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-11-11 12:49:54 +00:00
// @codeCoverageIgnoreEnd
2009-10-18 23:21:00 +00:00
2009-11-14 17:38:09 +00:00
/**
* Find all the parent categories of this post.
* @return array The parents in ascending child->parent relationship order.
*/
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
}
2009-11-13 21:15:36 +00:00
2009-11-14 17:38:09 +00:00
/**
* Update the ordering information for the post.
* @param array $info The new ordering information.
* @return array The reworked ordering.
*/
2009-11-13 21:15:36 +00:00
function update_post_media_data($info) {
$ordering = array();
foreach ($info as $image) {
2009-11-14 00:05:41 +00:00
$image = (array)$image;
2009-11-13 21:15:36 +00:00
if (isset($image['id'])) {
$data = array(
'enabled' => false
);
foreach ($image as $field => $value) {
switch ($field) {
2009-11-14 00:05:41 +00:00
case 'enabled': $data['enabled'] = $value; break;
2009-11-13 21:15:36 +00:00
case 'children':
2009-11-14 00:05:41 +00:00
foreach ((array)$value as $type => $attached_id) {
if (!empty($attached_id)) {
if (!isset($data['children'])) { $data['children'] = array(); }
$data['children'][$type] = $attached_id;
}
2009-11-13 21:15:36 +00:00
}
break;
}
}
$ordering[$image['id']] = $data;
}
}
update_post_meta($this->post->ID, 'image-ordering', $ordering);
return $ordering;
}
2009-11-14 16:46:20 +00:00
2009-11-14 17:38:09 +00:00
/**
* Rework normalized attachments for a simpler embed format.
* @param boolean $skip_checks If true, skip the normalization checks in normalize_ordering(). Don't use in the admin interface.
2009-11-14 17:38:09 +00:00
* @return array The simplified, normalized attachments.
*/
function get_attachments_with_children($skip_checks = false) {
2009-11-14 16:46:20 +00:00
$attachments_with_children = array();
foreach ($this->normalize_ordering($skip_checks) as $id => $info) {
2009-11-14 16:46:20 +00:00
if ($info['enabled']) {
$attachments_with_children[$id] = array();
if (isset($info['children'])) {
$attachments_with_children[$id] = $info['children'];
}
}
}
return $attachments_with_children;
}
}
2009-11-14 16:46:20 +00:00
?>