From 2d3c1407ff6766f889fa5d37257bb4d5b75566bc Mon Sep 17 00:00:00 2001 From: John Bintz Date: Thu, 31 Dec 2009 19:20:39 -0500 Subject: [PATCH] rework date handling in filesystem backend --- .../backends/ComicPressBackendFilesystem.inc | 34 ++++++++++++------- ...ComicPressBackendFilesystemFactoryTest.php | 10 +++--- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/classes/backends/ComicPressBackendFilesystem.inc b/classes/backends/ComicPressBackendFilesystem.inc index 48bf3fd..e31f143 100644 --- a/classes/backends/ComicPressBackendFilesystem.inc +++ b/classes/backends/ComicPressBackendFilesystem.inc @@ -77,16 +77,25 @@ class ComicPressBackendFilesystemFactory { 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; + $found = false; + $parts = explode('-', $matches[1]); + foreach (array( + '_replace_' . strtolower(str_replace('-', '_', $matches[1])) => null, + '_replace_' . strtolower($parts[0]) => implode('-', array_slice($parts, 1)) + ) as $method => $additional) { + if (method_exists($this, $method)) { + $found = true; + $result = $this->{$method}($post, $type, $additional); + if ($result !== false) { + $this->_searches[$i] = str_replace($matches[0], $result, $search); + } else { + // array state change, start over + break; + } } - } else { + } + + if (!$found) { $this->_searches[$i] = str_replace($matches[0], '', $search); } } @@ -104,9 +113,10 @@ class ComicPressBackendFilesystemFactory { // @codeCoverageIgnoreEnd 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_ymd($post, $type) { return date('Ymd', strtotime($post->post_date)); } - function _replace_year($post, $type) { return date('Y', strtotime($post->post_date)); } + function _replace_date($post, $type, $additional) { + return date($additional, 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); diff --git a/test/backends/ComicPressBackendFilesystemFactoryTest.php b/test/backends/ComicPressBackendFilesystemFactoryTest.php index 2018132..6bdb772 100644 --- a/test/backends/ComicPressBackendFilesystemFactoryTest.php +++ b/test/backends/ComicPressBackendFilesystemFactoryTest.php @@ -99,18 +99,18 @@ class ComicPressBackendFilesystemFactoryTest extends PHPUnit_Framework_TestCase array('%wordpress%/comic/*.jpg', array('/wordpress/comic/*.jpg')), array('%test%/comic/*.jpg', array('/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/%ymd%*.jpg', array('/wordpress/comic/20090101*.jpg')), - array('%wordpress%/comic/%year%/%y-m-d%*.jpg', array('/wordpress/comic/2009/2009-01-01*.jpg')), + array('%wordpress%/comic/%date-Y-m-d%*.jpg', array('/wordpress/comic/2009-01-01*.jpg')), + array('%wordpress%/comic/%date-Ymd%*.jpg', array('/wordpress/comic/20090101*.jpg')), + array('%wordpress%/comic/%date-Y%/%date-Y-m-d%*.jpg', array('/wordpress/comic/2009/2009-01-01*.jpg')), array( - '%wordpress%/comic/%categories%/%y-m-d%*.jpg', + '%wordpress%/comic/%categories%/%date-Y-m-d%*.jpg', array( '/wordpress/comic/parent/child/2009-01-01*.jpg', '/wordpress/comic/parent/2009-01-01*.jpg', ) ), array( - '%wordpress%/comic/%categories%/%y-m-d%*.jpg', + '%wordpress%/comic/%categories%/%date-Y-m-d%*.jpg', array( '/wordpress/comic//2009-01-01*.jpg', ),