active = false; } function header_inserts() { if ('edit.php' == $GLOBALS['pagenow']) { echo '' . "\n"; echo ''; $this->active = true; } } function wrap_the_category($category_html) { if ($this->active) { // This is the hook that the JavaScript will use to find the category links to // add Ajaxy stuff to. We include an extra span with the post id in it to reduce // the dependency on markup structure we can't control. $category_html = '' . $GLOBALS['post']->ID . '' . $category_html . ''; } return $category_html; } 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 "the_category" into giving us categories for the post we want. $GLOBALS['post']->ID = $post_id; the_category(', '); 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) { var new_contents = $(data).hide(); div_contents.remove(); target_div.append(new_contents); attach_page_handlers(); new_contents.fadeIn("fast"); } 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) { return function() { // This is sketchty. I'd prefer to do this... some other way. var cat_name = $(this).prev("a:first").text(); post_request({ "wp_cats": "remove", "post_id": post_id, "cat_name": cat_name }, $(this).parents("td:first")); return false; }; } function attach_page_handlers() { $("span.wp_cats_ajaxify").each(function(index) { var post_id = $('span.wp_cats_post_id', this).remove().text(); $("a", this) .after('-') .next('a') .click(create_remove_handler(post_id)); $(this) .prepend('+') .children('a:first') .click(create_popup_handler(post_id)) .dblclick(create_dblclick_handler(post_id)); $(this) .removeClass('wp_cats_ajaxify'); }); } 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 <<