active = false;
}
function header_inserts() {
if ('edit.php' == $GLOBALS['pagenow']) {
echo '' . "\n";
echo '';
$this->active = true;
}
}
function process_request() {
// Quick exit if it's not a wp-cats request.
if (isset($_REQUEST['wp_cats'])) {
$this->active = true;
switch($_REQUEST['wp_cats']) {
case 'cats':
remove_filter('list_cats', 'wptexturize');
echo '
';
break;
case 'add':
case 'remove':
global $wp_query;
(isset($_POST['post_id']) and isset($_POST['cat_name']) and isset($_POST['wp_cats']))
or die('Malformed WP-Cats request.');
$post_id = (int)$_POST['post_id'];
$cat_id = (string)get_cat_id($_POST['cat_name']);
current_user_can('edit_post', $post_id) or die('You don\'t have permission to modify this entry.');
($cat_id != 0) or die('Bad category number.');
$post_cats = wp_get_post_categories($post_id);
if (count($post_cats) == 1 && 'add' == $_POST['wp_cats'] && get_cat_name($post_cats[0]) == __('Uncategorized')) {
$post_cats = array();
}
$cat_search = array_search($cat_id, $post_cats);
if ('add' == $_POST['wp_cats'] and $cat_search === false) {
$post_cats[] = $cat_id;
}
if ('remove' == $_POST['wp_cats'] and $cat_search !== false) {
unset($post_cats[$cat_search]);
$post_cats = array_merge($post_cats); // re-number indices
}
wp_set_post_categories($post_id, $post_cats);
// Make sure we've got the latest news.
unset($GLOBALS['category_cache']);
// Spoof "get_the_category" into giving us categories for the post we want.
//$GLOBALS['post']->ID = $post_id;
// This is copied directly from wp-admin/edit-post-rows.php
$categories = get_the_category($post_id);
if ( !empty( $categories ) ) {
$out = array();
foreach ( $categories as $c )
$out[] = " " . wp_specialchars(sanitize_term_field('name', $c->name, $c->term_id, 'category', 'display')) . "";
echo join( ', ', $out );
} else {
_e('Uncategorized');
}
break;
case 'js':
header("Content-type: text/javascript");
echo $this->js();
break;
case 'css':
header("Content-type: text/css");
echo $this->css();
break;
default:
echo 'Unrecognized WP-Cats request.';
}
die();
}
}
function js()
{
return <<(.*?)<\/error>/.exec(data);
if (!error) {
target_div.html(data);
attach_page_handlers_row($(target_div).parents('tr:first')[0]);
} else {
// Produce error message, restore previous contents.
alert(error[1]);
//div_contents.fadeIn("fast");
}
});
$("span:first", target_div).fadeOut("fast");
}
function create_add_handler(post_id, popup) {
return function() {
// Start the request before doing our eye-candy stuff.
last_cat_name = $(this).text()
post_request({
"wp_cats": "add",
"post_id": post_id,
"cat_name": last_cat_name
},
$(this).parents("td:first"));
popup.trigger("finished");
return false;
};
}
function create_dblclick_handler(post_id) {
return function() {
if (last_cat_name) {
post_request({
"wp_cats": "add",
"post_id": post_id,
"cat_name": last_cat_name
},
$(this).parents("td:first"));
}
return false;
};
}
function create_remove_handler(post_id, cat_name) {
return function() {
post_request({
"wp_cats": "remove",
"post_id": post_id,
"cat_name": cat_name
},
$(this).parents("td:first"));
return false;
};
}
function attach_page_handlers_row(row) {
var post_id = /post-([0-9]+)/.exec(row.id)[1];
var first = true;
$("a[href^='edit.php?category_name=']", row).each(function() {
if (first) {
// insert the plus-button.
$(this)
.before('+')
.prev('a')
.click(create_popup_handler(post_id))
.dblclick(create_dblclick_handler(post_id));
first = false;
}
var cat_name = /category_name=([^ ]+)/.exec(this.href)[1];
$(this)
.after('-')
.next('a')
.click(create_remove_handler(post_id, cat_name));
});
}
function attach_page_handlers() {
$("tr[id^='post-']").each(function() {
attach_page_handlers_row(this);
});
}
attach_page_handlers();
});
EOJS;
}
function css() {
$css_extra = '';
if (isset($_GET['foo'])) { die(var_dump($this)); }
if ($GLOBALS['wp_cats_mozilla_columns'])
$css_extra .= '.wp-cats-popup { height: ' . $GLOBALS['wp_cats_mozilla_column_height'] . '; -moz-column-width: 120px; padding-right: 135px; }';
return <<