move final template tags out

This commit is contained in:
John Bintz 2010-02-04 22:10:52 -05:00
parent 3b1727e476
commit 23c145c262
4 changed files with 166 additions and 87 deletions

View File

@ -43,7 +43,7 @@ class ComicPressTagBuilderFactory {
}
class ComicPressTagBuilder {
public $categories, $restrictions, $storyline, $dbi, $parent_post, $post;
public $categories, $restrictions, $storyline, $dbi, $parent_post, $post, $category;
public function __construct($parent_post, $storyline, $dbi) {
$this->restrictions = array();
@ -61,6 +61,7 @@ class ComicPressTagBuilder {
}
// TODO categories (SC and SL)
// TODO filtered versions of template tags
public function __call($method, $arguments) {
$ok = false;
$return = $this;
@ -80,11 +81,48 @@ class ComicPressTagBuilder {
$this->parent_post = (object)$arguments[0];
$ok = true;
break;
case 'current':
if (isset($this->category)) {
if (isset($arguments[0])) {
return $this->category;
} else {
return get_category($this->category);
}
}
break;
case 'parent':
$id = $this->storyline->parent($this->category);
return isset($arguments[0]) ? get_category($id) : $id;
case 'children':
$children = $this->storyline->_find_children($this->category);
if (!isset($arguments[0])) {
foreach ($children as &$child) {
$child = get_category($child);
}
unset($child);
}
return $children;
case 'next':
case 'previous':
case 'first':
case 'last':
if (isset($this->category)) {
switch ($method) {
case 'next':
case 'previous':
$id = $this->storyline->{$method}($this->category);
break;
case 'first':
$id = reset(array_keys($this->storyline->_structure));
break;
case 'last':
$id = end(array_keys($this->storyline->_structure));
break;
}
return isset($arguments[0]) ? get_category($id) : $id;
} else {
$this->post = call_user_func(array($this->dbi, "get_${method}_post"), $this->storyline->build_from_restrictions($this->restrictions), $this->parent_post);
}
$ok = true;
break;
case 'in':
@ -117,6 +155,14 @@ class ComicPressTagBuilder {
$which = (isset($arguments[1])) ? $arguments[1] : 'default';
return ComicPressBackend::generate_from_id($arguments[0][$which]);
}
case 'category':
if (isset($arguments[0])) {
$this->category = $arguments[0];
} else {
$this->category = reset(wp_get_post_categories($this->parent_post->ID));
}
$ok = true;
break;
case 'structure':
return $this->storyline->_structure;
default:

View File

@ -5,10 +5,6 @@ foreach (array(
'Protect' => 0,
'Restore' => 0,
'Unprotect' => 0,
'M' => 1,
'EM' => 3,
'SL' => 1,
'SC' => 2,
) as $function => $param_count) {
if ($param_count == 0) {
add_action("comicpress-${function}", $function, 10);
@ -65,49 +61,3 @@ function Unprotect() {
$__post = $__wp_query = null;
}
/**
* Get a category relative to the provided category.
* If no category is provided, use the current post's category.
* Relative measures can be one of: current, previous, lext, level, parent
* If no relative measure is provided, the current category is returned.
* @param string $which The relative measure to use.
* @param object $relative_to The post object to be relative to.
* @return object|false The relative category object, or false if not found.
*/
function SC($which = 'current', $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 ($which == 'current') {
$result = $relative_to;
} else {
$result = $storyline->_get_field($which, $relative_to);
}
if ($result !== false) {
$category = get_category($result);
// sanity check
// @codeCoverageIgnoreStart
if (!empty($category)) {
return $category;
}
}
// @codeCoverageIgnoreEnd
}
return false;
}

View File

@ -378,4 +378,122 @@ class ComicPressTagBuilderTest extends PHPUnit_Framework_TestCase {
'3' => array('previous' => 2, 'level' => 1),
), $core->structure());
}
function providerTestCategoryTraversal() {
return array(
array(
array(
array('category'),
array('current')
),
2
),
array(
array(
array('category'),
array('current', true)
),
2
),
array(
array(
array('category'),
array('next')
),
3
),
array(
array(
array('category'),
array('previous')
),
1
),
array(
array(
array('category'),
array('first')
),
1
),
array(
array(
array('category'),
array('last')
),
5
),
array(
array(
array('category'),
array('parent')
),
false
),
array(
array(
array('from', (object)array('ID' => 2)),
array('category'),
array('parent')
),
2
),
array(
array(
array('category', 4),
array('parent')
),
2
),
array(
array(
array('category', 2),
array('children', true)
),
array(2, 3, 4)
),
);
}
/**
* @dataProvider providerTestCategoryTraversal
*/
function testCategoryTraversal($methods, $expected_result) {
global $post;
$storyline = new ComicPressStoryline();
$storyline->set_flattened_storyline('0/1,0/2,0/2/3,0/2/4,0/5');
foreach (array(
1 => array('cat_name' => 'Test 1', 'category_nicename' => 'category-1', 'category_parent' => 0),
2 => array('cat_name' => 'Test 2', 'category_nicename' => 'category-2', 'category_parent' => 0),
3 => array('cat_name' => 'Test 3', 'category_nicename' => 'category-3', 'category_parent' => 2),
4 => array('cat_name' => 'Test 4', 'category_nicename' => 'category-4', 'category_parent' => 2),
5 => array('cat_name' => 'Test 5', 'category_nicename' => 'category-5', 'category_parent' => 0),
) as $id => $category) {
add_category($id, (object)$category);
}
$post = (object)array('ID' => 1);
$dbi = $this->getMock('ComicPressDBInterface');
$core = new ComicPressTagBuilderFactory($dbi);
wp_insert_post($post);
wp_insert_post((object)array('ID' => 2));
wp_set_post_categories(1, array(2));
wp_set_post_categories(2, array(3));
foreach ($methods as $method_info) {
$method = array_shift($method_info);
$core = call_user_func_array(array($core, $method), $method_info);
}
if (is_object($core)) {
$this->assertEquals($expected_result, $core->cat_ID);
} else {
$this->assertEquals($expected_result, $core);
}
}
}

View File

@ -61,41 +61,6 @@ class FunctionsTest extends PHPUnit_Framework_TestCase {
$this->assertTrue(is_null($__wp_query));
}
function providerTestSC() {
return array(
array('next', 1, 2),
array('next', null, 3),
array('next', 4, false),
array('test', 4, false),
array('current', 1, 1),
array('current', null, 2),
);
}
/**
* @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);
}
}
function providerTestF() {
return array(
array(null, array(1 => 'one')),