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,28 +176,26 @@ 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(); if (!empty($current_user)) {
if (!empty($current_user)) { $comicpress_meta = get_usermeta($current_user->ID, 'comicpress-settings');
$comicpress_meta = get_usermeta($current_user->ID, 'comicpress-settings'); if (is_array($comicpress_meta)) {
if (is_array($comicpress_meta)) { if (isset($comicpress_meta['zoom_level'])) {
if (isset($comicpress_meta['zoom_level'])) { $zoom_level = floor($comicpress_meta['zoom_level']);
$zoom_level = floor($comicpress_meta['zoom_level']);
}
} }
} }
}
// from wp-admin/includes/media.php O_o // from wp-admin/includes/media.php O_o
$media_upload_iframe_src = "media-upload.php?post_id=$uploading_iframe_ID"; $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&amp;type=image&amp;TB_iframe=true"); $image_upload_iframe_src = apply_filters('image_upload_iframe_src', "$media_upload_iframe_src&amp;type=image&amp;TB_iframe=true");
if ($is_ajax === true) { if ($is_ajax === true) {
include(dirname(__FILE__) . '/partials/_comic-image-ordering-sorters.inc'); include(dirname(__FILE__) . '/partials/_comic-image-ordering-sorters.inc');
} else { } else {
include(dirname(__FILE__) . '/partials/_comic-image-ordering.inc'); include(dirname(__FILE__) . '/partials/_comic-image-ordering.inc');
}
} }
} }

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 $attachments;
}
return $this->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; $all_current_ids = array_keys($all_current_ids);
$found_post_ids[$ordering_post_id] = true;
} $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]);
} else {
if (!empty($comic_image_type)) { foreach ($current_ordering as $key => $children) {
if (!isset($found_post_ids[$attachment->ID])) { foreach ($children as $type => $kids) {
if (!isset($remaining_posts_to_sort[$comic_image_type])) { if (isset($kids[$attachment_id])) { unset($kids[$attachment_id]); }
$remaining_posts_to_sort[$comic_image_type] = array(); $children[$type] = $kids;
} }
$remaining_posts_to_sort[$comic_image_type][] = $attachment->ID; if (empty($children[$type])) {
} $current_ordering[$key] = true;
} } else {
$current_ordering[$key] = $children;
}
}
}
} }
foreach ($remaining_posts_to_sort as $type => $posts) { update_post_meta($this->post->ID, 'image-ordering', $current_ordering);
usort($remaining_posts_to_sort[$type], array(&$this, 'sort_remaining_comic_images')); return $current_ordering;
}
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;
}
}
update_post_meta($this->post->ID, 'comic_ordering', $comic_image_ordering);
return $comic_image_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 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() { function dims($size = 'comic') {
return wp_get_attachment_url($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 get_info() { function url($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 $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,29 +9,31 @@
<?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 foreach ($ordering as $type => $attachment_ids) { ?> <?php if (!empty($ordering)) { ?>
<h3><?php echo $this->comic_image_types[$type] ?></h3> <?php foreach ($ordering as $type => $attachment_ids) { ?>
<div class="comic-ordering" id="comic-ordering-<?php echo $type ?>"> <h3><?php echo $this->comic_image_types[$type] ?></h3>
<?php foreach ($attachment_ids as $attachment_id) { <div class="comic-ordering" id="comic-ordering-<?php echo $type ?>">
$backend = new ComicPressBackendAttachment($attachment_id); <?php foreach ($attachment_ids as $attachment_id) {
$info = $backend->get_info(); ?> $backend = new ComicPressBackendAttachment($attachment_id);
<div class="cp-comic-attachment" id="attachment_<?php echo $attachment_id ?>"> $info = $backend->get_info(); ?>
<img src="<?php echo $backend->get_url() ?>" border="0" height="<?php echo $zoom_level ?>" /> <div class="cp-comic-attachment" id="attachment_<?php echo $attachment_id ?>">
<div> <img src="<?php echo $backend->url() ?>" border="0" height="<?php echo $zoom_level ?>" />
<?php if (isset($info['file'])) { ?> <div>
<p><strong><?php echo basename($info['file']) ?></strong></p> <?php if (isset($info['file'])) { ?>
<?php } ?> <p><strong><?php echo basename($info['file']) ?></strong></p>
<?php if (isset($info['width']) && isset($info['height'])) { ?> <?php } ?>
<p> <?php if (isset($info['width']) && isset($info['height'])) { ?>
<?php _e('Size:', 'comicpress') ?> <p>
<?php printf('%dx%d', $info['width'], $info['height'] ) ?> <?php _e('Size:', 'comicpress') ?>
</p> <?php printf('%dx%d', $info['width'], $info['height'] ) ?>
<?php } ?> </p>
</div> <?php } ?>
<br style="clear: both" /> </div>
</div> <br style="clear: both" />
<?php } ?> </div>
</div> <?php } ?>
</div>
<?php } ?>
<?php } ?> <?php } ?>
<script type="text/javascript"> <script type="text/javascript">

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(
array(
array('attachment-1'),
array(),
array('attachment-1' => true)
),
array(
array('attachment-1'),
array('attachment-1' => true, 'attachment-2' => true),
array('attachment-1' => true)
),
array(
array('attachment-1'),
array('attachment-1' => array('rss' => array('attachment-2' => true))),
array('attachment-1' => true)
),
array(
array('attachment-1', 'attachment-2', 'attachment-3'),
array('attachment-1' => array('rss' => array('attachment-2' => true))),
array('attachment-1' => array('rss' => array('attachment-2' => true)), 'attachment-3' => true)
),
);
}
$comic_attachments = array( /**
array( * @dataProvider providerTestNormalizeOrdering
'ID' => 2, */
'post_parent' => 1, function testNormalizeOrdering($attachments, $current_meta, $expected_result) {
'post_title' => 'Comic one', $p = $this->getMock('ComicPressComicPost', array('get_attachments'));
'post_meta' => array(
'comic_image_type' => 'comic'
),
'post_date' => 1
),
array(
'ID' => 3,
'post_parent' => 1,
'post_title' => 'Comic two',
'post_meta' => array(
'comic_image_type' => 'comic'
),
'post_date' => 2
),
array(
'ID' => 4,
'post_parent' => 1,
'post_title' => 'Comic three',
'post_meta' => array(
'comic_image_type' => 'rss'
),
'post_date' => 4
),
array(
'ID' => 5,
'post_parent' => 1,
'post_title' => 'Comic four',
'post_meta' => array(
'comic_image_type' => 'rss'
),
'post_date' => 3
),
);
$attachments = array(); $attachment_objects = array();
foreach ($comic_attachments as $attachment_info) { foreach ($attachments as $attachment) {
$attachment = (object)array(); $attachment_objects[] = (object)array('id' => $attachment);
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)); $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));
}
}
}
}