start addons structure

This commit is contained in:
John Bintz 2009-07-08 19:51:02 -04:00
parent e72e6c39a6
commit a120fc059f
6 changed files with 197 additions and 162 deletions

115
addons/Core/Core.inc Normal file
View File

@ -0,0 +1,115 @@
<?php
class ComicPressAddonCore {
function init($comicpress) {
add_action('admin_init', array(&$this, 'add_theme_page'));
$this->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[] = '<option value="' . $category->term_id . '"' . (($category->term_id == $selected_id) ? ' selected="selected"' : '') . '>' . $category->name . '</option>';
}
}
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[] = '<label>' . $name . ': <input type="text" name="' . $root . '[' . $id . ']" value="' . $dim . '" size="4" />px</label><br />';
}
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();
}
}
?>

View File

@ -7,7 +7,7 @@
<th scope="row"><?php _e('Master Comic Category', 'comicpress') ?></th>
<td>
<select name="cp[comic_category_id]">
<?php echo $this->create_category_options($root_categories, $this->comicpress_options['comicpress_category_id']) ?>
<?php echo $this->create_category_options($root_categories, $this->comicpress->comicpress_options['comic_category_id']) ?>
</select>
</td>
</tr>
@ -19,7 +19,7 @@
<tr>
<th scope="row"><?php echo $name ?></th>
<td>
<?php echo $this->create_dimension_selector('cp[' . $field . ']', $this->comicpress_options[$field]) ?>
<?php echo $this->create_dimension_selector('cp[' . $field . ']', $this->comicpress->comicpress_options[$field]) ?>
</td>
</tr>
<?php } ?>

View File

@ -1,21 +1,21 @@
<?php
require_once('PHPUnit/Framework.php');
require_once(dirname(__FILE__) . '/../../mockpress/mockpress.php');
require_once(dirname(__FILE__) . '/../options.php');
require_once(dirname(__FILE__) . '/../../../../mockpress/mockpress.php');
require_once(dirname(__FILE__) . '/../Core.inc');
class OptionsPageTest extends PHPUnit_Framework_TestCase {
class CoreTest extends PHPUnit_Framework_TestCase {
function setUp() {
_reset_wp();
$_POST = array();
$this->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]);
}
}
}

29
classes/ComicPress.inc Normal file
View File

@ -0,0 +1,29 @@
<?php
class ComicPress {
var $comicpress_options = array(
'comic_category_id' => 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();
}
}
?>

View File

@ -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() {

View File

@ -1,149 +1,3 @@
<?php
class ComicPressOptionsAdmin {
var $comicpress_options = array(
'comic_category_id' => 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[] = '<option value="' . $category->term_id . '"' . ($category->term_id == $selected_id ? ' selected="selected"' : '') . '>' . $category->name . '</option>';
}
}
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[] = '<label>' . $name . ': <input type="text" name="' . $root . '[' . $id . ']" value="' . $dim . '" size="4" />px</label><br />';
}
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();
}
}
}
?>