get post parents

This commit is contained in:
John Bintz 2009-11-07 13:47:08 -05:00
parent a2bcd50093
commit c078f0cc53
3 changed files with 80 additions and 3 deletions

View File

@ -217,8 +217,28 @@ class ComicPressComicPost {
update_post_meta($this->post->ID, 'comic_ordering', $new_order);
}
function get_parent_categories() {
$parents = wp_get_post_categories($this->post->ID);
function find_parents() {
$parents = array();
$post_categories = wp_get_post_categories($this->post->ID);
if (count($post_categories) == 1) {
do {
$category_parent = 0;
$category = get_category(end($post_categories));
if (!empty($category)) {
$category_parent = $category->parent;
if ($category_parent != 0) {
$post_categories[] = $category_parent;
}
}
} while ($category_parent != 0);
foreach ($post_categories as $category_id) {
$category = get_category($category_id);
$parents[$category_id] = $category->slug;
}
}
return $parents;
}
}

View File

@ -162,8 +162,49 @@ class ComicPressComicPostTest extends PHPUnit_Framework_TestCase {
$this->assertEquals($expected_result, get_post_meta(1, 'comic_ordering', true));
}
function testFindParents() {
function providerTestFindParents() {
return array(
array(
array(),
array()
),
array(
array(1),
array(1 => 'root')
),
array(
array(2),
array(2 => 'comic', 1 => 'root')
),
array(
array(3),
array(3 => 'part-1', 2 => 'comic', 1 => 'root')
),
array(
array(4),
array(4 => 'blog', 1 => 'root')
),
array(
array(1, 4),
array()
),
);
}
/**
* @dataProvider providerTestFindParents
*/
function testFindParents($post_categories, $expected_result) {
add_category(1, (object)array('slug' => 'root', 'parent' => 0));
add_category(2, (object)array('slug' => 'comic', 'parent' => 1));
add_category(3, (object)array('slug' => 'part-1', 'parent' => 2));
add_category(4, (object)array('slug' => 'blog', 'parent' => 1));
wp_set_post_categories(1, $post_categories);
$this->p->post = (object)array('ID' => 1);
$this->assertEquals($expected_result, $this->p->find_parents());
}
}

View File

@ -44,6 +44,22 @@ class ComicPressTest extends PHPUnit_Framework_TestCase {
$result = $this->cp->cascade_search('file', $force_parent);
$this->assertTrue($result === $expected_result);
}
function providerTestCategorySearch() {
return array(
array(
)
);
}
/**
* @dataProvider providerTestCategorySearch
*/
function testCategorySearch() {
mkdir(vfsStream::url('root/style/site/comic/chapter-1/part-1'), 0777, true);
}
}
?>