working on theme options updates

This commit is contained in:
John Bintz 2009-07-05 23:09:30 -04:00
parent ddc296036b
commit 9ed017b883
2 changed files with 82 additions and 1 deletions

View File

@ -37,7 +37,7 @@ class ComicPressOptionsAdmin {
foreach ($categories as $category) {
if (is_numeric($category)) {
$result = get_category($category);
if (!is_a($result, "WP_Error")) {
if (!(is_a($result, "WP_Error") || empty($result))) {
$final_categories[] = $result;
}
}
@ -80,6 +80,30 @@ class ComicPressOptionsAdmin {
update_option('comicpress-options', $this->comicpress_options);
}
}
function handle_update() {
if (isset($_POST['cp'])) {
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 'tumbnail_dimensions':
break;
}
}
}
}
$this->update_comicpress_options();
}
}
$comicpress_options_admin = new ComicPressOptionsAdmin();
@ -87,6 +111,12 @@ $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'));
if (isset($_POST['cp']['_nonce'])) {
if (wp_verify_nonce($_POST['cp']['_nonce'], 'comicpress')) {
$comicpress_options_admin->handle_update();
}
}
}
?>

View File

@ -7,6 +7,7 @@ require_once(dirname(__FILE__) . '/../options.php');
class OptionsPageTest extends PHPUnit_Framework_TestCase {
function setUp() {
_reset_wp();
$_POST = array();
$this->admin = new ComicPressOptionsAdmin();
}
@ -88,6 +89,56 @@ class OptionsPageTest extends PHPUnit_Framework_TestCase {
$this->assertTrue(_xpath_test($xml, $xpath, $value), $xpath);
}
}
function providerTestHandleUpdate() {
return array(
array(
array('comic_category_id' => 1),
array('comic_category_id' => 2),
array('comic_category_id' => 1)
),
array(
array('comic_category_id' => 1),
array('cp' => array(
'comic_category_id' => 2),
),
array('comic_category_id' => 2)
),
array(
array('comic_category_id' => 1),
array('cp' => array(
'comic_category_id' => "cat"),
),
array('comic_category_id' => 1)
),
array(
array('comic_category_id' => 1),
array('cp' => array(
'comic_category_id' => 3),
),
array('comic_category_id' => 1)
),
);
}
/**
* @dataProvider providerTestHandleUpdate
*/
function testHandleUpdate($original, $change, $new) {
$merged = array_merge($this->admin->comicpress_options, $original);
update_option('comicpress-options', $merged);
add_category(2, (object)array('name' => 'test'));
$_POST = $change;
$this->admin->handle_update();
$result = get_option('comicpress-options');
foreach ($new as $key => $value) {
$this->assertEquals($value, $result[$key]);
}
}
}
?>