diff --git a/classes/ComicPressMediaHandling.inc b/classes/ComicPressMediaHandling.inc index 887c1aa..31f7e7d 100644 --- a/classes/ComicPressMediaHandling.inc +++ b/classes/ComicPressMediaHandling.inc @@ -80,8 +80,10 @@ class ComicPressMediaHandling { break; default: if (preg_match('#^date-(.*)$#', $matches[1], $date_matches) > 0) { - $value = date($date_matches[1], strtotime($this->post_to_use->post_date)); - break; + if (isset($this->post_to_use)) { + $value = date($date_matches[1], strtotime($this->post_to_use->post_date)); + break; + } } $value = $matches[0]; break; @@ -110,6 +112,54 @@ class ComicPressMediaHandling { return $results; } + function _check_post_meta_data($post_to_use, $type) { + if ($result = get_post_meta($post_to_use->ID, "backend_url_${type}", true)) { + if (is_string($result)) { + return $result; + } + } + + if ($result = get_post_meta($post_to_use->ID, "backend_url_images", true)) { + if (is_string($result)) { + parse_str($result, $types); + if (isset($types[$type])) { + return $types[$type]; + } + } + } + + if ($result = get_post_meta($post_to_use->ID, "backend_url", true)) { + if (is_string($result)) { + return $result; + } + } + return false; + } + + function _ensure_valid_uri($uri, $type) { + if (!empty($uri)) { + if (substr($uri, 0, 1) == '/') { + return $uri; + } else { + if (preg_match('#^[a-z]+://#', $uri) > 0) { + return $uri; + } else { + $bundle = $this->_bundle_global_variables(); + if (isset($bundle[$type])) { + $this->type_folder = $bundle[$type]; + } else { + $this->type_folder = ''; + } + + $uri = preg_replace_callback('#%([a-z0-9-]+)%#i', array(&$this, '_expand_filter_callback'), $uri); + + return trailingslashit(get_bloginfo('url')) . $uri; + } + } + } + return false; + } + /** * Get the comic path. * @param string $type The type to retrieve. @@ -122,6 +172,12 @@ class ComicPressMediaHandling { global $post; $post_to_use = !is_null($override_post) ? $override_post : $post; + if ($uri = $this->_check_post_meta($post_to_use, $type)) { + if ($result = $this->_ensure_valid_url($uri, $type)) { + return $result; + } + } + $filter = $this->_get_filter($filter); $globals = $this->_bundle_global_variables(); diff --git a/test/ComicPressMediaHandlingTest.php b/test/ComicPressMediaHandlingTest.php index 03ab451..d36181e 100644 --- a/test/ComicPressMediaHandlingTest.php +++ b/test/ComicPressMediaHandlingTest.php @@ -175,4 +175,47 @@ class ComicPressMediaHandlingTest extends PHPUnit_Framework_TestCase { $this->assertEquals($expected_result, _comicpress_pre_handle_comic_path_results(false, array('one/one', 'two/two', 'three/three'), 'comic', (object)array('ID' => 1))); } + + function providerTestCheckPostMetaData() { + return array( + array('comic', array(), false), + array('comic', array('backend_url_comic' => '/test'), '/test'), + array('comic', array('backend_url_images' => 'test=/test'), false), + array('comic', array('backend_url_images' => 'comic=/test'), '/test'), + array('comic', array('backend_url' => '/test'), '/test'), + ); + } + + /** + * @dataProvider providerTestCheckPostMetaData + */ + function testCheckPostMetaData($type, $metadata, $expected_result) { + foreach ($metadata as $key => $value) { + update_post_meta(1, $key, $value); + } + + $this->assertEquals($expected_result, $this->cpmh->_check_post_meta_data((object)array('ID' => 1), $type)); + } + + function providerTestEnsureValidURI() { + return array( + array('', false), + array('test', 'wordpress/test'), + array('%type-folder%/test', 'wordpress/comic-dir/test'), + array('/test', '/test'), + array('http://file', 'http://file'), + ); + } + + /** + * @dataProvider providerTestEnsureValidURI + */ + function testEnsureValidURI($uri, $expected_result) { + _set_bloginfo('url', 'wordpress'); + + $cpmh = $this->getMock('ComicPressMediaHandling', array('_bundle_global_variables')); + $cpmh->expects($this->any())->method('_bundle_global_variables')->will($this->returnValue(array('comic' => 'comic-dir'))); + + $this->assertEquals($expected_result, $cpmh->_ensure_valid_uri($uri, 'comic')); + } }