From 4cfacec35fec3d695cf794cba56be3495adf563b Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sun, 8 Nov 2009 22:11:26 -0500 Subject: [PATCH] working on new backend code --- classes/ComicPress.inc | 18 ++- classes/ComicPressAdmin.inc | 36 ++--- classes/ComicPressBackend.inc | 17 ++ classes/ComicPressComicPost.inc | 148 +++++++++--------- .../backends/ComicPressBackendAttachment.inc | 61 +++++++- .../_comic-image-ordering-sorters.inc | 48 +++--- functions.php | 8 + single.php | 5 + test/ComicPressComicPostTest.php | 141 +++++++---------- .../ComicPressBackendAttachmentTest.php | 39 +++++ 10 files changed, 309 insertions(+), 212 deletions(-) create mode 100644 classes/ComicPressBackend.inc create mode 100644 test/backends/ComicPressBackendAttachmentTest.php diff --git a/classes/ComicPress.inc b/classes/ComicPress.inc index 1e259a6..3aaead3 100644 --- a/classes/ComicPress.inc +++ b/classes/ComicPress.inc @@ -13,9 +13,16 @@ class ComicPress { 'mini_dimensions' => '100x', 'helpers' => array(), 'addons' => array(), - 'storyline_order' => '' + 'storyline_order' => '', + 'subattachment_types' => array( + 'rss' => 'RSS', + 'archive' => 'Archive', + 'mini' => 'Mini Thumb' + ) ); + var $backends = array('ComicPressBackendAttachment'); + function &get_instance() { static $instance; @@ -58,6 +65,7 @@ class ComicPress { } add_filter('intermediate_image_sizes', array(&$this, 'intermediate_image_sizes')); + add_filter('editor_max_image_size', array(&$this, 'editor_max_image_size'), 10, 2); foreach (array('comic', 'rss', 'archive', 'mini') as $size) { list($w, $h) = explode("x", $this->comicpress_options["${size}_dimensions"]); @@ -71,6 +79,14 @@ class ComicPress { return array_merge($sizes, array('comic', 'rss', 'archive', 'mini')); } + function editor_max_image_size($current_max, $size) { + if (isset($this->comicpress_options["${size}_dimensions"])) { + list($width, $height) = explode('x', $this->comicpress_options["${size}_dimensions"]); + $current_max = array(intval($width), intval($height)); + } + return $current_max; + } + function announce_activated_helpers() { echo "
[ Activated ComicPress helpers: " . implode(", ", array_keys($this->comicpress_options['helpers'])) . " ]
"; } diff --git a/classes/ComicPressAdmin.inc b/classes/ComicPressAdmin.inc index 8beda33..f4d42cf 100644 --- a/classes/ComicPressAdmin.inc +++ b/classes/ComicPressAdmin.inc @@ -80,7 +80,7 @@ class ComicPressAdmin { wp_enqueue_script('cp-admin', get_template_directory_uri() . '/js/Storyline.js', array('prototype', 'scriptaculous')); } - if (strpos($pagenow, "media-upload") === 0) { + if (strpos($pagenow, "-upload") !== false) { wp_enqueue_script('cp-media', get_template_directory_uri() . '/js/MediaUpload.js', array('prototype')); } } @@ -176,28 +176,26 @@ class ComicPressAdmin { $comic_post = new ComicPressComicPost(get_post($uploading_iframe_ID)); $ordering = $comic_post->normalize_comic_image_ordering(); - if (is_array($ordering)) { - $nonce = wp_create_nonce('comicpress'); - $zoom_level = 40; - $current_user = wp_get_current_user(); - if (!empty($current_user)) { - $comicpress_meta = get_usermeta($current_user->ID, 'comicpress-settings'); - if (is_array($comicpress_meta)) { - if (isset($comicpress_meta['zoom_level'])) { - $zoom_level = floor($comicpress_meta['zoom_level']); - } + $nonce = wp_create_nonce('comicpress'); + $zoom_level = 40; + $current_user = wp_get_current_user(); + if (!empty($current_user)) { + $comicpress_meta = get_usermeta($current_user->ID, 'comicpress-settings'); + if (is_array($comicpress_meta)) { + if (isset($comicpress_meta['zoom_level'])) { + $zoom_level = floor($comicpress_meta['zoom_level']); } } + } - // from wp-admin/includes/media.php O_o - $media_upload_iframe_src = "media-upload.php?post_id=$uploading_iframe_ID"; - $image_upload_iframe_src = apply_filters('image_upload_iframe_src', "$media_upload_iframe_src&type=image&TB_iframe=true"); + // from wp-admin/includes/media.php O_o + $media_upload_iframe_src = "media-upload.php?post_id=$uploading_iframe_ID"; + $image_upload_iframe_src = apply_filters('image_upload_iframe_src', "$media_upload_iframe_src&type=image&TB_iframe=true"); - if ($is_ajax === true) { - include(dirname(__FILE__) . '/partials/_comic-image-ordering-sorters.inc'); - } else { - include(dirname(__FILE__) . '/partials/_comic-image-ordering.inc'); - } + if ($is_ajax === true) { + include(dirname(__FILE__) . '/partials/_comic-image-ordering-sorters.inc'); + } else { + include(dirname(__FILE__) . '/partials/_comic-image-ordering.inc'); } } diff --git a/classes/ComicPressBackend.inc b/classes/ComicPressBackend.inc new file mode 100644 index 0000000..7ddd7ff --- /dev/null +++ b/classes/ComicPressBackend.inc @@ -0,0 +1,17 @@ +dims($size)) !== false) { + $extras = array_merge($extras, $dims); + } + foreach ($extras as $field => $value) { + $extras[] = "${field}=\"${value}\""; + unset($extras[$field]); + } + + $output = sprintf('%s', $this->url(), $this->alt(), $this->title(), implode(" ", $extras)); + return apply_filters('comicpress_embed_image', $output, $this); + } +} \ No newline at end of file diff --git a/classes/ComicPressComicPost.inc b/classes/ComicPressComicPost.inc index fe73f61..697e1f3 100644 --- a/classes/ComicPressComicPost.inc +++ b/classes/ComicPressComicPost.inc @@ -8,17 +8,15 @@ class ComicPressComicPost { if (!is_null($post)) { $this->post = $post; } } - function get_comic_image_attachments() { - 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; + 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; } - + /** * Display all the attached images. */ @@ -33,19 +31,19 @@ class ComicPressComicPost { if (get_post_meta($attachment_id, "comic_image_type", true) == $type) { $attachment = get_post($attachment_id); $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]; } - - $output[] = apply_filters('comicpress_attached_image', + + $output[] = apply_filters('comicpress_attached_image', sprintf($format, $this->get_comic_img_tag($url, $size_type, array('title' => $title))), $attachment_id, $i++); $found = true; - + if (!is_null($limit)) { if (--$limit == 0) { break; } } @@ -58,7 +56,7 @@ class ComicPressComicPost { return $found; } - + function _display_type($types, $format, $single = false) { $target_type = reset($types); foreach ($types as $type) { @@ -67,7 +65,7 @@ class ComicPressComicPost { } } } - + 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); } @@ -77,7 +75,7 @@ class ComicPressComicPost { */ function get_comic_img_tag($url, $type, $additional_parameters = array()) { $dimensions = array(); - + if (isset($this->comicpress->comicpress_options["${type}_dimensions"])) { $parts = explode("x", $this->comicpress->comicpress_options["${type}_dimensions"]); switch (count($parts)) { @@ -86,7 +84,7 @@ class ComicPressComicPost { } $dimensions = compact('width', 'height'); } - + $output = ' $value) { - $output .= $parameter . '="' . $value . '" '; + $output .= $parameter . '="' . $value . '" '; } } $output .= "/>"; - + return $output; } @@ -107,62 +105,58 @@ class ComicPressComicPost { * Normalize the ordering of comic images in this post. * If images have beed added or removed, intelligently update the metadata. */ - function normalize_comic_image_ordering() { - if (is_array($this->get_comic_image_attachments())) { - $ordering_by_type = array(); - $ordering_types = get_post_meta($this->post->ID, 'comic_ordering', true); - - $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) { - foreach ($this->get_comic_image_attachments() as $attachment) { - if (!isset($found_post_ids[$attachment->ID])) { - if ($attachment->ID == $ordering_post_id) { - $comic_image_ordering[$type][] = $attachment->ID; - $found_post_ids[$ordering_post_id] = true; - } - } - } - } - } + 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); + } + } } - - $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')); - } - - foreach ($remaining_posts_to_sort as $type => $posts) { - if (!isset($comic_image_ordering[$type])) { $comic_image_ordering[$type] = array(); } - if (is_array($comic_image_ordering[$type])) { - $comic_image_ordering[$type] = array_merge($comic_image_ordering[$type], $posts); - } else { - $comic_image_ordering[$type] = $posts; - } + $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; } - update_post_meta($this->post->ID, 'comic_ordering', $comic_image_ordering); - return $comic_image_ordering; + 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; + } + } + } + } + + update_post_meta($this->post->ID, 'image-ordering', $current_ordering); + return $current_ordering; } return false; } - + /** * Sort the remaining comic images by file date. * @param object $a @@ -172,7 +166,7 @@ class ComicPressComicPost { 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; - return $a_date - $b_date; + return $a_date - $b_date; } /** @@ -182,7 +176,7 @@ class ComicPressComicPost { function change_comic_image_ordering($requested_new_order) { $orderings = get_post_meta($this->post->ID, 'comic_ordering', true); if (!is_array($orderings)) { $orderings = array(); } - + $new_order = array(); $requested_new_order = (array)$requested_new_order; @@ -193,7 +187,7 @@ class ComicPressComicPost { $position = 0; foreach ($requested_new_order[$type] as $id) { if (!isset($sort_by_position[$position])) { - $sort_by_position[$position] = array(); + $sort_by_position[$position] = array(); } $sort_by_position[$position][] = $id; $position++; @@ -213,7 +207,7 @@ class ComicPressComicPost { } } } - + update_post_meta($this->post->ID, 'comic_ordering', $new_order); } @@ -228,16 +222,16 @@ class ComicPressComicPost { $category_parent = $category->parent; if ($category_parent != 0) { $post_categories[] = $category_parent; - } + } } } while ($category_parent != 0); - + foreach ($post_categories as $category_id) { $category = get_category($category_id); $parents[$category_id] = $category->slug; } } - + return $parents; } } diff --git a/classes/backends/ComicPressBackendAttachment.inc b/classes/backends/ComicPressBackendAttachment.inc index 13b04c8..e2d9434 100644 --- a/classes/backends/ComicPressBackendAttachment.inc +++ b/classes/backends/ComicPressBackendAttachment.inc @@ -1,17 +1,64 @@ attachment_id = $attachment_id; +require_once(dirname(__FILE__) . '/../ComicPressBackend.inc'); + +class ComicPressBackendAttachment extends ComicPressBackend { + var $root_id = 'attachment'; + + function generate_from_post($post) { + $result = array(); + if (is_object($post)) { + if (isset($post->ID)) { + $children = get_children(array( + 'post_parent' => $post->ID, + 'post_type' => 'attachment', + 'post_mime_type' => 'image' + )); + + if (!empty($children)) { + foreach ($children as $child) { + $result[] = new ComicPressBackendAttachment($child); + } + } + } + } + return $result; + } + + function ComicPressBackendAttachment($attachment) { + $this->attachment = $attachment; + $this->id = sprintf('%s-%d', $this->root_id, $this->attachment->ID); + $this->type = get_post_meta($this->attachment->ID, 'comic_image_type', true); } - function get_url() { - return wp_get_attachment_url($this->attachment_id); + function dims($size = 'comic') { + $metadata = image_downsize($this->attachment->ID, $size); + if (!empty($metadata)) { + if (is_array($metadata)) { + return array_combine(array('width', 'height'), array_slice($metadata, 1, 2)); + } + } + return false; } - function get_info() { - return wp_get_attachment_metadata($this->attachment_id); + function url($size = 'comic') { + $metadata = image_downsize($this->attachment->ID, $size); + if (!empty($metadata)) { + if (is_array($metadata)) { + return $metadata[0]; + } + } + return false; } + + function embed($size = 'comic') { + return $this->_embed_image($size); + } + + function alt() { return $this->attachment->post_title; } + function title() { return $this->attachment->post_excerpt; } + + function get_info() { return array(); } } ?> \ No newline at end of file diff --git a/classes/partials/_comic-image-ordering-sorters.inc b/classes/partials/_comic-image-ordering-sorters.inc index d3a4daf..9de8d93 100644 --- a/classes/partials/_comic-image-ordering-sorters.inc +++ b/classes/partials/_comic-image-ordering-sorters.inc @@ -9,29 +9,31 @@

- $attachment_ids) { ?> -

comic_image_types[$type] ?>

-
- get_info(); ?> -
- -
- -

- - -

- - -

- -
-
-
- -
+ + $attachment_ids) { ?> +

comic_image_types[$type] ?>

+
+ get_info(); ?> +
+ +
+ +

+ + +

+ + +

+ +
+
+
+ +
+