From c23d3d646ca7a93961c0f09c7170866777da4f33 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sun, 18 Oct 2009 19:21:00 -0400 Subject: [PATCH] stop duplicating serialize() --- classes/ComicPressComicPost.inc | 81 ++++++++++---------------------- test/ComicPressComicPostTest.php | 54 ++++----------------- 2 files changed, 34 insertions(+), 101 deletions(-) diff --git a/classes/ComicPressComicPost.inc b/classes/ComicPressComicPost.inc index 6bb91a8..aa98883 100644 --- a/classes/ComicPressComicPost.inc +++ b/classes/ComicPressComicPost.inc @@ -56,19 +56,18 @@ class ComicPressComicPost { return $found; } - function display_comics($format) { $this->display_attached_images('comic', null, 'comic', $format); } + function _display_type($types, $format, $single = false) { + $target_type = reset($types); + foreach ($types as $type) { + if ($this->display_attached_images($type, ($single ? 1 : null), $target_type, $format)) { + return; + } + } + } - function display_archive($format) { - if (!$this->display_attached_images('archive', 1, 'archive', $format)) { - $this->display_attached_images('comic', 1, 'archive', $format); - } - } - - function display_rss($format) { - if (!$this->display_attached_images('rss', null, 'rss', $formar)) { - $this->display_attached_images('comic', null, 'rss', $format); - } - } + function display_comics($format) { $this->_display_type(array('comic'), $format); } + function display_archive($format) { $this->_display_type(array('archive'. 'comic'), $format, true); } + function display_rss($format) { $this->_display_type(array('rss'. 'comic'), $format); } function get_comic_img_tag($url, $type, $additional_parameters = array()) { $dimensions = array(); @@ -93,46 +92,15 @@ class ComicPressComicPost { return $output; } - - 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 build_comic_ordering_string($order) { - $result_string_parts = array(); - - foreach ($order as $key => $values) { - $result_string_parts[] = "${key}:" . implode(",", $values); - } - sort($result_string_parts); - return implode(";", $result_string_parts); - } - + + /** + * Normalize the ordering of comic images in this post. + * If images have beed added or removed, intelligently update the metadata. + */ function normalize_comic_image_ordering() { if (is_array($this->get_comic_image_attachments())) { $ordering_by_type = array(); $ordering_types = get_post_meta($this->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(); @@ -178,20 +146,23 @@ class ComicPressComicPost { } } - update_post_meta($this->post->ID, 'comic_ordering', $this->build_comic_ordering_string($comic_image_ordering)); + update_post_meta($this->post->ID, 'comic_ordering', $comic_image_ordering); return $comic_image_ordering; } + return false; } function sort_remaining_comic_images($a, $b) { return strtotime($a->post_date) - strtotime($b->post_date); } - + + /** + * Change the ordering of comic images in the associated post. + * @param array $order The new requested order. + */ function change_comic_image_ordering($requested_new_order) { - $orderings = array(); - if (($order_string = get_post_meta($this->post->ID, 'comic_ordering', true)) !== false) { - $orderings = $this->breakdown_comic_ordering_string($order_string); - } + $orderings = get_post_meta($this->post->ID, 'comic_ordering', true); + if (!is_array($orderings)) { $orderings = array(); } $new_order = array(); @@ -220,7 +191,7 @@ class ComicPressComicPost { } } - update_post_meta($this->post->ID, 'comic_ordering', $this->build_comic_ordering_string($new_order)); + update_post_meta($this->post->ID, 'comic_ordering', $new_order); } } diff --git a/test/ComicPressComicPostTest.php b/test/ComicPressComicPostTest.php index e876e07..d4fac2b 100644 --- a/test/ComicPressComicPostTest.php +++ b/test/ComicPressComicPostTest.php @@ -47,44 +47,6 @@ class ComicPressComicPostTest extends PHPUnit_Framework_TestCase { } } - 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 testNormalizeComicImageOrdering() { $p = $this->getMock('ComicPressComicPost', array('get_comic_image_attachments')); @@ -151,38 +113,38 @@ class ComicPressComicPostTest extends PHPUnit_Framework_TestCase { $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"); + update_post_meta(1, 'comic_ordering', array('comic' => array(3))); $p->post = (object)array('ID' => 1); $result = $p->normalize_comic_image_ordering(); $this->assertEquals(array('comic' => array(3,2), 'rss' => array(5,4)), $result); - $this->assertEquals('comic:3,2;rss:5,4', get_post_meta(1, 'comic_ordering', true)); + $this->assertEquals(array('comic' => array(3,2), 'rss' => array(5,4)), get_post_meta(1, 'comic_ordering', true)); } function providerTestChangeComicImageOrdering() { return array( array( - 'comic:1,2,3', + array('comic' => array(1,2,3)), array( 'comic' => array('1' => 3, '2' => 1, '3' => 2) ), - 'comic:2,3,1' + array('comic' => array(2,3,1)) ), array( - 'comic:1,2,3', + array('comic' => array(1,2,3)), array( 'comic' => array('1' => 2, '2' => 2, '3' => 1) ), - 'comic:3,1,2' + array('comic' => array(3,1,2)) ), array( - 'comic:1,2,3', + array('comic' => array(1,2,3)), array( 'comic' => array('1' => 1, '2' => 2) ), - 'comic:1,2,3' + array('comic' => array(1,2,3)) ), ); }