From d94905eaa6dd62c48f43e519ac7795aa3e8cce03 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sat, 7 Nov 2009 10:50:21 -0500 Subject: [PATCH 01/32] strict warnings cleanup and phpunit.xml setup --- .gitignore | 4 +++- classes/ComicPress.inc | 5 +++-- classes/ComicPressComicPost.inc | 17 +++++++++++++++-- classes/ComicPressStoryline.inc | 16 +++++++++++----- test/ComicPressAdminTest.php | 8 +++++--- 5 files changed, 37 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index 4971ac3..7cbcfba 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ *~ .directory - +.buildpath +.project +.settings/ diff --git a/classes/ComicPress.inc b/classes/ComicPress.inc index 3e03079..cdeb337 100644 --- a/classes/ComicPress.inc +++ b/classes/ComicPress.inc @@ -234,7 +234,7 @@ class ComicPress { "Layout Name", "Sidebars" ) as $field) { if (preg_match('#/\*.*' . $field . ': ([^\n]+).*\*/#s', $content, $matches) > 0) { - if (!is_array($this->layouts[$basename])) { + if (!isset($this->layouts[$basename])) { $this->layouts[$basename] = array(); } $this->layouts[$basename][$field] = $matches[1]; @@ -292,7 +292,8 @@ class ComicPress { $sorted_categories = array(); foreach ($this->category_tree as $node) { - $category_id = end(explode("/", $node)); + $parts = explode("/", $node); + $category_id = end($parts); if (in_array($category_id, $categories)) { $sorted_categories[] = $category_id; } diff --git a/classes/ComicPressComicPost.inc b/classes/ComicPressComicPost.inc index 733397e..15b4e56 100644 --- a/classes/ComicPressComicPost.inc +++ b/classes/ComicPressComicPost.inc @@ -79,7 +79,11 @@ class ComicPressComicPost { $dimensions = array(); if (isset($this->comicpress->comicpress_options["${type}_dimensions"])) { - list($width, $height) = explode("x", $this->comicpress->comicpress_options["${type}_dimensions"]); + $parts = explode("x", $this->comicpress->comicpress_options["${type}_dimensions"]); + switch (count($parts)) { + case 1: list($width) = $parts; break; + case 2: list($width, $height) = $parts; break; + } $dimensions = compact('width', 'height'); } @@ -145,6 +149,7 @@ class ComicPressComicPost { } foreach ($remaining_posts_to_sort as $type => $posts) { + if (!isset($comic_image_ordering[$type])) { $comic_image_ordering[$type] = array(); } if (is_array($comic_image_ordering[$type])) { $comic_image_ordering[$type] = array_merge($comic_image_ordering[$type], $posts); } else { @@ -158,8 +163,16 @@ class ComicPressComicPost { return false; } + /** + * Sort the remaining comic images by file date. + * @param object $a + * @param object $b + * @return int + */ function sort_remaining_comic_images($a, $b) { - return strtotime($a->post_date) - strtotime($b->post_date); + $a_date = isset($a->post_date) ? $a->post_date : 0; + $b_date = isset($b->post_date) ? $b->post_date : 0; + return $a_date - $b_date; } /** diff --git a/classes/ComicPressStoryline.inc b/classes/ComicPressStoryline.inc index 08a3155..cd863ef 100644 --- a/classes/ComicPressStoryline.inc +++ b/classes/ComicPressStoryline.inc @@ -58,7 +58,11 @@ class ComicPressStoryline { $structure = explode(',', $structure); } else { if (is_array($structure)) { - $key = implode(',', $structure); + $fixed_structure = array(); + foreach ($structure as $s) { + if (!is_array($s)) { $fixed_structure[] = $s; } + } + $key = implode(',', $fixed_structure); } } @@ -383,10 +387,12 @@ class ComicPressStoryline { $found_children = false; foreach ($this->_structure as $category_id => $info) { if (!in_array($category_id, $children)) { - if (in_array($info['parent'], $children)) { - $children[] = $category_id; - $found_children = true; - } + if (isset($info['parent'])) { + if (in_array($info['parent'], $children)) { + $children[] = $category_id; + $found_children = true; + } + } } } } while ($found_children); diff --git a/test/ComicPressAdminTest.php b/test/ComicPressAdminTest.php index 8a6841f..6ee0a0a 100644 --- a/test/ComicPressAdminTest.php +++ b/test/ComicPressAdminTest.php @@ -149,8 +149,10 @@ class ComicPressAdminTest extends PHPUnit_Framework_TestCase { $_POST = $change; - $this->admin->handle_update_comicpress_options($_POST['cp']); - + if (isset($_POST['cp'])) { + $this->admin->handle_update_comicpress_options($_POST['cp']); + } + foreach ($new as $key => $value) { $this->assertEquals($value, $this->admin->comicpress->comicpress_options[$key]); } @@ -273,7 +275,7 @@ class ComicPressAdminTest extends PHPUnit_Framework_TestCase { $this->admin->handle_update_override_partial(array_merge(compact('code', 'action'), array('partial' => 'index'))); - if ($result && $action == "Update partial") { + if ($action == "Update partial") { $this->assertEquals($code, $this->admin->comicpress->comicpress_options['override_partials']['index']); } } From 365d85de7be58d3a5641ac053188ec44120e1530 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sat, 7 Nov 2009 10:51:08 -0500 Subject: [PATCH 02/32] remove more partials stuff --- classes/ComicPressAdmin.inc | 11 ----------- phpunit.xml | 2 ++ test/bootstrap.php | 5 +++++ 3 files changed, 7 insertions(+), 11 deletions(-) create mode 100644 phpunit.xml create mode 100644 test/bootstrap.php diff --git a/classes/ComicPressAdmin.inc b/classes/ComicPressAdmin.inc index 5dc12aa..35a773a 100644 --- a/classes/ComicPressAdmin.inc +++ b/classes/ComicPressAdmin.inc @@ -378,17 +378,6 @@ class ComicPressAdmin { } } - function handle_update_override_partial($info) { - switch ($info['action']) { - case __('Update partial', 'comicpress'): - $this->comicpress->comicpress_options['override_partials'][$info['partial']] = stripslashes($info['code']); - break; - case __('Delete override partial', 'comicpress'): - unset($this->comicpress->comicpress_options['override_partials'][$info['partial']]); - break; - } - } - function handle_update_zoom_slider($info) { $this->is_ajax = true; diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..11fbdd5 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/test/bootstrap.php b/test/bootstrap.php new file mode 100644 index 0000000..f31480b --- /dev/null +++ b/test/bootstrap.php @@ -0,0 +1,5 @@ + \ No newline at end of file From b3192c3bbbb0b2f24c582e406778a2674f3dc3dc Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sat, 7 Nov 2009 11:40:25 -0500 Subject: [PATCH 03/32] remove more partials stuff --- classes/ComicPressAdmin.inc | 4 ++++ test/ComicPressAdminTest.php | 19 ------------------- 2 files changed, 4 insertions(+), 19 deletions(-) diff --git a/classes/ComicPressAdmin.inc b/classes/ComicPressAdmin.inc index 35a773a..4245dee 100644 --- a/classes/ComicPressAdmin.inc +++ b/classes/ComicPressAdmin.inc @@ -378,6 +378,10 @@ class ComicPressAdmin { } } + /** + * Update the zoom slider info. + * @param $info The browser input. + */ function handle_update_zoom_slider($info) { $this->is_ajax = true; diff --git a/test/ComicPressAdminTest.php b/test/ComicPressAdminTest.php index 6ee0a0a..bd1756e 100644 --- a/test/ComicPressAdminTest.php +++ b/test/ComicPressAdminTest.php @@ -260,25 +260,6 @@ class ComicPressAdminTest extends PHPUnit_Framework_TestCase { ), ); } - - /** - * @dataProvider providerTestHandleUpdateOverridePartial - */ - function testHandleUpdateOverridePartial($code, $action) { - $this->admin->comicpress = (object)array( - 'comicpress_options' => array( - 'override_partials' => array( - 'index' => '$hiss;' - ) - ) - ); - - $this->admin->handle_update_override_partial(array_merge(compact('code', 'action'), array('partial' => 'index'))); - - if ($action == "Update partial") { - $this->assertEquals($code, $this->admin->comicpress->comicpress_options['override_partials']['index']); - } - } } ?> From f35adb3f51a7de79f4f8ef8ffd431aa00d06ffeb Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sat, 7 Nov 2009 11:52:01 -0500 Subject: [PATCH 04/32] make navigation use cache --- classes/ComicPressNavigation.inc | 70 +++++++++++++++++++------------ test/ComicPressNavigationTest.php | 4 ++ 2 files changed, 48 insertions(+), 26 deletions(-) diff --git a/classes/ComicPressNavigation.inc b/classes/ComicPressNavigation.inc index 06f8608..4dca364 100644 --- a/classes/ComicPressNavigation.inc +++ b/classes/ComicPressNavigation.inc @@ -11,32 +11,50 @@ class ComicPressNavigation { function get_post_nav($post) { $nav = array(); - - // global previous/next - foreach (array('previous', 'next') as $field) { - $nav[$field] = $this->_dbi->{"get_${field}_comic"}(null, $post); - } - - // global first/last - foreach (array('first', 'last') as $field) { - $nav[$field] = $this->_dbi->{"get_${field}_comic"}(null); - } - - if ($category = $this->_storyline->get_valid_post_category($post->ID)) { - // storyline previous/next - foreach (array('previous', 'next') as $field) { - $nav["storyline-${field}"] = $this->_dbi->{"get_${field}_comic"}($category, $post); - } - - // adjacent storyline nodes - if (is_array($valid = $this->_storyline->valid($category))) { - foreach ($valid as $field) { - $nav["storyline-chapter-${field}"] = $this->_dbi->get_first_comic($this->_storyline->{$field}($category)); - } - } - } - - return $nav; + if (is_object($post)) { + if (isset($post->ID)) { + $cache_key = 'navigation-' . $post->ID; + + if (($result = wp_cache_get($cache_key, 'comicpress')) !== false) { + foreach ($result as $key => $post_id) { + $nev[$key] = get_post($post_id); + } + } + + // global previous/next + foreach (array('previous', 'next') as $field) { + $nav[$field] = $this->_dbi->{"get_${field}_comic"}(null, $post); + } + + // global first/last + foreach (array('first', 'last') as $field) { + $nav[$field] = $this->_dbi->{"get_${field}_comic"}(null); + } + + if ($category = $this->_storyline->get_valid_post_category($post->ID)) { + // storyline previous/next + foreach (array('previous', 'next') as $field) { + $nav["storyline-${field}"] = $this->_dbi->{"get_${field}_comic"}($category, $post); + } + + // adjacent storyline nodes + if (is_array($valid = $this->_storyline->valid($category))) { + foreach ($valid as $field) { + $nav["storyline-chapter-${field}"] = $this->_dbi->get_first_comic($this->_storyline->{$field}($category)); + } + } + } + + $cache_data = array(); + foreach ($nav as $key => $output_post) { + if (!empty($output_post)) { $cache_data[$key] = $output_post->ID; } + } + + wp_cache_set($cache_key, $cache_data, 'comicpress'); + + return $nav; + } + } } } diff --git a/test/ComicPressNavigationTest.php b/test/ComicPressNavigationTest.php index fa484fc..a45c6ff 100644 --- a/test/ComicPressNavigationTest.php +++ b/test/ComicPressNavigationTest.php @@ -40,7 +40,11 @@ class ComicPressNavigationTest extends PHPUnit_Framework_TestCase { $this->nav->_dbi = $dbi; $this->nav->_storyline = $storyline; + $this->assertFalse(wp_cache_get('navigation-1', 'comicpress')); + $this->nav->get_post_nav($post); + + $this->assertTrue(wp_cache_get('navigation-1', 'comicpress') !== false); } } From ea3df9631501c27eee568fb8d37f3f23f22fdb1c Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sat, 7 Nov 2009 12:18:53 -0500 Subject: [PATCH 05/32] clean up tests a bit --- classes/ComicPressStorylineCategory.inc | 7 ------- test/ComicPressAdminTest.php | 2 +- test/ComicPressComicPostTest.php | 2 +- test/ComicPressDBInterfaceTest.php | 2 +- test/ComicPressNavigationTest.php | 2 +- test/ComicPressStorylineCategoryTest.php | 17 ----------------- test/ComicPressStorylineTest.php | 2 +- test/ComicPressTest.php | 2 +- 8 files changed, 6 insertions(+), 30 deletions(-) delete mode 100644 classes/ComicPressStorylineCategory.inc delete mode 100644 test/ComicPressStorylineCategoryTest.php diff --git a/classes/ComicPressStorylineCategory.inc b/classes/ComicPressStorylineCategory.inc deleted file mode 100644 index 500c9fc..0000000 --- a/classes/ComicPressStorylineCategory.inc +++ /dev/null @@ -1,7 +0,0 @@ - \ No newline at end of file diff --git a/test/ComicPressAdminTest.php b/test/ComicPressAdminTest.php index bd1756e..ff309eb 100644 --- a/test/ComicPressAdminTest.php +++ b/test/ComicPressAdminTest.php @@ -2,7 +2,7 @@ require_once('PHPUnit/Framework.php'); require_once('MockPress/mockpress.php'); -require_once(dirname(__FILE__) . '/../classes/ComicPressAdmin.inc'); +require_once('ComicPressAdmin.inc'); class ComicPressAdminTest extends PHPUnit_Framework_TestCase { function setUp() { diff --git a/test/ComicPressComicPostTest.php b/test/ComicPressComicPostTest.php index 1ba24a8..ce04737 100644 --- a/test/ComicPressComicPostTest.php +++ b/test/ComicPressComicPostTest.php @@ -2,7 +2,7 @@ require_once('PHPUnit/Framework.php'); require_once('MockPress/mockpress.php'); -require_once(dirname(__FILE__) . '/../classes/ComicPressComicPost.inc'); +require_once('ComicPressComicPost.inc'); class ComicPressComicPostTest extends PHPUnit_Framework_TestCase { function setUp() { diff --git a/test/ComicPressDBInterfaceTest.php b/test/ComicPressDBInterfaceTest.php index e6dd5b2..2c7dc1e 100644 --- a/test/ComicPressDBInterfaceTest.php +++ b/test/ComicPressDBInterfaceTest.php @@ -2,7 +2,7 @@ require_once('MockPress/mockpress.php'); require_once('PHPUnit/Framework.php'); -require_once(dirname(__FILE__) . '/../classes/ComicPressDBInterface.inc'); +require_once('ComicPressDBInterface.inc'); class ComicPressDBInterfaceTest extends PHPUnit_Framework_TestCase { function testSingleton() { diff --git a/test/ComicPressNavigationTest.php b/test/ComicPressNavigationTest.php index a45c6ff..e848ec8 100644 --- a/test/ComicPressNavigationTest.php +++ b/test/ComicPressNavigationTest.php @@ -2,7 +2,7 @@ require_once('MockPress/mockpress.php'); require_once('PHPUnit/Framework.php'); -require_once(dirname(__FILE__) . '/../classes/ComicPressNavigation.inc'); +require_once('ComicPressNavigation.inc'); /** * Integration Testing. Just make sure things are called correctly. diff --git a/test/ComicPressStorylineCategoryTest.php b/test/ComicPressStorylineCategoryTest.php deleted file mode 100644 index c448e6a..0000000 --- a/test/ComicPressStorylineCategoryTest.php +++ /dev/null @@ -1,17 +0,0 @@ - \ No newline at end of file diff --git a/test/ComicPressStorylineTest.php b/test/ComicPressStorylineTest.php index 6b05021..d4b8ddf 100644 --- a/test/ComicPressStorylineTest.php +++ b/test/ComicPressStorylineTest.php @@ -2,7 +2,7 @@ require_once('MockPress/mockpress.php'); require_once('PHPUnit/Framework.php'); -require_once(dirname(__FILE__) . '/../classes/ComicPressStoryline.inc'); +require_once('ComicPressStoryline.inc'); class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { function setUp() { diff --git a/test/ComicPressTest.php b/test/ComicPressTest.php index e0de19e..e78aa6c 100644 --- a/test/ComicPressTest.php +++ b/test/ComicPressTest.php @@ -2,7 +2,7 @@ require_once('PHPUnit/Framework.php'); require_once('MockPress/mockpress.php'); -require_once(dirname(__FILE__) . '/../classes/ComicPress.inc'); +require_once('ComicPress.inc'); class ComicPressTest extends PHPUnit_Framework_TestCase { function setUp() { From ce743bd8df64e0615082fdf7aa8a916318c0238e Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sat, 7 Nov 2009 12:19:50 -0500 Subject: [PATCH 06/32] finally remove functions legacy --- functions-legacy.php | 64 -------------------------------------------- 1 file changed, 64 deletions(-) delete mode 100644 functions-legacy.php diff --git a/functions-legacy.php b/functions-legacy.php deleted file mode 100644 index 4b48d62..0000000 --- a/functions-legacy.php +++ /dev/null @@ -1,64 +0,0 @@ -/** -* Find a comic file in the filesystem. -* @param string $folder The folder name to search. -* @param string $override_post A WP Post object to use in place of global $post. -* @param string $filter The $comic_filename_filters to use. -* @return string The relative path to the comic file, or false if not found. -*/ -function get_comic_path($folder = 'comic', $override_post = null, $filter = 'default') { - global $post, $comic_filename_filters, $comic_folder, $archive_comic_folder, $rss_comic_folder, $comic_pathfinding_errors; - - if (isset($comic_filename_filters[$filter])) { - $filter_to_use = $comic_filename_filters[$filter]; - } else { - $filter_to_use = '{date}*.*'; - } - - switch ($folder) { - case "rss": $folder_to_use = $rss_comic_folder; break; - case "archive": $folder_to_use = $archive_comic_folder; break; - case "comic": default: $folder_to_use = $comic_folder; break; - } - - $post_to_use = (is_object($override_post)) ? $override_post : $post; - $post_date = mysql2date(CP_DATE_FORMAT, $post_to_use->post_date); - - $filter_with_date = str_replace('{date}', $post_date, $filter_to_use); - - if (count($results = glob("${folder_to_use}/${filter_with_date}")) > 0) { - return reset($results); - } - - $comic_pathfinding_errors[] = sprintf(__("Unable to find the file in the %s folder that matched the pattern %s. Check your WordPress and ComicPress settings.", 'comicpress'), $folder, $filter_with_date); - return false; -} - -/** -* Find a comic file in the filesystem and return an absolute URL to that file. -* @param string $folder The folder name to search. -* @param string $override_post A WP Post object to use in place of global $post. -* @param string $filter The $comic_filename_filters to use. -* @return string The absolute URL to the comic file, or false if not found. -*/ -function get_comic_url($folder = 'comic', $override_post = null, $filter = 'default') { - if (($result = get_comic_path($folder, $override_post, $filter)) !== false) { - return get_option('home') . '/' . $result; - } - - return false; -} - -// ComicPress Template Functions - -function the_comic($filter = 'default') { echo get_comic_url('comic', null, $filter); } - //The following is deprecated... - function comic_display($filter = 'default') { echo get_comic_url('comic', null, $filter); } - -function the_comic_archive($filter = 'default') { echo get_comic_url('archive', null, $filter); } - //The following is deprecated... - function comic_archive($filter = 'default') { echo get_comic_url('archive', null, $filter); } - -function the_comic_rss($filter = 'default') { echo get_comic_url('rss', null, $filter); } - //The following is deprecated... - function comic_rss($filter = 'default') { echo get_comic_url('rss', null, $filter); } - From 88b9e6cf8315fd04fe2b98037579f6f3f76e9b58 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sat, 7 Nov 2009 12:23:10 -0500 Subject: [PATCH 07/32] remove layout stuff --- functions.php | 89 ---------------------------------------- layouts/classic.inc | 19 --------- layouts/three-column.inc | 21 ---------- test/ComicPressTest.php | 52 +---------------------- 4 files changed, 1 insertion(+), 180 deletions(-) delete mode 100644 layouts/classic.inc delete mode 100644 layouts/three-column.inc diff --git a/functions.php b/functions.php index 88721c8..0f80ea6 100644 --- a/functions.php +++ b/functions.php @@ -44,22 +44,6 @@ function __comicpress_init() { } } -function comicpress_init() { - global $post, $comicpress; - - if (!empty($post)) { - if (in_comic_category() && $comicpress->is_multicomic() && !is_index()) { - $comicpress->setup_multicomic_partial_paths($post->ID); - } - } - - $comicpress->partial_paths[] = get_template_directory() . '/partials'; -} - -function comicpress_get_header() { - get_header(); -} - function include_partial($partials = '') { global $comicpress, $post, $nav_comics; @@ -80,9 +64,6 @@ function include_partial($partials = '') { } } -function in_comic_category() { - global $post, $comicpress; -} /** * Display the list of Storyline categories. @@ -127,74 +108,4 @@ function comicpress_list_storyline_categories($args = "") { echo $output; } -/** -* Display the comic transcript -* Transcript must be entered into a custom field named "transcript" -* @param string $displaymode, "raw" (straight from the field), "br" (includes html line breaks), "styled" (fully css styled with JavaScript expander) -*/ -function the_transcript($displaymode = 'raw') { - $transcript = get_post_meta( get_the_ID(), "transcript", true ); - switch ($displaymode) { - case "raw": - echo $transcript; - break; - case "br": - echo nl2br($transcript); - break; - case "styled": - if (!empty($transcript)) { ?> - - - -
  • -

    Latest Comics

    -
      - -
    • - -
    -
  • - -
  • -

    ? Random Comic

    -
  • - -
  • - -
  • - diff --git a/layouts/classic.inc b/layouts/classic.inc deleted file mode 100644 index dd6a7e8..0000000 --- a/layouts/classic.inc +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - -
    - -
    - - - - diff --git a/layouts/three-column.inc b/layouts/three-column.inc deleted file mode 100644 index 85dd6c5..0000000 --- a/layouts/three-column.inc +++ /dev/null @@ -1,21 +0,0 @@ - - - - - - - - -
    - -
    - - - - diff --git a/test/ComicPressTest.php b/test/ComicPressTest.php index e78aa6c..e05481f 100644 --- a/test/ComicPressTest.php +++ b/test/ComicPressTest.php @@ -12,57 +12,7 @@ class ComicPressTest extends PHPUnit_Framework_TestCase { unset($post); $this->cp = new ComicPress(); } - - function providerTestGetLayoutChoices() { - return array( - array( - array(), - array() - ), - array( - array( - 'layout.php' => << << array( - 'Layout Name' => 'Test', - 'Sidebars' => 'left,right', - )) - ), - ); - } - - /** - * @dataProvider providerTestGetLayoutChoices - */ - function testGetLayoutChoices($files, $expected_results) { - $cp = $this->getMock('ComicPress', array('_glob', '_file_get_contents')); - - _set_template_directory('/test'); - - $file_names = array(); - foreach (array_keys($files) as $file) { $file_names[] = '/test/layouts/' . $file; } - - $cp->expects($this->once())->method('_glob')->with('/test/layouts/*')->will($this->returnValue($file_names)); - foreach ($files as $file => $contents) { - $cp->expects($this->once())->method('_file_get_contents')->with('/test/layouts/' . $file)->will($this->returnValue($contents)); - } - - $this->assertEquals($expected_results, $cp->get_layout_choices()); - } - + function providerTestGetSortedPostCategories() { return array( array( From a2bcd50093ea26e315d871444cdbcaf6d6106043 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sat, 7 Nov 2009 13:14:33 -0500 Subject: [PATCH 08/32] cascade search --- classes/ComicPress.inc | 225 +++------------------------------------- test/ComicPressTest.php | 103 ++++++------------ 2 files changed, 46 insertions(+), 282 deletions(-) diff --git a/classes/ComicPress.inc b/classes/ComicPress.inc index cdeb337..3f9a385 100644 --- a/classes/ComicPress.inc +++ b/classes/ComicPress.inc @@ -64,17 +64,8 @@ class ComicPress { function init() { $this->load(); - add_action('wp_head', array(&$this, 'wp_head')); - add_filter('comicpress_nav', array(&$this, 'comicpress_nav'), 10, 2); - add_filter('comicpress_nav_fields', array(&$this, 'comicpress_nav_fields')); - if (current_user_can('edit_themes')) { if (!empty($this->comicpress_options['helpers'])) { - if (isset($this->comicpress_options['helpers']['show_partials_info'])) { - add_filter('comicpress_partial', array(&$this, 'comicpress_partial'), 10, 2); - add_action('wp_head', array(&$this, 'setup_comicpress_partial')); - } - add_action('wp_footer', array(&$this, 'announce_activated_helpers')); } } @@ -92,114 +83,11 @@ class ComicPress { function intermediate_image_sizes($sizes) { return array_merge($sizes, array('comic', 'rss', 'archive', 'mini')); } - - function needs_storyline_nav() { - return (count($this->category_tree) > 1) && ($this->comicpress_options['category_usage'] == "storyline"); - } - - function is_multicomic() { - return $this->comicpress_options['category_usage'] == "multicomic"; - } - - function comicpress_nav($type, $content) { - return $type; - } - - function comicpress_nav_fields($nav_fields) { - $nav_fields = array( - 'first' => '‹‹ ' . __('First', 'comicpress'), - 'previous' => '‹ ' . __('Previous', 'comicpress'), - 'next' => __('Next', 'comicpress') . ' ›', - 'last' => __('Last', 'comicpress') . ' ››' - ); - - if ($this->needs_storyline_nav()) { - $nav_fields = array_merge( - array('prior' => '‹‹ ' . __('Prior Storyline', 'comicpress')), - $nav_fields, - array('upcoming' => __('Upcoming Storyline', 'comicpress') . ' ››') - ); - } - - return $nav_fields; - } - - function wp_head() { - foreach ($this->additional_stylesheets as $uri) { ?> - - additional_javascripts as $uri) { ?> - - [ Activated ComicPress helpers: " . implode(", ", array_keys($this->comicpress_options['helpers'])) . " ]"; } - function setup_comicpress_partial() { ?> - - - ' . str_replace(get_template_directory() . DIRECTORY_SEPARATOR, '', $target) . '' . $content; - } - - /** - * Get the path to a partial. - * @param array $partials The partials to search for in each path. - * @return string|boolean The partial path to use, or false if no matches. - */ - function get_partial_path($partials) { - foreach ($partials as $partial) { - foreach ($this->partial_paths as $path) { - $target = $path . '/' . $partial . '.inc'; - if (file_exists($target)) { - return $target; - } - } - } - return false; - } - - function get_options_partial($partials) { - foreach ($partials as $partial) { - foreach ($this->partial_paths as $path) { - $target = str_replace(get_template_directory() . DIRECTORY_SEPARATOR, '', $path) . DIRECTORY_SEPARATOR . $partial; - - if (isset($this->comicpress_options['override_partials'][$target])) { - return array($target, $this->comicpress_options['override_partials'][$target]); - } - } - } - return false; - } - /** * Gather blog posts for the given index page. */ @@ -221,103 +109,22 @@ class ComicPress { return $wp_query; } - function _glob($pattern) { return glob($pattern); } - function _file_get_contents($file) { return file_get_contents($file); } - - function get_layout_choices() { - if (!is_array($this->layouts)) { - $this->layouts = array(); - foreach ($this->_glob(get_template_directory() . '/layouts/*') as $file) { - $content = $this->_file_get_contents($file); - $basename = pathinfo($file, PATHINFO_BASENAME); - foreach (array( - "Layout Name", "Sidebars" - ) as $field) { - if (preg_match('#/\*.*' . $field . ': ([^\n]+).*\*/#s', $content, $matches) > 0) { - if (!isset($this->layouts[$basename])) { - $this->layouts[$basename] = array(); - } - $this->layouts[$basename][$field] = $matches[1]; - } - } - } - } - return $this->layouts; - } - - function get_previous_next_categories($category_id) { - $prev_next_categories = array(); - - for ($i = 0, $il = count($this->category_tree); $i < $il; ++$i) { - $parts = explode("/", $this->category_tree[$i]); - if (count($parts) > 2) { - if (end($parts) == $category_id) { - while (count($parts) > 2) { - foreach (array( - 'previous' => -1, - 'next' => 1 - ) as $key => $direction) { - $index = $i; - while (isset($this->category_tree[$index])) { - $index += $direction; - if (isset($this->category_tree[$index])) { - $compare_parts = explode("/", $this->category_tree[$index]); - if (count($compare_parts) == count($parts)) { - $target_category = array_pop($compare_parts); - $parent_category = array_pop($compare_parts); - - if (!isset($prev_next_categories[$parent_category])) { - $prev_next_categories[$parent_category] = array(); - } - $prev_next_categories[$parent_category][$key] = $target_category; - } - } - } - } - array_pop($parts); - } - } - } - } - - return $prev_next_categories; - } - - function get_sorted_post_categories($override_post = null) { - global $post; - $post_to_use = (!empty($override_post)) ? $override_post : $post; - - $categories = wp_get_post_categories($post_to_use->ID); - - $sorted_categories = array(); - - foreach ($this->category_tree as $node) { - $parts = explode("/", $node); - $category_id = end($parts); - if (in_array($category_id, $categories)) { - $sorted_categories[] = $category_id; - } - } - - return $sorted_categories; - } - - function _is_dir($dir) { return is_dir($dir); } - - function setup_multicomic_partial_paths($post_id) { - $this->partial_paths = array(); - $category_ids = wp_get_post_categories($post_id); - if (is_array($category_ids)) { - foreach ($category_ids as $id) { - $category = get_category($id); - if (!empty($category)) { - if ($this->_is_dir($target = get_template_directory() . '/subthemes/' . $category->slug)) { - $this->partial_paths[] = $target; - } - } - } - } - } + /** + * See if a file exists in either child or parent theme, and if it does, return the path to that file. + * @param string $path The path to find. + * @param string $force_parent If true, always find the parent. + * @return string|boolean The path to the file, or false if not found. + */ + function cascade_search($path, $force_parent = false) { + $dirs_to_search = array(get_template_directory()); + if (!$force_parent) { array_unshift($dirs_to_search, get_stylesheet_directory()); } + + foreach ($dirs_to_search as $dir) { + $dir = trailingslashit($dir); + if (file_exists($dir . $path)) { return $dir . $path; } + } + return false; + } } ?> \ No newline at end of file diff --git a/test/ComicPressTest.php b/test/ComicPressTest.php index e05481f..50a78fd 100644 --- a/test/ComicPressTest.php +++ b/test/ComicPressTest.php @@ -3,89 +3,46 @@ require_once('PHPUnit/Framework.php'); require_once('MockPress/mockpress.php'); require_once('ComicPress.inc'); +require_once('vfsStream/vfsStream.php'); class ComicPressTest extends PHPUnit_Framework_TestCase { function setUp() { - global $post; - _reset_wp(); - unset($post); $this->cp = new ComicPress(); + + vfsStreamWrapper::register(); + vfsStreamWrapper::setRoot(new vfsStreamDirectory('root')); } - function providerTestGetSortedPostCategories() { - return array( - array( - array(1), - array('0/1'), - array(1) - ), - array( - array(2, 1), - array('0/1', '0/1/2'), - array(1, 2) - ), - array( - array(2, 1), - array('0/1', '0/1/3', '0/1/2'), - array(1, 2) - ), - ); + function providerTestCascadeSearch() { + $parent = vfsStream::url('root/parent/file'); + $child = vfsStream::url('root/child/file'); + + return array( + array(array(), false, false), + array(array('child'), false, $child), + array(array('parent'), false, $parent), + array(array('child', 'parent'), false, $child), + array(array('child', 'parent'), true, $parent), + ); } - + /** - * @dataProvider providerTestGetSortedPostCategories + * @dataProvider providerTestCascadeSearch */ - function testGetSortedPostCategories($post_categories, $category_tree, $expected_sort_order) { - $this->cp->category_tree = $category_tree; - - wp_set_post_categories(1, $post_categories); - - $this->assertEquals($expected_sort_order, $this->cp->get_sorted_post_categories((object)array('ID' => 1))); - } - - function testSetupMulticomicPartialPaths() { - $cp = $this->getMock('ComicPress', array('_is_dir')); - - wp_set_post_categories(1, array('2', '3')); - - add_category('2', (object)array('slug' => 'test-one')); - add_category('3', (object)array('slug' => 'test-two')); - - $cp->expects($this->at(0))->method('_is_dir')->with('/subthemes/test-one')->will($this->returnValue(true)); - $cp->expects($this->at(1))->method('_is_dir')->with('/subthemes/test-two')->will($this->returnValue(false)); - - $cp->setup_multicomic_partial_paths(1); - - $this->assertEquals(array('/subthemes/test-one'), $cp->partial_paths); - } - - function providerTestGetOverridePartials() { - return array( - array( - array('partials'), - array('index'), - array('partials/index'), - array('partials/index', true) - ), - array( - array('partials'), - array('index'), - array('partials/single'), - false - ) - ); - } - - /** - * @dataProvider providerTestGetOverridePartials - */ - function testGetOverridePartials($partial_paths, $requested_partials, $override_partials, $expected_result) { - $this->cp->partial_paths = $partial_paths; - foreach ($override_partials as $partial) { - $this->cp->comicpress_options['override_partials'][$partial] = true; - } - $this->assertEquals($expected_result, $this->cp->get_options_partial($requested_partials)); + function testCascadeSearch($create, $force_parent, $expected_result) { + mkdir(vfsStream::url('root/parent'), 0777); + mkdir(vfsStream::url('root/child'), 0777); + + _set_template_directory(vfsStream::url('root/parent')); + _set_stylesheet_directory(vfsStream::url('root/child')); + + foreach ($create as $type) { + file_put_contents(vfsStream::url("root/${type}/file"), 'file'); + } + + $result = $this->cp->cascade_search('file', $force_parent); + $this->assertTrue($result === $expected_result); } } From c078f0cc53c3d5204c5d9b52b57d532fd6030cc4 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sat, 7 Nov 2009 13:47:08 -0500 Subject: [PATCH 09/32] get post parents --- classes/ComicPressComicPost.inc | 24 ++++++++++++++++-- test/ComicPressComicPostTest.php | 43 +++++++++++++++++++++++++++++++- test/ComicPressTest.php | 16 ++++++++++++ 3 files changed, 80 insertions(+), 3 deletions(-) diff --git a/classes/ComicPressComicPost.inc b/classes/ComicPressComicPost.inc index 15b4e56..fe73f61 100644 --- a/classes/ComicPressComicPost.inc +++ b/classes/ComicPressComicPost.inc @@ -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; } } diff --git a/test/ComicPressComicPostTest.php b/test/ComicPressComicPostTest.php index ce04737..23450bf 100644 --- a/test/ComicPressComicPostTest.php +++ b/test/ComicPressComicPostTest.php @@ -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()); } } diff --git a/test/ComicPressTest.php b/test/ComicPressTest.php index 50a78fd..674f461 100644 --- a/test/ComicPressTest.php +++ b/test/ComicPressTest.php @@ -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); + + } } ?> \ No newline at end of file From 6b0953f91b3a65650e28f0a04cb6448598055000 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sat, 7 Nov 2009 16:36:57 -0500 Subject: [PATCH 10/32] comic path cascading working --- classes/ComicPress.inc | 12 ++++++++++++ test/ComicPressTest.php | 19 ++++++++++++++++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/classes/ComicPress.inc b/classes/ComicPress.inc index 3f9a385..28aef09 100644 --- a/classes/ComicPress.inc +++ b/classes/ComicPress.inc @@ -125,6 +125,18 @@ class ComicPress { } return false; } + + function category_search($categories, $path) { + $path = trailingslashit($path); + $all_categories = array_reverse($categories); + $all_paths = array(); + while (count($all_categories) > 0) { + $target = $path . implode('/', $all_categories); + if (is_dir($target)) { $all_paths[] = $target; } + array_pop($all_categories); + } + return array_reverse($all_paths); + } } ?> \ No newline at end of file diff --git a/test/ComicPressTest.php b/test/ComicPressTest.php index 674f461..0eae06e 100644 --- a/test/ComicPressTest.php +++ b/test/ComicPressTest.php @@ -48,7 +48,19 @@ class ComicPressTest extends PHPUnit_Framework_TestCase { function providerTestCategorySearch() { return array( array( - + array('comic'), array(vfsStream::url('root/style/comic')) + ), + array( + array('chapter-1', 'comic'), array(vfsStream::url('root/style/comic'), vfsStream::url('root/style/comic/chapter-1')) + ), + array( + array('part-1', 'chapter-1', 'comic'), array(vfsStream::url('root/style/comic'), vfsStream::url('root/style/comic/chapter-1'), vfsStream::url('root/style/comic/chapter-1/part-1')) + ), + array( + array('comic', 'chapter-1'), array() + ), + array( + array(), array() ) ); } @@ -56,9 +68,10 @@ class ComicPressTest extends PHPUnit_Framework_TestCase { /** * @dataProvider providerTestCategorySearch */ - function testCategorySearch() { - mkdir(vfsStream::url('root/style/site/comic/chapter-1/part-1'), 0777, true); + function testCategorySearch($categories, $found_path) { + mkdir(vfsStream::url('root/style/comic/chapter-1/part-1'), 0777, true); + $this->assertEquals($found_path, $this->cp->category_search($categories, vfsStream::url('root/style'))); } } From 6e6f6f71c9f410d4fd326559b581f91c7645a37d Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sat, 7 Nov 2009 16:38:22 -0500 Subject: [PATCH 11/32] documentation --- classes/ComicPress.inc | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/classes/ComicPress.inc b/classes/ComicPress.inc index 28aef09..9bcf02e 100644 --- a/classes/ComicPress.inc +++ b/classes/ComicPress.inc @@ -126,6 +126,12 @@ class ComicPress { return false; } + /** + * Search a path for directories named after the slugs provided. + * @param array $categories A list of category slugs going from child -> parent -> root. + * @param string $path The path to search. + * @return array All matching paths. + */ function category_search($categories, $path) { $path = trailingslashit($path); $all_categories = array_reverse($categories); From f38823df7b23fdefa0dd4a6e9e4b185605aabd1e Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sat, 7 Nov 2009 17:00:14 -0500 Subject: [PATCH 12/32] better file search support --- classes/ComicPress.inc | 23 ++++++++++++++++++ test/ComicPressTest.php | 52 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/classes/ComicPress.inc b/classes/ComicPress.inc index 9bcf02e..288ab15 100644 --- a/classes/ComicPress.inc +++ b/classes/ComicPress.inc @@ -143,6 +143,29 @@ class ComicPress { } return array_reverse($all_paths); } + + /** + * Recursively search a particular template directory for a specified file, limiting the search to the provided categories. + * @param string $name The name of the file with no path information. + * @param string $path The path within the template directories to start the search. + * @param attay $categories The child -> root category slugs to search within the specified path. If not provided, retrieve from current post. + * @return string|boolean The path to the file, for false if not found. + */ + function find_file($name, $path, $categories = null) { + foreach (array(get_stylesheet_directory(), get_template_directory()) as $dir) { + $dir = trailingslashit($dir) . $path; + $dirs_to_search = $this->category_search($categories, $dir); + $dirs_to_search[] = $dir; + + foreach ($dirs_to_search as $category_path) { + $target = trailingslashit($category_path) . $name; + if (file_exists($target)) { + return $target; + } + } + } + return false; + } } ?> \ No newline at end of file diff --git a/test/ComicPressTest.php b/test/ComicPressTest.php index 0eae06e..f1bcac6 100644 --- a/test/ComicPressTest.php +++ b/test/ComicPressTest.php @@ -73,6 +73,58 @@ class ComicPressTest extends PHPUnit_Framework_TestCase { $this->assertEquals($found_path, $this->cp->category_search($categories, vfsStream::url('root/style'))); } + + function providerTestFindFile() { + return array( + array( + array(), array(), false, + ), + array( + array('root/parent/partials/index.inc'), array(), vfsStream::url('root/parent/partials/index.inc') + ), + array( + array( + 'root/parent/partials/index.inc', + 'root/child/partials/index.inc' + ), + array(), + vfsStream::url('root/child/partials/index.inc') + ), + array( + array( + 'root/child/partials/index.inc', + 'root/child/partials/comic/index.inc' + ), + array('comic'), + vfsStream::url('root/child/partials/comic/index.inc') + ), + array( + array( + 'root/child/partials/index.inc', + 'root/child/partials/comic/index.inc' + ), + array('chapter-1', 'comic'), + vfsStream::url('root/child/partials/comic/index.inc') + ) + ); + } + + /** + * @dataProvider providerTestFindFile + */ + function testFindFile($files_to_setup, $post_categories, $expected_path_result) { + mkdir(vfsStream::url('root/parent/partials/comic/chapter-1'), 0777, true); + mkdir(vfsStream::url('root/child/partials/comic/chapter-1'), 0777, true); + + foreach ($files_to_setup as $path) { + file_put_contents(vfsStream::url($path), "test"); + } + + _set_template_directory(vfsStream::url('root/parent')); + _set_stylesheet_directory(vfsStream::url('root/child')); + + $this->assertEquals($expected_path_result, $this->cp->find_file('index.inc', 'partials', $post_categories)); + } } ?> \ No newline at end of file From bd1c206ba4ac91547d71c54608f1795ddf8eec90 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sat, 7 Nov 2009 17:00:39 -0500 Subject: [PATCH 13/32] remove cascade search, supplanted by find file --- classes/ComicPress.inc | 17 ----------------- test/ComicPressTest.php | 31 ------------------------------- 2 files changed, 48 deletions(-) diff --git a/classes/ComicPress.inc b/classes/ComicPress.inc index 288ab15..9d1c0fe 100644 --- a/classes/ComicPress.inc +++ b/classes/ComicPress.inc @@ -108,23 +108,6 @@ class ComicPress { return $wp_query; } - - /** - * See if a file exists in either child or parent theme, and if it does, return the path to that file. - * @param string $path The path to find. - * @param string $force_parent If true, always find the parent. - * @return string|boolean The path to the file, or false if not found. - */ - function cascade_search($path, $force_parent = false) { - $dirs_to_search = array(get_template_directory()); - if (!$force_parent) { array_unshift($dirs_to_search, get_stylesheet_directory()); } - - foreach ($dirs_to_search as $dir) { - $dir = trailingslashit($dir); - if (file_exists($dir . $path)) { return $dir . $path; } - } - return false; - } /** * Search a path for directories named after the slugs provided. diff --git a/test/ComicPressTest.php b/test/ComicPressTest.php index f1bcac6..3a6b0bc 100644 --- a/test/ComicPressTest.php +++ b/test/ComicPressTest.php @@ -14,37 +14,6 @@ class ComicPressTest extends PHPUnit_Framework_TestCase { vfsStreamWrapper::setRoot(new vfsStreamDirectory('root')); } - function providerTestCascadeSearch() { - $parent = vfsStream::url('root/parent/file'); - $child = vfsStream::url('root/child/file'); - - return array( - array(array(), false, false), - array(array('child'), false, $child), - array(array('parent'), false, $parent), - array(array('child', 'parent'), false, $child), - array(array('child', 'parent'), true, $parent), - ); - } - - /** - * @dataProvider providerTestCascadeSearch - */ - function testCascadeSearch($create, $force_parent, $expected_result) { - mkdir(vfsStream::url('root/parent'), 0777); - mkdir(vfsStream::url('root/child'), 0777); - - _set_template_directory(vfsStream::url('root/parent')); - _set_stylesheet_directory(vfsStream::url('root/child')); - - foreach ($create as $type) { - file_put_contents(vfsStream::url("root/${type}/file"), 'file'); - } - - $result = $this->cp->cascade_search('file', $force_parent); - $this->assertTrue($result === $expected_result); - } - function providerTestCategorySearch() { return array( array( From fd1868e90e284b019a81a06f46ddd498f60109c8 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sat, 7 Nov 2009 17:05:38 -0500 Subject: [PATCH 14/32] duh --- test/ComicPressTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/ComicPressTest.php b/test/ComicPressTest.php index 3a6b0bc..317894c 100644 --- a/test/ComicPressTest.php +++ b/test/ComicPressTest.php @@ -49,7 +49,9 @@ class ComicPressTest extends PHPUnit_Framework_TestCase { array(), array(), false, ), array( - array('root/parent/partials/index.inc'), array(), vfsStream::url('root/parent/partials/index.inc') + array('root/parent/partials/index.inc'), + array(), + vfsStream::url('root/parent/partials/index.inc') ), array( array( From 23be631fbab1ba5feec3df6321d72a4602acab28 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sat, 7 Nov 2009 17:06:18 -0500 Subject: [PATCH 15/32] remove original include partials --- functions.php | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/functions.php b/functions.php index 0f80ea6..5c002d4 100644 --- a/functions.php +++ b/functions.php @@ -44,27 +44,6 @@ function __comicpress_init() { } } -function include_partial($partials = '') { - global $comicpress, $post, $nav_comics; - - if (!is_array($partials)) { $partials = func_get_args(); } - - $content = $target = null; - - $target = $comicpress->get_partial_path($partials); - - if ($target !== false) { - ob_start(); include($target); $content = ob_get_clean(); - } - - $target = str_replace(".inc", "", $target); - - if (!empty($target) && !empty($content)) { - echo apply_filters("comicpress_partial", $content, $target); - } -} - - /** * Display the list of Storyline categories. */ From ca3299a01e0fe5856a376d653a6076f4d0bac18f Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sat, 7 Nov 2009 17:13:13 -0500 Subject: [PATCH 16/32] works w/o specifying post --- classes/ComicPress.inc | 8 ++++++++ test/ComicPressTest.php | 16 ++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/classes/ComicPress.inc b/classes/ComicPress.inc index 9d1c0fe..acb12ac 100644 --- a/classes/ComicPress.inc +++ b/classes/ComicPress.inc @@ -1,5 +1,7 @@ find_parents(); + } + foreach (array(get_stylesheet_directory(), get_template_directory()) as $dir) { $dir = trailingslashit($dir) . $path; $dirs_to_search = $this->category_search($categories, $dir); diff --git a/test/ComicPressTest.php b/test/ComicPressTest.php index 317894c..cf69a34 100644 --- a/test/ComicPressTest.php +++ b/test/ComicPressTest.php @@ -76,6 +76,14 @@ class ComicPressTest extends PHPUnit_Framework_TestCase { ), array('chapter-1', 'comic'), vfsStream::url('root/child/partials/comic/index.inc') + ), + array( + array( + 'root/child/partials/index.inc', + 'root/child/partials/comic/index.inc' + ), + null, + vfsStream::url('root/child/partials/comic/index.inc') ) ); } @@ -84,6 +92,8 @@ class ComicPressTest extends PHPUnit_Framework_TestCase { * @dataProvider providerTestFindFile */ function testFindFile($files_to_setup, $post_categories, $expected_path_result) { + global $post; + mkdir(vfsStream::url('root/parent/partials/comic/chapter-1'), 0777, true); mkdir(vfsStream::url('root/child/partials/comic/chapter-1'), 0777, true); @@ -94,6 +104,12 @@ class ComicPressTest extends PHPUnit_Framework_TestCase { _set_template_directory(vfsStream::url('root/parent')); _set_stylesheet_directory(vfsStream::url('root/child')); + $post = (object)array('ID' => 1); + wp_set_post_categories(1, array(2)); + + add_category(1, (object)array('slug' => 'comic', 'parent' => 0)); + add_category(2, (object)array('slug' => 'chapter-1', 'parent' => 1)); + $this->assertEquals($expected_path_result, $this->cp->find_file('index.inc', 'partials', $post_categories)); } } From fbdacf3322a1f287369a9b8609850f3a14491a71 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sat, 7 Nov 2009 17:18:11 -0500 Subject: [PATCH 17/32] added F() function --- functions.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/functions.php b/functions.php index 5c002d4..47fec98 100644 --- a/functions.php +++ b/functions.php @@ -44,6 +44,14 @@ function __comicpress_init() { } } +function F($name, $path, $override_post = null) { + global $post; + + $comic_post = new ComicPressComicPost(is_null($override_post) ? $post : $override_post); + + return ComicPress::get_instance()->find_file($name, $path, $comic_post->find_parents()); +} + /** * Display the list of Storyline categories. */ From 0cad4b0d53305f4536f9f0684a329983736a5776 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sat, 7 Nov 2009 17:35:51 -0500 Subject: [PATCH 18/32] make sure no path works --- classes/ComicPress.inc | 2 +- test/ComicPressTest.php | 25 ++++++++++++++++++------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/classes/ComicPress.inc b/classes/ComicPress.inc index acb12ac..6ba5bfa 100644 --- a/classes/ComicPress.inc +++ b/classes/ComicPress.inc @@ -136,7 +136,7 @@ class ComicPress { * @param attay $categories The child -> root category slugs to search within the specified path. If not provided, retrieve from current post. * @return string|boolean The path to the file, for false if not found. */ - function find_file($name, $path, $categories = null) { + function find_file($name, $path = '', $categories = null) { global $post; if (!is_array($categories)) { $comic_post = new ComicPressComicPost($post); diff --git a/test/ComicPressTest.php b/test/ComicPressTest.php index cf69a34..3f643a1 100644 --- a/test/ComicPressTest.php +++ b/test/ComicPressTest.php @@ -46,26 +46,35 @@ class ComicPressTest extends PHPUnit_Framework_TestCase { function providerTestFindFile() { return array( array( - array(), array(), false, + array(), 'partials', array(), false, ), array( array('root/parent/partials/index.inc'), + 'partials', array(), vfsStream::url('root/parent/partials/index.inc') ), + array( + array('root/parent/index.inc'), + '', + array(), + vfsStream::url('root/parent/index.inc') + ), array( array( 'root/parent/partials/index.inc', 'root/child/partials/index.inc' - ), - array(), + ), + 'partials', + array(), vfsStream::url('root/child/partials/index.inc') ), array( array( 'root/child/partials/index.inc', 'root/child/partials/comic/index.inc' - ), + ), + 'partials', array('comic'), vfsStream::url('root/child/partials/comic/index.inc') ), @@ -73,7 +82,8 @@ class ComicPressTest extends PHPUnit_Framework_TestCase { array( 'root/child/partials/index.inc', 'root/child/partials/comic/index.inc' - ), + ), + 'partials', array('chapter-1', 'comic'), vfsStream::url('root/child/partials/comic/index.inc') ), @@ -82,6 +92,7 @@ class ComicPressTest extends PHPUnit_Framework_TestCase { 'root/child/partials/index.inc', 'root/child/partials/comic/index.inc' ), + 'partials', null, vfsStream::url('root/child/partials/comic/index.inc') ) @@ -91,7 +102,7 @@ class ComicPressTest extends PHPUnit_Framework_TestCase { /** * @dataProvider providerTestFindFile */ - function testFindFile($files_to_setup, $post_categories, $expected_path_result) { + function testFindFile($files_to_setup, $search_path, $post_categories, $expected_path_result) { global $post; mkdir(vfsStream::url('root/parent/partials/comic/chapter-1'), 0777, true); @@ -110,7 +121,7 @@ class ComicPressTest extends PHPUnit_Framework_TestCase { add_category(1, (object)array('slug' => 'comic', 'parent' => 0)); add_category(2, (object)array('slug' => 'chapter-1', 'parent' => 1)); - $this->assertEquals($expected_path_result, $this->cp->find_file('index.inc', 'partials', $post_categories)); + $this->assertEquals($expected_path_result, $this->cp->find_file('index.inc', $search_path, $post_categories)); } } From b59fea3a86a226e0db60302e6cde9e6a1d4051ec Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sat, 7 Nov 2009 17:40:16 -0500 Subject: [PATCH 19/32] using application.php now --- application.php | 37 +++++++++++++++++++++++++++++++++++++ footer.php | 17 ----------------- functions.php | 19 ++++++++----------- header.php | 25 +------------------------ ie6submenus.js | 14 -------------- index.php | 33 ++------------------------------- 6 files changed, 48 insertions(+), 97 deletions(-) create mode 100644 application.php delete mode 100644 ie6submenus.js diff --git a/application.php b/application.php new file mode 100644 index 0000000..67ff38d --- /dev/null +++ b/application.php @@ -0,0 +1,37 @@ + +> + + > +
    + + + + + + +
    + + +
    + + + + + \ No newline at end of file diff --git a/footer.php b/footer.php index 5406591..e69de29 100644 --- a/footer.php +++ b/footer.php @@ -1,17 +0,0 @@ -
    - - - - - - - - - - \ No newline at end of file diff --git a/functions.php b/functions.php index 47fec98..3610832 100644 --- a/functions.php +++ b/functions.php @@ -32,16 +32,6 @@ function __comicpress_init() { $comicpress_filters = new ComicPressFilters(); $comicpress_filters->init(); - - $layouts = $comicpress->get_layout_choices(); - if (isset($layouts[$comicpress->comicpress_options['layout']])) { - if (isset($layouts[$comicpress->comicpress_options['layout']]['Sidebars'])) { - foreach (explode(",", $layouts[$comicpress->comicpress_options['layout']]['Sidebars']) as $sidebar) { - $sidebar = trim($sidebar); - register_sidebar($sidebar); - } - } - } } function F($name, $path, $override_post = null) { @@ -52,6 +42,12 @@ function F($name, $path, $override_post = null) { return ComicPress::get_instance()->find_file($name, $path, $comic_post->find_parents()); } +function FinishComicPress() { + $content = ob_get_clean(); + + include(F('application.php', '')); +} + /** * Display the list of Storyline categories. */ @@ -95,4 +91,5 @@ function comicpress_list_storyline_categories($args = "") { echo $output; } -?> +ob_start(); +?> \ No newline at end of file diff --git a/header.php b/header.php index e5f9153..7e79f0c 100644 --- a/header.php +++ b/header.php @@ -1,5 +1,3 @@ - -> <?php bloginfo('name'); @@ -21,25 +19,4 @@ <link rel="alternate" type="application/atom+xml" title="<?php bloginfo('name') ?> Atom Feed" href="<?php bloginfo('atom_url') ?>" /> <link rel="pingback" href="<?php bloginfo('pingback_url') ?>" /> <?php wp_head() ?> - <!--[if lt IE 7]><script type="text/javascript" src="<?php bloginfo('template_directory') ?>/ie6submenus.js"></script><![endif]--> -</head> - -<body <?php if (function_exists('body_class')) { body_class(); } ?>> - -<div id="page"><!-- Defines entire site width - Ends in Footer --> - -<div id="header"> - <h1><a href="<?php echo get_settings('home') ?>"><?php bloginfo('name') ?></a></h1> - <div class="description"><?php bloginfo('description') ?></div> -</div> - -<div id="menubar"> - - <ul id="menu"> - <li><a href="<?php bloginfo('url') ?>">Home</a></li> - <?php wp_list_pages('sort_column=menu_order&depth=4&title_li=') ?> - <li><a href="<?php bloginfo('rss2_url') ?>">Subscribe</a></li> - </ul> - - <div class="clear"></div> -</div> \ No newline at end of file +</head> \ No newline at end of file diff --git a/ie6submenus.js b/ie6submenus.js deleted file mode 100644 index 72fd283..0000000 --- a/ie6submenus.js +++ /dev/null @@ -1,14 +0,0 @@ -//Suckerfish Dropdown for IE6 - -sfHover = function() { -var sfEls = document.getElementById("menu").getElementsByTagName("LI"); -for (var i=0; i<sfEls.length; i++) { -sfEls[i].onmouseover=function() { -this.className+=" sfhover"; -} -sfEls[i].onmouseout=function() { -this.className=this.className.replace(new RegExp(" sfhover\\b"), ""); -} -} -} -if (window.attachEvent) window.attachEvent("onload", sfHover); \ No newline at end of file diff --git a/index.php b/index.php index 0a85f81..0d7d2f6 100644 --- a/index.php +++ b/index.php @@ -1,34 +1,5 @@ <?php - global $comicpress, $post, $nav_comics; + echo "hello"; - comicpress_init(); - - $nav_comics = array(); - $t = $post; - $post = $nav_comics['last']; - setup_postdata($post); - - ob_start(); - - if (!is_paged()) { include_partial('index-display-comic'); } - - $comic = ob_get_clean(); - - ob_start(); - - if (!is_paged() && ($comicpress->comicpress_options['comic_space'] == "comic_only")) { include_partial('index-comic-post'); } - - include_partial('index-blog-header'); - - $index_posts_query = $comicpress->get_index_blog_posts_query(); - - while ($index_posts_query->have_posts()) { - $index_posts_query->the_post(); - include_partial('index-blog-post'); - } - - $content = ob_get_clean(); - $post = $t; - - include(get_template_directory() . '/layouts/' . $comicpress->comicpress_options['layout']); + FinishComicPress(); ?> \ No newline at end of file From 62603a49829eb66dc833432f9d0b742e6c0f8093 Mon Sep 17 00:00:00 2001 From: John Bintz <john@dawn.local> Date: Sat, 7 Nov 2009 17:50:47 -0500 Subject: [PATCH 20/32] gut more of admin --- classes/ComicPressAdmin.inc | 10 +++---- classes/partials/options-admin.inc | 48 ++++++------------------------ css/cp-admin.css | 4 +++ 3 files changed, 18 insertions(+), 44 deletions(-) diff --git a/classes/ComicPressAdmin.inc b/classes/ComicPressAdmin.inc index 4245dee..7b5017a 100644 --- a/classes/ComicPressAdmin.inc +++ b/classes/ComicPressAdmin.inc @@ -70,18 +70,18 @@ class ComicPressAdmin { if (strpos($pagenow, "post") === 0) { add_meta_box("comic-image-ordering", __("Comic Image Ordering", 'comicpress'), array(&$this, 'render_comic_image_ordering'), "post", "normal", "low"); - wp_enqueue_script('cp-ordering', get_stylesheet_directory_uri() . '/js/ComicImageOrdering.js', array('scriptaculous', 'scriptaculous-slider')); - wp_enqueue_style('cp-admin', get_stylesheet_directory_uri() . '/css/cp-admin.css'); + wp_enqueue_script('cp-ordering', get_template_directory_uri() . '/js/ComicImageOrdering.js', array('scriptaculous', 'scriptaculous-slider')); + wp_enqueue_style('cp-admin', get_template_directory_uri() . '/css/cp-admin.css'); add_action('admin_footer', array(&$this, 'admin_footer')); } if ($plugin_page == 'comicpress/render_admin') { - wp_enqueue_style('cp-admin', get_stylesheet_directory_uri() . '/css/cp-admin.css'); - wp_enqueue_script('cp-admin', get_stylesheet_directory_uri() . '/js/Storyline.js', array('prototype', 'scriptaculous')); + wp_enqueue_style('cp-admin', get_template_directory_uri() . '/css/cp-admin.css'); + wp_enqueue_script('cp-admin', get_template_directory_uri() . '/js/Storyline.js', array('prototype', 'scriptaculous')); } if (strpos($pagenow, "media-upload") === 0) { - wp_enqueue_script('cp-media', get_stylesheet_directory_uri() . '/js/MediaUpload.js', array('prototype')); + wp_enqueue_script('cp-media', get_template_directory_uri() . '/js/MediaUpload.js', array('prototype')); } } diff --git a/classes/partials/options-admin.inc b/classes/partials/options-admin.inc index 04f6271..9912fc6 100644 --- a/classes/partials/options-admin.inc +++ b/classes/partials/options-admin.inc @@ -5,11 +5,16 @@ <h3><?php _e('Global Options', 'comicpress') ?></h3> <table class="widefat fixed"> <tr> - <th scope="row" valign="top"><?php _e('Layout', 'comicpress') ?></th> + <th scope="row" valign="top"><?php _e("Arrange storyline category order", 'comicpress') ?></th> <td> - <select name="cp[layout]"> - <?php echo $this->create_layout_options($this->comicpress->get_layout_choices(), $this->comicpress->comicpress_options['layout']) ?> - </select> + <input type="hidden" name="cp[storyline_order]" /> + <div id="storyline-sorter" class="cp-children"> + <?php + $this->_render_admin_storyline_tree(reset($storyline->get_simple_storyline())) + ?> + </div> + <script type="text/javascript">Storyline.setup()</script> + <p><em><?php _e('(drag and drop desired order. categories can be modified on the Posts -> Categories page)', 'comicpress') ?></em></p> </td> </tr> <?php foreach (array( @@ -31,41 +36,6 @@ <input type="text" name="cp[blogpost_count]" value="<?php echo $this->comicpress->comicpress_options['blogpost_count'] ?>" size="3" /> </td> </tr> - <tr> - <th scope="row" valign="top"><?php _e("Arrange storyline category order", 'comicpress') ?></th> - <td> - <input type="hidden" name="cp[storyline_order]" /> - <div id="storyline-sorter" class="cp-children"> - <?php - $this->_render_admin_storyline_tree(reset($storyline->get_simple_storyline())) - ?> - </div> - <script type="text/javascript">Storyline.setup()</script> - <p><em><?php _e('(drag and drop desired order. categories can be modified on the Posts -> Categories page)', 'comicpress') ?></em></p> - </td> - </tr> - <tr> - <th scope="row"><?php _e('Insert into comic space:', 'comicpress') ?></th> - <td> - <?php foreach (array( - "comic_only" => __("The comic only, with post content below", 'comicpress'), - "post_content" => __("The post content", 'comicpress') - ) as $value => $label) { ?> - <label><input type="radio" name="cp[comic_space]" value="<?php echo $value ?>" <?php echo ($this->comicpress->comicpress_options['comic_space'] == $value) ? 'checked="checked"' : "" ?> /> <?php echo $label ?></label><br /> - <?php } ?> - </td> - </tr> - <tr> - <th scope="row"><?php _e('Use subcategories of the Master Comic Category as:', 'comicpress') ?></th> - <td> - <?php foreach (array( - "storyline" => __("Storyline indicators for a single comic", 'comicpress'), - "multicomic" => __("Multicomic indicators, with direct descendents being separate comics", 'comicpress') - ) as $value => $label) { ?> - <label><input type="radio" name="cp[category_usage]" value="<?php echo $value ?>" <?php echo ($this->comicpress->comicpress_options['category_usage'] == $value) ? 'checked="checked"' : "" ?> /> <?php echo $label ?></label><br /> - <?php } ?> - </td> - </tr> </table> <h3><?php _e('Admin Options', 'comicpress') ?></h3> <table class="widefat fixed"> diff --git a/css/cp-admin.css b/css/cp-admin.css index e03ce6f..c2c731b 100644 --- a/css/cp-admin.css +++ b/css/cp-admin.css @@ -2,6 +2,10 @@ padding-left: 20px } +#children-0 { + padding-left: 0 +} + .cp-category-info span { cursor: move; color: #004 From 57ef1784d6a054fa95b9061a69c23afc31a27368 Mon Sep 17 00:00:00 2001 From: John Bintz <john@dawn.local> Date: Sat, 7 Nov 2009 17:52:21 -0500 Subject: [PATCH 21/32] gut more of admin --- classes/ComicPress.inc | 52 +++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 29 deletions(-) diff --git a/classes/ComicPress.inc b/classes/ComicPress.inc index 6ba5bfa..fd105c4 100644 --- a/classes/ComicPress.inc +++ b/classes/ComicPress.inc @@ -7,40 +7,34 @@ require_once(dirname(__FILE__) . '/ComicPressComicPost.inc'); */ class ComicPress { var $comicpress_options = array( - 'comic_category_id' => 1, 'comic_dimensions' => '760x', 'rss_dimensions' => '350x', 'archive_dimensions' => '125x', 'mini_dimensions' => '100x', - 'category_order' => false, - 'blogpost_count' => 10, - 'comic_space' => 'comic_only', - 'category_usage' => 'storyline', - 'layout' => 'classic.inc', 'helpers' => array(), 'addons' => array(), 'storyline_order' => '' ); - + var $additional_stylesheets = array(); var $additional_javascripts = array(); - + var $comic_post_attachments_cache = array(); var $category_tree = array(); - + var $partial_paths = array(); var $layouts = null; - + function &get_instance() { static $instance; - + if (!$instance) { $instance = array(new ComicPress()); } - + return $instance[0]; } - + /** * Load ComicPress options. */ @@ -50,7 +44,7 @@ class ComicPress { $this->comicpress_options = array_merge($this->comicpress_options, $result); } } - + /** * Save ComicPress options. */ @@ -65,15 +59,15 @@ class ComicPress { */ function init() { $this->load(); - + if (current_user_can('edit_themes')) { if (!empty($this->comicpress_options['helpers'])) { add_action('wp_footer', array(&$this, 'announce_activated_helpers')); } } - + add_filter('intermediate_image_sizes', array(&$this, 'intermediate_image_sizes')); - + foreach (array('comic', 'rss', 'archive', 'mini') as $size) { list($w, $h) = explode("x", $this->comicpress_options["${size}_dimensions"]); update_option("${size}_size_w", $w); @@ -85,32 +79,32 @@ class ComicPress { function intermediate_image_sizes($sizes) { return array_merge($sizes, array('comic', 'rss', 'archive', 'mini')); } - + function announce_activated_helpers() { echo "<center>[ <strong>Activated ComicPress helpers:</strong> " . implode(", ", array_keys($this->comicpress_options['helpers'])) . " ]</center>"; } - + /** * Gather blog posts for the given index page. */ function get_index_blog_posts_query() { global $post, $paged; - + $t = $post; - + $wp_query = new WP_Query(); $wp_query->query( - 'showposts=' . + 'showposts=' . (int)$this->comicpress_options['blogpost_count'] . - '&cat=-' . - $this->comicpress_options['comic_category_id'] . + '&cat=-' . + $this->comicpress_options['comic_category_id'] . '&paged=' . $paged ); - + return $wp_query; } - + /** * Search a path for directories named after the slugs provided. * @param array $categories A list of category slugs going from child -> parent -> root. @@ -128,7 +122,7 @@ class ComicPress { } return array_reverse($all_paths); } - + /** * Recursively search a particular template directory for a specified file, limiting the search to the provided categories. * @param string $name The name of the file with no path information. @@ -142,12 +136,12 @@ class ComicPress { $comic_post = new ComicPressComicPost($post); $categories = $comic_post->find_parents(); } - + foreach (array(get_stylesheet_directory(), get_template_directory()) as $dir) { $dir = trailingslashit($dir) . $path; $dirs_to_search = $this->category_search($categories, $dir); $dirs_to_search[] = $dir; - + foreach ($dirs_to_search as $category_path) { $target = trailingslashit($category_path) . $name; if (file_exists($target)) { From 4d9392470cf81080f85a2d060ea455b9fa454bb3 Mon Sep 17 00:00:00 2001 From: John Bintz <john@dawn.local> Date: Sat, 7 Nov 2009 20:56:49 -0500 Subject: [PATCH 22/32] more restrictions --- classes/ComicPress.inc | 9 -- classes/ComicPressDBInterface.inc | 55 +++++----- classes/ComicPressStoryline.inc | 154 +++++++++++++++++++++------- functions.php | 76 +++++++++++++- index.php | 6 +- test/ComicPressDBInterfaceTest.php | 29 ------ test/ComicPressStorylineTest.php | 156 +++++++++++++++++++++++------ 7 files changed, 341 insertions(+), 144 deletions(-) diff --git a/classes/ComicPress.inc b/classes/ComicPress.inc index fd105c4..1e259a6 100644 --- a/classes/ComicPress.inc +++ b/classes/ComicPress.inc @@ -16,15 +16,6 @@ class ComicPress { 'storyline_order' => '' ); - var $additional_stylesheets = array(); - var $additional_javascripts = array(); - - var $comic_post_attachments_cache = array(); - var $category_tree = array(); - - var $partial_paths = array(); - var $layouts = null; - function &get_instance() { static $instance; diff --git a/classes/ComicPressDBInterface.inc b/classes/ComicPressDBInterface.inc index 736bfbf..4f5a323 100644 --- a/classes/ComicPressDBInterface.inc +++ b/classes/ComicPressDBInterface.inc @@ -1,40 +1,33 @@ <?php class ComicPressDBInterface { - var $_non_comic_categories, $_all_categories; - function ComicPressDBInterface() {} - + function get_instance() { static $instance; - + if (!isset($instance)) { $instance = new ComicPressDBInterface(); } return $instance; } function _get_categories() { return get_categories("hide_empty=0"); } - /** - * Set the comic categories for the current run of ComicPress. - */ - function set_comic_categories($categories) { - $this->_all_categories = get_all_category_ids(); - $this->_non_comic_categories = array_values(array_diff($this->_all_categories, $categories)); - } - - function _get_categories_to_exclude($category = null) { - return (is_null($category)) ? $this->_non_comic_categories : array_values(array_diff($this->_all_categories, array($category))); - } - - /** - * Find the terminal post in a specific category. - */ - function get_terminal_post_in_category($category_id, $first = true) { + function get_terminal_post($first = true, $include_categories = null) { $this->_prepare_wp_query(); - - $sort_order = $first ? "asc" : "desc"; + $terminal_comic_query = new WP_Query(); - $terminal_comic_query->query("showposts=1&order=${sort_order}&cat=${category_id}&status=publish"); + + $query_parameters = array( + 'showposts' => 1, + 'order' => $first ? "asc" : "desc", + 'status' => 'publish' + ); + + if (is_array($include_categories)) { + $query_parameters['category_in'] = $include_categories; + } + + $terminal_comic_query->query($query_parameters); $post = false; if ($terminal_comic_query->have_posts()) { $post = reset($terminal_comic_query->posts); @@ -43,19 +36,19 @@ class ComicPressDBInterface { $this->_reset_wp_query(); return $post; } - + /** * Get the first comic in a category. */ - function get_first_comic($category_id) { - return $this->get_terminal_post_in_category($category_id); + function get_first_comic($include_categories = null) { + return $this->get_terminal_post(true, $include_categories); } - + /** * Get the last comic in a category. */ - function get_last_comic($category_id) { - return $this->get_terminal_post_in_category($category_id, false); + function get_last_comic($include_categories = null) { + return $this->get_terminal_post(false, $include_categories); } /** @@ -67,7 +60,7 @@ class ComicPressDBInterface { $this->_prepare_wp_query(); if (!is_null($override_post)) { $temp_post = $post; $post = $override_post; } - + $result = get_adjacent_post(false, implode(" and ", $this->_get_categories_to_exclude($category)), !$next); $this->_reset_wp_query(); @@ -91,7 +84,7 @@ class ComicPressDBInterface { $wp_query->is_single = $this->is_single; $wp_query->in_the_loop = $this->in_the_loop; } - + /** * Get the previous comic from the current one. */ diff --git a/classes/ComicPressStoryline.inc b/classes/ComicPressStoryline.inc index cd863ef..bac9b8a 100644 --- a/classes/ComicPressStoryline.inc +++ b/classes/ComicPressStoryline.inc @@ -7,8 +7,9 @@ class ComicPressStoryline { var $_structure; var $_category_search; - function read_from_options() { + function &read_from_options() { $this->create_structure($this->get_flattened_storyline()); + return $this; } /** @@ -34,10 +35,10 @@ class ComicPressStoryline { function set_order_via_flattened_storyline($order) { $nodes = explode(',', $order); $original_nodes = explode(',', $this->get_flattened_storyline()); - + $missing_good_nodes = array_diff($original_nodes, $nodes); $any_bad_nodes = array_diff($nodes, $original_nodes); - + if (empty($missing_good_nodes) && empty($any_bad_nodes)) { $this->set_flattened_storyline($order); return true; @@ -141,7 +142,7 @@ class ComicPressStoryline { } return false; } - + function parent($id) { return $this->_get_field('parent', $id); } function previous($id) { return $this->_get_field('previous', $id); } function next($id) { return $this->_get_field('next', $id); } @@ -167,7 +168,7 @@ class ComicPressStoryline { */ function get_valid_post_category($post_id) { $result = false; - + foreach (wp_get_post_categories($post_id) as $category) { if ($this->valid($category)) { if ($result) { return false; } @@ -199,7 +200,7 @@ class ComicPressStoryline { } $simple_storyline[$parent][$category_id] = true; } - + return $this->_merge_simple_storyline($simple_storyline); } @@ -276,7 +277,7 @@ class ComicPressStoryline { $flattened_storyline = $this->get_flattened_storyline(); } $all_categories_flattened = $this->get_category_flattened(); - + $result = $this->normalize_flattened_storyline($flattened_storyline, $all_categories_flattened); if ($set) { $this->set_flattened_storyline($result); @@ -313,7 +314,7 @@ class ComicPressStoryline { $missing_from_storyline = array_diff($category_nodes, $storyline_nodes); $extra_in_storyline = array_diff($storyline_nodes, $category_nodes); - + if (!empty($missing_from_storyline)) { $missing_from_storyline = $this->_length_sort($missing_from_storyline); foreach ($missing_from_storyline as $node) { @@ -331,7 +332,7 @@ class ComicPressStoryline { } } } - + if (!empty($extra_in_storyline)) { $new = array(); foreach ($storyline_nodes as $node) { @@ -341,7 +342,7 @@ class ComicPressStoryline { } $storyline_nodes = $new; } - + return implode(',', $storyline_nodes); } @@ -382,34 +383,53 @@ class ComicPressStoryline { } function _find_children($parent) { - $children = array($parent); - do { - $found_children = false; - foreach ($this->_structure as $category_id => $info) { - if (!in_array($category_id, $children)) { - if (isset($info['parent'])) { - if (in_array($info['parent'], $children)) { - $children[] = $category_id; - $found_children = true; - } - } - } - } - } while ($found_children); + if (!is_numeric($parent)) { + foreach (get_all_category_ids() as $id) { + $category = get_category($id); + if ($category->slug == $parent) { + $parent = $id; break; + } + } + } + if (is_numeric($parent)) { + $children = array($parent); + do { + $found_children = false; + foreach ($this->_structure as $category_id => $info) { + if (!in_array($category_id, $children)) { + if (isset($info['parent'])) { + if (in_array($info['parent'], $children)) { + $children[] = $category_id; + $found_children = true; + } + } + } + } + } while ($found_children); - return $children; + return $children; + } + return false; + } + + function &_include($method, $parameter) { + $this->_category_search = array_unique(array_merge($this->_category_search, $this->{$method}($parameter))); + sort($this->_category_search); + return $this; + } + + function &_exclude($method, $parameter) { + $this->_category_search = array_diff($this->_category_search, $this->{$method}($parameter)); + sort($this->_category_search); + return $this; } function &include_children($parent = null) { - $this->_category_search = array_unique(array_merge($this->_category_search, $this->_find_children($parent))); - sort($this->_category_search); - return $this; + return $this->_include('_find_children', $parent); } function &exclude_children($parent = null) { - $this->_category_search = array_diff($this->_category_search, $this->_find_children($parent)); - sort($this->_category_search); - return $this; + return $this->_exclude('_find_children', $parent); } function _find_level_or_above($level = null) { @@ -421,15 +441,42 @@ class ComicPressStoryline { } function &include_level_or_above($level = null) { - $this->_category_search = array_unique(array_merge($this->_category_search, $this->_find_level_or_above($level))); - sort($this->_category_search); - return $this; + return $this->_include('_find_level_or_above', $level); } function &exclude_level_or_above($level = null) { - $this->_category_search = array_diff($this->_category_search, $this->_find_level_or_above($level)); - sort($this->_category_search); - return $this; + return $this->_exclude('_find_level_or_above', $level); + } + + function _find_only($id = null) { + if (isset($this->_structure[$id])) { + return array($id); + } + return array(); + } + + function &include_only($id = null) { + return $this->_include('_find_only', $id); + } + + function &exclude_only($id = null) { + return $this->_exclude('_find_only', $id); + } + + function _find_level($level = null) { + $found = array(); + foreach ($this->_structure as $category_id => $info) { + if ($info['level'] == $level) { $found[] = $category_id; } + } + return $found; + } + + function &include_level($id = null) { + return $this->_include('_find_level', $id); + } + + function &exclude_level($id = null) { + return $this->_exclude('_find_level', $id); } function end_search() { @@ -437,6 +484,39 @@ class ComicPressStoryline { $this->_category_search = array(); return $result; } + + function build_from_restrictions($restrictions = null) { + $this->read_from_options()->exclude_all(); + + $include_all = true; + if (is_array($restrictions)) { + if (!empty($restrictions)) { + $include_all = false; + } + } + + if (!$include_all) { + foreach ($restrictions as $type => $list) { + foreach ((array)$list as $restriction) { + switch ($type) { + case 'child_of': + $this->include_children($restriction); + break; + case 'only': + $this->include_only($restriction); + break; + case 'level': + $this->include_level($restriction); + break; + } + } + } + } else { + $this->include_all(); + } + + return $this->end_search(); + } } ?> \ No newline at end of file diff --git a/functions.php b/functions.php index 3610832..801ee01 100644 --- a/functions.php +++ b/functions.php @@ -5,7 +5,7 @@ add_action('init', '__comicpress_init'); function __comicpress_init() { global $comicpress, $wp_query; - + if (current_user_can('edit_files')) { wp_cache_flush(); } @@ -36,18 +36,86 @@ function __comicpress_init() { function F($name, $path, $override_post = null) { global $post; - + $comic_post = new ComicPressComicPost(is_null($override_post) ? $post : $override_post); return ComicPress::get_instance()->find_file($name, $path, $comic_post->find_parents()); } -function FinishComicPress() { +/** + * Finish rendering this template and shove the output into application.php. + */ +function finish_comicpress() { $content = ob_get_clean(); - + include(F('application.php', '')); } +/** + * Protect global $post and $wp_query. + */ +function protect_query() { + global $post, $wp_query, $__post, $__wp_query; + + $__post = $post; + $__wp_query = $wp_query; +} + +/** + * Restore global $post and $wp_query. + */ +function unprotect_query() { + global $post, $wp_query, $__post, $__wp_query; + + $post = $__post; + $wp_query = $__wp_query; + + $__post = $__wp_query = null; +} + +function retrieve_storyline_post($which, $restrictions = null, $override_post = null) { + global $post; + + $storyline = new ComicPressStoryline(); + $storyline->read_from_options()->exclude_all(); + + if (!is_null($restrictions)) { + if (is_array($restrictions)) { + foreach ($restrictions as $type => $list) { + switch ($type) { + case 'child_of': + foreach ($list as $restriction) { $storyline->include_children($restriction); } + break; + } + } + } + } else { + $storyline->include_all(); + } + + $categories = $storyline->end_search(); + + var_dump($categories); + + $dbi = ComicPressDBInterface::get_instance(); + + $new_post = false; + + switch ($which) { + case 'first': + case 'last': + case 'next': + case 'previous': + $method = "get_${which}_post"; + if (method_exists($dbi, $method)) { + $new_post = $dbi->{$method}($categories); + } + break; + } + + return $new_post; +} + /** * Display the list of Storyline categories. */ diff --git a/index.php b/index.php index 0d7d2f6..f93915d 100644 --- a/index.php +++ b/index.php @@ -1,5 +1,7 @@ <?php - echo "hello"; + $current_post = retrieve_storyline_post('last', array('child_of' => array('amoc', 'dawns-dictionary-drama'))); - FinishComicPress(); + var_dump($current_post); + + finish_comicpress(); ?> \ No newline at end of file diff --git a/test/ComicPressDBInterfaceTest.php b/test/ComicPressDBInterfaceTest.php index 2c7dc1e..570a2cf 100644 --- a/test/ComicPressDBInterfaceTest.php +++ b/test/ComicPressDBInterfaceTest.php @@ -14,35 +14,6 @@ class ComicPressDBInterfaceTest extends PHPUnit_Framework_TestCase { $b = ComicPressDBInterface::get_instance(); $this->assertEquals("test", $b->test); } - - function testSetComicCategories() { - $dbi = ComicPressDBInterface::get_instance(); - - for ($i = 1; $i <= 4; ++$i) { add_category($i, (object)array()); } - - $dbi->set_comic_categories(array(2,3)); - - $this->assertEquals(array(1,2,3,4), $dbi->_all_categories); - $this->assertEquals(array(1,4), $dbi->_non_comic_categories); - } - - function providerTestGetCategoriesToExclude() { - return array( - array(null, array(1 ,4)), - array(2, array(1, 3, 4)), - ); - } - - /** - * @dataProvider providerTestGetCategoriesToExclude - */ - function testGetCategoriesToExclude($category, $expected_results) { - $dbi = ComicPressDBInterface::get_instance(); - $dbi->_all_categories = array(1,2,3,4); - $dbi->_non_comic_categories = array(1,4); - - $this->assertEquals($expected_results, $dbi->_get_categories_to_exclude($category)); - } } ?> \ No newline at end of file diff --git a/test/ComicPressStorylineTest.php b/test/ComicPressStorylineTest.php index d4b8ddf..43a0d0d 100644 --- a/test/ComicPressStorylineTest.php +++ b/test/ComicPressStorylineTest.php @@ -7,7 +7,7 @@ require_once('ComicPressStoryline.inc'); class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { function setUp() { _reset_wp(); - + $this->css = new ComicPressStoryline(); } @@ -83,7 +83,7 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { function testCreateStorylineStructure($input, $expected_structure) { global $wp_object_cache; $this->assertTrue(empty($wp_object_cache->cache['comicpress'])); - + $this->assertEquals(is_array($expected_structure), $this->css->create_structure($input)); $this->assertEquals($expected_structure, $this->css->_structure); @@ -104,7 +104,7 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { $this->assertEquals(is_array($expected_structure), $this->css->create_structure($input)); $this->assertEquals($expected_structure, $this->css->_structure); } - + function providerTestGetFields() { return array( array('parent', 1, false), @@ -115,7 +115,7 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { array('valid', 6, false), ); } - + /** * @dataProvider providerTestGetFields */ @@ -126,7 +126,7 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { '3' => array('parent' => 2, 'next' => 4, 'previous' => 2), '4' => array('parent' => 2, 'previous' => 3) ); - + $this->assertEquals($expected_value, $this->css->{$field}($category)); } @@ -139,13 +139,13 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { array(array(3), array('previous')), ); } - + /** * @dataProvider providerTestGetValidNav */ function testGetValidNav($post_categories, $expected_navigation) { wp_set_post_categories(1, $post_categories); - + $this->css->_structure = array( '1' => array('next' => 2), '2' => array('previous' => 1, 'next' => 3), @@ -169,12 +169,12 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { function testGetValidPostCategory($post_categories, $expected_result) { $css = $this->getMock('ComicPressStoryline', array('valid')); $css->expects($this->any())->method('valid')->will($this->returnValue(true)); - + wp_set_post_categories(1, $post_categories); $this->assertEquals($expected_result, $css->get_valid_post_category(1)); } - + function testGetSimpleStoryline() { $this->css->_structure = array( '1' => array('next' => 2), @@ -182,7 +182,7 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { '3' => array('parent' => 2, 'next' => 4, 'previous' => 2), '4' => array('parent' => 2, 'previous' => 3) ); - + $expected_result = array( array( '1' => array( @@ -193,10 +193,10 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { ) ) ); - + $this->assertEquals($expected_result, $this->css->get_simple_storyline()); } - + function providerTestSetFlattenedStorylineOrder() { return array( array('0/1,0/1/2,0/1/2/3,0/1/2/4', '0/1,0/1/2,0/1/2/3,0/1/2/4', true), @@ -204,7 +204,7 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { array('0/1,0/1/2,0/1/2/5,0/1/2/3', '0/1,0/1/2,0/1/2/3,0/1/2/4', false), ); } - + /** * @dataProvider providerTestSetFlattenedStorylineOrder */ @@ -212,11 +212,11 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { $css = $this->getMock('ComicPressStoryline', array( 'get_flattened_storyline', 'set_flattened_storyline' )); - + $css->expects($this->once()) ->method('get_flattened_storyline') ->will($this->returnValue('0/1,0/1/2,0/1/2/3,0/1/2/4')); - + if ($expected_return === true) { $css->expects($this->once()) ->method('set_flattened_storyline') @@ -225,17 +225,17 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { $css->expects($this->never()) ->method('set_flattened_storyline'); } - + $this->assertEquals($expected_return, $css->set_order_via_flattened_storyline($input)); } - + function testMergeSimpleStoryline() { $original = array( 0 => array(1 => true), 1 => array(2 => true), 2 => array(3 => true, 4 => true) ); - + $expected = array( 0 => array( 1 => array( @@ -246,17 +246,17 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { ) ) ); - + $this->assertEquals($expected, $this->css->_merge_simple_storyline($original)); } - + function testGetCategorySimpleStructure() { add_category(1, (object)array('parent' => 0)); add_category(2, (object)array('parent' => 1)); add_category(3, (object)array('parent' => 2)); add_category(4, (object)array('parent' => 2)); add_category(5, (object)array('parent' => 0)); - + $this->assertEquals(array( '0' => array( '1' => array( @@ -267,7 +267,7 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { ) ) ), $this->css->get_category_simple_structure(1)); - + $this->assertEquals(array( '0' => array( '1' => array( @@ -280,7 +280,7 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { ) ), $this->css->get_category_simple_structure()); } - + function providerTestNormalizeFlattenedStoryline() { return array( array('0/1,0/1/2,0/1/2/4', '0/1,0/1/2,0/1/2/4,0/1/2/3'), @@ -289,7 +289,7 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { array('', '0/1,0/1/2,0/1/2/3,0/1/2/4'), ); } - + /** * @dataProvider providerTestNormalizeFlattenedStoryline */ @@ -299,7 +299,7 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { $this->css->normalize_flattened_storyline($original_structure, '0/1,0/1/2,0/1/2/3,0/1/2/4') ); } - + function testFlattenSimpleStoryline() { $this->assertEquals('0/1,0/1/2,0/1/2/3,0/1/2/4', $this->css->flatten_simple_storyline( array( @@ -314,16 +314,16 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { ) )); } - + function testLengthSort() { $data = array( '0/1', '0/1/3', '0/1/3/6', '0/1/3/7', '0/1/4', '0/1/4/2', '0/1/4/3' ); - + $expected_result = array( '0/1', '0/1/3', '0/1/4', '0/1/3/6', '0/1/3/7', '0/1/4/2', '0/1/4/3' ); - + $this->assertEquals($expected_result, $this->css->_length_sort($data)); } @@ -334,19 +334,28 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { '3' => array('parent' => 2, 'next' => 4, 'previous' => 2), '4' => array('parent' => 2, 'previous' => 3) ); - + $this->assertEquals($this->css, $this->css->include_all()); $this->assertEquals(array(1,2,3,4), $this->css->_category_search); } function testExcludeAll() { $this->css->_category_search = array(1,2,3,4); - + $this->assertEquals($this->css, $this->css->exclude_all()); $this->assertEquals(array(), $this->css->_category_search); } - function testFindChildren() { + function providerTestFindChildren() { + return array( + array(2), array('test') + ); + } + + /** + * @dataProvider providerTestFindChildren + */ + function testFindChildren($search) { $this->css->_structure = array( '1' => array('next' => 2), '2' => array('parent' => 1, 'previous' => 1, 'next' => 3), @@ -354,7 +363,9 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { '4' => array('parent' => 2, 'previous' => 3) ); - $this->assertEquals(array(2,3,4), $this->css->_find_children(2)); + add_category(2, (object)array('slug' => 'test')); + + $this->assertEquals(array(2,3,4), $this->css->_find_children($search)); } function testIncludeChildren() { @@ -413,6 +424,87 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { $this->assertEquals(array(1,2,3), $this->css->end_search()); $this->assertEquals(array(), $this->css->_category_search); } + + function providerTestFindOnly() { + return array( + array(1, array(1)), + array(5, array()), + ); + } + + /** + * @dataProvider providerTestFindOnly + */ + function testFindOnly($id, $expected_return) { + $this->css->_structure = array( + '1' => array('next' => 2, 'level' => 1), + '2' => array('parent' => 1, 'previous' => 1, 'next' => 3, 'level' => 2), + '3' => array('parent' => 2, 'next' => 4, 'previous' => 2, 'level' => 3), + '4' => array('parent' => 2, 'previous' => 3, 'level' => 3) + ); + + $this->assertEquals($expected_return, $this->css->_find_only($id)); + } + + function providerTestFindLevel() { + return array( + array(1, array(1)), + array(2, array(2)), + array(3, array(3,4)) + ); + } + + /** + * @dataProvider providerTestFindLevel + */ + function testFindLevel($id, $expected_return) { + $this->css->_structure = array( + '1' => array('next' => 2, 'level' => 1), + '2' => array('parent' => 1, 'previous' => 1, 'next' => 3, 'level' => 2), + '3' => array('parent' => 2, 'next' => 4, 'previous' => 2, 'level' => 3), + '4' => array('parent' => 2, 'previous' => 3, 'level' => 3) + ); + + $this->assertEquals($expected_return, $this->css->_find_level($id)); + } + + function providerTestBuildFromRestrictions() { + return array( + array( + null, + array(1,2,3,4,5,6,7) + ), + array( + array(), + array(1,2,3,4,5,6,7) + ), + array( + array('child_of' => 1), + array(1,2,3) + ), + array( + array('only' => 1), + array(1) + ), + array( + array('child_of' => 1, 'only' => 7), + array(1,2,3,7) + ), + array( + array('level' => 1), + array(1,4,7) + ) + ); + } + + /** + * @dataProvider providerTestBuildFromRestrictions + */ + function testBuildFromRestrictions($restrictions, $expected_categories) { + $this->css->set_flattened_storyline('0/1,0/1/2,0/1/3,0/4,0/4/5,0/4/6,0/7'); + + $this->assertEquals($expected_categories, $this->css->build_from_restrictions($restrictions)); + } } ?> \ No newline at end of file From 261fce60d525c415f0a7d9d2bc27814356e50de9 Mon Sep 17 00:00:00 2001 From: John Bintz <john@dawn.local> Date: Sat, 7 Nov 2009 22:00:14 -0500 Subject: [PATCH 23/32] a bunch of work on restrictions --- classes/ComicPressDBInterface.inc | 14 ++--- classes/ComicPressStoryline.inc | 96 +++++++++++++++++++++++++++---- functions.php | 66 ++++++++++++--------- index.php | 6 +- single.php | 44 +++++--------- test/ComicPressStorylineTest.php | 65 ++++++++++++++++++++- 6 files changed, 211 insertions(+), 80 deletions(-) diff --git a/classes/ComicPressDBInterface.inc b/classes/ComicPressDBInterface.inc index 4f5a323..41af87a 100644 --- a/classes/ComicPressDBInterface.inc +++ b/classes/ComicPressDBInterface.inc @@ -1,7 +1,7 @@ <?php class ComicPressDBInterface { - function ComicPressDBInterface() {} + function ComicPressDBInterface() {} function get_instance() { static $instance; @@ -40,14 +40,14 @@ class ComicPressDBInterface { /** * Get the first comic in a category. */ - function get_first_comic($include_categories = null) { + function get_first_post($include_categories = null) { return $this->get_terminal_post(true, $include_categories); } /** * Get the last comic in a category. */ - function get_last_comic($include_categories = null) { + function get_last_post($include_categories = null) { return $this->get_terminal_post(false, $include_categories); } @@ -55,13 +55,13 @@ class ComicPressDBInterface { * Get the comic post adjacent to the current comic. * Wrapper around get_adjacent_post(). Don't unit test this method. */ - function get_adjacent_comic($category, $next = false, $override_post = null) { + function get_adjacent_post($categories = array(), $next = false, $override_post = null) { global $post; $this->_prepare_wp_query(); if (!is_null($override_post)) { $temp_post = $post; $post = $override_post; } - $result = get_adjacent_post(false, implode(" and ", $this->_get_categories_to_exclude($category)), !$next); + $result = get_adjacent_post(false, implode(" and ", array_diff(get_all_category_ids(), $categories)), !$next); $this->_reset_wp_query(); if (!is_null($override_post)) { $post = $temp_post; } @@ -88,12 +88,12 @@ class ComicPressDBInterface { /** * Get the previous comic from the current one. */ - function get_previous_comic($category = null, $override_post = null) { return $this->get_adjacent_comic($category, false, $override_post); } + function get_previous_post($categories = array(), $override_post = null) { return $this->get_adjacent_post($categories, false, $override_post); } /** * Get the next comic from the current one. */ - function get_next_comic($category = null, $override_post = null) { return $this->get_adjacent_comic($category, true, $override_post); } + function get_next_post($categories = array(), $override_post = null) { return $this->get_adjacent_post($categories, true, $override_post); } } ?> \ No newline at end of file diff --git a/classes/ComicPressStoryline.inc b/classes/ComicPressStoryline.inc index bac9b8a..d59255d 100644 --- a/classes/ComicPressStoryline.inc +++ b/classes/ComicPressStoryline.inc @@ -412,14 +412,18 @@ class ComicPressStoryline { return false; } - function &_include($method, $parameter) { - $this->_category_search = array_unique(array_merge($this->_category_search, $this->{$method}($parameter))); + function &_include() { + $args = func_get_args(); + $method = array_shift($args); + $this->_category_search = array_unique(array_merge($this->_category_search, call_user_func_array(array($this, $method), $args))); sort($this->_category_search); return $this; } - function &_exclude($method, $parameter) { - $this->_category_search = array_diff($this->_category_search, $this->{$method}($parameter)); + function &_exclude() { + $args = func_get_args(); + $method = array_shift($args); + $this->_category_search = array_diff($this->_category_search, call_user_func_array(array($this, $method), $args)); sort($this->_category_search); return $this; } @@ -479,6 +483,56 @@ class ComicPressStoryline { return $this->_exclude('_find_level', $id); } + function _find_post_category($post = null) { + $found = array(); + + $id = null; + if (is_object($post)) { + if (isset($post->ID)) { $id = $post->ID; } + } else { + if (is_numeric($post)) { $id = $post; } + } + + if (!is_null($id)) { + if (count($categories = wp_get_post_categories($id)) == 1) { + $found = $categories; + } + } + return $found; + } + + function &include_post_category($post = null) { + return $this->_include('_find_post_category', $post); + } + + function &exclude_post_category($post = null) { + return $this->_exclude('_find_post_category', $post); + } + + function _find_adjacent($category = null, $next = false) { + $found = array(); + + if (!is_null($category)) { + if (isset($this->_structure[$category])) { + $field = $next ? 'next' : 'previous'; + if (isset($this->_structure[$category][$field])) { + $found = array($this->_structure[$category][$field]); + } + } + } + + return $found; + } + + function &include_adjacent($category = null, $next = false) { + return $this->_include('_find_adjacent', $category, $next); + } + + function &exclude_adjacent($category = null, $next = false) { + return $this->_exclude('_find_adjacent', $category, $next); + } + + function end_search() { $result = $this->_category_search; $this->_category_search = array(); @@ -486,6 +540,8 @@ class ComicPressStoryline { } function build_from_restrictions($restrictions = null) { + global $post; + $this->read_from_options()->exclude_all(); $include_all = true; @@ -497,17 +553,33 @@ class ComicPressStoryline { if (!$include_all) { foreach ($restrictions as $type => $list) { + if (substr($type, 0, 1) == "!") { + $method_root = 'exclude'; + $method_type = substr($type, 1); + } else { + $method_root = 'include'; + $method_type = $type; + } + foreach ((array)$list as $restriction) { - switch ($type) { - case 'child_of': - $this->include_children($restriction); + $method = ''; + $args = array($restriction); + switch ($method_type) { + case 'child_of': $method = 'children'; break; + case 'from_post': $method = 'post_category'; break; + case 'previous': + $method = 'adjacent'; + $args[] = false; break; - case 'only': - $this->include_only($restriction); - break; - case 'level': - $this->include_level($restriction); + case 'next': + $method = 'adjacent'; + $args[] = true; break; + default: + $method = $method_type; break; + } + if (!empty($method)) { + call_user_func_array(array($this, "${method_root}_${method}"), $args); } } } diff --git a/functions.php b/functions.php index 801ee01..240ce5e 100644 --- a/functions.php +++ b/functions.php @@ -54,17 +54,27 @@ function finish_comicpress() { /** * Protect global $post and $wp_query. */ -function protect_query() { +function Protect() { global $post, $wp_query, $__post, $__wp_query; $__post = $post; $__wp_query = $wp_query; } +/** + * Temporarily restore the global $post variable and set it up for use. + */ +function Restore() { + global $post, $__post; + + $post = $__post; + setup_postdata($post); +} + /** * Restore global $post and $wp_query. */ -function unprotect_query() { +function Unprotect() { global $post, $wp_query, $__post, $__wp_query; $post = $__post; @@ -73,49 +83,51 @@ function unprotect_query() { $__post = $__wp_query = null; } -function retrieve_storyline_post($which, $restrictions = null, $override_post = null) { +function R($which, $restrictions = null, $override_post = null) { global $post; + $post_to_use = !is_null($override_post) ? $override_post : $post; $storyline = new ComicPressStoryline(); - $storyline->read_from_options()->exclude_all(); - if (!is_null($restrictions)) { - if (is_array($restrictions)) { - foreach ($restrictions as $type => $list) { - switch ($type) { - case 'child_of': - foreach ($list as $restriction) { $storyline->include_children($restriction); } - break; - } - } + if (is_string($restrictions)) { + switch ($restrictions) { + case 'from_post': + $restrictions = array('from_post' => $post_to_use); + break; } - } else { - $storyline->include_all(); } - $categories = $storyline->end_search(); - - var_dump($categories); + $categories = $storyline->build_from_restrictions($restrictions); $dbi = ComicPressDBInterface::get_instance(); $new_post = false; switch ($which) { - case 'first': - case 'last': - case 'next': - case 'previous': - $method = "get_${which}_post"; - if (method_exists($dbi, $method)) { - $new_post = $dbi->{$method}($categories); - } - break; + case 'first': $new_post = $dbi->get_first_post($categories); break; + case 'last': $new_post = $dbi->get_last_post($categories); break; + case 'next': $new_post = $dbi->get_next_post($categories, $post_to_use); break; + case 'previous': $new_post = $dbi->get_previous_post($categories, $post_to_use); break; } return $new_post; } +function RT($which, $restrictions = null, $override_post = null) { + global $post, $__post; + if (!empty($override_post)) { + $post_to_use = $override_post; + } else { + $post_to_use = (!empty($__post)) ? $__post : $post; + } + + if (($new_post = R($which, $restrictions, $post_to_use)) !== false) { + $post = $new_post; + setup_postdata($post); + } + return $post; +} + /** * Display the list of Storyline categories. */ diff --git a/index.php b/index.php index f93915d..2a899a6 100644 --- a/index.php +++ b/index.php @@ -1,7 +1,9 @@ <?php - $current_post = retrieve_storyline_post('last', array('child_of' => array('amoc', 'dawns-dictionary-drama'))); + $previous_post = R('previous'); + $next_post = R('next'); - var_dump($current_post); + var_dump($previous_post); + var_dump($next_post); finish_comicpress(); ?> \ No newline at end of file diff --git a/single.php b/single.php index df5040d..b320837 100644 --- a/single.php +++ b/single.php @@ -1,37 +1,19 @@ - <?php - global $comicpress, $nav_comics; +<?php + the_title(); echo '<br />'; - comicpress_init(); + Protect(); - ob_start(); + RT('first', 'from_post'); the_title(); echo '<br />'; + RT('previous', 'from_post'); the_title(); echo '<br />'; + RT('previous'); the_title(); echo '<br />'; + Restore(); the_title(); echo '<br />'; + RT('next'); the_title(); echo '<br />'; + RT('next', 'from_post'); the_title(); echo '<br />'; + RT('last', 'from_post'); the_title(); echo '<br />'; - if (have_posts()) { - the_post(); - if (in_comic_category()) { include_partial('single-display-comic'); } - } - rewind_posts(); - - $comic = ob_get_clean(); + Unprotect(); - ob_start(); + the_title(); echo '<br />'; - $nav_comics = $comicpress->get_nav_comics(); - - if (have_posts()) { - while (have_posts()) { the_post(); - if (in_comic_category()) { - if ($comicpress->comicpress_options['comic_space'] == "comic_only") { - include_partial('single-comic-post'); - } - } else { - include_partial('single-blog-post'); - } - } - } else { - include_partial('single-no-matches'); - } - - $content = ob_get_clean(); - - include(get_template_directory() . '/layouts/' . $comicpress->comicpress_options['layout']); + finish_comicpress(); ?> \ No newline at end of file diff --git a/test/ComicPressStorylineTest.php b/test/ComicPressStorylineTest.php index 43a0d0d..0310634 100644 --- a/test/ComicPressStorylineTest.php +++ b/test/ComicPressStorylineTest.php @@ -468,6 +468,52 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { $this->assertEquals($expected_return, $this->css->_find_level($id)); } + function providerTestFindPostCategory() { + return array( + array(array(1), array(1)), + array(array(1,2), array()) + ); + } + + /** + * @dataProvider providerTestFindPostCategory + */ + function testFindPostCategory($post_categories, $expected_return) { + $this->css->_structure = array( + '1' => array('next' => 2, 'level' => 1), + '2' => array('parent' => 1, 'previous' => 1, 'next' => 3, 'level' => 2), + '3' => array('parent' => 2, 'next' => 4, 'previous' => 2, 'level' => 3), + '4' => array('parent' => 2, 'previous' => 3, 'level' => 3) + ); + + wp_set_post_categories(1, $post_categories); + + $this->assertEquals($expected_return, $this->css->_find_post_category(1)); + } + + function providerTestFindAdjacentCategory() { + return array( + array(3, false, array(2)), + array(3, true, array(4)), + array(1, false, array()), + array(4, true, array()), + ); + } + + /** + * @dataProvider providerTestFindAdjacentCategory + */ + function testFindAdjacentCategory($category, $which, $expected_return) { + $this->css->_structure = array( + '1' => array('next' => 2, 'level' => 1), + '2' => array('parent' => 1, 'previous' => 1, 'next' => 3, 'level' => 2), + '3' => array('parent' => 2, 'next' => 4, 'previous' => 2, 'level' => 3), + '4' => array('parent' => 2, 'previous' => 3, 'level' => 3) + ); + + $this->assertEquals($expected_return, $this->css->_find_adjacent($category, $which)); + } + function providerTestBuildFromRestrictions() { return array( array( @@ -493,7 +539,19 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { array( array('level' => 1), array(1,4,7) - ) + ), + array( + array('from_post' => 1), + array(3) + ), + array( + array('previous' => 3), + array(2) + ), + array( + array('next' => 3), + array(4) + ) ); } @@ -501,8 +559,13 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { * @dataProvider providerTestBuildFromRestrictions */ function testBuildFromRestrictions($restrictions, $expected_categories) { + global $post; + $this->css->set_flattened_storyline('0/1,0/1/2,0/1/3,0/4,0/4/5,0/4/6,0/7'); + wp_set_post_categories(1, array(3)); + $post = (object)array('ID' => 1); + $this->assertEquals($expected_categories, $this->css->build_from_restrictions($restrictions)); } } From 93f1e856b706852bf5c2bee83409d358391390e6 Mon Sep 17 00:00:00 2001 From: John Bintz <john@dawn.local> Date: Sat, 7 Nov 2009 22:09:06 -0500 Subject: [PATCH 24/32] remove a bunch of tests and methods no longer needed --- classes/ComicPressStoryline.inc | 52 ++------------------------------ single.php | 2 +- test/ComicPressStorylineTest.php | 42 +------------------------- 3 files changed, 4 insertions(+), 92 deletions(-) diff --git a/classes/ComicPressStoryline.inc b/classes/ComicPressStoryline.inc index d59255d..2762fc4 100644 --- a/classes/ComicPressStoryline.inc +++ b/classes/ComicPressStoryline.inc @@ -428,14 +428,6 @@ class ComicPressStoryline { return $this; } - function &include_children($parent = null) { - return $this->_include('_find_children', $parent); - } - - function &exclude_children($parent = null) { - return $this->_exclude('_find_children', $parent); - } - function _find_level_or_above($level = null) { $found = array(); foreach ($this->_structure as $category_id => $info) { @@ -444,14 +436,6 @@ class ComicPressStoryline { return $found; } - function &include_level_or_above($level = null) { - return $this->_include('_find_level_or_above', $level); - } - - function &exclude_level_or_above($level = null) { - return $this->_exclude('_find_level_or_above', $level); - } - function _find_only($id = null) { if (isset($this->_structure[$id])) { return array($id); @@ -459,14 +443,6 @@ class ComicPressStoryline { return array(); } - function &include_only($id = null) { - return $this->_include('_find_only', $id); - } - - function &exclude_only($id = null) { - return $this->_exclude('_find_only', $id); - } - function _find_level($level = null) { $found = array(); foreach ($this->_structure as $category_id => $info) { @@ -475,14 +451,6 @@ class ComicPressStoryline { return $found; } - function &include_level($id = null) { - return $this->_include('_find_level', $id); - } - - function &exclude_level($id = null) { - return $this->_exclude('_find_level', $id); - } - function _find_post_category($post = null) { $found = array(); @@ -501,14 +469,6 @@ class ComicPressStoryline { return $found; } - function &include_post_category($post = null) { - return $this->_include('_find_post_category', $post); - } - - function &exclude_post_category($post = null) { - return $this->_exclude('_find_post_category', $post); - } - function _find_adjacent($category = null, $next = false) { $found = array(); @@ -524,15 +484,6 @@ class ComicPressStoryline { return $found; } - function &include_adjacent($category = null, $next = false) { - return $this->_include('_find_adjacent', $category, $next); - } - - function &exclude_adjacent($category = null, $next = false) { - return $this->_exclude('_find_adjacent', $category, $next); - } - - function end_search() { $result = $this->_category_search; $this->_category_search = array(); @@ -579,7 +530,8 @@ class ComicPressStoryline { $method = $method_type; break; } if (!empty($method)) { - call_user_func_array(array($this, "${method_root}_${method}"), $args); + array_unshift($args, "_find_${method}"); + call_user_func_array(array($this, "_${method_root}"), $args); } } } diff --git a/single.php b/single.php index b320837..7e108cc 100644 --- a/single.php +++ b/single.php @@ -7,7 +7,7 @@ RT('previous', 'from_post'); the_title(); echo '<br />'; RT('previous'); the_title(); echo '<br />'; Restore(); the_title(); echo '<br />'; - RT('next'); the_title(); echo '<br />'; + RT('next', array('child_of' => 'amoc')); the_title(); echo '<br />'; RT('next', 'from_post'); the_title(); echo '<br />'; RT('last', 'from_post'); the_title(); echo '<br />'; diff --git a/test/ComicPressStorylineTest.php b/test/ComicPressStorylineTest.php index 0310634..2db5757 100644 --- a/test/ComicPressStorylineTest.php +++ b/test/ComicPressStorylineTest.php @@ -368,26 +368,6 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { $this->assertEquals(array(2,3,4), $this->css->_find_children($search)); } - function testIncludeChildren() { - $css = $this->getMock('ComicPressStoryline', array('_find_children')); - $css->expects($this->once())->method('_find_children')->will($this->returnValue(array(2,3,4))); - $css->_category_search = array(4,5); - - $css->include_children(2); - - $this->assertEquals(array(2,3,4,5), $css->_category_search); - } - - function testExcludeChildren() { - $css = $this->getMock('ComicPressStoryline', array('_find_children')); - $css->expects($this->once())->method('_find_children')->will($this->returnValue(array(2,3,4))); - $css->_category_search = array(4, 5, 6); - - $css->exclude_children(2); - - $this->assertEquals(array(5, 6), $css->_category_search); - } - function testFindLevelOrAbove() { $this->css->_structure = array( '1' => array('next' => 2, 'level' => 1), @@ -399,26 +379,6 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { $this->assertEquals(array(1, 2), $this->css->_find_level_or_above(2)); } - function testIncludeLevelOrAbove() { - $css = $this->getMock('ComicPressStoryline', array('_find_level_or_above')); - $css->expects($this->once())->method('_find_level_or_above')->will($this->returnValue(array(2,3,4))); - $css->_category_search = array(4,5); - - $css->include_level_or_above(2); - - $this->assertEquals(array(2,3,4,5), $css->_category_search); - } - - function testExcludeLevelOrAbove() { - $css = $this->getMock('ComicPressStoryline', array('_find_level_or_above')); - $css->expects($this->once())->method('_find_level_or_above')->will($this->returnValue(array(2,3,4))); - $css->_category_search = array(4, 5, 6); - - $css->exclude_level_or_above(2); - - $this->assertEquals(array(5, 6), $css->_category_search); - } - function testEndSearch() { $this->css->_category_search = array(1,2,3); $this->assertEquals(array(1,2,3), $this->css->end_search()); @@ -514,7 +474,7 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { $this->assertEquals($expected_return, $this->css->_find_adjacent($category, $which)); } - function providerTestBuildFromRestrictions() { + function providerTestBuildFromRestrictions() { return array( array( null, From f0bb9a0061d900d82bcd7e34267ffbad81d823ff Mon Sep 17 00:00:00 2001 From: John Bintz <john@dawn.local> Date: Sat, 7 Nov 2009 22:21:12 -0500 Subject: [PATCH 25/32] get post root --- classes/ComicPressStoryline.inc | 34 ++++++++++++++++++++++++++------ test/ComicPressStorylineTest.php | 33 +++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 6 deletions(-) diff --git a/classes/ComicPressStoryline.inc b/classes/ComicPressStoryline.inc index 2762fc4..3ea2d4e 100644 --- a/classes/ComicPressStoryline.inc +++ b/classes/ComicPressStoryline.inc @@ -451,15 +451,20 @@ class ComicPressStoryline { return $found; } + function _ensure_post_id($thing) { + $id = null; + if (is_object($thing)) { + if (isset($thing->ID)) { $id = $thing->ID; } + } else { + if (is_numeric($thing)) { $id = $thing; } + } + return $id; + } + function _find_post_category($post = null) { $found = array(); - $id = null; - if (is_object($post)) { - if (isset($post->ID)) { $id = $post->ID; } - } else { - if (is_numeric($post)) { $id = $post; } - } + $id = $this->_ensure_post_id($post); if (!is_null($id)) { if (count($categories = wp_get_post_categories($id)) == 1) { @@ -484,6 +489,23 @@ class ComicPressStoryline { return $found; } + function _find_post_root($post = null) { + $found = array(); + + $id = $this->_ensure_post_id($post); + + if (!is_null($id)) { + if (count($categories = wp_get_post_categories($id)) == 1) { + $comic_post = new ComicPressComicPost(get_post($id)); + $parents = $comic_post->find_parents(); + if (!empty($parents)) { + $parents = array_keys($parents); $found[] = end($parents); + } + } + } + return $found; + } + function end_search() { $result = $this->_category_search; $this->_category_search = array(); diff --git a/test/ComicPressStorylineTest.php b/test/ComicPressStorylineTest.php index 2db5757..739be69 100644 --- a/test/ComicPressStorylineTest.php +++ b/test/ComicPressStorylineTest.php @@ -474,6 +474,39 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { $this->assertEquals($expected_return, $this->css->_find_adjacent($category, $which)); } + function providerTestFindPostRoot() { + return array( + array(array(1), array(1)), + array(array(4), array(1)), + array(array(5), array(5)), + array(array(1, 5), array()), + ); + } + + /** + * @dataProvider providerTestFindPostRoot + */ + function testFindPostRoot($post_categories, $expected_return) { + $this->css->_structure = array( + '1' => array('next' => 2, 'level' => 1, 'parent' => 0), + '2' => array('parent' => 1, 'previous' => 1, 'next' => 3, 'level' => 2), + '3' => array('parent' => 2, 'next' => 4, 'previous' => 2, 'level' => 3), + '4' => array('parent' => 2, 'previous' => 3, 'next' => 5, 'level' => 3), + '5' => array('parent' => 0, 'previous' => 4, 'level' => 1), + ); + + wp_set_post_categories(1, $post_categories); + wp_insert_post(array('ID' => 1)); + + 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)); + add_category(5, (object)array('slug' => 'blog', 'parent' => 0)); + + $this->assertEquals($expected_return, $this->css->_find_post_root(1)); + } + function providerTestBuildFromRestrictions() { return array( array( From cab85e5f6064bd1940ac0eaa239b958c672c66b0 Mon Sep 17 00:00:00 2001 From: John Bintz <john@dawn.local> Date: Sat, 7 Nov 2009 22:42:09 -0500 Subject: [PATCH 26/32] RT() function pretty much working --- classes/ComicPressStoryline.inc | 10 +++++++--- functions.php | 16 ++++++++++++++++ single.php | 9 +++++++-- test/ComicPressStorylineTest.php | 4 ++-- 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/classes/ComicPressStoryline.inc b/classes/ComicPressStoryline.inc index 3ea2d4e..17f6cf2 100644 --- a/classes/ComicPressStoryline.inc +++ b/classes/ComicPressStoryline.inc @@ -499,8 +499,9 @@ class ComicPressStoryline { $comic_post = new ComicPressComicPost(get_post($id)); $parents = $comic_post->find_parents(); if (!empty($parents)) { - $parents = array_keys($parents); $found[] = end($parents); + $parents = array_keys($parents); $found = $this->_find_children(end($parents)); } + } } return $found; @@ -534,11 +535,14 @@ class ComicPressStoryline { $method_type = $type; } - foreach ((array)$list as $restriction) { + if (!is_array($list)) { $list = array($list); } + + foreach ($list as $restriction) { $method = ''; $args = array($restriction); switch ($method_type) { case 'child_of': $method = 'children'; break; + case 'root_of': $method = 'post_root'; break; case 'from_post': $method = 'post_category'; break; case 'previous': $method = 'adjacent'; @@ -555,7 +559,7 @@ class ComicPressStoryline { array_unshift($args, "_find_${method}"); call_user_func_array(array($this, "_${method_root}"), $args); } - } + } } } else { $this->include_all(); diff --git a/functions.php b/functions.php index 240ce5e..b6d07c4 100644 --- a/functions.php +++ b/functions.php @@ -97,6 +97,22 @@ function R($which, $restrictions = null, $override_post = null) { } } + if (is_array($restrictions)) { + $new_restrictions = array(); + foreach ($restrictions as $type => $list) { + if (is_string($list)) { + $value = $list; + switch ($list) { + case '__post': $value = $post_to_use; break; + } + $new_restrictions[$type] = $value; + } else { + $new_restrictions[$type] = $list; + } + } + $restrictions = $new_restrictions; + } + $categories = $storyline->build_from_restrictions($restrictions); $dbi = ComicPressDBInterface::get_instance(); diff --git a/single.php b/single.php index 7e108cc..bab1e6c 100644 --- a/single.php +++ b/single.php @@ -1,13 +1,18 @@ <?php + $storyline = new ComicPressStoryline(); + $storyline->read_from_options(); + the_title(); echo '<br />'; Protect(); RT('first', 'from_post'); the_title(); echo '<br />'; RT('previous', 'from_post'); the_title(); echo '<br />'; - RT('previous'); the_title(); echo '<br />'; + RT('previous', array('root_of' => '__post')); the_title(); echo '<br />'; + RT('previous'); the_title(); echo '<br />'; Restore(); the_title(); echo '<br />'; - RT('next', array('child_of' => 'amoc')); the_title(); echo '<br />'; + RT('next'); the_title(); echo '<br />'; + RT('next', array('root_of' => '__post')); the_title(); echo '<br />'; RT('next', 'from_post'); the_title(); echo '<br />'; RT('last', 'from_post'); the_title(); echo '<br />'; diff --git a/test/ComicPressStorylineTest.php b/test/ComicPressStorylineTest.php index 739be69..805ffc6 100644 --- a/test/ComicPressStorylineTest.php +++ b/test/ComicPressStorylineTest.php @@ -476,8 +476,8 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { function providerTestFindPostRoot() { return array( - array(array(1), array(1)), - array(array(4), array(1)), + array(array(1), array(1,2,3,4)), + array(array(4), array(1,2,3,4)), array(array(5), array(5)), array(array(1, 5), array()), ); From ead478fcec3bdbcc7aff568ad952d026f6f1786c Mon Sep 17 00:00:00 2001 From: John Bintz <john@dawn.local> Date: Sat, 7 Nov 2009 22:47:28 -0500 Subject: [PATCH 27/32] gut more stuff --- classes/ComicPressAdmin.inc | 99 +++++-------------------- classes/partials/options-admin.inc | 14 ++-- test/ComicPressAdminTest.php | 111 +++++------------------------ 3 files changed, 39 insertions(+), 185 deletions(-) diff --git a/classes/ComicPressAdmin.inc b/classes/ComicPressAdmin.inc index 7b5017a..8beda33 100644 --- a/classes/ComicPressAdmin.inc +++ b/classes/ComicPressAdmin.inc @@ -7,7 +7,7 @@ class ComicPressAdmin { */ function init() { $this->comicpress = &ComicPress::get_instance(); - + add_action('admin_menu', array(&$this, 'admin_menu')); add_filter('attachment_fields_to_edit', array(&$this, 'setup_comic_metadata_buttons'), 10, 2); @@ -39,10 +39,10 @@ class ComicPressAdmin { . ']" value="' . $index . '" /></label>'; - + return $content; } - + function comicpress_display_attached_images($content, $post_id) { $content = '<form method="post">' . '<input type="hidden" name="cp[_nonce]" value="' @@ -56,7 +56,7 @@ class ComicPressAdmin { . __('Change image ordering', 'comicpress') . '" />' . '</form>'; - + return $content; } @@ -118,7 +118,7 @@ class ComicPressAdmin { . $label . '</label>'; } - + $form_fields['comic_image_type'] = array( 'label' => __("Comic Image Type", 'comicpress'), 'input' => 'html', @@ -134,7 +134,7 @@ class ComicPressAdmin { . '</em>' ); } - + return $form_fields; } @@ -143,14 +143,13 @@ class ComicPressAdmin { */ function render_admin() { $nonce = wp_create_nonce('comicpress'); - $root_categories = $this->get_root_categories(); $storyline = new ComicPressStoryline(); $storyline->normalize(); $storyline->read_from_options(); - include(dirname(__FILE__) . '/partials/options-admin.inc'); + include(dirname(__FILE__) . '/partials/options-admin.inc'); } - + function _render_admin_storyline_tree($node, $parent_id = "0") { foreach ($node as $category_id => $children) { $category = get_category($category_id); @@ -164,7 +163,7 @@ class ComicPressAdmin { echo '</div>'; } } - + /** * Render the comic image ordering interface. */ @@ -173,7 +172,7 @@ class ComicPressAdmin { $uploading_iframe_ID = (int) (0 == $post_ID ? $temp_ID : $post_ID); if (is_numeric($override_post)) { $uploading_iframe_ID = $override_post; } - + $comic_post = new ComicPressComicPost(get_post($uploading_iframe_ID)); $ordering = $comic_post->normalize_comic_image_ordering(); @@ -202,52 +201,6 @@ class ComicPressAdmin { } } - /** - * Get all categories with a parent ID of 0. - * @return array All root categories. - */ - function get_root_categories() { - $root_categories = array(); - foreach (get_all_category_ids() as $id) { - $category = get_category($id); - if (!empty($category)) { - if ($category->parent == 0) { - $root_categories[] = $category; - } - } - } - return $root_categories; - } - - /** - * Create <option> elements for each of the provided categories. - * @param array $categories The categories to display as either IDs or category objects. - * @param int $selected_id The category to mark as selected. - * @return string The category options as HTML. - */ - function create_category_options($categories, $selected_id) { - $output = array(); - if (is_array($categories)) { - $final_categories = array(); - foreach ($categories as $category) { - if (is_numeric($category)) { - $result = get_category($category); - if (!(is_a($result, "WP_Error") || empty($result))) { - $final_categories[] = $result; - } - } - if (is_object($category)) { - $final_categories[] = $category; - } - } - - foreach ($final_categories as $category) { - $output[] = '<option value="' . $category->term_id . '"' . (($category->term_id == $selected_id) ? ' selected="selected"' : '') . '>' . $category->name . '</option>'; - } - } - return implode("\n", $output); - } - /** * Create a dimension selector. * @param string $root The field name root. @@ -256,7 +209,7 @@ class ComicPressAdmin { */ function create_dimension_selector($root, $dimension) { $output = array(); - + $parts = explode("x", $dimension); foreach (array( 'width' => __('Width', 'comicpress'), @@ -264,7 +217,7 @@ class ComicPressAdmin { ) as $id => $name) { $dim = array_shift($parts); if (!empty($dim) && !is_numeric($dim)) { $dim = ""; } - $output[] = '<label>' . $name . ': <input type="text" name="' . $root . '[' . $id . ']" value="' . $dim . '" size="4" />px</label><br />'; + $output[] = '<label>' . $name . ': <input type="text" name="' . $root . '[' . $id . ']" value="' . $dim . '" size="4" />px</label><br />'; } return implode("\n", $output); } @@ -283,7 +236,7 @@ class ComicPressAdmin { function handle_update_attachments() { foreach ($_POST['attachments'] as $post_id => $settings) { if (isset($settings['comic_image_type'])) { - update_post_meta($post_id, 'comic_image_type', $settings['comic_image_type']); + update_post_meta($post_id, 'comic_image_type', $settings['comic_image_type']); } if (isset($settings['auto_attach']) && isset($settings['post_parent'])) { $media_post = get_post($post_id); @@ -292,7 +245,7 @@ class ComicPressAdmin { } } } - + /** * Update ComicPress options. */ @@ -303,14 +256,6 @@ class ComicPressAdmin { foreach ($this->comicpress->comicpress_options as $option => $value) { if (isset($info[$option])) { switch ($option) { - case 'comic_category_id': - if (is_numeric($info[$option])) { - $result = get_category($info[$option]); - if (!(is_a($result, 'WP_Error') || empty($result))) { - $this->comicpress->comicpress_options[$option] = $info[$option]; - } - } - break; case 'comic_dimensions': case 'rss_dimensions': case 'archive_dimensions': @@ -330,20 +275,12 @@ class ComicPressAdmin { } } } - + if ($is_valid) { $this->comicpress->comicpress_options[$option] = implode("x", $dim_parts); } } break; - case 'blogpost_count': - $this->comicpress->comicpress_options[$option] = (int)$info[$option]; - break; - case 'comic_space': - case 'category_usage': - case 'layout'; - $this->comicpress->comicpress_options[$option] = $info[$option]; - break; case 'helpers': case 'addons': foreach ($info[$option] as $type => $set) { @@ -366,9 +303,9 @@ class ComicPressAdmin { require_once(ABSPATH."/wp-includes/js/tinymce/plugins/spellchecker/classes/utils/JSON.php"); $j = new Moxiecode_JSON(); return $j->decode($string); - } + } } - + function handle_update_comic_ordering() { if (is_numeric($_POST['post_ID'])) { if ($post = get_post($_POST['post_ID'])) { @@ -429,7 +366,7 @@ class ComicPressAdmin { } } } - + /** * Create the dropdown for choosing a layout. */ diff --git a/classes/partials/options-admin.inc b/classes/partials/options-admin.inc index 9912fc6..86accba 100644 --- a/classes/partials/options-admin.inc +++ b/classes/partials/options-admin.inc @@ -9,7 +9,7 @@ <td> <input type="hidden" name="cp[storyline_order]" /> <div id="storyline-sorter" class="cp-children"> - <?php + <?php $this->_render_admin_storyline_tree(reset($storyline->get_simple_storyline())) ?> </div> @@ -30,12 +30,6 @@ </td> </tr> <?php } ?> - <tr> - <th scope="row" valign="top"><?php _e("Number of blog posts on home page", 'comicpress') ?></th> - <td> - <input type="text" name="cp[blogpost_count]" value="<?php echo $this->comicpress->comicpress_options['blogpost_count'] ?>" size="3" /> - </td> - </tr> </table> <h3><?php _e('Admin Options', 'comicpress') ?></h3> <table class="widefat fixed"> @@ -64,11 +58,11 @@ <td> <?php foreach ($this->all_addons as $addon) { - if (!empty($addon->name)) { + if (!empty($addon->name)) { $enabled = ($addon->is_addon_manager !== true); $checked = $enabled && $this->comicpress->comicpress_options['addons'][$addon->name]; ?> - + <label> <input type="checkbox" name="cp[addons][<?php echo $addon->name ?>]" @@ -82,7 +76,7 @@ ?> </td> </tr> - <?php } ?> + <?php } ?> </table> <input class="button" type="submit" value="<?php _e('Submit Changes', 'comicpress') ?>" /> </form> diff --git a/test/ComicPressAdminTest.php b/test/ComicPressAdminTest.php index ff309eb..d3945a5 100644 --- a/test/ComicPressAdminTest.php +++ b/test/ComicPressAdminTest.php @@ -10,98 +10,22 @@ class ComicPressAdminTest extends PHPUnit_Framework_TestCase { $_POST = array(); $this->admin = new ComicPressAdmin(); } - - function providerTestGetRootComicCategories() { - return array( - array(array(), array()), - array( - array( - array('id' => 1, 'parent' => 0), - array('id' => 2, 'parent' => 1) - ), - array(1) - ) - ); - } - - /** - * @dataProvider providerTestGetRootComicCategories - */ - function testGetRootCategories($categories, $expected_result) { - foreach ($categories as $category) { - add_category($category['id'], (object)$category); - } - - $result_ids = array(); - foreach ($this->admin->get_root_categories() as $category) { - $result_ids[] = $category->term_id; - } - - $this->assertEquals($expected_result, $result_ids); - } - - function testCreateCategoryOptions() { - add_category(1, (object)array('name' => 'test-one')); - add_category(2, (object)array('name' => 'test-two')); - - foreach(array( - array(1,2), - array(get_category(1), get_category(2)) - ) as $category_test) { - $source = $this->admin->create_category_options($category_test, 1); - - $this->assertTrue(($xml = _to_xml($source, true)) !== false); - - foreach (array( - '//option[@value="1" and @selected="selected"]' => "test-one", - '//option[@value="2"]' => "test-two", - ) as $xpath => $value) { - $this->assertTrue(_xpath_test($xml, $xpath, $value), $xpath); - } - } - } function testCreateDimensionSelector() { $source = $this->admin->create_dimension_selector("test", "760x340"); - + $this->assertTrue(($xml = _to_xml($source, true)) !== false); - + foreach (array( '//input[@name="test[width]" and @value="760"]' => true, '//input[@name="test[height]" and @value="340"]' => true, ) as $xpath => $value) { - $this->assertTrue(_xpath_test($xml, $xpath, $value), $xpath); - } + $this->assertTrue(_xpath_test($xml, $xpath, $value), $xpath); + } } - + function providerTestHandleUpdate() { return array( - array( - array('comic_category_id' => 1), - array('comic_category_id' => 2), - array('comic_category_id' => 1) - ), - array( - array('comic_category_id' => 1), - array('cp' => array( - 'comic_category_id' => 2), - ), - array('comic_category_id' => 2) - ), - array( - array('comic_category_id' => 1), - array('cp' => array( - 'comic_category_id' => "cat"), - ), - array('comic_category_id' => 1) - ), - array( - array('comic_category_id' => 1), - array('cp' => array( - 'comic_category_id' => 3 - )), - array('comic_category_id' => 1) - ), array( array('comic_dimensions' => '150x150'), array('cp' => array( @@ -138,26 +62,25 @@ class ComicPressAdminTest extends PHPUnit_Framework_TestCase { function testHandleUpdate($original, $change, $new) { $this->admin->comicpress = $this->getMock('ComicPress', array('save', 'init')); $this->admin->comicpress->comicpress_options = array( - 'comic_category_id' => 1, 'comic_dimensions' => '760x', 'rss_dimensions' => '350x', 'archive_dimensions' => '125x' ); $this->admin->comicpress->comicpress_options = array_merge($this->admin->comicpress->comicpress_options, $original); - + add_category(2, (object)array('name' => 'test')); - + $_POST = $change; if (isset($_POST['cp'])) { $this->admin->handle_update_comicpress_options($_POST['cp']); } - + foreach ($new as $key => $value) { $this->assertEquals($value, $this->admin->comicpress->comicpress_options[$key]); } } - + function providerTestUpdateAttachments() { return array( array( @@ -187,7 +110,7 @@ class ComicPressAdminTest extends PHPUnit_Framework_TestCase { 'post_parent' => 0 ), ), - ), + ), array( array( 'post' => array( @@ -204,9 +127,9 @@ class ComicPressAdminTest extends PHPUnit_Framework_TestCase { ), ), ) - ); + ); } - + /** * @dataProvider providerTestUpdateAttachments */ @@ -225,20 +148,20 @@ class ComicPressAdminTest extends PHPUnit_Framework_TestCase { break; } } - + $_POST = array( 'attachments' => array('1' => $changes) ); - + $this->admin->handle_update_attachments(); - + foreach ($expected_settings as $settings_type => $settings) { switch ($settings_type) { case "post_meta": foreach ($settings as $key => $value) { $this->assertEquals($value, get_post_meta(1, $key, true)); } - break; + break; case "post": $post = get_post(1); foreach ($settings as $key => $value) { @@ -247,7 +170,7 @@ class ComicPressAdminTest extends PHPUnit_Framework_TestCase { } } } - + function providerTestHandleUpdateOverridePartial() { return array( array( From 4cfacec35fec3d695cf794cba56be3495adf563b Mon Sep 17 00:00:00 2001 From: John Bintz <john@dawn.local> Date: Sun, 8 Nov 2009 22:11:26 -0500 Subject: [PATCH 28/32] working on new backend code --- classes/ComicPress.inc | 18 ++- classes/ComicPressAdmin.inc | 36 ++--- classes/ComicPressBackend.inc | 17 ++ classes/ComicPressComicPost.inc | 148 +++++++++--------- .../backends/ComicPressBackendAttachment.inc | 61 +++++++- .../_comic-image-ordering-sorters.inc | 48 +++--- functions.php | 8 + single.php | 5 + test/ComicPressComicPostTest.php | 141 +++++++---------- .../ComicPressBackendAttachmentTest.php | 39 +++++ 10 files changed, 309 insertions(+), 212 deletions(-) create mode 100644 classes/ComicPressBackend.inc create mode 100644 test/backends/ComicPressBackendAttachmentTest.php diff --git a/classes/ComicPress.inc b/classes/ComicPress.inc index 1e259a6..3aaead3 100644 --- a/classes/ComicPress.inc +++ b/classes/ComicPress.inc @@ -13,9 +13,16 @@ class ComicPress { 'mini_dimensions' => '100x', 'helpers' => array(), 'addons' => array(), - 'storyline_order' => '' + 'storyline_order' => '', + 'subattachment_types' => array( + 'rss' => 'RSS', + 'archive' => 'Archive', + 'mini' => 'Mini Thumb' + ) ); + var $backends = array('ComicPressBackendAttachment'); + function &get_instance() { static $instance; @@ -58,6 +65,7 @@ class ComicPress { } add_filter('intermediate_image_sizes', array(&$this, 'intermediate_image_sizes')); + add_filter('editor_max_image_size', array(&$this, 'editor_max_image_size'), 10, 2); foreach (array('comic', 'rss', 'archive', 'mini') as $size) { list($w, $h) = explode("x", $this->comicpress_options["${size}_dimensions"]); @@ -71,6 +79,14 @@ class ComicPress { return array_merge($sizes, array('comic', 'rss', 'archive', 'mini')); } + function editor_max_image_size($current_max, $size) { + if (isset($this->comicpress_options["${size}_dimensions"])) { + list($width, $height) = explode('x', $this->comicpress_options["${size}_dimensions"]); + $current_max = array(intval($width), intval($height)); + } + return $current_max; + } + function announce_activated_helpers() { echo "<center>[ <strong>Activated ComicPress helpers:</strong> " . implode(", ", array_keys($this->comicpress_options['helpers'])) . " ]</center>"; } diff --git a/classes/ComicPressAdmin.inc b/classes/ComicPressAdmin.inc index 8beda33..f4d42cf 100644 --- a/classes/ComicPressAdmin.inc +++ b/classes/ComicPressAdmin.inc @@ -80,7 +80,7 @@ class ComicPressAdmin { wp_enqueue_script('cp-admin', get_template_directory_uri() . '/js/Storyline.js', array('prototype', 'scriptaculous')); } - if (strpos($pagenow, "media-upload") === 0) { + if (strpos($pagenow, "-upload") !== false) { wp_enqueue_script('cp-media', get_template_directory_uri() . '/js/MediaUpload.js', array('prototype')); } } @@ -176,28 +176,26 @@ class ComicPressAdmin { $comic_post = new ComicPressComicPost(get_post($uploading_iframe_ID)); $ordering = $comic_post->normalize_comic_image_ordering(); - if (is_array($ordering)) { - $nonce = wp_create_nonce('comicpress'); - $zoom_level = 40; - $current_user = wp_get_current_user(); - if (!empty($current_user)) { - $comicpress_meta = get_usermeta($current_user->ID, 'comicpress-settings'); - if (is_array($comicpress_meta)) { - if (isset($comicpress_meta['zoom_level'])) { - $zoom_level = floor($comicpress_meta['zoom_level']); - } + $nonce = wp_create_nonce('comicpress'); + $zoom_level = 40; + $current_user = wp_get_current_user(); + if (!empty($current_user)) { + $comicpress_meta = get_usermeta($current_user->ID, 'comicpress-settings'); + if (is_array($comicpress_meta)) { + if (isset($comicpress_meta['zoom_level'])) { + $zoom_level = floor($comicpress_meta['zoom_level']); } } + } - // from wp-admin/includes/media.php O_o - $media_upload_iframe_src = "media-upload.php?post_id=$uploading_iframe_ID"; - $image_upload_iframe_src = apply_filters('image_upload_iframe_src', "$media_upload_iframe_src&type=image&TB_iframe=true"); + // from wp-admin/includes/media.php O_o + $media_upload_iframe_src = "media-upload.php?post_id=$uploading_iframe_ID"; + $image_upload_iframe_src = apply_filters('image_upload_iframe_src', "$media_upload_iframe_src&type=image&TB_iframe=true"); - if ($is_ajax === true) { - include(dirname(__FILE__) . '/partials/_comic-image-ordering-sorters.inc'); - } else { - include(dirname(__FILE__) . '/partials/_comic-image-ordering.inc'); - } + if ($is_ajax === true) { + include(dirname(__FILE__) . '/partials/_comic-image-ordering-sorters.inc'); + } else { + include(dirname(__FILE__) . '/partials/_comic-image-ordering.inc'); } } diff --git a/classes/ComicPressBackend.inc b/classes/ComicPressBackend.inc new file mode 100644 index 0000000..7ddd7ff --- /dev/null +++ b/classes/ComicPressBackend.inc @@ -0,0 +1,17 @@ +<?php + +class ComicPressBackend { + function _embed_image($size) { + $extras = array(); + if (($dims = $this->dims($size)) !== false) { + $extras = array_merge($extras, $dims); + } + foreach ($extras as $field => $value) { + $extras[] = "${field}=\"${value}\""; + unset($extras[$field]); + } + + $output = sprintf('<img src="%s" alt="%s" title="%s" %s />', $this->url(), $this->alt(), $this->title(), implode(" ", $extras)); + return apply_filters('comicpress_embed_image', $output, $this); + } +} \ No newline at end of file diff --git a/classes/ComicPressComicPost.inc b/classes/ComicPressComicPost.inc index fe73f61..697e1f3 100644 --- a/classes/ComicPressComicPost.inc +++ b/classes/ComicPressComicPost.inc @@ -8,17 +8,15 @@ class ComicPressComicPost { if (!is_null($post)) { $this->post = $post; } } - function get_comic_image_attachments() { - if (is_null($this->attachments)) { - $this->attachments = get_children(array( - 'post_parent' => $this->post->ID, - 'post_type' => 'attachment', - 'post_mime_type' => 'image' - )); - } - return $this->attachments; + function get_attachments() { + $comicpress = ComicPress::get_instance(); + $attachments = array(); + foreach ($comicpress->backends as $backend) { + $attachments = array_merge($attachments, call_user_func(array($backend, 'generate_from_post'), $this->post)); + } + return $attachments; } - + /** * Display all the attached images. */ @@ -33,19 +31,19 @@ class ComicPressComicPost { if (get_post_meta($attachment_id, "comic_image_type", true) == $type) { $attachment = get_post($attachment_id); $title = (!empty($attachment->post_excerpt) ? $attachment->post_excerpt : $attachment->post_title); - + $url = wp_get_attachment_url($attachment->ID, ''); $sizes = image_downsize($attachment->ID, $size_type); if ($sizes) { $url = $sizes[0]; } - - $output[] = apply_filters('comicpress_attached_image', + + $output[] = apply_filters('comicpress_attached_image', sprintf($format, $this->get_comic_img_tag($url, $size_type, array('title' => $title))), $attachment_id, $i++); $found = true; - + if (!is_null($limit)) { if (--$limit == 0) { break; } } @@ -58,7 +56,7 @@ class ComicPressComicPost { return $found; } - + function _display_type($types, $format, $single = false) { $target_type = reset($types); foreach ($types as $type) { @@ -67,7 +65,7 @@ class ComicPressComicPost { } } } - + 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); } @@ -77,7 +75,7 @@ class ComicPressComicPost { */ function get_comic_img_tag($url, $type, $additional_parameters = array()) { $dimensions = array(); - + if (isset($this->comicpress->comicpress_options["${type}_dimensions"])) { $parts = explode("x", $this->comicpress->comicpress_options["${type}_dimensions"]); switch (count($parts)) { @@ -86,7 +84,7 @@ class ComicPressComicPost { } $dimensions = compact('width', 'height'); } - + $output = '<img src="' . $url . '" '; foreach (array('width', 'height') as $field) { if (!empty($dimensions[$field])) { @@ -95,11 +93,11 @@ class ComicPressComicPost { } if (is_array($additional_parameters)) { foreach ($additional_parameters as $parameter => $value) { - $output .= $parameter . '="' . $value . '" '; + $output .= $parameter . '="' . $value . '" '; } } $output .= "/>"; - + return $output; } @@ -107,62 +105,58 @@ class ComicPressComicPost { * 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); - - $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 $ordering_post_id) { - foreach ($this->get_comic_image_attachments() as $attachment) { - if (!isset($found_post_ids[$attachment->ID])) { - if ($attachment->ID == $ordering_post_id) { - $comic_image_ordering[$type][] = $attachment->ID; - $found_post_ids[$ordering_post_id] = true; - } - } - } - } - } + function normalize_ordering() { + $attachments = $this->get_attachments(); + if (is_array($attachments)) { + $new_ordering = array(); + $current_ordering = get_post_meta($this->post->ID, 'image-ordering', true); + if (!is_array($current_ordering)) { $current_ordering = array(); } + + $all_current_ids = array(); + foreach ($current_ordering as $key => $children) { + $all_current_ids[$key] = true; + if (is_array($children)) { + foreach ($children as $type => $kids) { + $all_current_ids = array_merge($all_current_ids, $kids); + } + } } - - $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')); - } - - foreach ($remaining_posts_to_sort as $type => $posts) { - if (!isset($comic_image_ordering[$type])) { $comic_image_ordering[$type] = array(); } - if (is_array($comic_image_ordering[$type])) { - $comic_image_ordering[$type] = array_merge($comic_image_ordering[$type], $posts); - } else { - $comic_image_ordering[$type] = $posts; - } + $all_current_ids = array_keys($all_current_ids); + + $attachment_ids = array(); + foreach ($attachments as $attachment) { $attachment_ids[] = $attachment->id; } + + $new_attachments = array_diff($attachment_ids, $all_current_ids); + $missing_attachments = array_diff($all_current_ids, $attachment_ids); + + foreach ($new_attachments as $attachment_id) { + $current_ordering[$attachment_id] = true; } - update_post_meta($this->post->ID, 'comic_ordering', $comic_image_ordering); - return $comic_image_ordering; + foreach ($missing_attachments as $attachment_id) { + if (isset($current_ordering[$attachment_id])) { + unset($current_ordering[$attachment_id]); + } else { + foreach ($current_ordering as $key => $children) { + foreach ($children as $type => $kids) { + if (isset($kids[$attachment_id])) { unset($kids[$attachment_id]); } + $children[$type] = $kids; + } + if (empty($children[$type])) { + $current_ordering[$key] = true; + } else { + $current_ordering[$key] = $children; + } + } + } + } + + update_post_meta($this->post->ID, 'image-ordering', $current_ordering); + return $current_ordering; } return false; } - + /** * Sort the remaining comic images by file date. * @param object $a @@ -172,7 +166,7 @@ class ComicPressComicPost { function sort_remaining_comic_images($a, $b) { $a_date = isset($a->post_date) ? $a->post_date : 0; $b_date = isset($b->post_date) ? $b->post_date : 0; - return $a_date - $b_date; + return $a_date - $b_date; } /** @@ -182,7 +176,7 @@ class ComicPressComicPost { function change_comic_image_ordering($requested_new_order) { $orderings = get_post_meta($this->post->ID, 'comic_ordering', true); if (!is_array($orderings)) { $orderings = array(); } - + $new_order = array(); $requested_new_order = (array)$requested_new_order; @@ -193,7 +187,7 @@ class ComicPressComicPost { $position = 0; foreach ($requested_new_order[$type] as $id) { if (!isset($sort_by_position[$position])) { - $sort_by_position[$position] = array(); + $sort_by_position[$position] = array(); } $sort_by_position[$position][] = $id; $position++; @@ -213,7 +207,7 @@ class ComicPressComicPost { } } } - + update_post_meta($this->post->ID, 'comic_ordering', $new_order); } @@ -228,16 +222,16 @@ class ComicPressComicPost { $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; } } diff --git a/classes/backends/ComicPressBackendAttachment.inc b/classes/backends/ComicPressBackendAttachment.inc index 13b04c8..e2d9434 100644 --- a/classes/backends/ComicPressBackendAttachment.inc +++ b/classes/backends/ComicPressBackendAttachment.inc @@ -1,17 +1,64 @@ <?php -class ComicPressBackendAttachment { - function ComicPressBackendAttachment($attachment_id) { - $this->attachment_id = $attachment_id; +require_once(dirname(__FILE__) . '/../ComicPressBackend.inc'); + +class ComicPressBackendAttachment extends ComicPressBackend { + var $root_id = 'attachment'; + + function generate_from_post($post) { + $result = array(); + if (is_object($post)) { + if (isset($post->ID)) { + $children = get_children(array( + 'post_parent' => $post->ID, + 'post_type' => 'attachment', + 'post_mime_type' => 'image' + )); + + if (!empty($children)) { + foreach ($children as $child) { + $result[] = new ComicPressBackendAttachment($child); + } + } + } + } + return $result; + } + + function ComicPressBackendAttachment($attachment) { + $this->attachment = $attachment; + $this->id = sprintf('%s-%d', $this->root_id, $this->attachment->ID); + $this->type = get_post_meta($this->attachment->ID, 'comic_image_type', true); } - function get_url() { - return wp_get_attachment_url($this->attachment_id); + function dims($size = 'comic') { + $metadata = image_downsize($this->attachment->ID, $size); + if (!empty($metadata)) { + if (is_array($metadata)) { + return array_combine(array('width', 'height'), array_slice($metadata, 1, 2)); + } + } + return false; } - function get_info() { - return wp_get_attachment_metadata($this->attachment_id); + function url($size = 'comic') { + $metadata = image_downsize($this->attachment->ID, $size); + if (!empty($metadata)) { + if (is_array($metadata)) { + return $metadata[0]; + } + } + return false; } + + function embed($size = 'comic') { + return $this->_embed_image($size); + } + + function alt() { return $this->attachment->post_title; } + function title() { return $this->attachment->post_excerpt; } + + function get_info() { return array(); } } ?> \ No newline at end of file diff --git a/classes/partials/_comic-image-ordering-sorters.inc b/classes/partials/_comic-image-ordering-sorters.inc index d3a4daf..9de8d93 100644 --- a/classes/partials/_comic-image-ordering-sorters.inc +++ b/classes/partials/_comic-image-ordering-sorters.inc @@ -9,29 +9,31 @@ <?php _e('Click the Refesh button underneath the zoom slider if you\'ve changed the images attached to this post.', 'comicpress') ?> </em></p> -<?php foreach ($ordering as $type => $attachment_ids) { ?> - <h3><?php echo $this->comic_image_types[$type] ?></h3> - <div class="comic-ordering" id="comic-ordering-<?php echo $type ?>"> - <?php foreach ($attachment_ids as $attachment_id) { - $backend = new ComicPressBackendAttachment($attachment_id); - $info = $backend->get_info(); ?> - <div class="cp-comic-attachment" id="attachment_<?php echo $attachment_id ?>"> - <img src="<?php echo $backend->get_url() ?>" border="0" height="<?php echo $zoom_level ?>" /> - <div> - <?php if (isset($info['file'])) { ?> - <p><strong><?php echo basename($info['file']) ?></strong></p> - <?php } ?> - <?php if (isset($info['width']) && isset($info['height'])) { ?> - <p> - <?php _e('Size:', 'comicpress') ?> - <?php printf('%dx%d', $info['width'], $info['height'] ) ?> - </p> - <?php } ?> - </div> - <br style="clear: both" /> - </div> - <?php } ?> - </div> +<?php if (!empty($ordering)) { ?> + <?php foreach ($ordering as $type => $attachment_ids) { ?> + <h3><?php echo $this->comic_image_types[$type] ?></h3> + <div class="comic-ordering" id="comic-ordering-<?php echo $type ?>"> + <?php foreach ($attachment_ids as $attachment_id) { + $backend = new ComicPressBackendAttachment($attachment_id); + $info = $backend->get_info(); ?> + <div class="cp-comic-attachment" id="attachment_<?php echo $attachment_id ?>"> + <img src="<?php echo $backend->url() ?>" border="0" height="<?php echo $zoom_level ?>" /> + <div> + <?php if (isset($info['file'])) { ?> + <p><strong><?php echo basename($info['file']) ?></strong></p> + <?php } ?> + <?php if (isset($info['width']) && isset($info['height'])) { ?> + <p> + <?php _e('Size:', 'comicpress') ?> + <?php printf('%dx%d', $info['width'], $info['height'] ) ?> + </p> + <?php } ?> + </div> + <br style="clear: both" /> + </div> + <?php } ?> + </div> + <?php } ?> <?php } ?> <script type="text/javascript"> diff --git a/functions.php b/functions.php index b6d07c4..7b2b6e1 100644 --- a/functions.php +++ b/functions.php @@ -144,6 +144,14 @@ function RT($which, $restrictions = null, $override_post = null) { return $post; } +function M($override_post = null) { + global $post; + $post_to_use = !is_null($override_post) ? $override_post : $post; + + $comic_post = new ComicPressComicPost($post_to_use); + return $comic_post->get_attachments(); +} + /** * Display the list of Storyline categories. */ diff --git a/single.php b/single.php index bab1e6c..80b62d9 100644 --- a/single.php +++ b/single.php @@ -11,6 +11,11 @@ RT('previous', array('root_of' => '__post')); the_title(); echo '<br />'; RT('previous'); the_title(); echo '<br />'; Restore(); the_title(); echo '<br />'; + + foreach (M() as $image) { + echo $image->embed(); + } + RT('next'); the_title(); echo '<br />'; RT('next', array('root_of' => '__post')); the_title(); echo '<br />'; RT('next', 'from_post'); the_title(); echo '<br />'; diff --git a/test/ComicPressComicPostTest.php b/test/ComicPressComicPostTest.php index 23450bf..1d3b9f2 100644 --- a/test/ComicPressComicPostTest.php +++ b/test/ComicPressComicPostTest.php @@ -20,7 +20,7 @@ class ComicPressComicPostTest extends PHPUnit_Framework_TestCase { ) ); } - + /** * @dataProvider providerTestGenerateComicTag */ @@ -30,7 +30,7 @@ class ComicPressComicPostTest extends PHPUnit_Framework_TestCase { 'comic_dimensions' => $dimensions ) ); - + $source = $this->p->get_comic_img_tag("test.gif", "comic"); if (count($parts = explode("x", $dimensions)) == 2) { @@ -46,83 +46,54 @@ class ComicPressComicPostTest extends PHPUnit_Framework_TestCase { } } } - - function testNormalizeComicImageOrdering() { - $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', 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(array('comic' => array(3,2), 'rss' => array(5,4)), get_post_meta(1, 'comic_ordering', true)); + + function providerTestNormalizeOrdering() { + return array( + array( + array('attachment-1'), + array(), + array('attachment-1' => true) + ), + array( + array('attachment-1'), + array('attachment-1' => true, 'attachment-2' => true), + array('attachment-1' => true) + ), + array( + array('attachment-1'), + array('attachment-1' => array('rss' => array('attachment-2' => true))), + array('attachment-1' => true) + ), + array( + array('attachment-1', 'attachment-2', 'attachment-3'), + array('attachment-1' => array('rss' => array('attachment-2' => true))), + array('attachment-1' => array('rss' => array('attachment-2' => true)), 'attachment-3' => true) + ), + ); } - + + /** + * @dataProvider providerTestNormalizeOrdering + */ + function testNormalizeOrdering($attachments, $current_meta, $expected_result) { + $p = $this->getMock('ComicPressComicPost', array('get_attachments')); + + $attachment_objects = array(); + foreach ($attachments as $attachment) { + $attachment_objects[] = (object)array('id' => $attachment); + } + + $p->expects($this->any())->method('get_attachments')->will($this->returnValue($attachment_objects)); + + wp_insert_post((object)array('ID' => 1)); + update_post_meta(1, 'image-ordering', $current_meta); + + $p->post = (object)array('ID' => 1); + + $this->assertEquals($expected_result, $p->normalize_ordering()); + $this->assertEquals($expected_result, get_post_meta(1, 'image-ordering', true)); + } + function providerTestChangeComicImageOrdering() { return array( array( @@ -148,17 +119,17 @@ class ComicPressComicPostTest extends PHPUnit_Framework_TestCase { ), ); } - + /** * @dataProvider providerTestChangeComicImageOrdering * @covers ComicPressComicPost::change_comic_image_ordering */ function testChangeComicImageOrdering($current_ordering, $revised_ordering, $expected_result) { update_post_meta(1, 'comic_ordering', $current_ordering); - + $this->p->post = (object)array('ID' => 1); $this->p->change_comic_image_ordering($revised_ordering); - + $this->assertEquals($expected_result, get_post_meta(1, 'comic_ordering', true)); } @@ -190,7 +161,7 @@ class ComicPressComicPostTest extends PHPUnit_Framework_TestCase { ), ); } - + /** * @dataProvider providerTestFindParents */ @@ -199,11 +170,11 @@ class ComicPressComicPostTest extends PHPUnit_Framework_TestCase { 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()); } } diff --git a/test/backends/ComicPressBackendAttachmentTest.php b/test/backends/ComicPressBackendAttachmentTest.php new file mode 100644 index 0000000..e1e0cd5 --- /dev/null +++ b/test/backends/ComicPressBackendAttachmentTest.php @@ -0,0 +1,39 @@ +<?php + +require_once('PHPUnit/Framework.php'); +require_once('MockPress/mockpress.php'); +require_once('backends/ComicPressBackendAttachment.inc'); + +class ComicPressBackendAttachmentTest extends PHPUnit_Framework_TestCase { + function setUp() { + _reset_wp(); + } + + function providerTestGenerateFromPost() { + return array( + array(array(), false), + array(array((object)array('ID' => 1)), array('attachment-1')) + ); + } + + /** + * @dataProvider providerTestGenerateFromPost + */ + function testGenerateFromPost($get_children_response, $expected_ids) { + _set_get_children(array( + 'post_parent' => 1, + 'post_type' => 'attachment', + 'post_mime_type' => 'image' + ), $get_children_response); + + $results = ComicPressBackendAttachment::generate_from_post((object)array('ID' => 1)); + if ($expected_ids === false) { + $this->assertTrue(empty($results)); + } else { + $this->assertEquals(count($expected_ids), count($results)); + foreach ($results as $result) { + $this->assertTrue(in_array($result->id, $expected_ids)); + } + } + } +} \ No newline at end of file From 9cd98b98f174846b7a90e7c813d1e8faf7a84c9a Mon Sep 17 00:00:00 2001 From: John Bintz <john@dawn.local> Date: Sun, 8 Nov 2009 23:13:34 -0500 Subject: [PATCH 29/32] add support for per-image flags --- classes/ComicPressComicPost.inc | 32 +++++++++++++++++++------------- test/ComicPressComicPostTest.php | 14 +++++++------- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/classes/ComicPressComicPost.inc b/classes/ComicPressComicPost.inc index 697e1f3..3147fb6 100644 --- a/classes/ComicPressComicPost.inc +++ b/classes/ComicPressComicPost.inc @@ -113,10 +113,10 @@ class ComicPressComicPost { if (!is_array($current_ordering)) { $current_ordering = array(); } $all_current_ids = array(); - foreach ($current_ordering as $key => $children) { + foreach ($current_ordering as $key => $properties) { $all_current_ids[$key] = true; - if (is_array($children)) { - foreach ($children as $type => $kids) { + if (isset($properties['children'])) { + foreach ($properties['children'] as $type => $kids) { $all_current_ids = array_merge($all_current_ids, $kids); } } @@ -130,22 +130,28 @@ class ComicPressComicPost { $missing_attachments = array_diff($all_current_ids, $attachment_ids); foreach ($new_attachments as $attachment_id) { - $current_ordering[$attachment_id] = true; + $current_ordering[$attachment_id] = array('enabled' => true); } foreach ($missing_attachments as $attachment_id) { if (isset($current_ordering[$attachment_id])) { unset($current_ordering[$attachment_id]); } else { - foreach ($current_ordering as $key => $children) { - foreach ($children as $type => $kids) { - if (isset($kids[$attachment_id])) { unset($kids[$attachment_id]); } - $children[$type] = $kids; - } - if (empty($children[$type])) { - $current_ordering[$key] = true; - } else { - $current_ordering[$key] = $children; + foreach ($current_ordering as $key => $properties) { + if (isset($properties['children'])) { + foreach ($properties['children'] as $type => $kids) { + if (isset($kids[$attachment_id])) { unset($kids[$attachment_id]); } + if (empty($kids)) { + unset($properties['children'][$type]); + } else { + $properties['children'][$type] = $kids; + } + } + if (empty($properties['children'])) { + unset($current_ordering[$key]['children']); + } else { + $current_ordering[$key] = $properties; + } } } } diff --git a/test/ComicPressComicPostTest.php b/test/ComicPressComicPostTest.php index 1d3b9f2..2754e63 100644 --- a/test/ComicPressComicPostTest.php +++ b/test/ComicPressComicPostTest.php @@ -52,22 +52,22 @@ class ComicPressComicPostTest extends PHPUnit_Framework_TestCase { array( array('attachment-1'), array(), - array('attachment-1' => true) + array('attachment-1' => array('enabled' => true)) ), array( array('attachment-1'), - array('attachment-1' => true, 'attachment-2' => true), - array('attachment-1' => true) + array('attachment-1' => array('enabled' => false), 'attachment-2' => array('enabled' => true)), + array('attachment-1' => array('enabled' => false)) ), array( array('attachment-1'), - array('attachment-1' => array('rss' => array('attachment-2' => true))), - array('attachment-1' => true) + array('attachment-1' => array('enabled' => true, 'children' => array('rss' => array('attachment-2' => true)))), + array('attachment-1' => array('enabled' => true)) ), array( array('attachment-1', 'attachment-2', 'attachment-3'), - array('attachment-1' => array('rss' => array('attachment-2' => true))), - array('attachment-1' => array('rss' => array('attachment-2' => true)), 'attachment-3' => true) + array('attachment-1' => array('enabled' => false, 'children' => array('rss' => array('attachment-2' => true)))), + array('attachment-1' => array('enabled' => false, 'children' => array('rss' => array('attachment-2' => true))), 'attachment-3' => array('enabled' => true)) ), ); } From 99939dac4cc2b1bf68aa30d75726302d9834684e Mon Sep 17 00:00:00 2001 From: John Bintz <john@coswellproductions.com> Date: Mon, 9 Nov 2009 20:54:45 -0500 Subject: [PATCH 30/32] add all adjacent --- classes/ComicPressStoryline.inc | 14 ++++++++++++++ test/ComicPressStorylineTest.php | 23 +++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/classes/ComicPressStoryline.inc b/classes/ComicPressStoryline.inc index 17f6cf2..046ea1f 100644 --- a/classes/ComicPressStoryline.inc +++ b/classes/ComicPressStoryline.inc @@ -153,6 +153,20 @@ class ComicPressStoryline { return false; } + function all_adjacent($id, $direction) { + if (isset($this->_structure[$id])) { + $all_adjacent = array(); + do { + if ($has_adjacent = isset($this->_structure[$id][$direction])) { + $all_adjacent[] = $this->_structure[$id][$direction]; + $id = $this->_structure[$id][$direction]; + } + } while ($has_adjacent); + return $all_adjacent; + } + return false; + } + /** * Get the valid navigation directions for a particular post. */ diff --git a/test/ComicPressStorylineTest.php b/test/ComicPressStorylineTest.php index 805ffc6..c780841 100644 --- a/test/ComicPressStorylineTest.php +++ b/test/ComicPressStorylineTest.php @@ -561,6 +561,29 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase { $this->assertEquals($expected_categories, $this->css->build_from_restrictions($restrictions)); } + + function providerTestAllAdjacent() { + return array( + array(3, 'previous', array(2, 1)), + array(2, 'next', array(3, 4)), + array(4, 'next', array()), + array(5, 'next', false) + ); + } + + /** + * @dataProvider providerTestAllAdjacent + */ + function testAllAdjacent($start, $direction, $expected_result) { + $this->css->_structure = array( + '1' => array('next' => 2), + '2' => array('previous' => 1, 'parent' => 1, 'next' => 3), + '3' => array('previous' => 2, 'parent' => 1, 'next' => 4), + '4' => array('previous' => 3, 'parent' => 1) + ); + + $this->assertEquals($expected_result, $this->css->all_adjacent($start, $direction)); + } } ?> \ No newline at end of file From 6c9d05f6afd05ff83697c9b4665dd76dadaf3635 Mon Sep 17 00:00:00 2001 From: John Bintz <john@coswellproductions.com> Date: Mon, 9 Nov 2009 21:12:42 -0500 Subject: [PATCH 31/32] fix FS#113 --- classes/ComicPressNavigation.inc | 24 ++++++++++------- test/ComicPressNavigationTest.php | 44 ++++++++++++++++++++++++++++--- 2 files changed, 55 insertions(+), 13 deletions(-) diff --git a/classes/ComicPressNavigation.inc b/classes/ComicPressNavigation.inc index 4dca364..31288a9 100644 --- a/classes/ComicPressNavigation.inc +++ b/classes/ComicPressNavigation.inc @@ -14,44 +14,50 @@ class ComicPressNavigation { if (is_object($post)) { if (isset($post->ID)) { $cache_key = 'navigation-' . $post->ID; - + if (($result = wp_cache_get($cache_key, 'comicpress')) !== false) { foreach ($result as $key => $post_id) { $nev[$key] = get_post($post_id); } } - + // global previous/next foreach (array('previous', 'next') as $field) { $nav[$field] = $this->_dbi->{"get_${field}_comic"}(null, $post); } - + // global first/last foreach (array('first', 'last') as $field) { $nav[$field] = $this->_dbi->{"get_${field}_comic"}(null); } - + if ($category = $this->_storyline->get_valid_post_category($post->ID)) { // storyline previous/next foreach (array('previous', 'next') as $field) { $nav["storyline-${field}"] = $this->_dbi->{"get_${field}_comic"}($category, $post); } - + // adjacent storyline nodes if (is_array($valid = $this->_storyline->valid($category))) { foreach ($valid as $field) { - $nav["storyline-chapter-${field}"] = $this->_dbi->get_first_comic($this->_storyline->{$field}($category)); + $all_adjacents = $this->_storyline->all_adjacent($category, $field); + foreach ($all_adjacents as $adjacent_category) { + $result = $this->_dbi->get_first_comic($adjacent_category); + if (!empty($result)) { + $nav["storyline-chapter-${field}"] = $result; break; + } + } } } } - + $cache_data = array(); foreach ($nav as $key => $output_post) { if (!empty($output_post)) { $cache_data[$key] = $output_post->ID; } } - + wp_cache_set($cache_key, $cache_data, 'comicpress'); - + return $nav; } } diff --git a/test/ComicPressNavigationTest.php b/test/ComicPressNavigationTest.php index e848ec8..7579fe1 100644 --- a/test/ComicPressNavigationTest.php +++ b/test/ComicPressNavigationTest.php @@ -12,7 +12,7 @@ class ComicPressNavigationTest extends PHPUnit_Framework_TestCase { _reset_wp(); $this->nav = new ComicPressNavigation(); } - + function testGetPostNav() { $dbi = $this->getMock('ComicPressDBInterface', array('get_previous_comic', 'get_next_comic', 'get_first_comic', 'get_last_comic')); $storyline = new ComicPressStoryline(); @@ -34,18 +34,54 @@ class ComicPressNavigationTest extends PHPUnit_Framework_TestCase { $dbi->expects($this->at(3))->method('get_last_comic')->with(null); $dbi->expects($this->at(4))->method('get_previous_comic')->with(2, $post); $dbi->expects($this->at(5))->method('get_next_comic')->with(2, $post); - $dbi->expects($this->at(6))->method('get_first_comic')->with(1); - $dbi->expects($this->at(7))->method('get_first_comic')->with(3); + $dbi->expects($this->at(6))->method('get_first_comic')->with(1)->will($this->returnValue((object)array('ID' => 1))); + $dbi->expects($this->at(7))->method('get_first_comic')->with(3)->will($this->returnValue((object)array('ID' => 1))); $this->nav->_dbi = $dbi; $this->nav->_storyline = $storyline; $this->assertFalse(wp_cache_get('navigation-1', 'comicpress')); - + $this->nav->get_post_nav($post); $this->assertTrue(wp_cache_get('navigation-1', 'comicpress') !== false); } + + function testSkipEmptyCategories() { + $dbi = $this->getMock('ComicPressDBInterface', array('get_previous_comic', 'get_next_comic', 'get_first_comic', 'get_last_comic')); + $storyline = new ComicPressStoryline(); + + $storyline->_structure = array( + '1' => array('next' => 2), + '2' => array('previous' => 1, 'next' => 3), + '3' => array('previous' => 2) + ); + + wp_insert_post(array('ID' => 1)); + $post = get_post(1); + + wp_set_post_categories(1, array(1)); + + $dbi->expects($this->any())->method('get_first_comic')->will($this->returnCallback(array(&$this, 'callbackTestSkipEmptyCategories'))); + + $this->nav->_dbi = $dbi; + $this->nav->_storyline = $storyline; + + $nav = $this->nav->get_post_nav($post); + + $this->assertEquals(10, $nav['storyline-chapter-next']->ID); + } + + function callbackTestSkipEmptyCategories($category_id) { + if (!is_null($category_id)) { + switch ($category_id) { + case 3: return (object)array('ID' => 10); + default: return false; + } + } else { + return (object)array('ID' => 1); + } + } } ?> \ No newline at end of file From bbe42eb9941030319c5225e9efd5b81a4c5c2d76 Mon Sep 17 00:00:00 2001 From: John Bintz <john@coswellproductions.com> Date: Tue, 10 Nov 2009 19:55:03 -0500 Subject: [PATCH 32/32] small cleanups --- classes/ComicPressAdmin.inc | 2 +- classes/ComicPressComicPost.inc | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/classes/ComicPressAdmin.inc b/classes/ComicPressAdmin.inc index f4d42cf..f63a2a4 100644 --- a/classes/ComicPressAdmin.inc +++ b/classes/ComicPressAdmin.inc @@ -174,7 +174,7 @@ class ComicPressAdmin { if (is_numeric($override_post)) { $uploading_iframe_ID = $override_post; } $comic_post = new ComicPressComicPost(get_post($uploading_iframe_ID)); - $ordering = $comic_post->normalize_comic_image_ordering(); + $ordering = $comic_post->normalize_ordering(); $nonce = wp_create_nonce('comicpress'); $zoom_level = 40; diff --git a/classes/ComicPressComicPost.inc b/classes/ComicPressComicPost.inc index 3147fb6..114aa41 100644 --- a/classes/ComicPressComicPost.inc +++ b/classes/ComicPressComicPost.inc @@ -8,6 +8,10 @@ class ComicPressComicPost { if (!is_null($post)) { $this->post = $post; } } + /** + * TODO normalize the attachments + * @return unknown_type + */ function get_attachments() { $comicpress = ComicPress::get_instance(); $attachments = array();