starting work on filesystem backend
This commit is contained in:
parent
f279ed8904
commit
f1845daed5
73
classes/backends/ComicPressBackendFilesystem.inc
Normal file
73
classes/backends/ComicPressBackendFilesystem.inc
Normal file
@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
require_once(dirname(__file__) . '/../ComicPressBackend.inc');
|
||||
|
||||
class ComicPressBackendFilesystem extends ComicPressBackend {
|
||||
var $search_string = '';
|
||||
|
||||
function process_search_string($post, $type) {
|
||||
$this->_searches = array($this->search_string);
|
||||
|
||||
do {
|
||||
$any_found = false;
|
||||
for ($i = 0; $i < count($this->_searches); ++$i) {
|
||||
$search = $this->_searches[$i];
|
||||
if (preg_match('#%([a-z0-9\-]+)%#i', $search, $matches) > 0) {
|
||||
$any_found = true;
|
||||
|
||||
$method = '_replace_' . strtolower(str_replace('-', '_', $matches[1]));
|
||||
if (method_exists($this, $method)) {
|
||||
$result = $this->{$method}($post, $type);
|
||||
if ($result !== false) {
|
||||
$this->_searches[$i] = str_replace($matches[0], $result, $search);
|
||||
} else {
|
||||
// array state change, start over
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
$this->_searches[$i] = str_replace($matches[0], '', $search);
|
||||
}
|
||||
}
|
||||
}
|
||||
} while ($any_found);
|
||||
|
||||
return $this->_searches;
|
||||
}
|
||||
|
||||
function _replace_wordpress($post, $type) { return ABSPATH; }
|
||||
function _replace_type($post, $type) { return $type; }
|
||||
function _replace_y_m_d($post, $type) { return date('Y-m-d', strtotime($post->post_date)); }
|
||||
function _replace_year($post, $type) { return date('Y', strtotime($post->post_date)); }
|
||||
function _replace_categories($post, $type) {
|
||||
if (count($post_categories = wp_get_post_categories($post->ID)) == 1) {
|
||||
$current_parent = reset($post_categories);
|
||||
$all_slugs = array();
|
||||
|
||||
do {
|
||||
if ($keep_searching = ($current_parent != 0)) {
|
||||
$category = get_category($current_parent);
|
||||
if (!empty($category)) {
|
||||
array_unshift($all_slugs, $category->slug);
|
||||
$current_parent = $category->parent;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} while ($keep_searching);
|
||||
|
||||
$new_searches = array();
|
||||
$slug_count = count($all_slugs);
|
||||
foreach ($this->_searches as $search) {
|
||||
if ($slug_count == 0) {
|
||||
$new_searches[] = preg_replace('#%categories%#i', '', $search);
|
||||
} else {
|
||||
for ($i = 0; $i < $slug_count; ++$i) {
|
||||
$new_searches[] = preg_replace('#%categories%#i', implode('/', array_slice($all_slugs, 0, $slug_count - $i)), $search);
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->_searches = $new_searches;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
49
test/backends/ComicPressBackendFilesystemTest.php
Normal file
49
test/backends/ComicPressBackendFilesystemTest.php
Normal file
@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
require_once('PHPUnit/Framework.php');
|
||||
require_once('MockPress/mockpress.php');
|
||||
require_once('backends/ComicPressBackendFilesystem.inc');
|
||||
require_once('vfsStream/vfsStream.php');
|
||||
|
||||
class ComicPressBackendFilesystemTest extends PHPUnit_Framework_TestCase {
|
||||
function setUp() {
|
||||
_reset_wp();
|
||||
$this->fs = new ComicPressBackendFilesystem();
|
||||
}
|
||||
|
||||
function providerTestProcessSearchString() {
|
||||
return array(
|
||||
array('/comic/*.jpg', array('/comic/*.jpg')),
|
||||
array('%wordpress%/comic/*.jpg', array('/wordpress/comic/*.jpg')),
|
||||
array('%wordpress%/%type%/*.jpg', array('/wordpress/comic/*.jpg')),
|
||||
array('%wordpress%/comic/%y-m-d%*.jpg', array('/wordpress/comic/2009-01-01*.jpg')),
|
||||
array('%wordpress%/comic/%year%/%y-m-d%*.jpg', array('/wordpress/comic/2009/2009-01-01*.jpg')),
|
||||
array(
|
||||
'%wordpress%/comic/%categories%/%y-m-d%*.jpg',
|
||||
array(
|
||||
'/wordpress/comic/parent/child/2009-01-01*.jpg',
|
||||
'/wordpress/comic/parent/2009-01-01*.jpg',
|
||||
)
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerTestProcessSearchString
|
||||
*/
|
||||
function testProcessSearchString($string, $expected_searches) {
|
||||
$fs = $this->getMock('ComicPressBackendFilesystem', array('_replace_wordpress'));
|
||||
|
||||
$fs->expects($this->any())->method('_replace_wordpress')->will($this->returnValue('/wordpress'));
|
||||
|
||||
$post = (object)array('ID' => 1, 'post_date' => '2009-01-01');
|
||||
add_category(1, (object)array('slug' => 'parent', 'parent' => 0));
|
||||
add_category(2, (object)array('slug' => 'child', 'parent' => 1));
|
||||
|
||||
wp_set_post_categories(1, array(2));
|
||||
|
||||
$fs->search_string = $string;
|
||||
|
||||
$this->assertEquals($expected_searches, $fs->process_search_string($post, 'comic'));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user