2 require_once('../wp-config.php');
3 require_once('includes/admin.php');
5 define('DOING_AJAX', true);
8 if ( !is_user_logged_in() )
11 function get_out_now() { exit; }
12 add_action( 'shutdown', 'get_out_now', -1 );
14 function wp_ajax_meta_row( $pid, $mid, $key, $value ) {
15 $value = attribute_escape($value);
16 $key_js = addslashes(wp_specialchars($key, 'double'));
17 $key = attribute_escape($key);
18 $r .= "<tr id='meta-$mid'><td valign='top'>";
19 $r .= "<input name='meta[$mid][key]' tabindex='6' onkeypress='return killSubmit(\"theList.ajaxUpdater('meta','meta-$mid');\",event);' type='text' size='20' value='$key' />";
20 $r .= "</td><td><textarea name='meta[$mid][value]' tabindex='6' rows='2' cols='30'>$value</textarea></td><td align='center'>";
21 $r .= "<input name='updatemeta' type='button' class='updatemeta' tabindex='6' value='".attribute_escape(__('Update'))."' onclick='return theList.ajaxUpdater('meta','meta-$mid');' /><br />";
22 $r .= "<input name='deletemeta[$mid]' type='submit' onclick=\"return deleteSomething( 'meta', $mid, '";
23 $r .= js_escape(sprintf(__("You are about to delete the '%s' custom field on this post.\n'OK' to delete, 'Cancel' to stop."), $key_js));
24 $r .= "' );\" class='deletemeta' tabindex='6' value='".attribute_escape(__('Delete'))."' /></td></tr>";
28 $id = (int) $_POST['id'];
29 switch ( $_POST['action'] ) :
30 case 'delete-comment' :
31 if ( !$comment = get_comment( $id ) )
33 if ( !current_user_can( 'edit_post', $comment->comment_post_ID ) )
36 if ( wp_delete_comment( $comment->comment_ID ) )
40 case 'delete-comment-as-spam' :
41 if ( !$comment = get_comment( $id ) )
43 if ( !current_user_can( 'edit_post', $comment->comment_post_ID ) )
46 if ( wp_set_comment_status( $comment->comment_ID, 'spam' ) )
51 if ( !current_user_can( 'manage_categories' ) )
54 if ( wp_delete_category( $id ) )
59 if ( !current_user_can( 'manage_links' ) )
62 if ( wp_delete_link( $id ) )
67 if ( !$meta = get_post_meta_by_id( $id ) )
69 if ( !current_user_can( 'edit_post', $meta->post_id ) )
71 if ( delete_meta( $meta->meta_id ) )
76 if ( !current_user_can( 'delete_post', $id ) )
79 if ( wp_delete_post( $id ) )
84 if ( !current_user_can( 'delete_page', $id ) )
87 if ( wp_delete_post( $id ) )
92 if ( !$comment = get_comment( $id ) )
94 if ( !current_user_can( 'edit_post', $comment->comment_post_ID ) )
96 if ( !current_user_can( 'moderate_comments' ) )
99 if ( 'unapproved' == wp_get_comment_status($comment->comment_ID) ) {
100 if ( wp_set_comment_status( $comment->comment_ID, 'approve' ) )
103 if ( wp_set_comment_status( $comment->comment_ID, 'hold' ) )
108 case 'add-category' : // On the Fly
109 if ( !current_user_can( 'manage_categories' ) )
111 $names = explode(',', $_POST['newcat']);
112 $x = new WP_Ajax_Response();
113 foreach ( $names as $cat_name ) {
114 $cat_name = trim($cat_name);
115 if ( !$category_nicename = sanitize_title($cat_name) )
117 if ( !$cat_id = category_exists( $cat_name ) )
118 $cat_id = wp_create_category( $cat_name );
119 $cat_name = wp_specialchars(stripslashes($cat_name));
121 'what' => 'category',
123 'data' => "<li id='category-$cat_id'><label for='in-category-$cat_id' class='selectit'><input value='$cat_id' type='checkbox' checked='checked' name='post_category[]' id='in-category-$cat_id'/> $cat_name</label></li>"
128 case 'add-link-category' : // On the Fly
129 if ( !current_user_can( 'manage_categories' ) )
131 $names = explode(',', $_POST['newcat']);
132 $x = new WP_Ajax_Response();
133 foreach ( $names as $cat_name ) {
134 $cat_name = trim($cat_name);
135 if ( !$slug = sanitize_title($cat_name) )
137 if ( !$cat_id = is_term( $cat_name, 'link_category' ) ) {
138 $cat_id = wp_insert_term( $cat_name, 'link_category' );
139 $cat_id = $cat_id['term_id'];
141 $cat_name = wp_specialchars(stripslashes($cat_name));
143 'what' => 'link-category',
145 'data' => "<li id='link-category-$cat_id'><label for='in-link-category-$cat_id' class='selectit'><input value='$cat_id' type='checkbox' checked='checked' name='link_category[]' id='in-link-category-$cat_id'/> $cat_name</label></li>"
150 case 'add-cat' : // From Manage->Categories
151 if ( !current_user_can( 'manage_categories' ) )
153 if ( !$cat = wp_insert_category( $_POST ) )
155 if ( !$cat = get_category( $cat ) )
158 $cat_full_name = $cat->cat_name;
160 while ( $_cat->category_parent ) {
161 $_cat = get_category( $_cat->category_parent );
162 $cat_full_name = $_cat->cat_name . ' — ' . $cat_full_name;
165 $cat_full_name = attribute_escape($cat_full_name);
167 $x = new WP_Ajax_Response( array(
169 'id' => $cat->cat_ID,
170 'data' => _cat_row( $cat, $level, $cat_full_name ),
171 'supplemental' => array('name' => $cat_full_name, 'show-link' => sprintf(__( 'Category <a href="#%s">%s</a> added' ), "cat-$cat->cat_ID", $cat_full_name))
176 if ( !current_user_can( 'edit_post', $id ) )
178 $search = isset($_POST['s']) ? $_POST['s'] : false;
179 $start = isset($_POST['page']) ? intval($_POST['page']) * 25 : 25;
181 list($comments, $total) = _wp_get_comment_list( $search, $start, 1 );
185 $x = new WP_Ajax_Response();
186 foreach ( (array) $comments as $comment ) {
187 get_comment( $comment );
189 _wp_comment_list_item( $comment->comment_ID );
190 $comment_list_item = ob_get_contents();
194 'id' => $comment->comment_ID,
195 'data' => $comment_list_item
201 if ( !current_user_can( 'edit_post', $id ) )
204 $now = current_time('timestamp', 1);
205 if ( $pid = wp_insert_post( array(
206 'post_title' => sprintf('Draft created on %s at %s', date(get_option('date_format'), $now), date(get_option('time_format'), $now))
208 if ( is_wp_error( $pid ) )
210 $mid = add_meta( $pid );
214 } else if ( !$mid = add_meta( $id ) ) {
218 $meta = get_post_meta_by_id( $mid );
219 $key = $meta->meta_key;
220 $value = $meta->meta_value;
221 $pid = (int) $meta->post_id;
223 $x = new WP_Ajax_Response( array(
226 'data' => wp_ajax_meta_row( $pid, $mid, $key, $value ),
227 'supplemental' => array('postid' => $pid)
232 $mid = (int) array_pop(array_keys($_POST['meta']));
233 $key = $_POST['meta'][$mid]['key'];
234 $value = $_POST['meta'][$mid]['value'];
235 if ( !$meta = get_post_meta_by_id( $mid ) )
236 die('0'); // if meta doesn't exist
237 if ( !current_user_can( 'edit_post', $meta->post_id ) )
239 if ( $u = update_meta( $mid, $key, $value ) ) {
240 $key = stripslashes($key);
241 $value = stripslashes($value);
242 $x = new WP_Ajax_Response( array(
245 'data' => wp_ajax_meta_row( $meta->post_id, $mid, $key, $value ),
246 'supplemental' => array('postid' => $meta->post_id)
250 die('1'); // We know meta exists; we also know it's unchanged (or DB error, in which case there are bigger problems).
253 if ( !current_user_can('edit_users') )
255 require_once(ABSPATH . WPINC . '/registration.php');
256 if ( !$user_id = add_user() )
258 elseif ( is_wp_error( $user_id ) ) {
259 foreach( $user_id->get_error_messages() as $message )
260 echo "<p>$message<p>";
263 $user_object = new WP_User( $user_id );
264 $x = new WP_Ajax_Response( array(
267 'data' => user_row( $user_object ),
268 'supplemental' => array('show-link' => sprintf(__( 'User <a href="#%s">%s</a> added' ), "user-$user_id", $user_object->user_login))
272 case 'autosave' : // The name of this action is hardcoded in edit_post()
273 $_POST['post_content'] = $_POST['content'];
274 $_POST['post_excerpt'] = $_POST['excerpt'];
275 $_POST['post_status'] = 'draft';
276 $_POST['post_category'] = explode(",", $_POST['catslist']);
277 if($_POST['post_type'] == 'page' || empty($_POST['post_category']))
278 unset($_POST['post_category']);
280 if($_POST['post_ID'] < 0) {
281 $_POST['temp_ID'] = $_POST['post_ID'];
282 $id = wp_write_post();
283 if( is_wp_error($id) )
284 die($id->get_error_message());
288 $post_ID = (int) $_POST['post_ID'];
289 $_POST['ID'] = $post_ID;
290 $post = get_post($post_ID);
291 if ( 'page' == $post->post_type ) {
292 if ( !current_user_can('edit_page', $post_ID) )
293 die(__('You are not allowed to edit this page.'));
295 if ( !current_user_can('edit_post', $post_ID) )
296 die(__('You are not allowed to edit this post.'));
298 wp_update_post($_POST);
302 case 'autosave-generate-nonces' :
303 $ID = (int) $_POST['post_ID'];
304 if($_POST['post_type'] == 'post') {
305 if(current_user_can('edit_post', $ID))
306 die(wp_create_nonce('update-post_' . $ID));
308 if($_POST['post_type'] == 'page') {
309 if(current_user_can('edit_page', $ID)) {
310 die(wp_create_nonce('update-page_' . $ID));
316 do_action( 'wp_ajax_' . $_POST['action'] );