Index: wp-ajax.php
===================================================================
--- wp-ajax.php	(revision 0)
+++ wp-ajax.php	(revision 0)
@@ -0,0 +1,42 @@
+<?php
+/**
+ * WordPress Primary AJAX Process Execution.
+ *
+ * @package WordPress
+ */
+
+/**
+ * Executing AJAX process.
+ *
+ * @since 2.1.0
+ */
+define( 'DOING_AJAX', true );
+
+if ( ! isset( $_REQUEST['action'] ) )
+	die( '-1' );
+
+if ( ! defined( 'ABSPATH' ) )
+	require_once( dirname( __FILE__ ) . '/wp-load.php' );
+
+if ( defined( 'WP_ADMIN' ) && WP_ADMIN )
+	require_once( ABSPATH . 'wp-admin/includes/admin.php' );
+
+@header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) );
+send_nosniff_header();
+
+if ( defined( 'WP_ADMIN' ) && WP_ADMIN )
+	do_action( 'admin_init' );
+
+if ( ! is_user_logged_in() ) {
+	if ( !empty( $_REQUEST['action'] ) )
+		do_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] );
+	die('-1');
+}
+
+if ( isset( $_REQUEST['action'] ) ) {
+	do_action( 'wp_ajax_' . $_REQUEST['action'] );
+	die( '0' );
+} else {
+	die( '1' );
+}
+?>
Index: wp-admin/admin-ajax.php
===================================================================
--- wp-admin/admin-ajax.php	(revision 17763)
+++ wp-admin/admin-ajax.php	(working copy)
@@ -1,57 +1,84 @@
 <?php
 /**
- * WordPress AJAX Process Execution.
+ * WordPress AJAX Process Execution for admin.
  *
  * @package WordPress
  * @subpackage Administration
  */
-
-/**
- * Executing AJAX process.
- *
- * @since 2.1.0
- */
-define('DOING_AJAX', true);
 define('WP_ADMIN', true);
 
-if ( ! isset( $_REQUEST['action'] ) )
-	die('-1');
+if ( ! defined( 'ABSPATH' ) )
+	require_once( '../wp-load.php' );
 
