diff --git a/classes/ComicPressBackend.inc b/classes/ComicPressBackend.inc
index f30de2f..ac216d5 100644
--- a/classes/ComicPressBackend.inc
+++ b/classes/ComicPressBackend.inc
@@ -36,11 +36,9 @@ class ComicPressBackend {
}
function ensure_type($type) {
- if (is_null($type)) {
+ if (is_null($type) || ($type == 'default')) {
$comicpress = ComicPress::get_instance();
- if (is_null($type)) {
- $type = $comicpress->get_default_image_type();
- }
+ $type = $comicpress->get_default_image_type();
}
return $type;
}
diff --git a/classes/ComicPressTagBuilder.inc b/classes/ComicPressTagBuilder.inc
index 3334b52..5ce272e 100644
--- a/classes/ComicPressTagBuilder.inc
+++ b/classes/ComicPressTagBuilder.inc
@@ -53,17 +53,9 @@ class ComicPressTagBuilderFactory {
global $post;
$tag = $this->_new_comicpresstagbuilder($post, $this->storyline, $this->dbi);
- $media = $tag->media();
- if (is_null($index)) {
- return $media;
- } else {
- $index = (int)$index;
- if (isset($media[$index])) {
- return $media[$index];
- } else {
- return false;
- }
- }
+ $tag->post = $post;
+
+ return $tag->media();
}
/**
@@ -221,7 +213,7 @@ class ComicPressTagBuilder {
case 'media':
if (isset($this->post)) {
$comic_post = $this->_new_comicpresscomicpost($this->post);
- return $comic_post->get_attachments_with_children(true);
+ return new ComicPressMediaWrapper($comic_post->get_attachments_with_children(true));
}
case 'category':
if (isset($arguments[0])) {
@@ -356,3 +348,47 @@ class ComicPressTagBuilder {
return $methods;
}
}
+
+class ComicPressMediaWrapper {
+ public $media;
+
+ public function __construct($media) {
+ $this->media = $media;
+ }
+
+ public function __call($method, $arguments) {
+ $type = (isset($arguments[0])) ? $arguments[0] : 'default';
+ $which = 'all';
+ $separator = '';
+
+ if (count($arguments) == 2) {
+ if (is_numeric($arguments[1])) {
+ $which = (int)$arguments[1];
+ } else {
+ $separator = $arguments[1];
+ }
+ }
+
+ if (is_numeric($which)) {
+ if (isset($this->media[$which][$type])) {
+ if ($media = ComicPressBackend::generate_from_id($this->media[$which][$type])) {
+ return $media->{$method}($type);
+ }
+ }
+ return false;
+ } else {
+ $output = array();
+ foreach ($this->media as $ids) {
+ if ($media = ComicPressBackend::generate_from_id($ids[$type])) {
+ $output[] = $media->{$method}($type);
+ }
+ }
+ if (is_string($separator)) {
+ $output = implode($separator, $output);
+ }
+ return $output;
+ }
+
+ return false;
+ }
+}
diff --git a/test/ComicPressBackendTest.php b/test/ComicPressBackendTest.php
index 7bf6296..8f7b4b2 100644
--- a/test/ComicPressBackendTest.php
+++ b/test/ComicPressBackendTest.php
@@ -72,7 +72,8 @@ class ComicPressBackendTest extends PHPUnit_Framework_TestCase {
return array(
array(null, 'comic'),
array('comic', 'comic'),
- array('rss', 'rss')
+ array('rss', 'rss'),
+ array('default', 'comic'),
);
}
@@ -114,4 +115,4 @@ class ComicPressBackendTest extends PHPUnit_Framework_TestCase {
'file' => '/root/file.jpg'
), $ba->get_info());
}
-}
\ No newline at end of file
+}
diff --git a/test/ComicPressTagBuilderTest.php b/test/ComicPressTagBuilderTest.php
index f060acd..8b1ce65 100644
--- a/test/ComicPressTagBuilderTest.php
+++ b/test/ComicPressTagBuilderTest.php
@@ -342,7 +342,7 @@ class ComicPressTagBuilderTest extends PHPUnit_Framework_TestCase {
->with('this-post')
->will($this->returnValue($comicpresscomicpost));
- $this->assertEquals(array('post-media'), $core->media());
+ $this->assertEquals(new ComicPressMediaWrapper(array('post-media')), $core->media());
}
function testMediaForCurrentPost() {
@@ -362,19 +362,16 @@ class ComicPressTagBuilderTest extends PHPUnit_Framework_TestCase {
$tag_builder = $this->getMock('ComicPressTagBuilder', array('media'), array($post, 'storyline', 'dbi'));
- $tag_builder->expects($this->any())
+ $tag_builder->expects($this->once())
->method('media')
->will($this->returnValue(array('post-media')));
- $core->expects($this->any())
+ $core->expects($this->once())
->method('_new_comicpresstagbuilder')
->with($post, 'storyline', 'dbi')
->will($this->returnValue($tag_builder));
$this->assertEquals(array('post-media'), $core->media());
-
- $this->assertEquals('post-media', $core->media(0));
- $this->assertEquals(false, $core->media(1));
}
function testComicPressComicPost() {
@@ -604,4 +601,78 @@ class ComicPressTagBuilderTest extends PHPUnit_Framework_TestCase {
$this->assertTrue(!isset($core->_post));
$this->assertTrue(!isset($core->_wp_query));
}
+
+ function providerTestComicPressMediaWrapper() {
+ return array(
+ array(
+ array(), 'default-id-1default-id-2'
+ ),
+ array(
+ array('comic'), 'comic-id-1comic-id-2'
+ ),
+ array(
+ array('default', '
'), 'default-id-1
default-id-2'
+ ),
+ array(
+ array('default', 0), 'default-id-1'
+ ),
+ array(
+ array('default', 2), false
+ ),
+ array(
+ array('archive', 0), false
+ ),
+ array(
+ array('comic', 0), false, true
+ ),
+ );
+ }
+
+ /**
+ * @dataProvider providerTestComicPressMediaWrapper
+ */
+ function testComicPressMediaWrapper($arguments, $expected_return, $is_total_fail = false) {
+ $backend = $this->getMock('ComicPressMockBackendFactory', array('embed', 'generate_from_id'));
+ $backend->expects($this->any())
+ ->method('generate_from_id')
+ ->will($this->returnCallback(function($id) use ($backend) {
+ if (in_array($id, array('comic-id-1', 'default-id-1', 'comic-id-2', 'default-id-2'))) {
+ $backend->_id = $id;
+ return $backend;
+ } else {
+ return false;
+ }
+ }));
+
+ $backend->expects($this->any())
+ ->method('embed')
+ ->will($this->returnCallback(function($which) use ($backend) {
+ switch ($which) {
+ case 'comic':
+ case 'default':
+ return $backend->_id;
+ break;
+ default:
+ return false;
+ }
+ }));
+
+ $comicpress = ComicPress::get_instance(true);
+ $comicpress->backends = array($backend);
+
+ $media = new ComicPressMediaWrapper(array(
+ array(
+ 'comic' => $is_total_fail ? 'total-fail' : 'comic-id-1',
+ 'default' => 'default-id-1'
+ ),
+ array(
+ 'comic' => 'comic-id-2',
+ 'default' => 'default-id-2'
+ ),
+ ));
+
+ $this->assertEquals($expected_return, call_user_func_array(array($media, 'embed'), $arguments));
+
+ $comicpress = ComicPress::get_instance(true);
+ }
}