start work on tag builder

This commit is contained in:
John Bintz 2010-01-27 20:21:53 -05:00
parent 229773a040
commit 6f5a5dc396
11 changed files with 205 additions and 27 deletions

View File

@ -31,7 +31,7 @@ class ComicPress {
), ),
'helpers' => array(), 'helpers' => array(),
'storyline_order' => '', 'storyline_order' => '',
'active_backends' => null 'enabled_backends' => null
); );
var $backends = array(); var $backends = array();
@ -156,19 +156,42 @@ class ComicPress {
$this->normalize_image_size_options(); $this->normalize_image_size_options();
$this->reset_backend_admin();
foreach ($this->normalize_active_backends() as $class) { foreach ($this->normalize_active_backends() as $class) {
$this->backends[] = new $class(); $this->backends[] = new $class();
$admin_class = preg_replace('#Factory$#', 'Admin', $class);
if (class_exists($admin_class)) {
if (method_exists($admin_class, 'actions')) {
foreach (call_user_func(array($admin_class, 'actions')) as $options) {
call_user_func_array('add_action', $options);
}
}
}
} }
} }
function reset_backend_admin() {
foreach ($this->_get_declared_classes() as $class) {
if (preg_match('#^ComicPressBackend(.*)Admin$#', $class)) {
if (method_exists($class, 'actions')) {
foreach (call_user_func(array($class, 'actions')) as $options) {
call_user_func_array('remove_action', $options);
}
}
}
}
}
function normalize_active_backends() { function normalize_active_backends() {
if (!is_array($this->comicpress_options['active_backends'])) { if (!is_array($this->comicpress_options['enabled_backends'])) {
$this->comicpress_options['active_backends'] = array(); $this->comicpress_options['enabled_backends'] = $this->get_valid_backends();
} }
$this->comicpress_options['active_backends'] = array_intersect($this->get_valid_backends(), $this->comicpress_options['active_backends']); $this->comicpress_options['enabled_backends'] = array_intersect($this->get_valid_backends(), $this->comicpress_options['enabled_backends']);
return $this->comicpress_options['active_backends']; return $this->comicpress_options['enabled_backends'];
} }
function get_valid_backends() { function get_valid_backends() {

View File

@ -167,6 +167,8 @@ class ComicPressAdmin {
$storyline->normalize(); $storyline->normalize();
$storyline->read_from_options(); $storyline->read_from_options();
$backends = $this->comicpress->get_valid_backends();
include(dirname(__FILE__) . '/partials/options-admin.inc'); include(dirname(__FILE__) . '/partials/options-admin.inc');
} }
@ -389,6 +391,9 @@ class ComicPressAdmin {
$storyline->normalize($info[$option]); $storyline->normalize($info[$option]);
break; break;
// @codeCoverageIgnoreEnd // @codeCoverageIgnoreEnd
case 'enabled_backends':
$this->comicpress->comicpress_options['enabled_backends'] = array_intersect(array_keys($info[$option]), $this->comicpress->get_valid_backends());
break;
} }
} }
} }

View File

@ -0,0 +1,49 @@
<?php
require_once('ComicPressStoryline.inc');
class ComicPressTagBuilderFactory {
private $storyline, $dbi;
public function __construct($dbi = null) {
$this->storyline = new ComicPressStoryline();
$this->storyline->read_from_options();
if (is_null($dbi)) {
$this->dbi = ComicPressDBInterface::get_instance();
} else {
$this->dbi = $dbi;
}
}
public function __call($method, $arguments) {
global $post;
$a = new ComicPressTagBuilder($post, $this->storyline, $this->dbi);
return call_user_func_array(array($a, $method), $arguments);
}
}
class ComicPressTagBuilder {
public $categories, $restrictions, $storyline, $dbi, $parent_post, $post;
public function __construct($parent_post, $storyline, $dbi) {
$this->restrictions = array();
$this->storyline = $storyline;
$this->dbi = $dbi;
$this->parent_post = $parent_post;
}
public function __call($method, $arguments) {
switch ($method) {
case 'next':
case 'previous':
case 'first':
case 'last':
$this->post = call_user_func(array($this->dbi, "get_${method}_post"), $this->storyline->build_from_restrictions($this->restrictions), $this->parent_post);
break;
}
return $this;
}
}

View File

@ -8,6 +8,10 @@ class ComicPressBackendFilesystem extends ComicPressBackend {
} }
class ComicPressBackendFilesystemFactory { class ComicPressBackendFilesystemFactory {
function __construct() {
$this->description = __('Uses files on the filesystem as comic images, similar to ComicPress Legacy.', 'comicpress');
}
function generate_from_id($id) { function generate_from_id($id) {
if (preg_match('#^filesystem-([0-9]+)-(.*)$#', $id, $matches) > 0) { if (preg_match('#^filesystem-([0-9]+)-(.*)$#', $id, $matches) > 0) {
list($all, $post_id, $root) = $matches; list($all, $post_id, $root) = $matches;
@ -239,10 +243,6 @@ class ComicPressBackendFilesystemFactory {
// @codeCoverageIgnoreStart // @codeCoverageIgnoreStart
class ComicPressBackendFilesystemAdmin { class ComicPressBackendFilesystemAdmin {
function __construct() {
$this->description = __('Uses files on the filesystem as comic images, similar to ComicPress Legacy.', 'comicpress');
}
function options_admin() { function options_admin() {
$pattern = ComicPressBackendFilesystemFactory::_get_search_pattern(); $pattern = ComicPressBackendFilesystemFactory::_get_search_pattern();
@ -287,9 +287,13 @@ class ComicPressBackendFilesystemAdmin {
$comicpress->save(); $comicpress->save();
} }
} }
function actions() {
return array(
array('comicpress-options-admin', array('ComicPressBackendFilesystemAdmin', 'options_admin')),
array('comicpress-image-type-holder', array('ComicPressBackendFilesystemAdmin', 'image_type_holder'), 10, 1),
array('comicpress-handle_update_comicpress_options', array('ComicPressBackendFilesystemAdmin', 'handle_update_comicpress_options'), 10, 1)
);
}
} }
// @codeCoverageIgnoreEnd // @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

@ -149,8 +149,12 @@ class ComicPressBackendURLAdmin {
} }
} }
// @codeCoverageIgnoreEnd // @codeCoverageIgnoreEnd
}
add_action('admin_menu', array('ComicPressBackendURLAdmin', 'admin_menu')); function actions() {
add_action('comicpress-handle_update_backend_url_new_editor', array('ComicPressBackendURLAdmin', 'handle_update_backend_url_new_editor')); return array(
add_action('save_post', array('ComicPressBackendURLAdmin', 'save_post'), 10, 1); array('admin_menu', array('ComicPressBackendURLAdmin', 'admin_menu')),
array('comicpress-handle_update_backend_url_new_editor', array('ComicPressBackendURLAdmin', 'handle_update_backend_url_new_editor')),
array('save_post', array('ComicPressBackendURLAdmin', 'save_post'), 10, 1)
);
}
}

View File

@ -1,5 +1,5 @@
<div class="wrap comicpress-admin"> <div class="wrap comicpress-admin">
<h2><?php _e('ComicPress Config', 'comicpress') ?></h2> <h2><?php _e('ComicPress Core Config', 'comicpress') ?></h2>
<form method="post" action=""> <form method="post" action="">
<input type="hidden" name="cp[_nonce]" value="<?php echo esc_attr($nonce) ?>" /> <input type="hidden" name="cp[_nonce]" value="<?php echo esc_attr($nonce) ?>" />
<input type="hidden" name="cp[action]" value="comicpress-options" /> <input type="hidden" name="cp[action]" value="comicpress-options" />
@ -9,8 +9,32 @@
<?php include('_comic-image-types.inc') ?> <?php include('_comic-image-types.inc') ?>
<h3><?php _e('Active Components', 'comicpress') ?></h3> <h3><?php _e('Enabled Backends', 'comicpress') ?></h3>
<div id="comicpress-active-components-holder" class="comicpress-holder"> <div id="comicpress-enabled-backends-holder" class="comicpress-holder">
<table class="widefat">
<?php foreach ($backends as $backend) {
$obj = new $backend(); ?>
<tr>
<th scope="row">
<label>
<input type="checkbox"
name="cp[enabled_backends][<?php echo esc_attr($backend) ?>]"
value="yes"
<?php echo in_array($backend, $this->comicpress->comicpress_options['enabled_backends']) ? 'checked="checked"' : '' ?> />
<?php echo esc_html(preg_replace('#^ComicPressBackend(.*)Factory$#', '\1', $backend)) ?>
</label>
</th>
<td>
<?php echo esc_html($obj->description) ?>
</td>
</tr>
<?php } ?>
</table>
<p>
<?php _e("Disable backends you're not using to save system resources.", 'comicpress') ?>
</p>
<input class="button-primary" type="submit" value="<?php _e('Submit Updated ComicPress Options', 'comicpress') ?>" />
</div> </div>
<?php do_action('comicpress-options-admin') ?> <?php do_action('comicpress-options-admin') ?>

View File

@ -51,5 +51,3 @@ function __comicpress_init() {
$comicpress_filters = new ComicPressFilters(); $comicpress_filters = new ComicPressFilters();
$comicpress_filters->init(); $comicpress_filters->init();
} }

View File

@ -114,7 +114,21 @@ class ComicPressAdminTest extends PHPUnit_Framework_TestCase {
'image_types' => array( 'image_types' => array(
'newcomic' => array('default' => true, 'dimensions' => '100x100'), 'newcomic' => array('default' => true, 'dimensions' => '100x100'),
) )
) ),
),
array(
array(
'enabled_backends' => array()
),
array(
'enabled_backends' => array(
'ComicPressBackendURLFactory' => 'yes',
'BadBackEnd' => 'yes'
)
),
array(
'enabled_backends' => array('ComicPressBackendURLFactory')
),
) )
); );
} }

View File

@ -0,0 +1,54 @@
<?php
require_once('PHPUnit/Framework.php');
require_once('MockPress/mockpress.php');
require_once(dirname(__FILE__) . '/../classes/ComicPressTagBuilder.inc');
require_once(dirname(__FILE__) . '/../classes/ComicPressStoryline.inc');
class ComicPressTagBuilderTest extends PHPUnit_Framework_TestCase {
function setUp() {
_reset_wp();
}
function providerTestBuilder() {
return array(
array(
array('next'),
array('get_next_post', array(1,2,3,4,5), 'current-post')
)
);
}
/**
* @dataProvider providerTestBuilder
*/
function testStorylineBuilder($instructions, $expected_dbi_call) {
global $post;
$post = 'current-post';
$method = array_shift($expected_dbi_call);
$dbi = $this->getMock('ComicPressDBInterface', array($method));
$expectation = $dbi->expects($this->once())->method($method);
call_user_func_array(array($expectation, 'with'), $expected_dbi_call);
$core = new ComicPressTagBuilderFactory($dbi);
$storyline = new ComicPressStoryline();
$storyline->set_flattened_storyline('0/1,0/2,0/2/3,0/2/4,0/5');
foreach (array(
array('cat_ID' => 1, 'cat_name' => 'Test 1', 'category_nicename' => 'category-1', 'category_parent' => 0),
array('cat_ID' => 2, 'cat_name' => 'Test 2', 'category_nicename' => 'category-2', 'category_parent' => 0),
array('cat_ID' => 3, 'cat_name' => 'Test 3', 'category_nicename' => 'category-3', 'category_parent' => 2),
array('cat_ID' => 4, 'cat_name' => 'Test 4', 'category_nicename' => 'category-4', 'category_parent' => 2),
array('cat_ID' => 5, 'cat_name' => 'Test 5', 'category_nicename' => 'category-5', 'category_parent' => 0),
) as $category) {
wp_insert_category($category);
}
foreach ($instructions as $instruction) {
$core = $core->{$instruction}();
}
}
}

View File

@ -323,7 +323,7 @@ class ComicPressTest extends PHPUnit_Framework_TestCase {
function testInit() { function testInit() {
$cp = $this->getMock('ComicPress', array('load', 'normalize_image_size_options', 'normalize_active_backends')); $cp = $this->getMock('ComicPress', array('load', 'normalize_image_size_options', 'normalize_active_backends'));
$cp->comicpress_options = array( $cp->comicpress_options = array(
'active_backends' => array('ComicPressBackendURLFactory') 'enabled_backends' => array('ComicPressBackendURLFactory')
); );
$cp->expects($this->once())->method('load'); $cp->expects($this->once())->method('load');
@ -360,12 +360,12 @@ class ComicPressTest extends PHPUnit_Framework_TestCase {
*/ */
function testNormalizeActiveBackends($available_backends, $enabled_backends, $expected_backends) { function testNormalizeActiveBackends($available_backends, $enabled_backends, $expected_backends) {
$cp = $this->getMock('ComicPress', array('_get_declared_classes')); $cp = $this->getMock('ComicPress', array('_get_declared_classes'));
$cp->comicpress_options['active_backends'] = $enabled_backends; $cp->comicpress_options['enabled_backends'] = $enabled_backends;
$cp->expects($this->once())->method('_get_declared_classes')->will($this->returnValue($available_backends)); $cp->expects($this->any())->method('_get_declared_classes')->will($this->returnValue($available_backends));
$cp->normalize_active_backends(); $cp->normalize_active_backends();
$this->assertEquals($expected_backends, $cp->comicpress_options['active_backends']); $this->assertEquals($expected_backends, $cp->comicpress_options['enabled_backends']);
} }
} }

3
test/fixtures/reset-options.inc vendored Normal file
View File

@ -0,0 +1,3 @@
<?php
$builder->option('comicpress-options', false);