comicpress-core/addons/Core/Core.inc

228 lines
7.7 KiB
PHP
Raw Normal View History

2009-07-08 23:51:02 +00:00
<?php
2009-07-09 00:07:52 +00:00
class ComicPressAddonCore extends ComicPressAddon {
2009-07-08 23:51:02 +00:00
function init($comicpress) {
add_action('admin_init', array(&$this, 'setup_admin_interface'));
2009-07-11 00:27:36 +00:00
add_filter('attachment_fields_to_edit', array(&$this, 'setup_comic_metadata_buttons'), 10, 2);
add_action('show_comic', array(&$this, 'show_comic'), 1, 1);
2009-07-13 02:31:14 +00:00
add_action('show_archive', array(&$this, 'show_archive'), 1, 1);
add_action('show_rss', array(&$this, 'show_rss'), 1, 1);
add_filter('the_content', array(&$this, 'insert_comic_feed'));
2009-07-08 23:51:02 +00:00
$this->comicpress = $comicpress;
}
2009-07-13 02:31:14 +00:00
function comic_feed() { ?>
<p><a href="<?php the_permalink() ?>"><?php do_action('show_rss') ?></a></p><?php
}
function insert_comic_feed($content) {
if (is_feed() && in_comic_category()) {
return $this->comic_feed() . $content;
} else {
return $content;
}
}
function setup_admin_interface() {
2009-07-08 23:51:02 +00:00
add_theme_page(__("ComicPress Core", 'comicpress'), __('ComicPress Core', 'comicpress'), 'edit_themes', basename(__FILE__), array(&$this, 'render_admin'));
if (isset($_REQUEST['post'])) {
add_meta_box("comic-image-ordering", __("Comic Image Ordering", 'comicpress'), array(&$this, 'render_comic_image_ordering'), "post", "normal", "low");
}
2009-07-08 23:51:02 +00:00
}
2009-07-11 00:27:36 +00:00
2009-07-13 02:31:14 +00:00
function show_media($override_post, $method) {
global $post;
$post_to_use = (is_null($override_post)) ? $this->comicpress->get_last_comic() : $post;
2009-07-13 02:31:14 +00:00
$comic_post = new ComicPressComicPost($post_to_use, &$this->comicpress);
$comic_post->{$method}();
}
function show_comic($override_post = null) {
$this->show_media($override_post, "display_comics");
}
2009-07-13 02:31:14 +00:00
function show_archive($override_post = null) {
$this->show_media($override_post, "display_archive");
}
function show_rss($override_post = null) {
$this->show_media($override_post, "display_rss");
}
2009-07-11 00:27:36 +00:00
function setup_comic_metadata_buttons($form_fields, $post) {
2009-07-13 08:26:26 +00:00
global $pagenow;
2009-07-11 00:27:36 +00:00
$comic_image_types = array(
'none' => __('Not a comic', 'comicpress'),
'comic' => __('Comic', 'comicpress'),
'rss' => __('RSS', 'comicpress'),
'archive' => __('Archive', 'comicpress'),
);
$current_type = get_post_meta($post->ID, 'comic_image_type', true);
$field_html_lines = array();
$field_html_lines[] = '<input type="hidden" name="cp[_nonce]" value="' . wp_create_nonce('comicpress') . '" />';
foreach ($comic_image_types as $field => $label) {
$field_html_lines[] = '<label>'
. ' <input type="radio" name="attachments['
. $post->ID
. '][comic_image_type]" value="'
. $field
. '"'
. (($field == $current_type) ? ' checked="checked"' : '')
. ' /> '
. $label
. '</label>';
}
$form_fields['comic_image_type'] = array(
'label' => __("Comic Image Type", 'comicpress'),
'input' => 'html',
'html' => '<center>' . implode("\n", $field_html_lines) . '</center>'
);
2009-07-13 08:26:26 +00:00
if ($pagenow !== "media.php") {
$form_fields['auto_attach'] = array(
'label' => __("Auto-attach?", 'comicpress'),
'input' => 'html',
'html' => '<input type="checkbox" name="attachments[' . $post->ID . '][auto_attach]" value="yes" checked="checked" /> <em>'
. __('Attach to this post w/o needing to insert into post', 'comicpress')
. '</em>'
);
}
2009-07-12 22:50:55 +00:00
2009-07-11 00:27:36 +00:00
return $form_fields;
}
2009-07-08 23:51:02 +00:00
function render_admin() {
$nonce = wp_create_nonce('comicpress');
$root_categories = $this->get_root_categories();
include(dirname(__FILE__) . '/partials/options-admin.inc');
}
function render_comic_image_ordering() {
echo "made it";
}
2009-07-08 23:51:02 +00:00
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() {
2009-07-11 00:27:36 +00:00
if (isset($_POST['attachments'])) {
//coming from media editor
foreach ($_POST['attachments'] as $post_id => $settings) {
if (isset($settings['comic_image_type'])) {
update_post_meta($post_id, 'comic_image_type', $settings['comic_image_type']);
}
2009-07-12 22:50:55 +00:00
if (isset($settings['auto_attach'])) {
if (isset($settings['post_parent'])) {
$media_post = get_post($post_id);
$media_post->post_parent = $settings['post_parent'];
wp_update_post($media_post);
}
}
2009-07-11 00:27:36 +00:00
}
} else {
//coming from us
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 == "") {
2009-07-08 23:51:02 +00:00
$dim_parts[] = $requested_dim;
} else {
2009-07-11 00:27:36 +00:00
if ((int)$requested_dim == $requested_dim) {
$dim_parts[] = $requested_dim;
} else {
$is_valid = false; break;
}
2009-07-08 23:51:02 +00:00
}
}
2009-07-11 00:27:36 +00:00
if ($is_valid) {
$this->comicpress->comicpress_options[$option] = implode("x", $dim_parts);
}
2009-07-08 23:51:02 +00:00
}
2009-07-11 00:27:36 +00:00
break;
case 'blogpost_count':
$this->comicpress->comicpress_options[$option] = (int)$_POST['cp'][$option];
break;
2009-07-11 00:27:36 +00:00
}
2009-07-08 23:51:02 +00:00
}
}
2009-07-11 00:27:36 +00:00
$this->comicpress->save();
$this->info("ComicPress configuration updated.");
2009-07-08 23:51:02 +00:00
}
}
}
?>