ui for filesystem backend

This commit is contained in:
John Bintz 2010-01-01 16:35:48 -05:00
parent 3d9b1033c5
commit e7afb296ee
7 changed files with 158 additions and 9 deletions

View File

@ -1,5 +1,7 @@
<?php <?php
// TODO Be able to disable backends
class ComicPressAdmin { class ComicPressAdmin {
// @codeCoverageIgnoreStart // @codeCoverageIgnoreStart
/** /**

View File

@ -24,17 +24,23 @@ class ComicPressBackendFilesystemFactory {
return false; return false;
} }
function _get_search_pattern() {
$comicpress = ComicPress::get_instance();
if (isset(
$comicpress->comicpress_options['backend_options']['filesystem']['search_pattern']
)) {
return (string)$comicpress->comicpress_options['backend_options']['filesystem']['search_pattern'];
}
return '';
}
function generate_from_post($post) { function generate_from_post($post) {
$return = array(); $return = array();
$comicpress = ComicPress::get_instance(); $comicpress = ComicPress::get_instance();
if (isset( $this->search_pattern = $this->_get_search_pattern();
$comicpress->comicpress_options['backend_options'],
$comicpress->comicpress_options['backend_options']['filesystem'],
$comicpress->comicpress_options['backend_options']['filesystem']['search_pattern']
)) {
$this->search_pattern = (string)$comicpress->comicpress_options['backend_options']['filesystem']['search_pattern'];
}
if (isset($comicpress->comicpress_options['image_types'])) { if (isset($comicpress->comicpress_options['image_types'])) {
$files = array(); $files = array();
@ -66,7 +72,6 @@ class ComicPressBackendFilesystemFactory {
return $return; return $return;
} }
// TODO Make this more generic (ex: date-Ymd calls _replace_date($post, $type, "Ymd"))
function process_search_string($post, $type) { function process_search_string($post, $type) {
$this->_searches = array($this->search_string); $this->_searches = array($this->search_string);
@ -88,6 +93,7 @@ class ComicPressBackendFilesystemFactory {
$result = $this->{$method}($post, $type, $additional); $result = $this->{$method}($post, $type, $additional);
if ($result !== false) { if ($result !== false) {
$this->_searches[$i] = str_replace($matches[0], $result, $search); $this->_searches[$i] = str_replace($matches[0], $result, $search);
break;
} else { } else {
// array state change, start over // array state change, start over
break; break;
@ -113,6 +119,16 @@ class ComicPressBackendFilesystemFactory {
// @codeCoverageIgnoreEnd // @codeCoverageIgnoreEnd
function _replace_type($post, $type) { return $type; } function _replace_type($post, $type) { return $type; }
function _replace_type_folder($post, $type) {
$comicpress = ComicPress::get_instance();
if (isset($comicpress->comicpress_options['backend_options']['filesystem']['folders'][$type])) {
return $comicpress->comicpress_options['backend_options']['filesystem']['folders'][$type];
}
return false;
}
function _replace_date($post, $type, $additional) { function _replace_date($post, $type, $additional) {
return date($additional, strtotime($post->post_date)); return date($additional, strtotime($post->post_date));
} }
@ -218,3 +234,56 @@ class ComicPressBackendFilesystemFactory {
return $roots; return $roots;
} }
} }
// @codeCoverageIgnoreStart
class ComicPressBackendFilesystemAdmin {
function options_admin() {
$pattern = ComicPressBackendFilesystemFactory::_get_search_pattern();
include('partials/backend-filesystem/options-admin.inc');
}
function image_type_holder($type) {
$comicpress = ComicPress::get_instance();
$path = '';
if (
isset($comicpress->comicpress_options['backend_options']['filesystem']['folders'][$type])
) {
$path = $comicpress->comicpress_options['backend_options']['filesystem']['folders'][$type];
}
include('partials/backend-filesystem/image-type-holder.inc');
}
function handle_update_comicpress_options($info) {
if (isset($info['backend_options']['filesystem'])) {
$info = $info['backend_options']['filesystem'];
$comicpress = ComicPress::get_instance();
if (!isset($comicpress->comicpress_options['backend_options'])) {
$comicpress->comicpress_options['backend_options'] = array();
}
if (!isset($comicpress->comicpress_options['backend_options']['filesystem'])) {
$comicpress->comicpress_options['backend_options']['filesystem'] = array();
}
foreach (array('folders', 'search_pattern') as $valid_field) {
if (is_array($info[$valid_field])) {
$comicpress->comicpress_options['backend_options']['filesystem'][$valid_field] = array();
foreach ($info[$valid_field] as $field => $value) {
$comicpress->comicpress_options['backend_options']['filesystem'][$valid_field][$field] = strip_tags($value);
}
} else {
$comicpress->comicpress_options['backend_options']['filesystem'][$valid_field] = strip_tags($info[$valid_field]);
}
}
$comicpress->save();
}
}
}
// @codeCoverageIgnoreEnd
add_action('comicpress-options-admin', array('ComicPressBackendFilesystemAdmin', 'options_admin'));
add_action('comicpress-image-type-holder', array('ComicPressBackendFilesystemAdmin', 'image_type_holder'), 10, 1);
add_action('comicpress-handle_update_comicpress_options', array('ComicPressBackendFilesystemAdmin', 'handle_update_comicpress_options'), 10, 1);

View File

@ -0,0 +1,7 @@
<tr>
<th scope="row" valign="top"><?php _e('Directory', 'comicpress') ?></th>
<td>
<input type="text" name="cp[backend_options][filesystem][folders][<?php echo esc_attr($type) ?>]" value="<?php echo esc_attr($path) ?>" />
<br /><em><?php _e('(the folder path to fill in when using %type-folder%)', 'comicpress') ?></em>
</td>
</tr>

View File

@ -0,0 +1,24 @@
<h3><?php _e('URL Backend Configuration', 'comicpress') ?></h3>
<div id="comicpress-url-backend-configuration" class="comicpress-holder">
<table width="100%">
<tr>
<th scope="row">
<?php _e('URL Pattern:', 'comicpress') ?>
</th>
<td>
<input style="font-size: 1.5em; width: 100%" type="text" name="cp[backend_options][filesystem][search_pattern]" value="<?php echo $pattern ?>" />
</td>
</tr>
</table>
<p>
<?php _e('<strong>URL Pattern</strong> is similar to how WordPress permalinks are constructed:', 'comicpress') ?>
</p>
<ul>
<li><?php _e('<strong>%wordpress%</strong>: the local path to the WordPress installation', 'comicpress') ?></li>
<li><?php _e('<strong>%type%</strong>: the image type short name', 'comicpress') ?></li>
<li><?php _e('<strong>%type-folder%</strong>: the image type folder', 'comicpress') ?></li>
<li><?php _e('<strong>%date-(pattern)%</strong>: the date of the post as run through the date() function. Ex: <em>%date-Y-m-d%</em>', 'comicpress') ?></li>
</ul>
<input class="button-primary" type="submit" value="<?php _e('Submit Updated ComicPress Options', 'comicpress') ?>" />
</div>

View File

@ -28,6 +28,6 @@
<em>(<?php _e('Leave a dimension blank to allow WordPress to calculate that dimension while scaling', 'comicpress') ?>)</em> <em>(<?php _e('Leave a dimension blank to allow WordPress to calculate that dimension while scaling', 'comicpress') ?>)</em>
</td> </td>
</tr> </tr>
<?php do_action('comicpress_image_type_holder') ?> <?php do_action('comicpress-image-type-holder', $type) ?>
</table> </table>
</div> </div>

View File

@ -35,5 +35,7 @@
<input class="button-primary" type="submit" value="<?php _e('Submit Updated ComicPress Options', 'comicpress') ?>" /> <input class="button-primary" type="submit" value="<?php _e('Submit Updated ComicPress Options', 'comicpress') ?>" />
</div> </div>
<?php do_action('comicpress-options-admin') ?>
</form> </form>
</div> </div>

View File

@ -99,6 +99,7 @@ class ComicPressBackendFilesystemFactoryTest extends PHPUnit_Framework_TestCase
array('%wordpress%/comic/*.jpg', array('/wordpress/comic/*.jpg')), array('%wordpress%/comic/*.jpg', array('/wordpress/comic/*.jpg')),
array('%test%/comic/*.jpg', array('/comic/*.jpg')), array('%test%/comic/*.jpg', array('/comic/*.jpg')),
array('%wordpress%/%type%/*.jpg', array('/wordpress/comic/*.jpg')), array('%wordpress%/%type%/*.jpg', array('/wordpress/comic/*.jpg')),
array('%wordpress%/%type-folder%/*.jpg', array('/wordpress/comic-folder/*.jpg')),
array('%wordpress%/comic/%date-Y-m-d%*.jpg', array('/wordpress/comic/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-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/%date-Y%/%date-Y-m-d%*.jpg', array('/wordpress/comic/2009/2009-01-01*.jpg')),
@ -141,6 +142,11 @@ class ComicPressBackendFilesystemFactoryTest extends PHPUnit_Framework_TestCase
$fs->search_string = $string; $fs->search_string = $string;
$comicpress = ComicPress::get_instance(true);
$comicpress->comicpress_options = array(
'backend_options' => array('filesystem' => array('folders' => array('comic' => 'comic-folder')))
);
$this->assertEquals($expected_searches, $fs->process_search_string($posts[$post_id_to_use], 'comic')); $this->assertEquals($expected_searches, $fs->process_search_string($posts[$post_id_to_use], 'comic'));
} }
@ -255,4 +261,43 @@ class ComicPressBackendFilesystemFactoryTest extends PHPUnit_Framework_TestCase
function testGetRegexFilename($input, $expected_output) { function testGetRegexFilename($input, $expected_output) {
$this->assertEquals($expected_output, $this->fa->get_regex_filename($input)); $this->assertEquals($expected_output, $this->fa->get_regex_filename($input));
} }
function providerTestGetSearchPattern() {
return array(
array(false, ''),
array(true, 'test')
);
}
/**
* @dataProvider providerTestGetSearchPattern
*/
function testGetSearchPattern($set_pattern, $expected_result) {
$comicpress = ComicPress::get_instance(true);
if ($set_pattern) {
$comicpress->comicpress_options = array(
'backend_options' => array('filesystem' => array('search_pattern' => 'test'))
);
}
$this->assertEquals($expected_result, $this->fa->_get_search_pattern());
}
function providerTestReplaceTypeFolder() {
return array(
array('comic', 'comic'),
array('rss', false)
);
}
/**
* @dataProvider providerTestReplaceTypeFolder
*/
function testReplaceTypeFolder($type, $expected_result) {
$comicpress = ComicPress::get_instance(true);
$comicpress->comicpress_options = array(
'backend_options' => array('filesystem' => array('folders' => array('comic' => 'comic')))
);
$this->assertEquals($expected_result, $this->fa->_replace_type_folder(null, $type));
}
} }