comicpress-core/classes/ComicPressAdmin.inc

544 lines
18 KiB
PHP
Raw Normal View History

2009-07-08 23:51:02 +00:00
<?php
2010-01-01 21:35:48 +00:00
// TODO Be able to disable backends
class ComicPressAdmin {
2009-11-17 01:24:19 +00:00
// @codeCoverageIgnoreStart
/**
* Initialize the addon.
* @param ComicPress $comicpress The master ComicPress object.
*/
function init() {
2009-11-01 19:04:15 +00:00
$this->comicpress = &ComicPress::get_instance();
2009-11-08 03:47:28 +00:00
add_action('admin_menu', array(&$this, 'admin_menu'));
2009-11-17 01:24:19 +00:00
add_filter('attachment_fields_to_edit', array(&$this, 'setup_comic_metadata_buttons'), 10, 2);
if (current_user_can('edit_posts') && isset($comicpress->comicpress_options['helpers']['show_inline_comic_ordering'])) {
add_filter('comicpress_attached_image', array(&$this, 'comicpress_attached_image'), 10, 3);
add_filter('comicpress_display_attached_images', array(&$this, 'comicpress_display_attached_images'), 10, 2);
}
2009-12-07 02:36:44 +00:00
add_action('edit_form_advanced', array(&$this, 'edit_form_advanced'));
add_action('save_post', array(&$this, 'save_post'), 10, 1);
2009-12-07 02:36:44 +00:00
add_filter('comicpress_core_version', array(&$this, 'comicpress_core_version'));
2009-11-17 01:24:19 +00:00
$this->comic_image_types = array(
'none' => __('Not a comic', 'comicpress'),
'comic' => __('Comic', 'comicpress'),
'rss' => __('RSS', 'comicpress'),
'archive' => __('Archive', 'comicpress')
);
if (is_admin()) {
add_action('admin_notices', array(&$this, 'display_messages'));
} else {
add_action('wp_head', array(&$this, 'display_messages'));
}
}
2009-12-07 02:36:44 +00:00
function edit_form_advanced() { ?>
<input type="hidden" name="cp[_nonce]" value="<?php echo esc_attr(wp_create_nonce('comicpress')) ?>" />
<input type="hidden" name="cp[action]" value="edit-form-advanced" />
<input type="hidden" name="cp[_action_nonce]" value="<?php echo esc_attr(wp_create_nonce('comicpress-edit-form-advanced')) ?>" />
<?php }
function comicpress_core_version($version = '') { return '1.0'; }
2009-11-17 01:24:19 +00:00
function comicpress_attached_image($content, $attachment_id, $index) {
$content .= '<label class="comic-image-ordering">'
. __('Image index:', 'comicpress')
. ' '
. '<input type="text" name="cp[ordering][comic]['
. $attachment_id
. ']" value="'
. $index
. '" /></label>';
return $content;
}
function comicpress_display_attached_images($content, $post_id) {
$content = '<form method="post">'
. '<input type="hidden" name="cp[_nonce]" value="'
. wp_create_nonce('comicpress')
. '" />'
. '<input type="hidden" name="post_ID" value="'
. $post_id
. '" />'
. $content
. '<input type="submit" value="'
. __('Change image ordering', 'comicpress')
. '" />'
. '</form>';
return $content;
}
/**
* Set up the admin interface and meta boxes.
*/
function admin_menu() {
2009-11-02 01:20:40 +00:00
global $plugin_page, $pagenow, $post;
2009-11-01 18:15:59 +00:00
2009-11-17 01:24:19 +00:00
add_theme_page(__("ComicPress", 'comicpress'), __('ComicPress', 'comicpress'), 'edit_themes', 'comicpress/render_admin', array(&$this, 'render_admin'));
2009-11-18 12:02:51 +00:00
add_theme_page(__("ComicPress Documentation", 'comicpress'), __('ComicPress Docs', 'comicpress'), 'edit_themes', 'comicpress/comicpress_docs', array(&$this, 'render_documentation'));
2009-11-17 01:24:19 +00:00
if (strpos($pagenow, "post") === 0) {
add_meta_box("comic-image-ordering", __("Comic Image Ordering", 'comicpress'), array(&$this, 'render_comic_image_ordering'), "post", "normal", "low");
2009-11-17 02:12:04 +00:00
wp_enqueue_script('cp-ordering', plugin_dir_url(dirname(__FILE__)) . '/js/ComicImageOrdering.js', array('scriptaculous', 'scriptaculous-slider'));
wp_enqueue_style('cp-admin', plugin_dir_url(dirname(__FILE__)) . '/css/cp-admin.css');
2009-11-17 01:24:19 +00:00
add_action('admin_footer', array(&$this, 'admin_footer'));
}
2009-11-01 18:15:59 +00:00
if ($plugin_page == 'comicpress/render_admin') {
2009-11-17 02:12:04 +00:00
wp_enqueue_style('cp-admin', plugin_dir_url(dirname(__FILE__)) . '/css/cp-admin.css');
wp_enqueue_script('cp-admin', plugin_dir_url(dirname(__FILE__)) . '/js/Storyline.js', array('prototype', 'scriptaculous'));
2009-11-17 01:24:19 +00:00
add_action('admin_footer', array(&$this, 'admin_footer'));
2009-11-01 18:15:59 +00:00
}
2009-11-02 01:20:40 +00:00
2009-11-18 12:02:51 +00:00
if ($plugin_page == 'comicpress/comicpress_docs') {
wp_enqueue_style('cp-admin', plugin_dir_url(dirname(__FILE__)) . '/css/cp-admin.css');
wp_enqueue_script('prettify', plugin_dir_url(dirname(__FILE__)) . '/js/prettify/prettify.js');
wp_enqueue_style('prettify', plugin_dir_url(dirname(__FILE__)) . '/js/prettify/prettify.css');
wp_enqueue_script('scriptaculous-effects');
}
2009-11-09 03:11:26 +00:00
if (strpos($pagenow, "-upload") !== false) {
2009-11-17 02:53:40 +00:00
wp_enqueue_style('cp-admin', plugin_dir_url(dirname(__FILE__)) . '/css/cp-admin.css');
2009-11-17 02:12:04 +00:00
wp_enqueue_script('cp-media', plugin_dir_url(dirname(__FILE__)) . '/js/MediaUpload.js', array('prototype'));
2009-11-02 01:20:40 +00:00
}
}
2009-11-17 01:24:19 +00:00
function admin_footer() {
$nonce = wp_create_nonce('comicpress');
?><script type="text/javascript">
var ComicPressAdmin = {
nonce: '<?php echo $nonce ?>',
ajax_uri: '<?php echo trailingslashit(get_bloginfo('url')) ?>'
}
</script>
<?php }
/**
* Modify the Media Gallery for ComicPress use.
*/
function setup_comic_metadata_buttons($form_fields, $post) {
global $pagenow, $post_id;
2009-11-17 01:24:19 +00:00
$comicpress_info = get_post_meta($post->ID, 'comicpress', true);
$is_managed = false;
if (isset($comicpress_info['managed'])) {
$is_managed = $comicpress_info['managed'];
}
2009-11-19 02:25:35 +00:00
$form_fields['comicpress_management'] = array(
'label' => __("Let ComicPress Manage?", 'comicpress'),
'input' => 'html',
'html' => '<label><input type="checkbox" name="attachments[' . esc_attr($post->ID) . '][comicpress_management]" value="yes" ' . ($is_managed ? 'checked="checked"' : '') . '/> '
. __('Let ComicPress treat this image as a comic media file', 'comicpress')
. '</label>'
. '<input type="hidden" name="cp[_nonce]" value="' . wp_create_nonce('comicpress') . '" />'
. '<input type="hidden" name="attachments[' . $post->ID . '][post_parent]" value="' . esc_attr($_REQUEST['post_id']) . '" />'
2009-11-19 02:25:35 +00:00
);
2009-11-17 01:24:19 +00:00
if ($pagenow !== "media.php") {
2009-11-19 02:25:35 +00:00
$form_fields['attach_parent'] = array(
'label' => __("Change parent?", 'comicpress'),
2009-11-17 01:24:19 +00:00
'input' => 'html',
2009-11-19 02:25:35 +00:00
'html' => '<label><input type="checkbox" name="attachments[' . esc_attr($post->ID) . '][change_parent]" value="yes" /> '
. __('Attach this image to the current post being edited?', 'comicpress')
2009-11-17 01:24:19 +00:00
. '</label>'
);
}
return $form_fields;
}
/**
* Render the admin interface.
*/
function render_admin() {
$nonce = wp_create_nonce('comicpress');
$action_nonce = wp_create_nonce('comicpress-comicpress-options');
$storyline = new ComicPressStoryline();
2009-11-01 19:04:15 +00:00
$storyline->normalize();
2009-11-01 18:15:59 +00:00
$storyline->read_from_options();
2009-07-08 23:51:02 +00:00
2009-11-17 01:24:19 +00:00
include(dirname(__FILE__) . '/partials/options-admin.inc');
}
2009-11-08 03:47:28 +00:00
function render_documentation() {
2009-11-18 12:02:51 +00:00
$path = false;
foreach (array(get_locale(), 'en_US') as $locale) {
if (is_dir(plugin_dir_path(dirname(__FILE__)) . 'docs/' . $locale)) {
$path = plugin_dir_path(dirname(__FILE__)) . 'docs/' . $locale;
}
}
if (!empty($path)) {
$all_docs = array();
if (($dh = opendir($path)) !== false) {
while (($file = readdir($dh)) !== false) {
2009-11-20 02:48:15 +00:00
if (is_file($path . '/' . $file)) {
$content = file($path . '/' . $file);
$title = false;
foreach ($content as $line) {
if (preg_match('#<h1>(.*)</h1>#', $line, $matches) > 0) {
list($all, $title) = $matches;
2009-11-20 02:48:15 +00:00
$id = preg_replace('#[^a-z0-9]+#', '_', strtolower($title));
$content = implode('', $content);
2009-11-18 12:02:51 +00:00
2009-11-20 02:48:15 +00:00
$content = str_replace('src="', 'src="' . plugin_dir_url($path . '/' . $file), $content);
2009-11-20 02:48:15 +00:00
$all_docs[$id] = compact('title', 'content');
}
2009-11-18 12:02:51 +00:00
}
}
}
closedir($dh);
}
ksort($all_docs);
include(dirname(__FILE__) . '/partials/documentation-viewer.inc');
}
}
2009-11-01 18:15:59 +00:00
function _render_admin_storyline_tree($node, $parent_id = "0") {
2009-11-17 01:24:19 +00:00
foreach ($node as $category_id => $children) {
2009-11-01 18:15:59 +00:00
$category = get_category($category_id);
2009-11-13 19:45:28 +00:00
echo '<div id="category_' . $parent_id . '-' . $category_id . '" class="cp-category-info">';
echo '<span>' . esc_html($category->name) . ' <span class="slug">(slug: ' . esc_html($category->slug) . ')</span></span>';
2009-11-01 18:15:59 +00:00
if (is_array($children)) {
echo '<div class="cp-children">';
2009-11-13 19:45:28 +00:00
$this->_render_admin_storyline_tree($children, $parent_id . '-' . $category_id);
2009-11-01 18:15:59 +00:00
echo '</div>';
}
echo '</div>';
}
}
2009-11-08 03:47:28 +00:00
2009-11-17 01:24:19 +00:00
/**
* Render the comic image ordering interface.
*/
function render_comic_image_ordering($is_ajax = false, $override_post = null) {
global $post_ID, $temp_ID;
$uploading_iframe_ID = (int) (0 == $post_ID ? $temp_ID : $post_ID);
if (is_numeric($override_post)) { $uploading_iframe_ID = $override_post; }
2009-11-19 02:25:35 +00:00
if ($uploading_iframe_ID > 0) {
$comic_post = new ComicPressComicPost(get_post($uploading_iframe_ID));
$ordering = $comic_post->normalize_ordering();
$nonce = wp_create_nonce('comicpress');
$action_nonce = wp_create_nonce('comicpress-comic-ordering');
$zoom_level = 40;
$current_user = wp_get_current_user();
if (!empty($current_user)) {
$comicpress_meta = get_usermeta($current_user->ID, 'comicpress-settings');
if (is_array($comicpress_meta)) {
if (isset($comicpress_meta['zoom_level'])) {
$zoom_level = floor($comicpress_meta['zoom_level']);
}
2009-11-17 01:24:19 +00:00
}
}
2009-11-19 02:25:35 +00:00
// from wp-admin/includes/media.php O_o
$media_upload_iframe_src = "media-upload.php?post_id=$uploading_iframe_ID";
$image_upload_iframe_src = apply_filters('image_upload_iframe_src', "$media_upload_iframe_src&amp;type=image&amp;TB_iframe=true");
2009-11-17 01:24:19 +00:00
2009-11-19 02:25:35 +00:00
$comicpress = ComicPress::get_instance();
2009-11-17 01:24:19 +00:00
2009-11-19 02:25:35 +00:00
$available_attachments = array();
foreach ($this->get_editable_attachment_list($ordering) as $id => $info) {
$result = ComicPressBackend::generate_from_id($id);
if (!empty($result)) {
$attachment_info = $result->get_info();
2009-11-13 21:15:36 +00:00
2009-11-19 02:25:35 +00:00
$available_attachments[] = array('id' => $id, 'name' => basename($attachment_info['file']), 'type' => $result->source_name, 'attachment' => $attachment_info, 'ordering' => $info);
}
2009-11-13 21:15:36 +00:00
}
2009-11-19 02:25:35 +00:00
if ($is_ajax === true) {
include(dirname(__FILE__) . '/partials/_comic-image-ordering-sorters.inc');
} else {
include(dirname(__FILE__) . '/partials/_comic-image-ordering.inc');
}
2009-11-17 01:24:19 +00:00
} else {
2009-11-19 02:25:35 +00:00
_e('You\'ll need to save your post as a draft or publish it before you can order comic images.', 'comicpress');
2009-11-17 01:24:19 +00:00
}
}
// @codeCoverageIgnoreEnd
function get_editable_attachment_list($ordering) {
foreach ($ordering as $id => $info) {
if (isset($info['children'])) {
foreach (array_values($info['children']) as $new_id) {
$ordering[$new_id] = array('enabled' => true);
}
}
}
return $ordering;
}
/**
* Create a dimension selector.
* @param string $root The field name root.
* @param $dimension The dimension to pre-fill into the fields.
* @return string The dimension selector as HTML.
*/
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);
}
// @codeCoverageIgnoreStart
/**
* Update attachment information.
*/
function handle_update_refresh_ordering($info) {
$this->render_comic_image_ordering(true, $info['post_id']);
exit(0);
}
// @codeCoverageIgnoreEnd
/**
* Update attachment information.
*/
function handle_update_attachments() {
foreach ($_POST['attachments'] as $post_id => $settings) {
2009-11-19 02:25:35 +00:00
if (isset($settings['change_parent'])) {
2009-11-17 01:24:19 +00:00
$media_post = get_post($post_id);
if (isset($media_post->post_parent)) {
$media_post->post_parent = $settings['post_parent'];
wp_update_post($media_post);
}
}
update_post_meta($post_id, 'comicpress', array(
'managed' => isset($settings['comicpress_management'])
));
}
}
/**
* Update ComicPress options.
*/
function handle_update_comicpress_options($info) {
foreach ($this->comicpress->comicpress_options as $option => $value) {
if (isset($info[$option])) {
switch ($option) {
case 'image_types':
2009-11-13 01:25:46 +00:00
if (is_array($info['image_types'])) {
2009-11-17 01:24:19 +00:00
$this->comicpress->comicpress_options['image_types'] = array();
$defined_default = null;
2009-11-13 01:25:46 +00:00
foreach ($info['image_types'] as $type => $image_info) {
if (is_array($image_info)) {
2009-11-13 03:13:05 +00:00
$new_value = array();
$new_type = $type;
2009-11-13 01:25:46 +00:00
foreach ($image_info as $field => $field_value) {
switch ($field) {
2009-11-13 03:13:05 +00:00
case 'default': $defined_default = $type; break;
case 'dimensions':
2009-11-13 01:25:46 +00:00
if (is_array($field_value)) {
if (count(array_intersect(array('width', 'height'), array_keys($field_value))) == 2) {
$new_value['dimensions'] = "{$field_value['width']}x{$field_value['height']}";
}
}
break;
2009-11-13 03:13:05 +00:00
case 'name': $new_value['name'] = (string)$field_value; break;
case 'short_name': $new_type = $field_value; break;
2009-11-13 01:25:46 +00:00
}
}
2009-11-13 03:13:05 +00:00
if ($type != $new_type) {
2009-11-17 01:24:19 +00:00
unset($this->comicpress->comicpress_options['image_types'][$new_type]);
2009-11-13 03:13:05 +00:00
}
$this->comicpress->comicpress_options['image_types'][$new_type] = $new_value;
2009-11-13 01:25:46 +00:00
}
}
2009-11-16 17:35:48 +00:00
$this->comicpress->normalize_image_size_options();
2009-11-13 01:25:46 +00:00
}
if (is_null($defined_default)) {
$defined_default = array_keys($this->comicpress->comicpress_options['image_types']);
if (!empty($defined_default)) { $defined_default = reset($defined_default); }
}
if (!is_null($defined_default)) {
if (isset($this->comicpress->comicpress_options['image_types'][$defined_default])) {
$this->comicpress->comicpress_options['image_types'][$defined_default]['default'] = true;
}
}
2009-11-17 01:24:19 +00:00
break;
// @codeCoverageIgnoreStart
2009-11-01 18:15:59 +00:00
case 'storyline_order':
$storyline = new ComicPressStoryline();
2009-11-01 19:04:15 +00:00
$storyline->normalize($info[$option]);
2009-11-01 18:15:59 +00:00
break;
2009-11-17 01:24:19 +00:00
// @codeCoverageIgnoreEnd
}
}
}
$this->comicpress->save();
$this->info(__("ComicPress configuration updated.", 'comicpress'));
$this->comicpress->init();
}
// @codeCoverageIgnoreStart
function _json_decode($string) {
if (function_exists('json_decode')) {
return json_decode($string);
} else {
require_once(ABSPATH."/wp-includes/js/tinymce/plugins/spellchecker/classes/utils/JSON.php");
$j = new Moxiecode_JSON();
return $j->decode($string);
}
}
2009-11-08 03:47:28 +00:00
2009-11-13 03:13:05 +00:00
function _json_encode($data) {
2009-11-17 01:24:19 +00:00
if (function_exists('json_encode')) {
return json_encode($data);
} else {
require_once(ABSPATH."/wp-includes/js/tinymce/plugins/spellchecker/classes/utils/JSON.php");
$j = new Moxiecode_JSON();
return $j->encode($data);
}
}
function save_post($post_id) {
if (ComicPressAdmin::verify_nonces() === 'handle_update_edit_form_advanced') {
if ($post = get_post($psot_id)) {
$comic_post = new ComicPressComicPost($post);
$data = $this->_json_decode(stripslashes($_POST['cp']['comic_order']));
if (!empty($data)) {
if (is_array($data)) {
$comic_post->update_post_media_data($data);
}
2009-11-17 01:24:19 +00:00
}
}
}
}
function handle_update_get_new_image_type_editor($info) {
$type = substr(md5(rand()), 0, 6);
$info = array(
'dimensions' => '100x100',
'default' => false,
'name' => 'New Type'
);
require_once('partials/_image-type-editor.inc');
exit(0);
}
/**
* Update the zoom slider info.
* @param $info The browser input.
*/
function handle_update_zoom_slider($info) {
$this->is_ajax = true;
$current_user = wp_get_current_user();
if (!empty($current_user)) {
$this->_update_zoom_slider_meta($current_user->ID, $info['zoom_level']);
exit(0);
}
header('HTTP/1.1 500 Internal Server Error');
exit(0);
}
// @codeCoverageIgnoreEnd
/**
* Update the user's zoom slider metadata.
*/
function _update_zoom_slider_meta($user_id, $level) {
$comicpress_meta = get_usermeta($user_id, 'comicpress-settings');
if (!is_array($comicpress_meta)) { $comicpress_meta = array(); }
$comicpress_meta['zoom_level'] = $level;
update_usermeta($user_id, 'comicpress-settings', $comicpress_meta);
}
/**
* Handle an update.
*/
function handle_update() {
2010-01-01 20:14:37 +00:00
if ($method = $this->verify_nonces()) {
switch ($method) {
case 'attachments':
$this->handle_update_attachments();
break;
default:
if (method_exists($this, $method)) {
$this->{$method}($_REQUEST['cp']);
2009-11-17 01:24:19 +00:00
}
do_action("comicpress-${method}", $_REQUEST['cp']);
break;
2009-11-17 01:24:19 +00:00
}
}
}
// @codeCoverageIgnoreStart
var $messages = array(
'info' => array(),
'warn' => array(),
'error' => array()
);
function info($message) { $this->messages['info'][] = $message; }
function warn($message) { $this->messages['warn'][] = $message; }
function error($message) { $this->messages['error'][] = $message; }
function display_messages() {
foreach ($this->messages as $type => $messages) {
if (!empty($messages)) {
echo '<div class="updated fade cp-' . $type . '">';
foreach ($messages as $message) {
echo '<p>' . $message . '</p>';
}
echo '</div>';
}
}
}
// @codeCoverageIgnoreEnd
function verify_nonces() {
if (isset($_REQUEST['cp'])) {
if (is_array($_REQUEST['cp'])) {
if (isset($_REQUEST['cp']['_nonce'])) {
if (wp_verify_nonce($_REQUEST['cp']['_nonce'], 'comicpress')) {
if (isset($_POST['attachments'])) {
//coming from media editor
return 'attachments';
} else if (isset($_REQUEST['cp']['action'])) {
$action = $_REQUEST['cp']['action'];
if (isset($_REQUEST['cp']['_action_nonce'])) {
if (wp_verify_nonce($_REQUEST['cp']['_action_nonce'], "comicpress-${action}")) {
$method = 'handle_update_' . strtolower(str_replace('-', '_', $action));
return $method;
}
}
}
}
}
}
}
return false;
}
2009-07-08 23:51:02 +00:00
}