2 define('DOING_AJAX', true);
4 require_once('../wp-load.php');
5 require_once('includes/admin.php');
7 if ( !is_user_logged_in() )
10 if ( isset($_GET['action']) && 'ajax-tag-search' == $_GET['action'] ) {
11 if ( !current_user_can( 'manage_categories' ) )
14 $s = $_GET['q']; // is this slashed already?
16 if ( strstr( $s, ',' ) ) {
17 $s = explode( ',', $s );
18 $s = $s[count( $s ) - 1];
21 if ( strlen( $s ) < 2 )
22 die; // require 2 chars for matching
23 $results = $wpdb->get_col( "SELECT name FROM $wpdb->terms WHERE name LIKE ('%". $s . "%')" );
24 echo join( $results, "\n" );
28 $id = isset($_POST['id'])? (int) $_POST['id'] : 0;
29 switch ( $action = $_POST['action'] ) :
30 case 'delete-comment' :
31 check_ajax_referer( "delete-comment_$id" );
32 if ( !$comment = get_comment( $id ) )
34 if ( !current_user_can( 'edit_post', $comment->comment_post_ID ) )
37 if ( isset($_POST['spam']) && 1 == $_POST['spam'] ) {
38 if ( 'spam' == wp_get_comment_status( $comment->comment_ID ) )
40 $r = wp_set_comment_status( $comment->comment_ID, 'spam' );
42 $r = wp_delete_comment( $comment->comment_ID );
45 die( $r ? '1' : '0' );
48 check_ajax_referer( "delete-category_$id" );
49 if ( !current_user_can( 'manage_categories' ) )
52 $cat = get_category( $id );
53 if ( !$cat || is_wp_error( $cat ) )
56 if ( wp_delete_category( $id ) )
62 check_ajax_referer( "delete-tag_$id" );
63 if ( !current_user_can( 'manage_categories' ) )
66 $tag = get_term( $id, 'post_tag' );
67 if ( !$tag || is_wp_error( $tag ) )
70 if ( wp_delete_term($id, 'post_tag'))
75 case 'delete-link-cat' :
76 check_ajax_referer( "delete-link-category_$id" );
77 if ( !current_user_can( 'manage_categories' ) )
80 $cat = get_term( $id, 'link_category' );
81 if ( !$cat || is_wp_error( $cat ) )
84 $cat_name = get_term_field('name', $id, 'link_category');
86 // Don't delete the default cats.
87 if ( $id == get_option('default_link_category') ) {
88 $x = new WP_AJAX_Response( array(
91 'data' => new WP_Error( 'default-link-cat', sprintf(__("Can’t delete the <strong>%s</strong> category: this is the default one"), $cat_name) )
96 $r = wp_delete_term($id, 'link_category');
99 if ( is_wp_error($r) ) {
100 $x = new WP_AJAX_Response( array(
101 'what' => 'link-cat',
110 check_ajax_referer( "delete-bookmark_$id" );
111 if ( !current_user_can( 'manage_links' ) )
114 $link = get_bookmark( $id );
115 if ( !$link || is_wp_error( $link ) )
118 if ( wp_delete_link( $id ) )
124 check_ajax_referer( "delete-meta_$id" );
125 if ( !$meta = get_post_meta_by_id( $id ) )
128 if ( !current_user_can( 'edit_post', $meta->post_id ) )
130 if ( delete_meta( $meta->meta_id ) )
135 check_ajax_referer( "{$action}_$id" );
136 if ( !current_user_can( 'delete_post', $id ) )
139 if ( !get_post( $id ) )
142 if ( wp_delete_post( $id ) )
148 check_ajax_referer( "{$action}_$id" );
149 if ( !current_user_can( 'delete_page', $id ) )
152 if ( !get_page( $id ) )
155 if ( wp_delete_post( $id ) )
161 if ( !$comment = get_comment( $id ) )
164 if ( !current_user_can( 'edit_post', $comment->comment_post_ID ) )
166 if ( !current_user_can( 'moderate_comments' ) )
169 $current = wp_get_comment_status( $comment->comment_ID );
170 if ( $_POST['new'] == $current )
173 if ( 'unapproved' == $current ) {
174 check_ajax_referer( "approve-comment_$id" );
175 if ( wp_set_comment_status( $comment->comment_ID, 'approve' ) )
178 check_ajax_referer( "unapprove-comment_$id" );
179 if ( wp_set_comment_status( $comment->comment_ID, 'hold' ) )
184 case 'add-category' : // On the Fly
185 check_ajax_referer( $action );
186 if ( !current_user_can( 'manage_categories' ) )
188 $names = explode(',', $_POST['newcat']);
189 if ( 0 > $parent = (int) $_POST['newcat_parent'] )
191 $post_category = isset($_POST['post_category'])? (array) $_POST['post_category'] : array();
192 $checked_categories = array_map( 'absint', (array) $post_category );
193 $popular_ids = isset( $_POST['popular_ids'] ) ?
194 array_map( 'absint', explode( ',', $_POST['popular_ids'] ) ) :
197 $x = new WP_Ajax_Response();
198 foreach ( $names as $cat_name ) {
199 $cat_name = trim($cat_name);
200 $category_nicename = sanitize_title($cat_name);
201 if ( '' === $category_nicename )
203 $cat_id = wp_create_category( $cat_name, $parent );
204 $checked_categories[] = $cat_id;
205 if ( $parent ) // Do these all at once in a second
207 $category = get_category( $cat_id );
209 wp_category_checklist( 0, $cat_id, $checked_categories, $popular_ids );
210 $data = ob_get_contents();
213 'what' => 'category',
219 if ( $parent ) { // Foncy - replace the parent and all its children
220 $parent = get_category( $parent );
222 dropdown_categories( 0, $parent );
223 $data = ob_get_contents();
226 'what' => 'category',
227 'id' => $parent->term_id,
228 'old_id' => $parent->term_id,
236 case 'add-link-category' : // On the Fly
237 check_ajax_referer( $action );
238 if ( !current_user_can( 'manage_categories' ) )
240 $names = explode(',', $_POST['newcat']);
241 $x = new WP_Ajax_Response();
242 foreach ( $names as $cat_name ) {
243 $cat_name = trim($cat_name);
244 $slug = sanitize_title($cat_name);
247 if ( !$cat_id = is_term( $cat_name, 'link_category' ) ) {
248 $cat_id = wp_insert_term( $cat_name, 'link_category' );
250 $cat_id = $cat_id['term_id'];
251 $cat_name = wp_specialchars(stripslashes($cat_name));
253 'what' => 'link-category',
255 '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>",
261 case 'add-cat' : // From Manage->Categories
262 check_ajax_referer( 'add-category' );
263 if ( !current_user_can( 'manage_categories' ) )
266 if ( '' === trim($_POST['cat_name']) ) {
267 $x = new WP_Ajax_Response( array(
269 'id' => new WP_Error( 'cat_name', __('You did not enter a category name.') )
274 if ( category_exists( trim( $_POST['cat_name'] ) ) ) {
275 $x = new WP_Ajax_Response( array(
277 'id' => new WP_Error( 'cat_exists', __('The category you are trying to create already exists.'), array( 'form-field' => 'cat_name' ) ),
282 $cat = wp_insert_category( $_POST, true );
284 if ( is_wp_error($cat) ) {
285 $x = new WP_Ajax_Response( array(
292 if ( !$cat || (!$cat = get_category( $cat )) )
296 $cat_full_name = $cat->name;
298 while ( $_cat->parent ) {
299 $_cat = get_category( $_cat->parent );
300 $cat_full_name = $_cat->name . ' — ' . $cat_full_name;
303 $cat_full_name = attribute_escape($cat_full_name);
305 $x = new WP_Ajax_Response( array(
307 'id' => $cat->term_id,
308 'data' => _cat_row( $cat, $level, $cat_full_name ),
309 'supplemental' => array('name' => $cat_full_name, 'show-link' => sprintf(__( 'Category <a href="#%s">%s</a> added' ), "cat-$cat->term_id", $cat_full_name))
313 case 'add-link-cat' : // From Blogroll -> Categories
314 check_ajax_referer( 'add-link-category' );
315 if ( !current_user_can( 'manage_categories' ) )
318 if ( '' === trim($_POST['name']) ) {
319 $x = new WP_Ajax_Response( array(
320 'what' => 'link-cat',
321 'id' => new WP_Error( 'name', __('You did not enter a category name.') )
326 $r = wp_insert_term($_POST['name'], 'link_category', $_POST );
327 if ( is_wp_error( $r ) ) {
328 $x = new WP_AJAX_Response( array(
329 'what' => 'link-cat',
335 extract($r, EXTR_SKIP);
337 if ( !$link_cat = link_cat_row( $term_id ) )
340 $x = new WP_Ajax_Response( array(
341 'what' => 'link-cat',
347 case 'add-tag' : // From Manage->Tags
348 check_ajax_referer( 'add-tag' );
349 if ( !current_user_can( 'manage_categories' ) )
352 if ( '' === trim($_POST['name']) ) {
353 $x = new WP_Ajax_Response( array(
355 'id' => new WP_Error( 'name', __('You did not enter a tag name.') )
360 $tag = wp_insert_term($_POST['name'], 'post_tag', $_POST );
362 if ( is_wp_error($tag) ) {
363 $x = new WP_Ajax_Response( array(
370 if ( !$tag || (!$tag = get_term( $tag['term_id'], 'post_tag' )) )
373 $tag_full_name = $tag->name;
374 $tag_full_name = attribute_escape($tag_full_name);
376 $x = new WP_Ajax_Response( array(
378 'id' => $tag->term_id,
379 'data' => _tag_row( $tag ),
380 'supplemental' => array('name' => $tag_full_name, 'show-link' => sprintf(__( 'Tag <a href="#%s">%s</a> added' ), "tag-$tag->term_id", $tag_full_name))
385 check_ajax_referer( $action );
386 if ( !current_user_can( 'edit_post', $id ) )
388 $search = isset($_POST['s']) ? $_POST['s'] : false;
389 $start = isset($_POST['page']) ? intval($_POST['page']) * 25 - 1: 24;
390 $status = isset($_POST['comment_status']) ? $_POST['comment_status'] : false;
391 $mode = isset($_POST['mode']) ? $_POST['mode'] : 'detail';
393 list($comments, $total) = _wp_get_comment_list( $status, $search, $start, 1 );
395 if ( get_option('show_avatars') )
396 add_filter( 'comment_author', 'floated_admin_avatar' );
400 $x = new WP_Ajax_Response();
401 foreach ( (array) $comments as $comment ) {
402 get_comment( $comment );
404 _wp_comment_row( $comment->comment_ID, $mode, $status );
405 $comment_list_item = ob_get_contents();
409 'id' => $comment->comment_ID,
410 'data' => $comment_list_item
416 check_ajax_referer( 'add-meta' );
418 $pid = (int) $_POST['post_id'];
419 if ( isset($_POST['metakeyselect']) || isset($_POST['metakeyinput']) ) {
420 if ( !current_user_can( 'edit_post', $pid ) )
422 if ( '#NONE#' == $_POST['metakeyselect'] && empty($_POST['metakeyinput']) )
425 $now = current_time('timestamp', 1);
426 if ( $pid = wp_insert_post( array(
427 'post_title' => sprintf('Draft created on %s at %s', date(get_option('date_format'), $now), date(get_option('time_format'), $now))
429 if ( is_wp_error( $pid ) ) {
430 $x = new WP_Ajax_Response( array(
436 $mid = add_meta( $pid );
440 } else if ( !$mid = add_meta( $pid ) ) {
444 $meta = get_post_meta_by_id( $mid );
445 $pid = (int) $meta->post_id;
446 $meta = get_object_vars( $meta );
447 $x = new WP_Ajax_Response( array(
450 'data' => _list_meta_row( $meta, $c ),
452 'supplemental' => array('postid' => $pid)
455 $mid = (int) array_pop(array_keys($_POST['meta']));
456 $key = $_POST['meta'][$mid]['key'];
457 $value = $_POST['meta'][$mid]['value'];
458 if ( !$meta = get_post_meta_by_id( $mid ) )
459 die('0'); // if meta doesn't exist
460 if ( !current_user_can( 'edit_post', $meta->post_id ) )
462 if ( !$u = update_meta( $mid, $key, $value ) )
463 die('1'); // We know meta exists; we also know it's unchanged (or DB error, in which case there are bigger problems).
464 $key = stripslashes($key);
465 $value = stripslashes($value);
466 $x = new WP_Ajax_Response( array(
468 'id' => $mid, 'old_id' => $mid,
469 'data' => _list_meta_row( array(
471 'meta_value' => $value,
475 'supplemental' => array('postid' => $meta->post_id)
481 check_ajax_referer( $action );
482 if ( !current_user_can('create_users') )
484 require_once(ABSPATH . WPINC . '/registration.php');
485 if ( !$user_id = add_user() )
487 elseif ( is_wp_error( $user_id ) ) {
488 $x = new WP_Ajax_Response( array(
494 $user_object = new WP_User( $user_id );
496 $x = new WP_Ajax_Response( array(
499 'data' => user_row( $user_object, '', $user_object->roles[0] ),
500 'supplemental' => array(
501 'show-link' => sprintf(__( 'User <a href="#%s">%s</a> added' ), "user-$user_id", $user_object->user_login),
502 'role' => $user_object->roles[0]
507 case 'autosave' : // The name of this action is hardcoded in edit_post()
508 define( 'DOING_AUTOSAVE', true );
510 $nonce_age = check_ajax_referer( 'autosave', 'autosavenonce' );
511 global $current_user;
513 $_POST['post_category'] = explode(",", $_POST['catslist']);
514 $_POST['tags_input'] = explode(",", $_POST['tags_input']);
515 if($_POST['post_type'] == 'page' || empty($_POST['post_category']))
516 unset($_POST['post_category']);
518 $do_autosave = (bool) $_POST['autosave'];
522 $message = sprintf( __('Draft Saved at %s.'), date( __('g:i:s a'), current_time( 'timestamp', true ) ) );
524 $supplemental = array();
526 $id = $revision_id = 0;
527 if($_POST['post_ID'] < 0) {
528 $_POST['post_status'] = 'draft';
529 $_POST['temp_ID'] = $_POST['post_ID'];
530 if ( $do_autosave ) {
531 $id = wp_write_post();
535 $post_ID = (int) $_POST['post_ID'];
536 $_POST['ID'] = $post_ID;
537 $post = get_post($post_ID);
539 if ( $last = wp_check_post_lock( $post->ID ) ) {
540 $do_autosave = $do_lock = false;
542 $last_user = get_userdata( $last );
543 $last_user_name = $last_user ? $last_user->display_name : __( 'Someone' );
544 $data = new WP_Error( 'locked', sprintf(
545 $_POST['post_type'] == 'page' ? __( 'Autosave disabled: %s is currently editing this page.' ) : __( 'Autosave disabled: %s is currently editing this post.' ),
546 wp_specialchars( $last_user_name )
549 $supplemental['disable_autosave'] = 'disable';
552 if ( 'page' == $post->post_type ) {
553 if ( !current_user_can('edit_page', $post_ID) )
554 die(__('You are not allowed to edit this page.'));
556 if ( !current_user_can('edit_post', $post_ID) )
557 die(__('You are not allowed to edit this post.'));
560 if ( $do_autosave ) {
561 // Drafts are just overwritten by autosave
562 if ( 'draft' == $post->post_status ) {
564 } else { // Non drafts are not overwritten. The autosave is stored in a special post revision.
565 $revision_id = wp_create_post_autosave( $post->ID );
566 if ( is_wp_error($revision_id) )
577 if ( $do_lock && $id && is_numeric($id) )
578 wp_set_post_lock( $id );
580 if ( $nonce_age == 2 ) {
581 $supplemental['replace-autosavenonce'] = wp_create_nonce('autosave');
582 $supplemental['replace-getpermalinknonce'] = wp_create_nonce('getpermalink');
583 $supplemental['replace-samplepermalinknonce'] = wp_create_nonce('samplepermalink');
584 $supplemental['replace-closedpostboxesnonce'] = wp_create_nonce('closedpostboxes');
586 if ( $_POST['post_type'] == 'post' )
587 $supplemental['replace-_wpnonce'] = wp_create_nonce('update-post_' . $id);
588 elseif ( $_POST['post_type'] == 'page' )
589 $supplemental['replace-_wpnonce'] = wp_create_nonce('update-page_' . $id);
593 $x = new WP_Ajax_Response( array(
594 'what' => 'autosave',
596 'data' => $id ? $data : '',
597 'supplemental' => $supplemental
601 case 'autosave-generate-nonces' :
602 check_ajax_referer( 'autosave', 'autosavenonce' );
603 $ID = (int) $_POST['post_ID'];
604 if($_POST['post_type'] == 'post') {
605 if(current_user_can('edit_post', $ID))
606 die(wp_create_nonce('update-post_' . $ID));
608 if($_POST['post_type'] == 'page') {
609 if(current_user_can('edit_page', $ID)) {
610 die(wp_create_nonce('update-page_' . $ID));
615 case 'closed-postboxes' :
616 check_ajax_referer( 'closedpostboxes', 'closedpostboxesnonce' );
617 $closed = isset( $_POST['closed'] )? $_POST['closed'] : '';
618 $closed = explode( ',', $_POST['closed'] );
619 $page = isset( $_POST['page'] )? $_POST['page'] : '';
620 if ( !preg_match( '/^[a-z-]+$/', $page ) ) {
623 if (!is_array($closed)) break;
624 $current_user = wp_get_current_user();
625 update_usermeta($current_user->ID, 'closedpostboxes_'.$page, $closed);
627 case 'get-permalink':
628 check_ajax_referer( 'getpermalink', 'getpermalinknonce' );
629 $post_id = isset($_POST['post_id'])? intval($_POST['post_id']) : 0;
630 die(add_query_arg(array('preview' => 'true'), get_permalink($post_id)));
632 case 'sample-permalink':
633 check_ajax_referer( 'samplepermalink', 'samplepermalinknonce' );
634 $post_id = isset($_POST['post_id'])? intval($_POST['post_id']) : 0;
635 $title = isset($_POST['new_title'])? $_POST['new_title'] : '';
636 $slug = isset($_POST['new_slug'])? $_POST['new_slug'] : '';
637 die(get_sample_permalink_html($post_id, $title, $slug));
640 do_action( 'wp_ajax_' . $_POST['action'] );