working on new backend code

This commit is contained in:
John Bintz 2009-11-08 22:11:26 -05:00
parent ead478fcec
commit 4cfacec35f
10 changed files with 309 additions and 212 deletions

View File

@ -13,9 +13,16 @@ class ComicPress {
'mini_dimensions' => '100x', 'mini_dimensions' => '100x',
'helpers' => array(), 'helpers' => array(),
'addons' => array(), 'addons' => array(),
'storyline_order' => '' 'storyline_order' => '',
'subattachment_types' => array(
'rss' => 'RSS',
'archive' => 'Archive',
'mini' => 'Mini Thumb'
)
); );
var $backends = array('ComicPressBackendAttachment');
function &get_instance() { function &get_instance() {
static $instance; static $instance;
@ -58,6 +65,7 @@ class ComicPress {
} }
add_filter('intermediate_image_sizes', array(&$this, 'intermediate_image_sizes')); 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) { foreach (array('comic', 'rss', 'archive', 'mini') as $size) {
list($w, $h) = explode("x", $this->comicpress_options["${size}_dimensions"]); 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')); 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() { function announce_activated_helpers() {
echo "<center>[ <strong>Activated ComicPress helpers:</strong> " . implode(", ", array_keys($this->comicpress_options['helpers'])) . " ]</center>"; echo "<center>[ <strong>Activated ComicPress helpers:</strong> " . implode(", ", array_keys($this->comicpress_options['helpers'])) . " ]</center>";
} }

View File

@ -80,7 +80,7 @@ class ComicPressAdmin {
wp_enqueue_script('cp-admin', get_template_directory_uri() . '/js/Storyline.js', array('prototype', 'scriptaculous')); 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')); wp_enqueue_script('cp-media', get_template_directory_uri() . '/js/MediaUpload.js', array('prototype'));
} }
} }
@ -176,7 +176,6 @@ class ComicPressAdmin {
$comic_post = new ComicPressComicPost(get_post($uploading_iframe_ID)); $comic_post = new ComicPressComicPost(get_post($uploading_iframe_ID));
$ordering = $comic_post->normalize_comic_image_ordering(); $ordering = $comic_post->normalize_comic_image_ordering();
if (is_array($ordering)) {
$nonce = wp_create_nonce('comicpress'); $nonce = wp_create_nonce('comicpress');
$zoom_level = 40; $zoom_level = 40;
$current_user = wp_get_current_user(); $current_user = wp_get_current_user();
@ -199,7 +198,6 @@ class ComicPressAdmin {
include(dirname(__FILE__) . '/partials/_comic-image-ordering.inc'); include(dirname(__FILE__) . '/partials/_comic-image-ordering.inc');
} }
} }
}
/** /**
* Create a dimension selector. * Create a dimension selector.

View File

@ -0,0 +1,17 @@
<?php
class ComicPressBackend {
function _embed_image($size) {
$extras = array();
if (($dims = $this->dims($size)) !== false) {
$extras = array_merge($extras, $dims);
}
foreach ($extras as $field => $value) {
$extras[] = "${field}=\"${value}\"";
unset($extras[$field]);
}
$output = sprintf('<img src="%s" alt="%s" title="%s" %s />', $this->url(), $this->alt(), $this->title(), implode(" ", $extras));
return apply_filters('comicpress_embed_image', $output, $this);
}
}

View File

@ -8,15 +8,13 @@ class ComicPressComicPost {
if (!is_null($post)) { $this->post = $post; } if (!is_null($post)) { $this->post = $post; }
} }
function get_comic_image_attachments() { function get_attachments() {
if (is_null($this->attachments)) { $comicpress = ComicPress::get_instance();
$this->attachments = get_children(array( $attachments = array();
'post_parent' => $this->post->ID, foreach ($comicpress->backends as $backend) {
'post_type' => 'attachment', $attachments = array_merge($attachments, call_user_func(array($backend, 'generate_from_post'), $this->post));
'post_mime_type' => 'image'
));
} }
return $this->attachments; return $attachments;
} }
/** /**
@ -107,58 +105,54 @@ class ComicPressComicPost {
* Normalize the ordering of comic images in this post. * Normalize the ordering of comic images in this post.
* If images have beed added or removed, intelligently update the metadata. * If images have beed added or removed, intelligently update the metadata.
*/ */
function normalize_comic_image_ordering() { function normalize_ordering() {
if (is_array($this->get_comic_image_attachments())) { $attachments = $this->get_attachments();
$ordering_by_type = array(); if (is_array($attachments)) {
$ordering_types = get_post_meta($this->post->ID, 'comic_ordering', true); $new_ordering = array();
$current_ordering = get_post_meta($this->post->ID, 'image-ordering', true);
if (!is_array($current_ordering)) { $current_ordering = array(); }
$comic_image_ordering = array(); $all_current_ids = array();
$found_post_ids = array(); foreach ($current_ordering as $key => $children) {
if (!empty($ordering_types)) { $all_current_ids[$key] = true;
foreach ($ordering_types as $type => $post_ids) { if (is_array($children)) {
$comic_image_ordering[$type] = array(); foreach ($children as $type => $kids) {
foreach ($post_ids as $ordering_post_id) { $all_current_ids = array_merge($all_current_ids, $kids);
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;
}
}
} }
} }
} }
$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;
} }
$remaining_posts_to_sort = array(); foreach ($missing_attachments as $attachment_id) {
foreach ($this->get_comic_image_attachments() as $attachment) { if (isset($current_ordering[$attachment_id])) {
$comic_image_type = get_post_meta($attachment->ID, 'comic_image_type', true); unset($current_ordering[$attachment_id]);
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 { } else {
$comic_image_ordering[$type] = $posts; 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, 'comic_ordering', $comic_image_ordering); update_post_meta($this->post->ID, 'image-ordering', $current_ordering);
return $comic_image_ordering; return $current_ordering;
} }
return false; return false;
} }

