diff --git a/classes/ComicPress.inc b/classes/ComicPress.inc index 17ae07d..cadcbc1 100644 --- a/classes/ComicPress.inc +++ b/classes/ComicPress.inc @@ -31,7 +31,7 @@ class ComicPress { ), 'helpers' => array(), 'storyline_order' => '', - 'active_backends' => null + 'enabled_backends' => null ); var $backends = array(); @@ -156,19 +156,42 @@ class ComicPress { $this->normalize_image_size_options(); + $this->reset_backend_admin(); + foreach ($this->normalize_active_backends() as $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() { - if (!is_array($this->comicpress_options['active_backends'])) { - $this->comicpress_options['active_backends'] = array(); + if (!is_array($this->comicpress_options['enabled_backends'])) { + $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() { diff --git a/classes/ComicPressAdmin.inc b/classes/ComicPressAdmin.inc index b4222e6..ba9c0d9 100644 --- a/classes/ComicPressAdmin.inc +++ b/classes/ComicPressAdmin.inc @@ -167,6 +167,8 @@ class ComicPressAdmin { $storyline->normalize(); $storyline->read_from_options(); + $backends = $this->comicpress->get_valid_backends(); + include(dirname(__FILE__) . '/partials/options-admin.inc'); } @@ -389,6 +391,9 @@ class ComicPressAdmin { $storyline->normalize($info[$option]); break; // @codeCoverageIgnoreEnd + case 'enabled_backends': + $this->comicpress->comicpress_options['enabled_backends'] = array_intersect(array_keys($info[$option]), $this->comicpress->get_valid_backends()); + break; } } } diff --git a/classes/ComicPressTagBuilder.inc b/classes/ComicPressTagBuilder.inc new file mode 100644 index 0000000..855e145 --- /dev/null +++ b/classes/ComicPressTagBuilder.inc @@ -0,0 +1,49 @@ +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; + } +} diff --git a/classes/backends/ComicPressBackendFilesystem.inc b/classes/backends/ComicPressBackendFilesystem.inc index f014177..a6824fc 100644 --- a/classes/backends/ComicPressBackendFilesystem.inc +++ b/classes/backends/ComicPressBackendFilesystem.inc @@ -8,6 +8,10 @@ class ComicPressBackendFilesystem extends ComicPressBackend { } class ComicPressBackendFilesystemFactory { + function __construct() { + $this->description = __('Uses files on the filesystem as comic images, similar to ComicPress Legacy.', 'comicpress'); + } + function generate_from_id($id) { if (preg_match('#^filesystem-([0-9]+)-(.*)$#', $id, $matches) > 0) { list($all, $post_id, $root) = $matches; @@ -239,10 +243,6 @@ class ComicPressBackendFilesystemFactory { // @codeCoverageIgnoreStart class ComicPressBackendFilesystemAdmin { - function __construct() { - $this->description = __('Uses files on the filesystem as comic images, similar to ComicPress Legacy.', 'comicpress'); - } - function options_admin() { $pattern = ComicPressBackendFilesystemFactory::_get_search_pattern(); @@ -287,9 +287,13 @@ class ComicPressBackendFilesystemAdmin { $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 - -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); diff --git a/classes/backends/ComicPressBackendURL.inc b/classes/backends/ComicPressBackendURL.inc index 774b30f..d9d6cef 100644 --- a/classes/backends/ComicPressBackendURL.inc +++ b/classes/backends/ComicPressBackendURL.inc @@ -149,8 +149,12 @@ class ComicPressBackendURLAdmin { } } // @codeCoverageIgnoreEnd -} -add_action('admin_menu', array('ComicPressBackendURLAdmin', 'admin_menu')); -add_action('comicpress-handle_update_backend_url_new_editor', array('ComicPressBackendURLAdmin', 'handle_update_backend_url_new_editor')); -add_action('save_post', array('ComicPressBackendURLAdmin', 'save_post'), 10, 1); + function actions() { + return array( + 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) + ); + } +} diff --git a/classes/partials/options-admin.inc b/classes/partials/options-admin.inc index b4e0bf4..b3324ad 100644 --- a/classes/partials/options-admin.inc +++ b/classes/partials/options-admin.inc @@ -1,5 +1,5 @@
-

+

@@ -9,8 +9,32 @@ -

-
+

+
+ + + + + + + +
+ + + description) ?> +
+ +

+ +

+
diff --git a/comicpress-core.php b/comicpress-core.php index 34f6918..7eb5450 100644 --- a/comicpress-core.php +++ b/comicpress-core.php @@ -51,5 +51,3 @@ function __comicpress_init() { $comicpress_filters = new ComicPressFilters(); $comicpress_filters->init(); } - - diff --git a/test/ComicPressAdminTest.php b/test/ComicPressAdminTest.php index 6ecbd92..0ed9642 100644 --- a/test/ComicPressAdminTest.php +++ b/test/ComicPressAdminTest.php @@ -114,7 +114,21 @@ class ComicPressAdminTest extends PHPUnit_Framework_TestCase { 'image_types' => array( 'newcomic' => array('default' => true, 'dimensions' => '100x100'), ) - ) + ), + ), + array( + array( + 'enabled_backends' => array() + ), + array( + 'enabled_backends' => array( + 'ComicPressBackendURLFactory' => 'yes', + 'BadBackEnd' => 'yes' + ) + ), + array( + 'enabled_backends' => array('ComicPressBackendURLFactory') + ), ) ); } diff --git a/test/ComicPressTagBuilderTest.php b/test/ComicPressTagBuilderTest.php new file mode 100644 index 0000000..f0aa493 --- /dev/null +++ b/test/ComicPressTagBuilderTest.php @@ -0,0 +1,54 @@ +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}(); + } + } +} diff --git a/test/ComicPressTest.php b/test/ComicPressTest.php index c66f469..9a8421e 100644 --- a/test/ComicPressTest.php +++ b/test/ComicPressTest.php @@ -323,7 +323,7 @@ class ComicPressTest extends PHPUnit_Framework_TestCase { function testInit() { $cp = $this->getMock('ComicPress', array('load', 'normalize_image_size_options', 'normalize_active_backends')); $cp->comicpress_options = array( - 'active_backends' => array('ComicPressBackendURLFactory') + 'enabled_backends' => array('ComicPressBackendURLFactory') ); $cp->expects($this->once())->method('load'); @@ -360,12 +360,12 @@ class ComicPressTest extends PHPUnit_Framework_TestCase { */ function testNormalizeActiveBackends($available_backends, $enabled_backends, $expected_backends) { $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(); - $this->assertEquals($expected_backends, $cp->comicpress_options['active_backends']); + $this->assertEquals($expected_backends, $cp->comicpress_options['enabled_backends']); } } diff --git a/test/fixtures/reset-options.inc b/test/fixtures/reset-options.inc new file mode 100644 index 0000000..d623fe8 --- /dev/null +++ b/test/fixtures/reset-options.inc @@ -0,0 +1,3 @@ +option('comicpress-options', false);