comicpress-core/addons/Core/Core.inc

588 lines
20 KiB
PHP
Raw Normal View History

2009-07-08 23:51:02 +00:00
<?php
class ComicPressAddonCore extends ComicPressAddon {
var $is_addon_manager = true;
var $cannot_be_disabled = true;
var $name = "ComicPress Core";
2009-07-20 22:27:12 +00:00
/**
* Initialize the addon.
* @param ComicPress $comicpress The master ComicPress object.
*/
2009-11-01 18:15:59 +00:00
function init() {
2009-11-01 19:04:15 +00:00
$this->comicpress = &ComicPress::get_instance();
2009-11-01 18:15:59 +00:00
add_action('admin_menu', 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
2009-07-13 08:30:01 +00:00
if (isset($_GET['randomcomic'])) {
add_action('template_redirect', array(&$this, 'go_to_random_comic'));
}
2009-08-04 01:28:59 +00:00
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-08-07 00:35:58 +00:00
if (isset($this->comicpress->comicpress_options['helpers']['show_partials_info'])) {
wp_enqueue_script('prototype');
add_filter('comicpress_partial', array(&$this, 'show_inline_comicpress_partial_editor'), 20, 2);
add_action('wp_head', array(&$this, 'add_inline_comicpress_partial_edit_hooks'));
add_action('wp_footer', array(&$this, 'add_inline_comicpress_partial_holders'));
}
if (is_admin()) {
add_action('admin_head', array(&$this, 'add_edit_area'));
} else {
add_action('wp_head', array(&$this, 'add_edit_area'));
}
2009-07-23 00:55:12 +00:00
$this->comic_image_types = array(
'none' => __('Not a comic', 'comicpress'),
'comic' => __('Comic', 'comicpress'),
'rss' => __('RSS', 'comicpress'),
'archive' => __('Archive', 'comicpress')
);
2009-07-08 23:51:02 +00:00
}
2009-07-13 02:31:14 +00:00
2009-08-07 00:35:58 +00:00
function add_edit_area() { ?>
<script type="text/javascript" src="<?php echo get_template_directory_uri() . str_replace(realpath(get_template_directory()), '', realpath(dirname(__FILE__) . '/edit_area/edit_area_full.js')) ?>"></script>
<?php }
function show_inline_comicpress_partial_editor($content, $target) {
$target = str_replace(get_template_directory() . DIRECTORY_SEPARATOR, '', $target);
$content = preg_replace('#(<div class="partial-helper">)([^\<]+)(</div>)#', '\1\2 | <a href="#" rel="edit-' . md5($target) . '">Edit</a>\3', $content);
return $content;
}
function add_inline_comicpress_partial_edit_hooks() { ?>
<style type="text/css">
.inline-partial-editor {
display: none;
background: white;
border: solid #333 1px;
padding: 5px;
z-index: 200
}
</style>
<script type="text/javascript">
Event.observe(window, 'load', function() {
$$('.partial-helper a').each(function(a) {
Event.observe(a, 'click', function(e) {
Event.stop(e);
var edit_form = $(a.rel);
if (edit_form) {
if (!edit_form.visible()) {
var holder = $(a.parentNode);
var holder_dim = holder.getDimensions();
edit_form.style.display = "block";
edit_form.style.position = "absolute";
edit_form.clonePosition(holder, { setWidth: false, setHeight: false, offsetTop: holder_dim['height'] });
editAreaLoader.init({
id: "text-" + a.rel,
syntax: "php",
start_highlight: true
});
}
}
});
});
});
</script>
<?php }
function add_inline_comicpress_partial_holders() {
$nonce = wp_create_nonce('comicpress');
$valid_partials = array();
foreach (glob(get_template_directory() . DIRECTORY_SEPARATOR . 'partials' . DIRECTORY_SEPARATOR . '*.inc') as $partial) {
if (preg_match('#(partials.*)\.inc$#', $partial, $matches) > 0) {
$valid_partials[] = $matches[1];
}
}
foreach ($valid_partials as $key) {
if (isset($this->comicpress->comicpress_options['override_partials'][$key])) {
$partial_code = htmlentities($this->comicpress->comicpress_options['override_partials'][$key]);
} else {
$partial_code = htmlentities(file_get_contents(get_template_directory() . DIRECTORY_SEPARATOR . $key . '.inc'));
}
$hash = md5($key); ?>
<form id="edit-<?php echo $hash ?>" method="post" class="inline-partial-editor" style="display: none">
<input type="hidden" name="cp[_nonce]" value="<?php echo $nonce ?>" />
<textarea id="text-edit-<?php echo $hash ?>" name="cp[code]" cols="80" rows="15" wrap="off"><?php echo $partial_code ?></textarea>
<input type="hidden" name="cp[partial]" value="<?php echo $key ?>" /><br />
<input type="submit" class="button" name="cp[action]" value="<?php _e('Update partial', 'comicpress') ?>" />
</form>
<?php
}
}
2009-08-04 01:28:59 +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;
}
2009-07-20 22:27:12 +00:00
/**
* Callback to send the reader to a random comic.
*/
2009-07-13 08:30:01 +00:00
function go_to_random_comic() {
$random_comic_query = new WP_Query();
$random_comic_query->query('showposts=1&orderby=rand&cat=' . $this->comicpress->get_all_comic_categories_as_cat_string());
while ($random_comic_query->have_posts()) {
$random_comic_query->the_post();
$random_comic_id = get_the_ID();
break;
}
if (!empty($random_comic_id)) {
wp_redirect(get_permalink( $random_comic_id ));
}
}
2009-07-20 22:27:12 +00:00
/**
* Markup to insert a comic into the comic feed.
*/
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
}
2009-07-20 22:27:12 +00:00
/**
* Filter to insert the comic into the RSS feed.
*/
2009-07-13 02:31:14 +00:00
function insert_comic_feed($content) {
if (is_feed() && in_comic_category()) {
return $this->comic_feed() . $content;
} else {
return $content;
}
}
2009-07-20 22:27:12 +00:00
/**
* Set up the admin interface and meta boxes.
*/
function setup_admin_interface() {
2009-11-02 01:20:40 +00:00
global $plugin_page, $pagenow, $post;
2009-11-01 18:15:59 +00:00
2009-08-06 02:29:54 +00:00
add_theme_page(__("ComicPress Core", 'comicpress'), __('ComicPress Core', 'comicpress'), 'edit_themes', 'comicpress/render_admin', array(&$this, 'render_admin'));
add_theme_page(__("Edit Partials", 'comicpress'), __('Edit Partials', 'comicpress'), 'edit_themes', 'comicpress/edit_partials', array(&$this, 'render_edit_partials'));
2009-11-02 01:20:40 +00:00
if (strpos($page, "edit") === 0) {
var_dump($post);
add_meta_box("comic-image-ordering", __("Comic Image Ordering", 'comicpress'), array(&$this, 'render_comic_image_ordering'), "post", "normal", "low");
}
2009-11-01 18:15:59 +00:00
if ($plugin_page == 'comicpress/render_admin') {
wp_enqueue_style('cp-admin', get_stylesheet_directory_uri() . '/css/cp-admin.css');
wp_enqueue_script('cp-admin', get_stylesheet_directory_uri() . '/js/Storyline.js', array('jquery', 'jquery-ui-sortable'));
}
2009-11-02 01:20:40 +00:00
if (strpos($pagenow, "media-upload") === 0) {
wp_enqueue_script('cp-media', get_stylesheet_directory_uri() . '/js/MediaUpload.js', array('jquery'));
}
}
2009-07-11 00:27:36 +00:00
2009-07-20 22:27:12 +00:00
/**
* Show comic media.
* @param object $override_post If not nul;, the post to use instead of the current Loop post.
* @param string $method The method to call on the comic post.
*/
2009-08-04 01:28:59 +00:00
function show_media($override_post, $method, $format) {
global $post;
$post_to_use = $post;
2009-07-16 01:34:34 +00:00
switch ($this->comicpress->comicpress_options['comic_space']) {
case "comic_only":
$comic_post = new ComicPressComicPost($post_to_use, &$this->comicpress);
2009-08-04 01:28:59 +00:00
$comic_post->{$method}($format);
2009-07-16 01:34:34 +00:00
break;
case "post_content":
$t = $post;
$post = $post_to_use;
include_partial('index-blog-post');
$post = $t;
break;
}
2009-07-13 02:31:14 +00:00
}
2009-07-20 22:27:12 +00:00
/**
* Show a comic.
*/
2009-08-04 01:28:59 +00:00
function show_comic($override_post = null, $format = "%s<br />") {
$this->show_media($override_post, "display_comics", $format);
}
2009-07-20 22:27:12 +00:00
/**
* Show an archive comic.
*/
function show_archive($override_post = null, $format = "%s<br />") {
2009-08-04 01:28:59 +00:00
$this->show_media($override_post, "display_archive", $format);
2009-07-13 02:31:14 +00:00
}
2009-07-20 22:27:12 +00:00
/**
* Show an RSS comic.
*/
2009-08-04 01:28:59 +00:00
function show_rss($override_post = null, $format = "%s<br />") {
$this->show_media($override_post, "display_rss", $format);
2009-07-13 02:31:14 +00:00
}
2009-07-20 22:27:12 +00:00
/**
* Modify the Media Gallery for ComicPress use.
*/
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-11-02 01:20:40 +00:00
2009-07-11 00:27:36 +00:00
$current_type = get_post_meta($post->ID, 'comic_image_type', true);
2009-11-02 01:20:40 +00:00
2009-07-11 00:27:36 +00:00
$field_html_lines = array();
$field_html_lines[] = '<input type="hidden" name="cp[_nonce]" value="' . wp_create_nonce('comicpress') . '" />';
2009-07-22 22:18:57 +00:00
foreach ($this->comic_image_types as $field => $label) {
2009-07-11 00:27:36 +00:00
$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
2009-07-20 22:27:12 +00:00
/**
* Render the admin interface.
*/
2009-07-08 23:51:02 +00:00
function render_admin() {
$nonce = wp_create_nonce('comicpress');
$root_categories = $this->get_root_categories();
$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
include(dirname(__FILE__) . '/partials/options-admin.inc');
}
2009-11-01 18:15:59 +00:00
function _render_admin_storyline_tree($node, $parent_id = "0") {
foreach ($node as $category_id => $children) {
$category = get_category($category_id);
echo '<div class="cp-category-info category-' . $parent_id . '/' . $category_id . '">';
echo '<span>' . $category->name . '</span>';
if (is_array($children)) {
echo '<div class="cp-children">';
$this->_render_admin_storyline_tree($children, $parent_id . '/' . $category_id);
echo '</div>';
}
echo '</div>';
}
}
2009-08-06 02:29:54 +00:00
function render_edit_partials() {
$nonce = wp_create_nonce('comicpress');
include(dirname(__FILE__) . '/partials/edit-partials.inc');
}
2009-07-08 23:51:02 +00:00
2009-07-20 22:27:12 +00:00
/**
* Render the comic image ordering interface.
*/
function render_comic_image_ordering() {
2009-07-22 22:18:57 +00:00
if (isset($_REQUEST['post'])) {
2009-08-01 15:05:39 +00:00
$comic_post = new ComicPressComicPost(get_post($_REQUEST['post']), &$this->comicpress);
2009-07-22 22:18:57 +00:00
$ordering = $comic_post->normalize_comic_image_ordering();
echo '<input type="hidden" name="cp[_nonce]" value="' . wp_create_nonce('comicpress') . '" />';
2009-07-22 22:18:57 +00:00
if (is_array($ordering)) {
foreach ($ordering as $type => $attachment_ids) {
echo '<h3>' . $this->comic_image_types[$type] . '</h3>';
$index = 1;
foreach ($attachment_ids as $attachment_id) {
echo '<img src="' . wp_get_attachment_url($attachment_id) . '" width="60" height="60" />';
2009-08-01 15:05:39 +00:00
echo '<input size="3" type="text" name="cp[ordering][' . $type . '][' . $attachment_id . ']" value="' . $index . '" />';
++$index;
2009-07-22 22:18:57 +00:00
}
}
}
}
}
2009-07-20 22:27:12 +00:00
/**
* Get all categories with a parent ID of 0.
* @return array All root categories.
*/
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;
}
2009-07-20 22:27:12 +00:00
/**
* Create &lt;option&gt; elements for each of the provided categories.
* @param array $categories The categories to display as either IDs or category objects.
* @param int $selected_id The category to mark as selected.
* @return string The category options as HTML.
*/
2009-07-08 23:51:02 +00:00
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);
}
2009-07-20 22:27:12 +00:00
/**
* 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.
*/
2009-07-08 23:51:02 +00:00
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);
}
2009-11-01 18:15:59 +00:00
/**
2009-07-21 17:40:24 +00:00
* Update attachment information.
2009-07-20 22:27:12 +00:00
*/
2009-07-21 17:40:24 +00:00
function handle_update_attachments() {
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-11 00:27:36 +00:00
}
2009-07-21 17:40:24 +00:00
if (isset($settings['auto_attach']) && isset($settings['post_parent'])) {
$media_post = get_post($post_id);
$media_post->post_parent = $settings['post_parent'];
wp_update_post($media_post);
}
}
}
/**
* Update ComicPress options.
*/
2009-11-01 18:15:59 +00:00
function handle_update_comicpress_options($info) {
foreach (array('helpers', 'options') as $type) {
$this->comicpress->comicpress_options[$type] = array();
}
2009-07-21 17:40:24 +00:00
foreach ($this->comicpress->comicpress_options as $option => $value) {
2009-11-01 18:15:59 +00:00
if (isset($info[$option])) {
2009-07-21 17:40:24 +00:00
switch ($option) {
case 'comic_category_id':
2009-11-01 18:15:59 +00:00
if (is_numeric($info[$option])) {
$result = get_category($info[$option]);
2009-07-21 17:40:24 +00:00
if (!(is_a($result, 'WP_Error') || empty($result))) {
2009-11-01 18:15:59 +00:00
$this->comicpress->comicpress_options[$option] = $info[$option];
2009-07-21 17:40:24 +00:00
}
}
break;
case 'comic_dimensions':
case 'rss_dimensions':
case 'archive_dimensions':
case 'mini_dimensions':
2009-11-01 18:15:59 +00:00
if (is_array($info[$option])) {
2009-07-21 17:40:24 +00:00
$dim_parts = array();
$is_valid = true;
foreach (array('width', 'height') as $field) {
2009-11-01 18:15:59 +00:00
$requested_dim = trim($info[$option][$field]);
2009-07-21 17:40:24 +00:00
if ($requested_dim == "") {
$dim_parts[] = $requested_dim;
} else {
if ((int)$requested_dim == $requested_dim) {
2009-07-08 23:51:02 +00:00
$dim_parts[] = $requested_dim;
} else {
2009-07-21 17:40:24 +00:00
$is_valid = false; break;
2009-07-08 23:51:02 +00:00
}
}
}
2009-07-21 17:40:24 +00:00
if ($is_valid) {
$this->comicpress->comicpress_options[$option] = implode("x", $dim_parts);
}
}
break;
case 'blogpost_count':
2009-11-01 18:15:59 +00:00
$this->comicpress->comicpress_options[$option] = (int)$info[$option];
2009-07-21 17:40:24 +00:00
break;
case 'comic_space':
2009-07-29 02:38:20 +00:00
case 'category_usage':
2009-07-23 22:25:02 +00:00
case 'layout';
2009-11-01 18:15:59 +00:00
$this->comicpress->comicpress_options[$option] = $info[$option];
break;
case 'helpers':
case 'addons':
2009-11-01 18:15:59 +00:00
foreach ($info[$option] as $type => $set) {
$this->comicpress->comicpress_options[$option][$type] = true;
}
break;
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-07-08 23:51:02 +00:00
}
}
2009-07-21 17:40:24 +00:00
}
}
function handle_update_comic_ordering() {
if (is_numeric($_POST['post_ID'])) {
if ($post = get_post($_POST['post_ID'])) {
$comic_post = new ComicPressComicPost(&$post);
$comic_post->change_comic_image_ordering($_POST['cp']['ordering']);
2009-08-04 01:28:59 +00:00
if (isset($this->comicpress->comicpress_options['helpers']['show_inline_comic_ordering']) && !is_admin()) {
$this->info(sprintf(__("Comic ordering for post #%s updated", 'comicpress'), $_POST['post_ID']));
}
}
}
}
2009-08-06 02:29:54 +00:00
function handle_update_override_partial($info) {
switch ($info['action']) {
case __('Update partial', 'comicpress'):
2009-08-07 19:50:06 +00:00
$this->comicpress->comicpress_options['override_partials'][$info['partial']] = stripslashes($info['code']);
2009-08-06 02:29:54 +00:00
break;
case __('Delete override partial', 'comicpress'):
unset($this->comicpress->comicpress_options['override_partials'][$info['partial']]);
break;
}
}
2009-07-21 17:40:24 +00:00
/**
* Handle an update.
*/
2009-11-01 18:15:59 +00:00
function handle_update($info) {
2009-07-21 17:40:24 +00:00
if (isset($_POST['attachments'])) {
//coming from media editor
$this->handle_update_attachments();
2009-11-01 18:15:59 +00:00
} else if (is_array($info['ordering'])) {
2009-08-01 15:05:39 +00:00
// comic ordering
2009-11-01 18:15:59 +00:00
/*
2009-08-04 01:28:59 +00:00
if (isset($_POST['meta'])) {
$meta_key_to_ignore = false;
foreach ($_POST['meta'] as $meta_key => $params) {
foreach ($params as $type => $value) {
if ($type == "key" && $value == "comic_ordering") {
$meta_key_to_ignore = $meta_key; break;
}
}
}
2009-08-04 01:28:59 +00:00
if ($meta_key_to_ignore !== false) {
unset($_POST['meta'][$meta_key_to_ignore]);
}
}
2009-11-01 18:15:59 +00:00
$this->handle_update_comic_ordering($info);
*/
} else if (isset($info['partial'])) {
$this->handle_update_override_partial($info);
$this->info(sprintf(__("Partial %s updated.", 'comicpress'), $info['partial']));
2009-08-06 02:29:54 +00:00
$this->comicpress->save();
$this->comicpress->init();
2009-07-21 17:40:24 +00:00
} else {
//coming from us
2009-11-01 18:15:59 +00:00
$this->handle_update_comicpress_options($info);
2009-07-21 17:40:24 +00:00
2009-07-11 00:27:36 +00:00
$this->comicpress->save();
2009-08-06 02:29:54 +00:00
$this->info(__("ComicPress configuration updated.", 'comicpress'));
$this->comicpress->init();
2009-07-08 23:51:02 +00:00
}
}
2009-07-23 00:55:32 +00:00
2009-07-23 22:25:02 +00:00
/**
* Create the dropdown for choosing a layout.
*/
2009-07-23 18:43:15 +00:00
function create_layout_options($layouts, $current_layout) {
$output = array();
2009-07-23 22:25:02 +00:00
foreach ($layouts as $layout_filename => $info) {
$output[] = '<option value="' . $layout_filename . '"' . (($layout_filename == $current_layout) ? ' selected="selected"' : '') . '>' . $info['Layout Name'] . '</option>';
2009-07-23 18:43:15 +00:00
}
return implode("\n", $output);
}
2009-07-08 23:51:02 +00:00
}
2009-08-07 19:50:06 +00:00
?>