get attachment info

This commit is contained in:
John Bintz 2009-11-11 22:31:36 -05:00
parent bf9ea351f0
commit b287c05e21
4 changed files with 57 additions and 22 deletions

View File

@ -21,7 +21,7 @@ class ComicPress {
)
);
var $backends = array('ComicPressBackendAttachment');
var $backends = array();
function &get_instance() {
static $instance;
@ -53,7 +53,7 @@ class ComicPress {
}
// @codeCoverageIgnoreStart
/**
* Initialize the class.
*/
@ -75,6 +75,12 @@ class ComicPress {
update_option("${size}_size_h", $h);
update_option("${size}_crop", 0);
}
foreach (get_declared_classes() as $class) {
if (preg_match('#^ComicPressBackend.+$#', $class) > 0) {
$this->backends[] = new $class();
}
}
}
// WordPress Filters

View File

@ -57,23 +57,32 @@ class ComicPressBackendAttachment extends ComicPressBackend {
}
// @codeCoverageIgnoreStart
function embed($size = 'comic') {
return $this->_embed_image($size);
}
function embed($size = 'comic') { return $this->_embed_image($size); }
function file($size = 'comic') { return $this->attachment->guid; }
function alt() { return $this->attachment->post_title; }
function title() { return $this->attachment->post_excerpt; }
function get_info() { return array(); }
// @codeCoverageIgnoreEnd
function get_info($size = 'comic') {
$info = array();
foreach (array('dims', 'url', 'file') as $field) {
$result = $this->{$field}($size);
if (is_array($result)) {
$info = array_merge($info, $result);
} else {
$info[$field] = $result;
}
}
return $info;
}
function generate_from_id($id) {
if (strpos($id, 'attachment-') === 0) {
$id = str_replace('attachment-', '', $id);
if (is_numeric($id)) {
$post = get_post($id);
if (!empty($post)) {
return $post;
return new ComicPressBackendAttachment($post);
}
}
}

View File

@ -1,5 +1,5 @@
<p><em>
<?php _e('Drag and drop the comic files below to change the order in which they\'ll appear in within each category.', 'comicpress') ?>
<?php _e('Drag and drop the comic files below to change the order in which they\'ll appear.', 'comicpress') ?>
<?php
printf(
__('To upload new images, use the %s.', 'comicpress'),
@ -10,14 +10,13 @@
</em></p>
<?php if (!empty($ordering)) { ?>
<?php foreach ($ordering as $type => $attachment_ids) { ?>
<h3><?php echo $this->comic_image_types[$type] ?></h3>
<div class="comic-ordering" id="comic-ordering-<?php echo $type ?>">
<?php foreach ($attachment_ids as $attachment_id) {
$backend = new ComicPressBackendAttachment($attachment_id);
$info = $backend->get_info(); ?>
<div class="cp-comic-attachment" id="attachment_<?php echo $attachment_id ?>">
<img src="<?php echo $backend->url() ?>" border="0" height="<?php echo $zoom_level ?>" />
<div class="comic-ordering">
<?php foreach ($ordering as $id => $info) {
$result = ComicPressBackend::generate_from_id($id);
if (!empty($result)) {
$info = $result->get_info(); ?>
<div class="cp-comic-attachment" id="attachment_<?php echo $id ?>">
<img src="<?php echo $result->url() ?>" border="0" height="<?php echo $zoom_level ?>" />
<div>
<?php if (isset($info['file'])) { ?>
<p><strong><?php echo basename($info['file']) ?></strong></p>
@ -31,9 +30,9 @@
</div>
<br style="clear: both" />
</div>
<?php } ?>
</div>
<?php } ?>
<?php } ?>
<?php } ?>
</div>
<?php } ?>
<script type="text/javascript">

View File

@ -92,6 +92,27 @@ class ComicPressBackendAttachmentTest extends PHPUnit_Framework_TestCase {
function testGenerateFromID($id, $is_successful) {
wp_insert_post(array('ID' => 1));
$this->assertTrue($is_successful == is_object($this->ba->generate_from_id($id)));
if ($is_successful) {
$return = new ComicPressBackendAttachment((object)array('ID' => 1));
} else {
$return = false;
}
$this->assertEquals($return, $this->ba->generate_from_id($id));
}
function testGetInfo() {
$ba = $this->getMock('ComicPressBackendAttachment', array('dims', 'url', 'file'), array(), 'Mock_ComicPressBackendAttachment', false);
$ba->expects($this->once())->method('dims')->will($this->returnValue(array('width' => 320, 'height' => 240)));
$ba->expects($this->once())->method('url')->will($this->returnValue('http://blah/file.jpg'));
$ba->expects($this->once())->method('file')->will($this->returnValue('/root/file.jpg'));
$this->assertEquals(array(
'width' => 320,
'height' => 240,
'url' => 'http://blah/file.jpg',
'file' => '/root/file.jpg'
), $ba->get_info());
}
}