cascade search
This commit is contained in:
parent
88b9e6cf83
commit
a2bcd50093
|
@ -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) { ?>
|
||||
<link rel="stylesheet" href="<?php echo get_template_directory_uri() . $uri ?>" type="text/css" />
|
||||
<?php }
|
||||
foreach ($this->additional_javascripts as $uri) { ?>
|
||||
<script type="text/javascript" src="<?php echo get_template_directory_uri() . $uri ?>"></script>
|
||||
<?php }
|
||||
}
|
||||
|
||||
function announce_activated_helpers() {
|
||||
echo "<center>[ <strong>Activated ComicPress helpers:</strong> " . implode(", ", array_keys($this->comicpress_options['helpers'])) . " ]</center>";
|
||||
}
|
||||
|
||||
function setup_comicpress_partial() { ?>
|
||||
<style type="text/css">
|
||||
.partial-helper {
|
||||
position: absolute;
|
||||
z-index: 100;
|
||||
padding: 4px;
|
||||
border: solid #333 1px;
|
||||
background-color: #99d;
|
||||
opacity: 0.2;
|
||||
-moz-opacity: 0.2;
|
||||
-khtml-opacity: 0.2;
|
||||
zoom: 1;
|
||||
cursor: crosshair
|
||||
}
|
||||
|
||||
.partial-helper:hover {
|
||||
opacity: 1;
|
||||
-moz-opacity: 1;
|
||||
-khtml-opacity: 1;
|
||||
}
|
||||
</style>
|
||||
<!--[if IE gte 6]>
|
||||
<style type="text/css">
|
||||
.partial-helper { filter: alpha(opacity=20); }
|
||||
.partial-helper:hover { filter: alpha(opacity=100); }
|
||||
</style>
|
||||
<![endif]-->
|
||||
<?php }
|
||||
|
||||
function comicpress_partial($content, $target) {
|
||||
return '<div class="partial-helper">' . str_replace(get_template_directory() . DIRECTORY_SEPARATOR, '', $target) . '</div>' . $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;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue