Merge branch 'media-handling'

This commit is contained in:
John Bintz 2009-12-02 22:51:34 -05:00
commit 181de6bc29
5 changed files with 367 additions and 67 deletions

View File

@ -0,0 +1,150 @@
<?php
class ComicPressMediaHandling {
var $root_filter = '%wordpress%/%type-folder%/';
var $default_filter = '%wordpress%/%type-folder%/%date-Y-m-d%*.*';
function _bundle_global_variables() {
global $comic_folder, $archive_comic_folder, $rss_comic_folder, $mini_comic_folder;
$bundle = array();
foreach (array('comic', 'archive', 'rss', 'mini') as $which) {
switch ($which) {
case 'comic':
$bundle['comic'] = $comic_folder;
break;
default:
$bundle[$which] = ${"${which}_comic_folder"};
break;
}
}
return $bundle;
}
function _get_filter($filter_to_use = null) {
global $comic_filename_filters;
if (!is_null($filter_to_use)) {
if (is_string($filter_to_use)) {
if (isset($comic_filename_filters[$filter_to_use])) {
return $this->_convert_to_percent_filter($comic_filename_filters[$filter_to_use]);
}
}
}
return $this->default_filter;
}
function _convert_to_percent_filter($old) {
if (strpos(strtolower($old), '%wordpress%') !== 0) {
$old = str_replace('{date}', '%date-Y-m-d%', $old);
return $this->root_filter . $old;
}
return $old;
}
function _expand_filter($filter, $type_folder, $override_post = null) {
global $post;
$this->post_to_use = !is_null($override_post) ? $override_post : $post;
$this->type_folder = $type_folder;
$result = preg_replace_callback('#%([a-z0-9-]+)%#i', array(&$this, '_expand_filter_callback'), $filter);
$result = str_replace('.', '\.', $result);
$result = str_replace('*', '.*', $result);
unset($this->post_to_use);
unset($this->type_folder);
return $result;
}
function _resolve_regex_path($input) {
$input = str_replace('\.', '.', $input);
return $input;
}
// @codeCoverageIgnoreStart
function _abspath() {
return realpath(ABSPATH);
}
// @codeCoverageIgnoreEnd
function _expand_filter_callback($matches) {
$value = '';
switch (strtolower($matches[1])) {
case 'wordpress':
$value = $this->_abspath();
break;
case 'type-folder':
$value = $this->type_folder;
break;
default:
if (preg_match('#^date-(.*)$#', $matches[1], $date_matches) > 0) {
$value = date($date_matches[1], strtotime($this->post_to_use->post_date));
break;
}
$value = $matches[0];
break;
}
return apply_filters('comicpress_expand_filter_callback', $value, $matches);
}
function _read_directory($pattern) {
$dirname = $this->_resolve_regex_path(dirname($pattern));
$results = false;
if (is_dir($dirname)) {
$results = array();
if (($dh = opendir($dirname)) !== false) {
$filename_pattern = str_replace('#', '\#', basename($pattern));
while (($file = readdir($dh)) !== false) {
$target = $dirname . '/' . $file;
if (is_file($target)) {
if (preg_match("#^${filename_pattern}$#", $file) > 0) {
$results[] = $target;
}
}
}
closedir($dh);
}
}
return $results;
}
/**
* Get the comic path.
* @param string $type The type to retrieve.
* @param object $override_post The post to use in place of the Loop post.
* @param string $filter The filter to use.
* @param boolean $multi If true, return all matching files.
* @return string|array|boolean A single comic URI relative to the WordPress base, multiple comic URIs, or false if an error occurred.
*/
function get_comic_path($type = 'comic', $override_post = null, $filter = 'default', $multi = false) {
global $post;
$post_to_use = !is_null($override_post) ? $override_post : $post;
$filter = $this->_get_filter($filter);
$globals = $this->_bundle_global_variables();
if (isset($globals[$type])) {
$filter = $this->_expand_filter($filter, $globals[$type], $post_to_use);
if (is_array($results = $this->_read_directory($filter))) {
if (($pre_handle = apply_filters('comicpress_pre_handle_comic_path_results', false, $results, $type, $post_to_use)) !== false) {
return $pre_handle;
}
$new_results = array();
foreach ($results as $result) {
$new_results[] = str_replace($this->_abspath(), '', $result);
}
if ($multi) {
return $new_results;
} else {
return reset($new_results);
}
}
}
return false;
}
}

View File

@ -181,11 +181,9 @@ if ($comicpress_options['remove_wptexturize']) {
// WIDGETS WP 2.8 compatible ONLY, no backwards compatibility here. // WIDGETS WP 2.8 compatible ONLY, no backwards compatibility here.
$dirs_to_search = array_unique(array(get_template_directory(),get_stylesheet_directory())); $dirs_to_search = array_unique(array(get_template_directory(),get_stylesheet_directory()));
foreach ($dirs_to_search as $dir) { foreach ($dirs_to_search as $dir) {
// Widgets foreach (array('widgets' => 'php', 'functions' => 'php', 'classes' => 'inc') as $folder => $extension) {
foreach (glob($dir . '/widgets/*.php') as $__file) { require_once($__file); } foreach (glob($dir . "/${folder}/*.${extension}") as $__file) { require_once($__file); }
}
// FUNCTIONS & Extra's
foreach (glob($dir . '/functions/*.php') as $__file) { require_once($__file); }
} }
// Dashboard Menu Comicpress Options and ComicPress CSS // Dashboard Menu Comicpress Options and ComicPress CSS
@ -217,7 +215,6 @@ if (defined("CPM_DATE_FORMAT")) {
// Note that it's quite possible to slurp up the wrong file if your expressions are too broad. // Note that it's quite possible to slurp up the wrong file if your expressions are too broad.
$comic_filename_filters = array(); $comic_filename_filters = array();
$comic_filename_filters['default'] = "{date}*.*";
// load all of the comic & non-comic category information // load all of the comic & non-comic category information
add_action('init', 'get_all_comic_categories'); add_action('init', 'get_all_comic_categories');
@ -427,61 +424,8 @@ function get_adjacent_storyline_category_id($next = false) {
*/ */
function get_comic_path($folder = 'comic', $override_post = null, $filter = 'default', $multi = null) { function get_comic_path($folder = 'comic', $override_post = null, $filter = 'default', $multi = null) {
global $post, $comic_filename_filters, $comic_folder, $archive_comic_folder, $rss_comic_folder, $mini_comic_folder, $comic_pathfinding_errors, $wpmu_version; $mh = new ComicPressMediaHandling();
return $mh->get_comic_path($folder, $override_post, $filter, $multi);
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 "mini": $folder_to_use = $mini_comic_folder; break;
case "comic": default: $folder_to_use = $comic_folder; break;
}
if (!empty($wpmu_version)) {
if (($wpmu_path = get_option('upload_path')) !== false) {
$folder_to_use = $wpmu_path . '/' . $folder_to_use;
}
}
$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);
$cwd = get_template_directory();
if ($cwd !== false) {
// Strip the wp-admin part and just get to the root.
$root_path = preg_replace('#[\\/]wp-(admin|content).*#', '', $cwd);
}
$results = array();
/* have to use root_path to get subdirectory installation directories */
if (count($results = glob("${root_path}/${folder_to_use}/${filter_with_date}")) > 0) {
if (!empty($wpmu_version)) {
$comic = reset($results);
$comic = $folder_to_use . '/'. basename($comic);
if ($wpmu_path !== false) { $comic = str_replace($wpmu_path, "files", $comic); }
return $comic;
}
if (!empty($multi)) {
return $results;
} else {
/* clear the root path */
$comic = reset($results);
$comic = $folder_to_use .'/'. basename($comic);
return $comic;
}
}
$comic_pathfinding_errors[] = sprintf(__("Unable to find the file in the <strong>%s</strong> folder that matched the pattern <strong>%s</strong>. Check your WordPress and ComicPress settings.", 'comicpress'), $folder_to_use, $filter_with_date);
return false;
} }

33
functions/wpmu.php Normal file
View File

@ -0,0 +1,33 @@
<?php
function _comicpress_pre_handle_comic_path_results($return, $results, $type, $post_to_use) {
global $wpmu_version;
if (!empty($wpmu_version)) {
$globals = ComicPressMediaHandling::_bundle_global_variables();
$comic = $globals[$type] . '/'. basename(reset($results));
if (($wpmu_path = get_option('upload_path')) !== false) {
$comic = str_replace($wpmu_path, "files", $comic);
}
return $comic;
}
return false;
}
function _comicpress_expand_filter_callback($value, $matches) {
global $wpmu_version;
if (!empty($wpmu_version)) {
if (strtolower($matches[1]) == 'wordpress') {
if ($path = get_option('upload_path')) {
return $path;
}
}
}
return $value;
}
add_filter('comicpress_pre_handle_comic_path_results', '_comicpress_pre_handle_comic_path_results', 10, 4);
add_filter('comicpress_expand_filter_callback', '_comicpress_expand_filter_callback', 10, 2);

View File

@ -0,0 +1,178 @@
<?php
require_once('PHPUnit/Framework.php');
require_once('MockPress/mockpress.php');
require_once('vfsStream/vfsStream.php');
require_once(dirname(__FILE__) . '/../classes/ComicPressMediaHandling.inc');
require_once(dirname(__FILE__) . '/../functions/wpmu.php');
class ComicPressMediaHandlingTest extends PHPUnit_Framework_TestCase {
function setUp() {
_reset_wp();
$this->cpmh = new ComicPressMediaHandling();
$this->default_filter = $this->cpmh->default_filter;
vfsStreamWrapper::register();
vfsStreamWrapper::setRoot(new vfsStreamDirectory('root'));
}
function testBundleGlobalVariables() {
global $comic_folder, $archive_comic_folder, $rss_comic_folder, $mini_comic_folder;
$comic_folder = 'comic';
$archive_comic_folder = 'archive';
$rss_comic_folder = 'rss';
$mini_comic_folder = 'mini';
$this->assertEquals(array(
'comic' => 'comic',
'archive' => 'archive',
'rss' => 'rss',
'mini' => 'mini'
), $this->cpmh->_bundle_global_variables());
}
function providerTestGetFilter() {
$cpmh = new ComicPressMediaHandling();
return array(
array(null, $cpmh->default_filter),
array('fail', $cpmh->default_filter),
array(array(), $cpmh->default_filter),
array('test', 'test')
);
}
/**
* @dataProvider providerTestGetFilter
*/
function testGetFilter($filter_to_use, $expected_result) {
global $comic_filename_filters;
$comic_filename_filters['test'] = 'test';
$cpmh = $this->getMock('ComicPressMediaHandling', array('_convert_to_percent_filter'));
if ($expected_result !== $cpmh->default_filter) {
$cpmh->expects($this->once())->method('_convert_to_percent_filter')->with($expected_result)->will($this->returnValue($expected_result));
} else {
$cpmh->expects($this->never())->method('_convert_to_percent_filter');
}
$this->assertEquals($expected_result, $cpmh->_get_filter($filter_to_use));
}
function providerTestConvertToPercentFilter() {
return array(
array('', '%wordpress%/%type-folder%/'),
array('{date}', '%wordpress%/%type-folder%/%date-Y-m-d%'),
array('%wordpress%/%type-folder%/{date}', '%wordpress%/%type-folder%/{date}'),
);
}
/**
* @dataProvider providerTestConvertToPercentFilter
*/
function testConvertToPercentFilter($old_filter, $new_filter) {
$this->assertEquals($new_filter, $this->cpmh->_convert_to_percent_filter($old_filter));
}
function providerTestExpandFilter() {
return array(
array('', ''),
array('%test%', '%test%'),
array('%wordpress%', vfsStream::url('root')),
array('%wordpress%%wordpress%', vfsStream::url('root') . vfsStream::url('root')),
array('%test test%', '%test test%'),
array('%wordpress%/%type-folder%', vfsStream::url('root') . '/comic'),
array('%date-Y%', '2009'),
array('%wordpress%/%type-folder%/%date-Y-m-d%*.*', vfsStream::url('root') . '/comic/2009-01-01.*\..*')
);
}
/**
* @dataProvider providerTestExpandFilter
*/
function testExpandFilter($filter, $expected_result) {
$cpmh = $this->getMock('ComicPressMediaHandling', array('_abspath'));
$cpmh->expects($this->any())->method('_abspath')->will($this->returnValue(vfsStream::url('root')));
$this->assertEquals($expected_result, $cpmh->_expand_filter($filter, 'comic', (object)array('ID' => 1, 'post_date' => '2009-01-01 15:00:00')));
}
function providerTestExpandFilterWPMUCallback() {
return array(
array('', '', 'original'),
array('', 'new', 'original'),
array('1', 'new', 'new'),
);
}
/**
* @dataProvider providerTestExpandFilterWPMUCallback
*/
function testExpandFilterWPMUCallback($_wpmu_version, $option_value, $expected_result) {
global $wpmu_version;
$wpmu_version = $_wpmu_version;
update_option('upload_path', $option_value);
$this->assertEquals($expected_result, _comicpress_expand_filter_callback('original', array('all', 'wordpress')));
}
function providerTestReadDirectory() {
return array(
array(vfsStream::url('root2/.*'), false),
array(vfsStream::url('root/.*'), array('2009-01-01.jpg', '2009-01-02.jpg', '2009-02-02-two.jpg', '2008-01-01.jpg')),
array(vfsStream::url('root/2009.*'), array('2009-01-01.jpg', '2009-01-02.jpg', '2009-02-02-two.jpg')),
array(vfsStream::url('root/2009-01.*'), array('2009-01-01.jpg', '2009-01-02.jpg')),
array(vfsStream::url('root/2009-01-01.*'), array('2009-01-01.jpg')),
);
}
/**
* @dataProvider providerTestReadDirectory
*/
function testReadDirectory($pattern, $expected_results) {
foreach (array('2009-01-01.jpg', '2009-01-02.jpg', '2009-02-02-two.jpg', '2008-01-01.jpg') as $file) {
file_put_contents(vfsStream::url("root/${file}"), 'file');
}
if (is_array($expected_results)) {
foreach ($expected_results as &$result) { $result = vfsStream::url("root/${result}"); }
}
$this->assertEquals($expected_results, $this->cpmh->_read_directory($pattern));
}
function providerTestResolveRegexPath() {
return array(
array('test', 'test'),
array('te\.st', 'te.st')
);
}
/**
* @dataProvider providerTestResolveRegexPath
*/
function testResolveRegexPath($input, $expected_output) {
$this->assertEquals($expected_output, $this->cpmh->_resolve_regex_path($input));
}
function providerTestPreHandleComicPathResults() {
return array(
array('', '', false),
array('1', '', 'comic/one'),
array('1', 'comic', 'files/one'),
);
}
/**
* @dataProvider providerTestPreHandleComicPathResults
*/
function testPreHandleComicPathResults($_wpmu_version, $upload_path, $expected_result) {
global $wpmu_version, $comic_folder;
$wpmu_version = $_wpmu_version;
$comic_folder = 'comic';
update_option('upload_path', $upload_path);
$this->assertEquals($expected_result, _comicpress_pre_handle_comic_path_results(false, array('one/one', 'two/two', 'three/three'), 'comic', (object)array('ID' => 1)));
}
}