View File

@ -1,17 +1,64 @@
<?php <?php
class ComicPressBackendAttachment { require_once(dirname(__FILE__) . '/../ComicPressBackend.inc');
function ComicPressBackendAttachment($attachment_id) {
$this->attachment_id = $attachment_id; 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 get_url() { function ComicPressBackendAttachment($attachment) {
return wp_get_attachment_url($this->attachment_id); $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_info() { function dims($size = 'comic') {
return wp_get_attachment_metadata($this->attachment_id); $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 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(); }
}
?> ?>

View File

@ -9,6 +9,7 @@
<?php _e('Click the Refesh button underneath the zoom slider if you\'ve changed the images attached to this post.', 'comicpress') ?> <?php _e('Click the Refesh button underneath the zoom slider if you\'ve changed the images attached to this post.', 'comicpress') ?>
</em></p> </em></p>
<?php if (!empty($ordering)) { ?>
<?php foreach ($ordering as $type => $attachment_ids) { ?> <?php foreach ($ordering as $type => $attachment_ids) { ?>
<h3><?php echo $this->comic_image_types[$type] ?></h3> <h3><?php echo $this->comic_image_types[$type] ?></h3>
<div class="comic-ordering" id="comic-ordering-<?php echo $type ?>"> <div class="comic-ordering" id="comic-ordering-<?php echo $type ?>">
@ -16,7 +17,7 @@
$backend = new ComicPressBackendAttachment($attachment_id); $backend = new ComicPressBackendAttachment($attachment_id);
$info = $backend->get_info(); ?> $info = $backend->get_info(); ?>
<div class="cp-comic-attachment" id="attachment_<?php echo $attachment_id ?>"> <div class="cp-comic-attachment" id="attachment_<?php echo $attachment_id ?>">
<img src="<?php echo $backend->get_url() ?>" border="0" height="<?php echo $zoom_level ?>" /> <img src="<?php echo $backend->url() ?>" border="0" height="<?php echo $zoom_level ?>" />
<div> <div>
<?php if (isset($info['file'])) { ?> <?php if (isset($info['file'])) { ?>
<p><strong><?php echo basename($info['file']) ?></strong></p> <p><strong><?php echo basename($info['file']) ?></strong></p>
@ -33,6 +34,7 @@
<?php } ?> <?php } ?>
</div> </div>
<?php } ?> <?php } ?>
<?php } ?>
<script type="text/javascript"> <script type="text/javascript">
(function() { (function() {

View File

@ -144,6 +144,14 @@ function RT($which, $restrictions = null, $override_post = null) {
return $post; return $post;
} }
function M($override_post = null) {
global $post;
$post_to_use = !is_null($override_post) ? $override_post : $post;
$comic_post = new ComicPressComicPost($post_to_use);
return $comic_post->get_attachments();
}
/** /**
* Display the list of Storyline categories. * Display the list of Storyline categories.
*/ */

View File

@ -11,6 +11,11 @@
RT('previous', array('root_of' => '__post')); the_title(); echo '<br />'; RT('previous', array('root_of' => '__post')); the_title(); echo '<br />';
RT('previous'); the_title(); echo '<br />'; RT('previous'); the_title(); echo '<br />';
Restore(); the_title(); echo '<br />'; Restore(); the_title(); echo '<br />';
foreach (M() as $image) {
echo $image->embed();
}
RT('next'); the_title(); echo '<br />'; RT('next'); the_title(); echo '<br />';
RT('next', array('root_of' => '__post')); the_title(); echo '<br />'; RT('next', array('root_of' => '__post')); the_title(); echo '<br />';
RT('next', 'from_post'); the_title(); echo '<br />'; RT('next', 'from_post'); the_title(); echo '<br />';

View File

@ -47,80 +47,51 @@ class ComicPressComicPostTest extends PHPUnit_Framework_TestCase {
} }
} }
function testNormalizeComicImageOrdering() { function providerTestNormalizeOrdering() {
$p = $this->getMock('ComicPressComicPost', array('get_comic_image_attachments')); return array(
$comic_attachments = array(
array( array(
'ID' => 2, array('attachment-1'),
'post_parent' => 1, array(),
'post_title' => 'Comic one', array('attachment-1' => true)
'post_meta' => array(
'comic_image_type' => 'comic'
),
'post_date' => 1
), ),
array( array(
'ID' => 3, array('attachment-1'),
'post_parent' => 1, array('attachment-1' => true, 'attachment-2' => true),
'post_title' => 'Comic two', array('attachment-1' => true)
'post_meta' => array(
'comic_image_type' => 'comic'
),
'post_date' => 2
), ),
array( array(
'ID' => 4, array('attachment-1'),
'post_parent' => 1, array('attachment-1' => array('rss' => array('attachment-2' => true))),
'post_title' => 'Comic three', array('attachment-1' => true)
'post_meta' => array(
'comic_image_type' => 'rss'
),
'post_date' => 4
), ),
array( array(
'ID' => 5, array('attachment-1', 'attachment-2', 'attachment-3'),
'post_parent' => 1, array('attachment-1' => array('rss' => array('attachment-2' => true))),
'post_title' => 'Comic four', array('attachment-1' => array('rss' => array('attachment-2' => true)), 'attachment-3' => true)
'post_meta' => array(
'comic_image_type' => 'rss'
),
'post_date' => 3
), ),
); );
$attachments = array();
foreach ($comic_attachments as $attachment_info) {
$attachment = (object)array();
foreach ($attachment_info as $field => $value) {
switch ($field) {
case "post_meta":
foreach ($value as $meta => $meta_value) {
update_post_meta($attachment_info['ID'], $meta, $meta_value);
}
break;
case "post_date":
$attachment->{$field} = date("r", $value);
break;
default:
$attachment->{$field} = $value;
break;
}
}
$attachments[] = $attachment;
} }
$p->expects($this->any())->method('get_comic_image_attachments')->will($this->returnValue($attachments)); /**
* @dataProvider providerTestNormalizeOrdering
*/
function testNormalizeOrdering($attachments, $current_meta, $expected_result) {
$p = $this->getMock('ComicPressComicPost', array('get_attachments'));
$attachment_objects = array();
foreach ($attachments as $attachment) {
$attachment_objects[] = (object)array('id' => $attachment);
}
$p->expects($this->any())->method('get_attachments')->will($this->returnValue($attachment_objects));
wp_insert_post((object)array('ID' => 1)); wp_insert_post((object)array('ID' => 1));
update_post_meta(1, 'comic_ordering', array('comic' => array(3))); update_post_meta(1, 'image-ordering', $current_meta);
$p->post = (object)array('ID' => 1); $p->post = (object)array('ID' => 1);
$result = $p->normalize_comic_image_ordering(); $this->assertEquals($expected_result, $p->normalize_ordering());
$this->assertEquals($expected_result, get_post_meta(1, 'image-ordering', true));
$this->assertEquals(array('comic' => array(3,2), 'rss' => array(5,4)), $result);
$this->assertEquals(array('comic' => array(3,2), 'rss' => array(5,4)), get_post_meta(1, 'comic_ordering', true));
} }
function providerTestChangeComicImageOrdering() { function providerTestChangeComicImageOrdering() {

View File

@ -0,0 +1,39 @@
<?php
require_once('PHPUnit/Framework.php');
require_once('MockPress/mockpress.php');
require_once('backends/ComicPressBackendAttachment.inc');
class ComicPressBackendAttachmentTest extends PHPUnit_Framework_TestCase {
function setUp() {
_reset_wp();
}
function providerTestGenerateFromPost() {
return array(
array(array(), false),
array(array((object)array('ID' => 1)), array('attachment-1'))
);
}
/**
* @dataProvider providerTestGenerateFromPost
*/
function testGenerateFromPost($get_children_response, $expected_ids) {
_set_get_children(array(
'post_parent' => 1,
'post_type' => 'attachment',
'post_mime_type' => 'image'
), $get_children_response);
$results = ComicPressBackendAttachment::generate_from_post((object)array('ID' => 1));
if ($expected_ids === false) {
$this->assertTrue(empty($results));
} else {
$this->assertEquals(count($expected_ids), count($results));
foreach ($results as $result) {
$this->assertTrue(in_array($result->id, $expected_ids));
}
}
}
}