get comic image ordering working

This commit is contained in:
John Bintz 2009-07-22 14:59:59 -07:00
parent d55cc95d6b
commit 0d8b995764
2 changed files with 180 additions and 2 deletions

View File

@ -79,8 +79,78 @@ class ComicPressComicPost {
return $output; return $output;
} }
function normalize_attachment_sorting() { function breakdown_comic_ordering_string($string) {
$parts = explode(";", $string);
$orderings = array();
foreach ($parts as $part) {
$type_key_value = explode(":", $part);
if (count($type_key_value) == 2) {
list ($key, $value) = $type_key_value;
if (preg_match('#[^a-z0-9\_\-]#', $key) == 0) {
$orderings[$key] = array();
$values = explode(",", $value);
foreach ($values as $value) {
$value = trim($value);
if (is_numeric($value)) {
$orderings[$key][] = $value;
}
}
}
}
}
return $orderings;
}
function get_comic_image_ordering($post_id) {
if (is_array($this->get_comic_image_attachments())) {
$ordering_by_type = array();
$ordering_types = get_post_meta($post_id, 'comic_ordering', true);
if (!empty($ordering_types)) { $ordering_types = $this->breakdown_comic_ordering_string($ordering_types); }
$comic_image_ordering = array();
$found_post_ids = array();
if (!empty($ordering_types)) {
foreach ($ordering_types as $type => $post_ids) {
$comic_image_ordering[$type] = array();
foreach ($post_ids as $post_id) {
foreach ($this->get_comic_image_attachments() as $attachment) {
if (!isset($found_post_ids[$attachment->ID])) {
if ($attachment->ID == $post_id) {
$comic_image_ordering[$type][] = $attachment->ID;
$found_post_ids[$post_id] = true;
}
}
}
}
}
}
$remaining_posts_to_sort = array();
foreach ($this->get_comic_image_attachments() as $attachment) {
$comic_image_type = get_post_meta($attachment->ID, 'comic_image_type', true);
if (!empty($comic_image_type)) {
if (!isset($found_post_ids[$attachment->ID])) {
if (!isset($remaining_posts_to_sort[$comic_image_type])) {
$remaining_posts_to_sort[$comic_image_type] = array();
}
$remaining_posts_to_sort[$comic_image_type][] = $attachment->ID;
}
}
}
foreach ($remaining_posts_to_sort as $type => $posts) {
usort($remaining_posts_to_sort[$type], array(&$this, 'sort_remaining_comic_images'));
}
return array_merge($comic_image_ordering, $remaining_posts_to_sort);
}
}
function sort_remaining_comic_images($a, $b) {
return strtotime($a->post_date) - strtotime($b->post_date);
} }
} }

View File

@ -47,7 +47,115 @@ class ComicPressComicPostTest extends PHPUnit_Framework_TestCase {
} }
} }
function testNormalizeAttachmentSorting() { function providerTestBreakdownComicOrderingString() {
return array(
array(
"",
array()
),
array(
"comic|123",
array()
),
array(
"comic|123:meow",
array()
),
array(
"comic123:meow",
array("comic123" => array())
),
array(
"comic123:1",
array("comic123" => array(1))
),
array(
"comic123:1,2;comic234:meow",
array("comic123" => array(1, 2),
"comic234" => array())
),
);
}
/**
* @dataProvider providerTestBreakdownComicOrderingString
*/
function testBreakdownComicOrderingString($string, $expected_result) {
$this->assertEquals($expected_result, $this->p->breakdown_comic_ordering_string($string));
}
function testGetComicImageOrdering() {
$p = $this->getMock('ComicPressComicPost', array('get_comic_image_attachments'));
$comic_attachments = array(
array(
'ID' => 2,
'post_parent' => 1,
'post_title' => 'Comic one',
'post_meta' => array(
'comic_image_type' => 'comic'
),
'post_date' => 1
),
array(
'ID' => 3,
'post_parent' => 1,
'post_title' => 'Comic two',
'post_meta' => array(
'comic_image_type' => 'comic'
),
'post_date' => 2
),
array(
'ID' => 4,
'post_parent' => 1,
'post_title' => 'Comic three',
'post_meta' => array(
'comic_image_type' => 'rss'
),
'post_date' => 4
),
array(
'ID' => 5,
'post_parent' => 1,
'post_title' => 'Comic four',
'post_meta' => array(
'comic_image_type' => 'rss'
),
'post_date' => 3
),
);
$attachments = array();
foreach ($comic_attachments as $attachment_info) {
$attachment = (object)array();
foreach ($attachment_info as $field => $value) {
switch ($field) {
case "post_meta":
foreach ($value as $meta => $meta_value) {
update_post_meta($attachment_info['ID'], $meta, $meta_value);
}
break;
case "post_date":
$attachment->{$field} = date("r", $value);
break;
default:
$attachment->{$field} = $value;
break;
}
}
$attachments[] = $attachment;
}
$p->expects($this->any())->method('get_comic_image_attachments')->will($this->returnValue($attachments));
wp_insert_post((object)array('ID' => 1));
update_post_meta(1, 'comic_ordering', "comic:3,2");
$result = $p->get_comic_image_ordering(1);
$this->assertEquals(array('comic' => array(3,2), 'rss' => array(5,4)), $result);
} }
} }