a bunch of work on restrictions

This commit is contained in:
John Bintz 2009-11-07 22:00:14 -05:00
parent 4d9392470c
commit 261fce60d5
6 changed files with 211 additions and 80 deletions

View File

@ -1,7 +1,7 @@
<?php <?php
class ComicPressDBInterface { class ComicPressDBInterface {
function ComicPressDBInterface() {} function ComicPressDBInterface() {}
function get_instance() { function get_instance() {
static $instance; static $instance;
@ -40,14 +40,14 @@ class ComicPressDBInterface {
/** /**
* Get the first comic in a category. * 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); return $this->get_terminal_post(true, $include_categories);
} }
/** /**
* Get the last comic in a category. * 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); return $this->get_terminal_post(false, $include_categories);
} }
@ -55,13 +55,13 @@ class ComicPressDBInterface {
* Get the comic post adjacent to the current comic. * Get the comic post adjacent to the current comic.
* Wrapper around get_adjacent_post(). Don't unit test this method. * 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; global $post;
$this->_prepare_wp_query(); $this->_prepare_wp_query();
if (!is_null($override_post)) { $temp_post = $post; $post = $override_post; } 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(); $this->_reset_wp_query();
if (!is_null($override_post)) { $post = $temp_post; } if (!is_null($override_post)) { $post = $temp_post; }
@ -88,12 +88,12 @@ class ComicPressDBInterface {
/** /**
* Get the previous comic from the current one. * 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. * 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); }
} }
?> ?>

View File

@ -412,14 +412,18 @@ class ComicPressStoryline {
return false; return false;
} }
function &_include($method, $parameter) { function &_include() {
$this->_category_search = array_unique(array_merge($this->_category_search, $this->{$method}($parameter))); $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); sort($this->_category_search);
return $this; return $this;
} }
function &_exclude($method, $parameter) { function &_exclude() {
$this->_category_search = array_diff($this->_category_search, $this->{$method}($parameter)); $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); sort($this->_category_search);
return $this; return $this;
} }
@ -479,6 +483,56 @@ class ComicPressStoryline {
return $this->_exclude('_find_level', $id); 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() { function end_search() {
$result = $this->_category_search; $result = $this->_category_search;
$this->_category_search = array(); $this->_category_search = array();
@ -486,6 +540,8 @@ class ComicPressStoryline {
} }
function build_from_restrictions($restrictions = null) { function build_from_restrictions($restrictions = null) {
global $post;
$this->read_from_options()->exclude_all(); $this->read_from_options()->exclude_all();
$include_all = true; $include_all = true;
@ -497,17 +553,33 @@ class ComicPressStoryline {
if (!$include_all) { if (!$include_all) {
foreach ($restrictions as $type => $list) { 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) { foreach ((array)$list as $restriction) {
switch ($type) { $method = '';
case 'child_of': $args = array($restriction);
$this->include_children($restriction); switch ($method_type) {
case 'child_of': $method = 'children'; break;
case 'from_post': $method = 'post_category'; break;
case 'previous':
$method = 'adjacent';
$args[] = false;
break; break;
case 'only': case 'next':
$this->include_only($restriction); $method = 'adjacent';
break; $args[] = true;
case 'level':
$this->include_level($restriction);
break; break;
default:
$method = $method_type; break;
}
if (!empty($method)) {
call_user_func_array(array($this, "${method_root}_${method}"), $args);
} }
} }
} }

View File

@ -54,17 +54,27 @@ function finish_comicpress() {
/** /**
* Protect global $post and $wp_query. * Protect global $post and $wp_query.
*/ */
function protect_query() { function Protect() {
global $post, $wp_query, $__post, $__wp_query; global $post, $wp_query, $__post, $__wp_query;
$__post = $post; $__post = $post;
$__wp_query = $wp_query; $__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. * Restore global $post and $wp_query.
*/ */
function unprotect_query() { function Unprotect() {
global $post, $wp_query, $__post, $__wp_query; global $post, $wp_query, $__post, $__wp_query;
$post = $__post; $post = $__post;
@ -73,49 +83,51 @@ function unprotect_query() {
$__post = $__wp_query = null; $__post = $__wp_query = null;
} }
function retrieve_storyline_post($which, $restrictions = null, $override_post = null) { function R($which, $restrictions = null, $override_post = null) {
global $post; global $post;
$post_to_use = !is_null($override_post) ? $override_post : $post;
$storyline = new ComicPressStoryline(); $storyline = new ComicPressStoryline();
$storyline->read_from_options()->exclude_all();
if (!is_null($restrictions)) { if (is_string($restrictions)) {
if (is_array($restrictions)) { switch ($restrictions) {
foreach ($restrictions as $type => $list) { case 'from_post':
switch ($type) { $restrictions = array('from_post' => $post_to_use);
case 'child_of': break;
foreach ($list as $restriction) { $storyline->include_children($restriction); }
break;
}
}
} }
} else {
$storyline->include_all();
} }
$categories = $storyline->end_search(); $categories = $storyline->build_from_restrictions($restrictions);
var_dump($categories);
$dbi = ComicPressDBInterface::get_instance(); $dbi = ComicPressDBInterface::get_instance();
$new_post = false; $new_post = false;
switch ($which) { switch ($which) {
case 'first': case 'first': $new_post = $dbi->get_first_post($categories); break;
case 'last': case 'last': $new_post = $dbi->get_last_post($categories); break;
case 'next': case 'next': $new_post = $dbi->get_next_post($categories, $post_to_use); break;
case 'previous': case 'previous': $new_post = $dbi->get_previous_post($categories, $post_to_use); break;
$method = "get_${which}_post";
if (method_exists($dbi, $method)) {
$new_post = $dbi->{$method}($categories);
}
break;
} }
return $new_post; 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. * Display the list of Storyline categories.
*/ */

View File

@ -1,7 +1,9 @@
<?php <?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(); finish_comicpress();
?> ?>

View File

@ -1,37 +1,19 @@
<?php <?php
global $comicpress, $nav_comics; 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()) { Unprotect();
the_post();
if (in_comic_category()) { include_partial('single-display-comic'); }
}
rewind_posts();
$comic = ob_get_clean();
ob_start(); the_title(); echo '<br />';
$nav_comics = $comicpress->get_nav_comics(); finish_comicpress();
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']);
?> ?>

View File

@ -468,6 +468,52 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase {
$this->assertEquals($expected_return, $this->css->_find_level($id)); $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() { function providerTestBuildFromRestrictions() {
return array( return array(
array( array(
@ -493,7 +539,19 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase {
array( array(
array('level' => 1), array('level' => 1),
array(1,4,7) 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 * @dataProvider providerTestBuildFromRestrictions
*/ */
function testBuildFromRestrictions($restrictions, $expected_categories) { 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'); $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)); $this->assertEquals($expected_categories, $this->css->build_from_restrictions($restrictions));
} }
} }