View File

@ -10,9 +10,6 @@ class GraphicalNavigationWidgetTest extends PHPUnit_Framework_TestCase {
$this->w = new GraphicalNavigationWidget(); $this->w = new GraphicalNavigationWidget();
} }
/**
* @covers WidgetComicPressGraphicalStorylineNavigation::update
*/
function testUpdateWidget() { function testUpdateWidget() {
$result = $this->w->update(array( $result = $this->w->update(array(
"next" => "<b>test</b>", "next" => "<b>test</b>",
@ -41,7 +38,6 @@ class GraphicalNavigationWidgetTest extends PHPUnit_Framework_TestCase {
/** /**
* @dataProvider providerTestIsNavLinkVisible * @dataProvider providerTestIsNavLinkVisible
* @covers WidgetComicPressGraphicalStorylineNavigation::_will_display_nav_link
*/ */
function testIsNavLinkVisible($which, $current_id, $target_id, $expected_result) { function testIsNavLinkVisible($which, $current_id, $target_id, $expected_result) {
$current = (object)array('ID' => $current_id); $current = (object)array('ID' => $current_id);
@ -76,7 +72,6 @@ class GraphicalNavigationWidgetTest extends PHPUnit_Framework_TestCase {
/** /**
* @dataProvider providerTestGroupNavigationButtons * @dataProvider providerTestGroupNavigationButtons
* @covers WidgetComicPressGraphicalStorylineNavigation::_group_navigation_buttons
*/ */
function testGroupNavigationButtons($buttons, $expected_grouping) { function testGroupNavigationButtons($buttons, $expected_grouping) {
_set_filter_expectation('comicpress_navigation_grouping_details', array(array( _set_filter_expectation('comicpress_navigation_grouping_details', array(array(