diff --git a/classes/ComicPressMediaHandling.inc b/classes/ComicPressMediaHandling.inc new file mode 100644 index 0000000..887c1aa --- /dev/null +++ b/classes/ComicPressMediaHandling.inc @@ -0,0 +1,150 @@ +_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; + } +} diff --git a/functions.php b/functions.php index 6f0f669..8f63e5c 100644 --- a/functions.php +++ b/functions.php @@ -75,7 +75,7 @@ function comicpress_load_options() { global $comicpress_options; $comicpress_options = get_option('comicpress_options'); if (empty($comicpress_options)) { - $comicpress_options['comicpress_version'] = '2.9.0.4'; + $comicpress_options['comicpress_version'] = '2.9.0.4'; foreach (array( 'disable_comic_frontpage' => false, 'disable_comic_blog_frontpage' => false, @@ -181,11 +181,9 @@ if ($comicpress_options['remove_wptexturize']) { // WIDGETS WP 2.8 compatible ONLY, no backwards compatibility here. $dirs_to_search = array_unique(array(get_template_directory(),get_stylesheet_directory())); foreach ($dirs_to_search as $dir) { - // Widgets - foreach (glob($dir . '/widgets/*.php') as $__file) { require_once($__file); } - - // FUNCTIONS & Extra's - foreach (glob($dir . '/functions/*.php') as $__file) { require_once($__file); } + foreach (array('widgets' => 'php', 'functions' => 'php', 'classes' => 'inc') as $folder => $extension) { + foreach (glob($dir . "/${folder}/*.${extension}") as $__file) { require_once($__file); } + } } // 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. $comic_filename_filters = array(); -$comic_filename_filters['default'] = "{date}*.*"; // load all of the comic & non-comic category information 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) { - global $post, $comic_filename_filters, $comic_folder, $archive_comic_folder, $rss_comic_folder, $mini_comic_folder, $comic_pathfinding_errors, $wpmu_version; - - 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 %s folder that matched the pattern %s. Check your WordPress and ComicPress settings.", 'comicpress'), $folder_to_use, $filter_with_date); - return false; + $mh = new ComicPressMediaHandling(); + return $mh->get_comic_path($folder, $override_post, $filter, $multi); } diff --git a/functions/wpmu.php b/functions/wpmu.php new file mode 100644 index 0000000..f2651dd --- /dev/null +++ b/functions/wpmu.php @@ -0,0 +1,33 @@ +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))); + } +} diff --git a/test/widgets/GraphicalNavigationWidgetTest.php b/test/widgets/GraphicalNavigationWidgetTest.php index cab99c2..709acf4 100644 --- a/test/widgets/GraphicalNavigationWidgetTest.php +++ b/test/widgets/GraphicalNavigationWidgetTest.php @@ -10,9 +10,6 @@ class GraphicalNavigationWidgetTest extends PHPUnit_Framework_TestCase { $this->w = new GraphicalNavigationWidget(); } - /** - * @covers WidgetComicPressGraphicalStorylineNavigation::update - */ function testUpdateWidget() { $result = $this->w->update(array( "next" => "test", @@ -41,7 +38,6 @@ class GraphicalNavigationWidgetTest extends PHPUnit_Framework_TestCase { /** * @dataProvider providerTestIsNavLinkVisible - * @covers WidgetComicPressGraphicalStorylineNavigation::_will_display_nav_link */ function testIsNavLinkVisible($which, $current_id, $target_id, $expected_result) { $current = (object)array('ID' => $current_id); @@ -76,7 +72,6 @@ class GraphicalNavigationWidgetTest extends PHPUnit_Framework_TestCase { /** * @dataProvider providerTestGroupNavigationButtons - * @covers WidgetComicPressGraphicalStorylineNavigation::_group_navigation_buttons */ function testGroupNavigationButtons($buttons, $expected_grouping) { _set_filter_expectation('comicpress_navigation_grouping_details', array(array(