comic attachment metadata working

This commit is contained in:
John Bintz 2009-07-10 20:27:36 -04:00
parent 3df4ad4ff5
commit 10baa173ec
2 changed files with 92 additions and 43 deletions

View File

@ -1,8 +1,11 @@
<?php <?php
require_once('FirePHPCore/fb.php');
class ComicPressAddonCore extends ComicPressAddon { class ComicPressAddonCore extends ComicPressAddon {
function init($comicpress) { function init($comicpress) {
add_action('admin_init', array(&$this, 'setup_admin_interface')); add_action('admin_init', array(&$this, 'setup_admin_interface'));
add_filter('attachment_fields_to_edit', array(&$this, 'setup_comic_metadata_buttons'), 10, 2);
$this->comicpress = $comicpress; $this->comicpress = $comicpress;
} }
@ -14,6 +17,40 @@ class ComicPressAddonCore extends ComicPressAddon {
add_meta_box("comic-image-ordering", __("Comic Image Ordering", 'comicpress'), array(&$this, 'render_comic_image_ordering'), "post", "normal", "low"); add_meta_box("comic-image-ordering", __("Comic Image Ordering", 'comicpress'), array(&$this, 'render_comic_image_ordering'), "post", "normal", "low");
} }
} }
function setup_comic_metadata_buttons($form_fields, $post) {
$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>'
);
return $form_fields;
}
function render_admin() { function render_admin() {
$nonce = wp_create_nonce('comicpress'); $nonce = wp_create_nonce('comicpress');
@ -78,47 +115,57 @@ class ComicPressAddonCore extends ComicPressAddon {
} }
function handle_update() { function handle_update() {
foreach ($this->comicpress->comicpress_options as $option => $value) { if (isset($_POST['attachments'])) {
if (isset($_POST['cp'][$option])) { //coming from media editor
switch ($option) { foreach ($_POST['attachments'] as $post_id => $settings) {
case 'comic_category_id': if (isset($settings['comic_image_type'])) {
if (is_numeric($_POST['cp'][$option])) { update_post_meta($post_id, 'comic_image_type', $settings['comic_image_type']);
$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;
} }
} }
} 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 == "") {
$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();
$this->info("ComicPress configuration updated.");
} }
$this->comicpress->save();
$this->info("ComicPress configuration updated.");
} }
} }

View File

@ -2,7 +2,7 @@
class ComicPressComicPost { class ComicPressComicPost {
var $post; var $post;
var $attachments; var $attachments = null;
var $comicpress; var $comicpress;
function ComicPressComicPost($post = null, $comicpress = null) { function ComicPressComicPost($post = null, $comicpress = null) {
@ -11,7 +11,7 @@ class ComicPressComicPost {
} }
function get_comic_image_attachments() { function get_comic_image_attachments() {
if (!isset($this->attachments)) { if (is_null($this->attachments)) {
$this->attachments = get_children(array( $this->attachments = get_children(array(
'post_parent' => $this->post->ID, 'post_parent' => $this->post->ID,
'post_type' => 'attachment', 'post_type' => 'attachment',
@ -22,9 +22,11 @@ class ComicPressComicPost {
} }
function display_comics() { function display_comics() {
foreach ($this->get_comic_image_attachments() as $attachment) { if (is_array($this->get_comic_image_attachments())) {
echo $this->get_comic_img_tag(wp_get_attachment_url($attachment->ID), "comic", array('title' => $attachment->post_title)); foreach ($this->get_comic_image_attachments() as $attachment) {
} echo $this->get_comic_img_tag(wp_get_attachment_url($attachment->ID), "comic", array('title' => $attachment->post_title));
}
}
} }
function get_comic_img_tag($url, $type, $additional_parameters = array()) { function get_comic_img_tag($url, $type, $additional_parameters = array()) {