diff --git a/classes/ComicPressStoryline.inc b/classes/ComicPressStoryline.inc index 7b6bf87..77d46a5 100644 --- a/classes/ComicPressStoryline.inc +++ b/classes/ComicPressStoryline.inc @@ -142,6 +142,7 @@ class ComicPressStoryline { function _get_field($field, $id) { if (isset($this->_structure)) { + $id = $this->_ensure_numeric_category($id); if (isset($this->_structure[$id])) { if (isset($this->_structure[$id][$field])) { return $this->_structure[$id][$field]; @@ -157,8 +158,9 @@ class ComicPressStoryline { function next($id) { return $this->_get_field('next', $id); } // @codeCoverageIgnoreEnd - function valid($id) { - if (isset($this->_structure[$id])) { + function valid($id) { + $id = $this->_ensure_numeric_category($id); + if (isset($this->_structure[$id])) { return array_keys($this->_structure[$id]); } return false; diff --git a/functions.inc b/functions.inc index 6d4c5fc..2634d25 100644 --- a/functions.inc +++ b/functions.inc @@ -172,4 +172,33 @@ function SL() { $storyline = new ComicPressStoryline(); $storyline->read_from_options(); return $storyline->_structure; +} + +function SC($which, $relative_to = null) { + global $post; + + $storyline = new ComicPressStoryline(); + $storyline->read_from_options(); + + if (is_null($relative_to)) { + if (is_object($post)) { + if (isset($post->ID)) { + $categories = wp_get_post_categories($post->ID); + if (is_array($categories)) { + $relative_to = reset($categories); + } + } + } + } + + if (!is_null($relative_to)) { + if ($result = $storyline->_get_field($which, $relative_to)) { + $category = get_category($result); + if (!empty($category)) { + return $category; + } + } + } + + return false; } \ No newline at end of file diff --git a/test/FunctionsTest.php b/test/FunctionsTest.php index 894763b..1cb5e72 100644 --- a/test/FunctionsTest.php +++ b/test/FunctionsTest.php @@ -188,4 +188,37 @@ class FunctionsTest extends PHPUnit_Framework_TestCase { $this->assertEquals($s->_structure, SL()); } + + function providerTestSC() { + return array( + array('next', 1, 2), + array('next', null, 3), + array('next', 4, false), + array('test', 4, false), + ); + } + + /** + * @dataProvider providerTestSC + */ + function testSC($which, $relative_to, $expected_result) { + global $post; + + $post = (object)array('ID' => 1); + wp_set_post_categories(1, array(2)); + + $s = new ComicPressStoryline(); + $s->set_flattened_storyline('0/1,0/2,0/2/3,0/2/4'); + + for ($i = 1; $i <= 4; ++$i) { + add_category($i, (object)array('slug' => 'test-' . $i)); + } + + $result = SC($which, $relative_to); + if ($expected_result === false) { + $this->assertTrue(false === $result); + } else { + $this->assertEquals($expected_result, $result->term_id); + } + } }