From bc964971f76fe91593dc29fc37cbbfb83e3992c9 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sat, 4 Jul 2009 12:08:02 -0400 Subject: [PATCH] start work on theme options page --- functions.php | 11 ++++++-- options.php | 36 +++++++++++++++++++++++++ test/OptionsPageTest.php | 58 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 options.php create mode 100644 test/OptionsPageTest.php diff --git a/functions.php b/functions.php index da84cbe..d5e413d 100644 --- a/functions.php +++ b/functions.php @@ -30,8 +30,15 @@ if (defined("CPM_DATE_FORMAT")) { $comic_filename_filters = array(); $comic_filename_filters['default'] = "{date}*.*"; +require_once(dirname(__FILE__) . '/options.php'); + // load all of the comic & non-comic category information -add_action('init', 'get_all_comic_categories'); +add_action('init', '__comicpress_init'); + +function __comicpress_init() { + get_all_comic_categories(); + add_action('admin_init', '__comicpress_add_options_admin'); +} function get_first_comic() { return get_terminal_post_in_category(get_all_comic_categories_as_cat_string()); @@ -635,4 +642,4 @@ function szub_is_search_key($key='') { return false; } -?> \ No newline at end of file +?> diff --git a/options.php b/options.php new file mode 100644 index 0000000..554ade3 --- /dev/null +++ b/options.php @@ -0,0 +1,36 @@ +'; + echo '

ComicPress Config

'; + echo '
'; + echo ''; + echo '
'; + echo ''; + } + + function get_root_categories() { + $root_categories = array(); + foreach (get_all_category_ids() as $id) { + $category = get_category($id); + if (!empty($category)) { + if ($category->parent == 0) { + $root_categories[] = $category; + } + } + } + return $root_categories; + } +} + +$comicpress_options_admin = new ComicPressOptionsAdmin(); + +function __comicpress_add_options_admin() { + global $comicpress_options_admin; + add_theme_page(__("ComicPress Options", 'comicpress'), __('ComicPress Options', 'comicpress'), 'edit_themes', basename(__FILE__), array($comicpress_options_admin, 'render_admin')); +} + +?> diff --git a/test/OptionsPageTest.php b/test/OptionsPageTest.php new file mode 100644 index 0000000..a2c88a9 --- /dev/null +++ b/test/OptionsPageTest.php @@ -0,0 +1,58 @@ +admin = new ComicPressOptionsAdmin(); + } + + function testShowOptionsPage() { + $nonce = wp_create_nonce('comicpress'); + + ob_start(); + $this->admin->render_admin(); + $source = ob_get_clean(); + + $this->assertTrue(($xml = _to_xml($source)) !== false); + foreach (array( + '//input[@name="cp[_nonce]" and @value="' . $nonce . '"]' => true + ) as $xpath => $value) { + $this->assertTrue(_xpath_test($xml, $xpath, $value), $xpath); + } + } + + function providerTestGetRootComicCategories() { + return array( + array(array(), array()), + array( + array( + array('id' => 1, 'parent' => 0), + array('id' => 2, 'parent' => 1) + ), + array(1) + ) + ); + } + + /** + * @dataProvider providerTestGetRootComicCategories + */ + function testGetRootCategories($categories, $expected_result) { + foreach ($categories as $category) { + add_category($category['id'], (object)$category); + } + + $result_ids = array(); + foreach ($this->admin->get_root_categories() as $category) { + $result_ids[] = $category->term_id; + } + + $this->assertEquals($expected_result, $result_ids); + } +} + +?>