more restrictions

This commit is contained in:
John Bintz 2009-11-07 20:56:49 -05:00
parent 57ef1784d6
commit 4d9392470c
7 changed files with 341 additions and 144 deletions

View File

@ -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;

View File

@ -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.
*/

View File

@ -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();
}
}
?>

View File

@ -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.
*/

View File

@ -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();
?>

View File

@ -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));
}
}
?>

View File

@ -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));
}
}
?>