new storyline functions
This commit is contained in:
parent
8d57acb09d
commit
b2410a2678
|
@ -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];
|
||||
|
@ -158,6 +159,7 @@ class ComicPressStoryline {
|
|||
// @codeCoverageIgnoreEnd
|
||||
|
||||
function valid($id) {
|
||||
$id = $this->_ensure_numeric_category($id);
|
||||
if (isset($this->_structure[$id])) {
|
||||
return array_keys($this->_structure[$id]);
|
||||
}
|
||||
|
|
|
@ -173,3 +173,32 @@ function SL() {
|
|||
$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;
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue