diff --git a/addons/Core/Core.inc b/addons/Core/Core.inc new file mode 100644 index 0000000..5bc66f3 --- /dev/null +++ b/addons/Core/Core.inc @@ -0,0 +1,115 @@ +comicpress = $comicpress; + } + + function add_theme_page() { + add_theme_page(__("ComicPress Core", 'comicpress'), __('ComicPress Core', 'comicpress'), 'edit_themes', basename(__FILE__), array(&$this, 'render_admin')); + } + + function render_admin() { + $nonce = wp_create_nonce('comicpress'); + $root_categories = $this->get_root_categories(); + + include(dirname(__FILE__) . '/partials/options-admin.inc'); + } + + 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; + } + + function create_category_options($categories, $selected_id) { + $output = array(); + if (is_array($categories)) { + $final_categories = array(); + foreach ($categories as $category) { + if (is_numeric($category)) { + $result = get_category($category); + if (!(is_a($result, "WP_Error") || empty($result))) { + $final_categories[] = $result; + } + } + if (is_object($category)) { + $final_categories[] = $category; + } + } + + foreach ($final_categories as $category) { + $output[] = ''; + } + } + return implode("\n", $output); + } + + function create_dimension_selector($root, $dimension) { + $output = array(); + + $parts = explode("x", $dimension); + foreach (array( + 'width' => __('Width', 'comicpress'), + 'height' => __('Height', 'comicpress') + ) as $id => $name) { + $dim = array_shift($parts); + if (!empty($dim) && !is_numeric($dim)) { $dim = ""; } + $output[] = '
'; + } + return implode("\n", $output); + } + + function handle_update() { + foreach ($this->comicpress->comicpress_options as $option => $value) { + if (isset($_POST['cp'][$option])) { + switch ($option) { + case 'comic_category_id': + if (is_numeric($_POST['cp'][$option])) { + $result = get_category($_POST['cp'][$option]); + if (!(is_a($result, 'WP_Error') || empty($result))) { + $this->comicpress->comicpress_options[$option] = $_POST['cp'][$option]; + } + } + break; + case 'comic_dimensions': + case 'rss_dimensions': + case 'archive_dimensions': + if (is_array($_POST['cp'][$option])) { + $dim_parts = array(); + $is_valid = true; + foreach (array('width', 'height') as $field) { + $requested_dim = trim($_POST['cp'][$option][$field]); + if ($requested_dim == "") { + $dim_parts[] = $requested_dim; + } else { + if ((int)$requested_dim == $requested_dim) { + $dim_parts[] = $requested_dim; + } else { + $is_valid = false; break; + } + } + } + + if ($is_valid) { + $this->comicpress->comicpress_options[$option] = implode("x", $dim_parts); + } + } + break; + } + } + } + $this->comicpress->save(); + } +} + +?> \ No newline at end of file diff --git a/partials/options-admin.inc b/addons/Core/partials/options-admin.inc similarity index 88% rename from partials/options-admin.inc rename to addons/Core/partials/options-admin.inc index 75d7b9f..c1deea8 100644 --- a/partials/options-admin.inc +++ b/addons/Core/partials/options-admin.inc @@ -7,7 +7,7 @@ @@ -19,7 +19,7 @@ - create_dimension_selector('cp[' . $field . ']', $this->comicpress_options[$field]) ?> + create_dimension_selector('cp[' . $field . ']', $this->comicpress->comicpress_options[$field]) ?> diff --git a/test/OptionsPageTest.php b/addons/Core/test/OptionsPageTest.php similarity index 80% rename from test/OptionsPageTest.php rename to addons/Core/test/OptionsPageTest.php index ab2b709..9c9fcc5 100644 --- a/test/OptionsPageTest.php +++ b/addons/Core/test/OptionsPageTest.php @@ -1,21 +1,21 @@ admin = new ComicPressOptionsAdmin(); + $this->core = new ComicPressAddonCore(); } function testShowOptionsPage() { $nonce = wp_create_nonce('comicpress'); ob_start(); - $this->admin->render_admin(); + $this->core->render_admin(); $source = ob_get_clean(); $this->assertTrue(($xml = _to_xml($source)) !== false); @@ -49,7 +49,7 @@ class OptionsPageTest extends PHPUnit_Framework_TestCase { } $result_ids = array(); - foreach ($this->admin->get_root_categories() as $category) { + foreach ($this->core->get_root_categories() as $category) { $result_ids[] = $category->term_id; } @@ -64,7 +64,7 @@ class OptionsPageTest extends PHPUnit_Framework_TestCase { array(1,2), array(get_category(1), get_category(2)) ) as $category_test) { - $source = $this->admin->create_category_options($category_test, 1); + $source = $this->core->create_category_options($category_test, 1); $this->assertTrue(($xml = _to_xml($source, true)) !== false); @@ -78,7 +78,7 @@ class OptionsPageTest extends PHPUnit_Framework_TestCase { } function testCreateDimensionSelector() { - $source = $this->admin->create_dimension_selector("test", "760x340"); + $source = $this->core->create_dimension_selector("test", "760x340"); $this->assertTrue(($xml = _to_xml($source, true)) !== false); @@ -152,18 +152,23 @@ class OptionsPageTest extends PHPUnit_Framework_TestCase { * @dataProvider providerTestHandleUpdate */ function testHandleUpdate($original, $change, $new) { - $merged = array_merge($this->admin->comicpress_options, $original); - update_option('comicpress-options', $merged); + $this->core->comicpress = $this->getMock('ComicPress', array('save')); + $this->core->comicpress->comicpress_options = array( + 'comic_category_id' => 1, + 'comic_dimensions' => '760x', + 'rss_dimensions' => '350x', + 'archive_dimensions' => '125x' + ); + $this->core->comicpress->comicpress_options = array_merge($this->core->comicpress->comicpress_options, $original); add_category(2, (object)array('name' => 'test')); $_POST = $change; - $this->admin->handle_update(); + $this->core->handle_update(); - $result = get_option('comicpress-options'); foreach ($new as $key => $value) { - $this->assertEquals($value, $result[$key]); + $this->assertEquals($value, $this->core->comicpress->comicpress_options[$key]); } } } diff --git a/classes/ComicPress.inc b/classes/ComicPress.inc new file mode 100644 index 0000000..9fc3c4a --- /dev/null +++ b/classes/ComicPress.inc @@ -0,0 +1,29 @@ + 1, + 'comic_dimensions' => '760x', + 'rss_dimensions' => '350x', + 'archive_dimensions' => '125x' + ); + + function load() { + $result = get_option('comicpress-options'); + if (is_array($result)) { + $this->comicpress_options = $result; + } + } + + function save() { + if (is_array($this->comicpress_options)) { + update_option('comicpress-options', $this->comicpress_options); + } + } + + function init() { + $this->load(); + } +} + +?> \ No newline at end of file diff --git a/functions.php b/functions.php index 924bfb9..e743b97 100644 --- a/functions.php +++ b/functions.php @@ -36,8 +36,40 @@ require_once(dirname(__FILE__) . '/options.php'); add_action('init', '__comicpress_init'); function __comicpress_init() { + foreach (glob(dirname(__FILE__) . '/classes/*.inc') as $file) { + if (is_file($file)) { require_once($file); } + } + + $comicpress = new ComicPress(); + $comicpress->init(); + + if (is_dir($addons_dir = (dirname(__FILE__) . '/addons'))) { + $entries = glob($addons_dir . '/*'); + if (is_array($entries)) { + foreach ($entries as $entry) { + if (is_dir($entry)) { + $classname = basename($entry); + if (file_exists($entry . "/${classname}.inc")) { + require_once($entry . "/${classname}.inc"); + $classname = "ComicPressAddon${classname}"; + if (class_exists($classname)) { + $addon = new $classname(); + $addon->init(&$comicpress); + if (is_array($_POST['cp'])) { + if (isset($_POST['cp']['_nonce'])) { + if (wp_verify_nonce($_POST['cp']['_nonce'], 'comicpress')) { + $addon->handle_update(); + } + } + } + } + } + } + } + } + } + get_all_comic_categories(); - add_action('admin_init', '__comicpress_add_options_admin'); } function get_first_comic() { diff --git a/options.php b/options.php index a333f01..62a2de0 100644 --- a/options.php +++ b/options.php @@ -1,149 +1,3 @@ 1, - 'comic_dimensions' => '760x', - 'rss_dimensions' => '350x', - 'archive_dimensions' => '125x' - ); - - function render_admin() { - $nonce = wp_create_nonce('comicpress'); - $root_categories = $this->get_root_categories(); - - $this->get_comicpress_options(); - - include(dirname(__FILE__) . '/partials/options-admin.inc'); - } - - 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; - } - - function create_category_options($categories, $selected_id) { - $output = array(); - if (is_array($categories)) { - $final_categories = array(); - foreach ($categories as $category) { - if (is_numeric($category)) { - $result = get_category($category); - if (!(is_a($result, "WP_Error") || empty($result))) { - $final_categories[] = $result; - } - } - if (is_object($category)) { - $final_categories[] = $category; - } - } - - foreach ($final_categories as $category) { - $output[] = ''; - } - } - return implode("\n", $output); - } - - function create_dimension_selector($root, $dimension) { - $output = array(); - - $parts = explode("x", $dimension); - foreach (array( - 'width' => __('Width', 'comicpress'), - 'height' => __('Height', 'comicpress') - ) as $id => $name) { - $dim = array_shift($parts); - if (!empty($dim) && !is_numeric($dim)) { $dim = ""; } - $output[] = '
'; - } - return implode("\n", $output); - } - - function get_comicpress_options() { - $result = get_option('comicpress-options'); - if (is_array($result)) { - $this->comicpress_options = $result; - } - } - - function update_comicpress_options() { - if (is_array($this->comicpress_options)) { - update_option('comicpress-options', $this->comicpress_options); - } - } - - function handle_update() { - if (isset($_POST['cp'])) { - $this->get_comicpress_options(); - foreach ($this->comicpress_options as $option => $value) { - if (isset($_POST['cp'][$option])) { - switch ($option) { - case 'comic_category_id': - if (is_numeric($_POST['cp'][$option])) { - $result = get_category($_POST['cp'][$option]); - if (!(is_a($result, 'WP_Error') || empty($result))) { - $this->comicpress_options[$option] = $_POST['cp'][$option]; - } - } - break; - case 'comic_dimensions': - case 'rss_dimensions': - case 'archive_dimensions': - if (is_array($_POST['cp'][$option])) { - $dim_parts = array(); - $is_valid = true; - foreach (array('width', 'height') as $field) { - $requested_dim = trim($_POST['cp'][$option][$field]); - if ($requested_dim == "") { - $dim_parts[] = $requested_dim; - } else { - if ((int)$requested_dim == $requested_dim) { - $dim_parts[] = $requested_dim; - } else { - $is_valid = false; break; - } - } - } - - if ($is_valid) { - $this->comicpress_options[$option] = implode("x", $dim_parts); - } - } - break; - } - } - } - $this->update_comicpress_options(); - } - } - - function init() { - $this->get_comicpress_options(); - } -} - -$comicpress_options_admin = new ComicPressOptionsAdmin(); - -add_action('init', array(&$comicpress_options_admin, 'init')); - -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')); - - if (isset($_POST['cp']['_nonce'])) { - if (wp_verify_nonce($_POST['cp']['_nonce'], 'comicpress')) { - $comicpress_options_admin->handle_update(); - } - } -} - ?>