-require_once('../wp-load.php');
-
-require_once('./includes/admin.php');
-@header('Content-Type: text/html; charset=' . get_option('blog_charset'));
-send_nosniff_header();
-
-do_action('admin_init');
-
 if ( ! is_user_logged_in() ) {
+	if ( isset( $_POST['action'] ) )
+		add_action( 'wp_ajax_nopriv_autosave', 'ajax_logged_out_autosave' );
+} else {
+	if ( isset( $_GET['action'] ) ) {
+		add_action( 'wp_ajax_fetch-list', 'ajax_fetch_list' );
+		add_action( 'wp_ajax_ajax-tag-search', 'ajax_tag_search' );
+		add_action( 'wp_ajax_wp-compression-test', 'ajax_wp_compression_test' );
+		add_action( 'wp_ajax_imgedit-preview', 'ajax_imgedit_preview' );
+		add_action( 'wp_ajax_menu-quick-search', 'ajax_menu_quick_search' );
+		add_action( 'wp_ajax_oembed-cache', 'ajax_oembed_cache' );
+	} else if ( isset( $_POST['action'] ) ) {
+		add_action( 'wp_ajax_delete-comment', 'ajax_delete_comment' );
+		add_action( 'wp_ajax_delete-tag', 'ajax_delete_tag' );
+		add_action( 'wp_ajax_delete-link', 'ajax_delete_link' );
+		add_action( 'wp_ajax_delete-meta', 'ajax_delete_meta' );
+		add_action( 'wp_ajax_delete-post', 'ajax_delete_post' );
+		add_action( 'wp_ajax_trash-post', 'ajax_trash_untrash_post' );
+		add_action( 'wp_ajax_untrash-post', 'ajax_trash_untrash_post' );
+		add_action( 'wp_ajax_delete-page', 'ajax_delete_page' );
+		add_action( 'wp_ajax_dim-comment', 'ajax_dim_comment' );
+		add_action( 'wp_ajax_add-link-category', 'ajax_add_link_category' );
+		add_action( 'wp_ajax_add-tag', 'ajax_add_tag' );
+		add_action( 'wp_ajax_get-tagcloud', 'ajax_get_tagcloud' );
+		add_action( 'wp_ajax_get-comments', 'ajax_get_comments' );
+		add_action( 'wp_ajax_replyto-comment', 'ajax_replyto_comment' );
+		add_action( 'wp_ajax_edit-comment', 'ajax_edit_comment' );
+		add_action( 'wp_ajax_add-menu-item', 'ajax_add_menu_item' );
+		add_action( 'wp_ajax_add-meta', 'ajax_add_meta' );
+		add_action( 'wp_ajax_add-user', 'ajax_add_user' );
+		add_action( 'wp_ajax_autosave', 'ajax_autosave' );
+		add_action( 'wp_ajax_closed-postboxes', 'ajax_closed_postboxes' );
+		add_action( 'wp_ajax_hidden-columns', 'ajax_hidden_columns' );
+		add_action( 'wp_ajax_menu-get-metabox', 'ajax_menu_get_metabox' );
+		add_action( 'wp_ajax_menu-quick-search', 'ajax_menu_quick_search' );
+		add_action( 'wp_ajax_wp-link-ajax', 'ajax_wp_link_ajax' );
+		add_action( 'wp_ajax_menu-locations-save', 'ajax_menu_locations_save' );
+		add_action( 'wp_ajax_meta-box-order', 'ajax_meta_box_order' );
+		add_action( 'wp_ajax_get-permalink', 'ajax_get_permalink' );
+		add_action( 'wp_ajax_sample-permalink', 'ajax_sample_permalink' );
+		add_action( 'wp_ajax_inline-save', 'ajax_inline_save' );
+		add_action( 'wp_ajax_inline-save-tax', 'ajax_inline_save_tax' );
+		add_action( 'wp_ajax_find_posts', 'ajax_find_posts' );
+		add_action( 'wp_ajax_widgets-order', 'ajax_widgets_order' );
+		add_action( 'wp_ajax_save-widget', 'ajax_save_widget' );
+		add_action( 'wp_ajax_image-editor', 'ajax_image_editor' );
+		add_action( 'wp_ajax_set-post-thumbnail', 'ajax_set_post_thumbnail' );
+		add_action( 'wp_ajax_date_format', 'ajax_date_format' );
+		add_action( 'wp_ajax_time_format', 'ajax_time_format' );
+		add_action( 'wp_ajax_wp-fullscreen-save-post', 'ajax_wp_fullscreen_save_post' );
+	}
+}
 
-	if ( isset( $_POST['action'] ) && $_POST['action'] == 'autosave' ) {
-		$id = isset($_POST['post_ID'])? (int) $_POST['post_ID'] : 0;
+function ajax_logged_out_autosave() {
+	$id = isset($_POST['post_ID'])? (int) $_POST['post_ID'] : 0;
 
-		if ( ! $id )
-			die('-1');
+	if ( ! $id )
+		die('-1');
 
-		$message = sprintf( __('<strong>ALERT: You are logged out!</strong> Could not save draft. <a href="%s" target="_blank">Please log in again.</a>'), wp_login_url() );
-		$x = new WP_Ajax_Response( array(
-			'what' => 'autosave',
-			'id' => $id,
-			'data' => $message
-		) );
-		$x->send();
-	}
-
-	if ( !empty( $_REQUEST['action'] ) )
-		do_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] );
-
-	die('-1');
+	$message = sprintf( __('<strong>ALERT: You are logged out!</strong> Could not save draft. <a href="%s" target="_blank">Please log in again.</a>'), wp_login_url() );
+	$x = new WP_Ajax_Response( array(
+		'what' => 'autosave',
+		'id' => $id,
+		'data' => $message
+	) );
+	$x->send();
 }
 
-if ( isset( $_GET['action'] ) ) :
-switch ( $action = $_GET['action'] ) :
-case 'fetch-list' :
-
+function ajax_fetch_list() {
 	$list_class = $_GET['list_args']['class'];
 	check_ajax_referer( "fetch-list-$list_class", '_ajax_fetch_list_nonce' );
 
@@ -73,8 +100,9 @@
 	$wp_list_table->ajax_response();
 
 	die( '0' );
-	break;
-case 'ajax-tag-search' :
+}
+
+function ajax_tag_search() {
 	if ( isset( $_GET['tax'] ) ) {
 		$taxonomy = sanitize_key( $_GET['tax'] );
 		$tax = get_taxonomy( $taxonomy );
@@ -100,8 +128,9 @@
 
 	echo join( $results, "\n" );
 	die;
-	break;
-case 'wp-compression-test' :
+}
+
+function wp_compression_test() {
 	if ( !current_user_can( 'manage_options' ) )
 		die('-1');
 
@@ -144,8 +173,9 @@
 	}
 
 	die('0');
-	break;
-case 'imgedit-preview' :
+}
+
+function ajax_imgedit_preview() {
 	$post_id = intval($_GET['postid']);
 	if ( empty($post_id) || !current_user_can('edit_post', $post_id) )
 		die('-1');
@@ -157,8 +187,9 @@
 		die('-1');
 
 	die();
-	break;
-case 'menu-quick-search':
+}
+
+function admin_menu_quick_search() {
 	if ( ! current_user_can( 'edit_theme_options' ) )
 		die('-1');
 
@@ -167,18 +198,14 @@
 	_wp_ajax_menu_quick_search( $_REQUEST );
 
 	exit;
-	break;
-case 'oembed-cache' :
+}
+
+function oembed_cache() {
 	$return = ( $wp_embed->cache_oembed( $_GET['post'] ) ) ? '1' : '0';
 	die( $return );
-	break;
-default :
-	do_action( 'wp_ajax_' . $_GET['action'] );
-	die('0');
-	break;
-endswitch;
-endif;
+}
 
+
 /**
  * Sends back current comment total and new page links if they need to be updated.
  *
@@ -316,9 +343,8 @@
 	$x->send();
 }
 
-$id = isset($_POST['id'])? (int) $_POST['id'] : 0;
-switch ( $action = $_POST['action'] ) :
-case 'delete-comment' : // On success, die with time() instead of 1
+function ajax_delete_comment() { // On success, die with time() instead of 1
+	$id = isset($_POST['id'])? (int) $_POST['id'] : 0;
 	if ( !$comment = get_comment( $id ) )
 		die( (string) time() );
 	if ( !current_user_can( 'edit_post', $comment->comment_post_ID ) )
@@ -357,8 +383,9 @@
 	if ( $r ) // Decide if we need to send back '1' or a more complicated response including page links and comment counts
 		_wp_ajax_delete_comment_response( $comment->comment_ID, $delta );
 	die( '0' );
-	break;
-case 'delete-tag' :
+}
+
+function ajax_delete_tag() {
 	$tag_id = (int) $_POST['tag_ID'];
 	check_ajax_referer( "delete-tag_$tag_id" );
 
@@ -376,8 +403,10 @@
 		die('1');
 	else
 		die('0');
-	break;
-case 'delete-link' :
+}
+
+function ajax_delete_link() {
+	$id = isset($_POST['id'])? (int) $_POST['id'] : 0;
 	check_ajax_referer( "delete-bookmark_$id" );
 	if ( !current_user_can( 'manage_links' ) )
 		die('-1');
@@ -390,8 +419,10 @@
 		die('1');
 	else
 		die('0');
-	break;
-case 'delete-meta' :
+}
+
+function ajax_delete_meta() {
+	$id = isset($_POST['id'])? (int) $_POST['id'] : 0;
 	check_ajax_referer( "delete-meta_$id" );
 	if ( !$meta = get_post_meta_by_id( $id ) )
 		die('1');
@@ -401,8 +432,10 @@
 	if ( delete_meta( $meta->meta_id ) )
 		die('1');
 	die('0');
-	break;
-case 'delete-post' :
+}
+
+function ajax_delete_post() {
+	$id = isset($_POST['id'])? (int) $_POST['id'] : 0;
 	check_ajax_referer( "{$action}_$id" );
 	if ( !current_user_can( 'delete_post', $id ) )
 		die('-1');
@@ -414,9 +447,10 @@
 		die('1');
 	else
 		die('0');
-	break;
-case 'trash-post' :
-case 'untrash-post' :
+}
+
+function ajax_trash_untrash_post() {
+	$id = isset($_POST['id'])? (int) $_POST['id'] : 0;
 	check_ajax_referer( "{$action}_$id" );
 	if ( !current_user_can( 'delete_post', $id ) )
 		die('-1');
@@ -433,8 +467,10 @@
 		die('1');
 
 	die('0');
-	break;
-case 'delete-page' :
+}
+
+function ajax_delete_page() {
+	$id = isset($_POST['id'])? (int) $_POST['id'] : 0;
 	check_ajax_referer( "{$action}_$id" );
 	if ( !current_user_can( 'delete_page', $id ) )
 		die('-1');
@@ -446,9 +482,10 @@
 		die('1');
 	else
 		die('0');
-	break;
-case 'dim-comment' : // On success, die with time() instead of 1
+}
 
+function ajax_dim_comment() { // On success, die with time() instead of 1
+	$id = isset($_POST['id'])? (int) $_POST['id'] : 0;
 	if ( !$comment = get_comment( $id ) ) {
 		$x = new WP_Ajax_Response( array(
 			'what' => 'comment',
@@ -481,8 +518,9 @@
 	// Decide if we need to send back '1' or a more complicated response including page links and comment counts
 	_wp_ajax_delete_comment_response( $comment->comment_ID );
 	die( '0' );
-	break;
-case 'add-link-category' : // On the Fly
+}
+
+function ajax_add_link_category() { // On the Fly
 	check_ajax_referer( $action );
 	if ( !current_user_can( 'manage_categories' ) )
 		die('-1');
@@ -506,8 +544,9 @@
 		) );
 	}
 	$x->send();
-	break;
-case 'add-tag' :
+}
+
+function ajax_add_tag() {
 	check_ajax_referer( 'add-tag' );
 	$post_type = !empty($_POST['post_type']) ? $_POST['post_type'] : 'post';
 	$taxonomy = !empty($_POST['taxonomy']) ? $_POST['taxonomy'] : 'post_tag';
@@ -558,8 +597,9 @@
 		'supplemental' => (array) $tag
 		) );
 	$x->send();
-	break;
-case 'get-tagcloud' :
+}
+
+function ajax_get_tagcloud() {
 	if ( isset( $_POST['tax'] ) ) {
 		$taxonomy = sanitize_key( $_POST['tax'] );
 		$tax = get_taxonomy( $taxonomy );
@@ -593,8 +633,9 @@
 	echo $return;
 
 	exit;
-	break;
-case 'get-comments' :
+}
+
+function ajax_get_comments() {
 	check_ajax_referer( $action );
 
 	set_current_screen( 'edit-comments' );
@@ -623,8 +664,9 @@
 		'data' => $comment_list_item
 	) );
 	$x->send();
-	break;
-case 'replyto-comment' :
+}
+
+function ajax_replyto_comment() {
 	check_ajax_referer( $action, '_ajax_nonce-replyto-comment' );
 
 	set_current_screen( 'edit-comments' );
@@ -693,8 +735,9 @@
 	));
 
 	$x->send();
-	break;
-case 'edit-comment' :
+}
+
+function ajax_edit_comment() {
 	check_ajax_referer( 'replyto-comment', '_ajax_nonce-replyto-comment' );
 
 	set_current_screen( 'edit-comments' );
@@ -731,8 +774,9 @@
 	));
 
 	$x->send();
-	break;
-case 'add-menu-item' :
+}
+
+function ajax_add_menu_item() {
 	if ( ! current_user_can( 'edit_theme_options' ) )
 		die('-1');
 
@@ -793,8 +837,9 @@
 		);
 		echo walk_nav_menu_tree( $menu_items, 0, (object) $args );
 	}
-	break;
-case 'add-meta' :
+}
+
+function ajax_add_meta() {
 	check_ajax_referer( 'add-meta', '_ajax_nonce-add-meta' );
 	$c = 0;
 	$pid = (int) $_POST['post_id'];
@@ -875,8 +920,9 @@
 		) );
 	}
 	$x->send();
-	break;
-case 'add-user' :
+}
+
+function ajax_add_user() {
 	check_ajax_referer( $action );
 	if ( !current_user_can('create_users') )
 		die('-1');
@@ -903,8 +949,9 @@
 		)
 	) );
 	$x->send();
-	break;
-case 'autosave' : // The name of this action is hardcoded in edit_post()
+}
+
+function ajax_autosave() { // The name of this action is hardcoded in edit_post()
 	define( 'DOING_AUTOSAVE', true );
 
 	$nonce_age = check_ajax_referer( 'autosave', 'autosavenonce' );
@@ -998,8 +1045,9 @@
 		'supplemental' => $supplemental
 	) );
 	$x->send();
-	break;
-case 'closed-postboxes' :
+}
+
+function ajax_closed_postboxes() {
 	check_ajax_referer( 'closedpostboxes', 'closedpostboxesnonce' );
 	$closed = isset( $_POST['closed'] ) ? explode( ',', $_POST['closed']) : array();
 	$closed = array_filter($closed);
@@ -1024,8 +1072,9 @@
 	}
 
 	die('1');
-	break;
-case 'hidden-columns' :
+}
+
+function ajax_hidden_columns() {
 	check_ajax_referer( 'screen-options-nonce', 'screenoptionnonce' );
 	$hidden = isset( $_POST['hidden'] ) ? $_POST['hidden'] : '';
 	$hidden = explode( ',', $_POST['hidden'] );
@@ -1041,8 +1090,9 @@
 		update_user_option($user->ID, "manage{$page}columnshidden", $hidden, true);
 
 	die('1');
-	break;
-case 'menu-get-metabox' :
+}
+
+function ajax_menu_get_metabox() {
 	if ( ! current_user_can( 'edit_theme_options' ) )
 		die('-1');
 
@@ -1080,8 +1130,9 @@
 	}
 
 	exit;
-	break;
-case 'menu-quick-search':
+}
+
+function ajax_menu_quick_search() {
 	if ( ! current_user_can( 'edit_theme_options' ) )
 		die('-1');
 
@@ -1090,8 +1141,9 @@
 	_wp_ajax_menu_quick_search( $_REQUEST );
 
 	exit;
-	break;
-case 'wp-link-ajax':
+}
+
+function ajax_wp_link_ajax() {
 	require_once ABSPATH . 'wp-admin/includes/internal-linking.php';
 
 	check_ajax_referer( 'internal-linking', '_ajax_linking_nonce' );
@@ -1111,8 +1163,9 @@
 	echo "\n";
 
 	exit;
-	break;
-case 'menu-locations-save':
+}
+
+function ajax_menu_locations_save() {
 	if ( ! current_user_can( 'edit_theme_options' ) )
 		die('-1');
 	check_ajax_referer( 'add-menu_item', 'menu-settings-column-nonce' );
@@ -1120,8 +1173,9 @@
 		die('0');
 	set_theme_mod( 'nav_menu_locations', array_map( 'absint', $_POST['menu-locations'] ) );
 	die('1');
-	break;
-case 'meta-box-order':
+}
+
+function ajax_meta_box_order() {
 	check_ajax_referer( 'meta-box-order' );
 	$order = isset( $_POST['order'] ) ? (array) $_POST['order'] : false;
 	$page_columns = isset( $_POST['page_columns'] ) ? (int) $_POST['page_columns'] : 0;
@@ -1140,20 +1194,23 @@
 		update_user_option($user->ID, "screen_layout_$page", $page_columns, true);
 
 	die('1');
-	break;
-case 'get-permalink':
+}
+
+function ajax_get_permalink() {
 	check_ajax_referer( 'getpermalink', 'getpermalinknonce' );
 	$post_id = isset($_POST['post_id'])? intval($_POST['post_id']) : 0;
 	die(add_query_arg(array('preview' => 'true'), get_permalink($post_id)));
-break;
-case 'sample-permalink':
+}
+
+function ajax_sample_permalink() {
 	check_ajax_referer( 'samplepermalink', 'samplepermalinknonce' );
 	$post_id = isset($_POST['post_id'])? intval($_POST['post_id']) : 0;
 	$title = isset($_POST['new_title'])? $_POST['new_title'] : '';
 	$slug = isset($_POST['new_slug'])? $_POST['new_slug'] : null;
 	die(get_sample_permalink_html($post_id, $title, $slug));
-break;
-case 'inline-save':
+}
+
+function ajax_inline_save() {
 	check_ajax_referer( 'inlineeditnonce', '_inline_edit' );
 
 	if ( ! isset($_POST['post_ID']) || ! ( $post_ID = (int) $_POST['post_ID'] ) )
@@ -1210,8 +1267,9 @@
 	$wp_list_table->display_rows( array( get_post( $_POST['post_ID'] ) ) );
 
 	exit;
-	break;
-case 'inline-save-tax':
+}
+
+function ajax_inline_save_tax() {
 	check_ajax_referer( 'taxinlineeditnonce', '_inline_edit' );
 
 	$taxonomy = sanitize_key( $_POST['taxonomy'] );
@@ -1249,8 +1307,9 @@
 	}
 
 	exit;
-	break;
-case 'find_posts':
+}
+
+function ajax_find_posts() {
 	check_ajax_referer( 'find-posts' );
 
 	if ( empty($_POST['ps']) )
@@ -1319,9 +1378,9 @@
 		'data' => $html
 	));
 	$x->send();
+}
 
-	break;
-case 'widgets-order' :
+function ajax_widgets_order() {
 	check_ajax_referer( 'save-sidebar-widgets', 'savewidgets' );
 
 	if ( !current_user_can('edit_theme_options') )
@@ -1350,8 +1409,9 @@
 	}
 
 	die('-1');
-	break;
-case 'save-widget' :
+}
+
+function ajax_save_widget() {
 	check_ajax_referer( 'save-sidebar-widgets', 'savewidgets' );
 
 	if ( !current_user_can('edit_theme_options') || !isset($_POST['id_base']) )
@@ -1418,8 +1478,9 @@
 		call_user_func_array( $form['callback'], $form['params'] );
 
 	die();
-	break;
-case 'image-editor':
+}
+
+function ajax_image_editor() {
 	$attachment_id = intval($_POST['postid']);
 	if ( empty($attachment_id) || !current_user_can('edit_post', $attachment_id) )
 		die('-1');
@@ -1444,8 +1505,9 @@
 
 	wp_image_editor($attachment_id, $msg);
 	die();
-	break;
-case 'set-post-thumbnail':
+}
+
+function ajax_set_post_thumbnail() {
 	$post_ID = intval( $_POST['post_id'] );
 	if ( !current_user_can( 'edit_post', $post_ID ) )
 		die( '-1' );
@@ -1461,14 +1523,17 @@
 	if ( set_post_thumbnail( $post_ID, $thumbnail_id ) )
 		die( _wp_post_thumbnail_html( $thumbnail_id ) );
 	die( '0' );
-	break;
-case 'date_format' :
+}
+
+function ajax_date_format() {
 	die( date_i18n( sanitize_option( 'date_format', $_POST['date'] ) ) );
-	break;
-case 'time_format' :
+}
+
+function ajax_time_format() {
 	die( date_i18n( sanitize_option( 'time_format', $_POST['date'] ) ) );
-	break;
-case 'wp-fullscreen-save-post' :
+}
+
+function ajax_wp_fullscreen_save_post() {
 	if ( isset($_POST['post_ID']) )
 		$post_id = (int) $_POST['post_ID'];
 	else
@@ -1529,10 +1594,7 @@
 
 	echo json_encode( array( 'message' => $message, 'last_edited' => $last_edited ) );
 	die();
-	break;
-default :
-	do_action( 'wp_ajax_' . $_POST['action'] );
-	die('0');
-	break;
-endswitch;
+}
+
+include( '../wp-ajax.php' );
 ?>
