3 * WordPress AJAX Process Execution.
6 * @subpackage Administration
10 * Executing AJAX process.
14 define('DOING_AJAX', true);
15 define('WP_ADMIN', true);
17 require_once('../wp-load.php');
18 require_once('includes/admin.php');
20 if ( ! is_user_logged_in() ) {
22 if ( $_POST['action'] == 'autosave' ) {
23 $id = isset($_POST['post_ID'])? (int) $_POST['post_ID'] : 0;
28 $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() );
29 $x = new WP_Ajax_Response( array(
40 if ( isset( $_GET['action'] ) ) :
41 switch ( $action = $_GET['action'] ) :
42 case 'ajax-tag-search' :
43 if ( !current_user_can( 'manage_categories' ) )
46 $s = $_GET['q']; // is this slashed already?
48 if ( false !== strpos( $s, ',' ) ) {
49 $s = explode( ',', $s );
50 $s = $s[count( $s ) - 1];
53 if ( strlen( $s ) < 2 )
54 die; // require 2 chars for matching
55 $results = $wpdb->get_col( "SELECT t.name FROM $wpdb->term_taxonomy AS tt INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = 'post_tag' AND t.name LIKE ('%". $s . "%')" );
56 echo join( $results, "\n" );
60 do_action( 'wp_ajax_' . $_GET['action'] );
66 $id = isset($_POST['id'])? (int) $_POST['id'] : 0;
67 switch ( $action = $_POST['action'] ) :
68 case 'delete-comment' :
69 check_ajax_referer( "delete-comment_$id" );
70 if ( !$comment = get_comment( $id ) )
72 if ( !current_user_can( 'edit_post', $comment->comment_post_ID ) )
75 if ( isset($_POST['spam']) && 1 == $_POST['spam'] ) {
76 if ( 'spam' == wp_get_comment_status( $comment->comment_ID ) )
78 $r = wp_set_comment_status( $comment->comment_ID, 'spam' );
80 $r = wp_delete_comment( $comment->comment_ID );
83 die( $r ? '1' : '0' );
86 check_ajax_referer( "delete-category_$id" );
87 if ( !current_user_can( 'manage_categories' ) )
90 $cat = get_category( $id );
91 if ( !$cat || is_wp_error( $cat ) )
94 if ( wp_delete_category( $id ) )
100 check_ajax_referer( "delete-tag_$id" );
101 if ( !current_user_can( 'manage_categories' ) )
104 $tag = get_term( $id, 'post_tag' );
105 if ( !$tag || is_wp_error( $tag ) )
108 if ( wp_delete_term($id, 'post_tag'))
113 case 'delete-link-cat' :
114 check_ajax_referer( "delete-link-category_$id" );
115 if ( !current_user_can( 'manage_categories' ) )
118 $cat = get_term( $id, 'link_category' );
119 if ( !$cat || is_wp_error( $cat ) )
122 $cat_name = get_term_field('name', $id, 'link_category');
124 // Don't delete the default cats.
125 if ( $id == get_option('default_link_category') ) {
126 $x = new WP_AJAX_Response( array(
127 'what' => 'link-cat',
129 'data' => new WP_Error( 'default-link-cat', sprintf(__("Can’t delete the <strong>%s</strong> category: this is the default one"), $cat_name) )
134 $r = wp_delete_term($id, 'link_category');
137 if ( is_wp_error($r) ) {
138 $x = new WP_AJAX_Response( array(
139 'what' => 'link-cat',
148 check_ajax_referer( "delete-bookmark_$id" );
149 if ( !current_user_can( 'manage_links' ) )
152 $link = get_bookmark( $id );
153 if ( !$link || is_wp_error( $link ) )
156 if ( wp_delete_link( $id ) )
162 check_ajax_referer( "delete-meta_$id" );
163 if ( !$meta = get_post_meta_by_id( $id ) )
166 if ( !current_user_can( 'edit_post', $meta->post_id ) )
168 if ( delete_meta( $meta->meta_id ) )
173 check_ajax_referer( "{$action}_$id" );
174 if ( !current_user_can( 'delete_post', $id ) )
177 if ( !get_post( $id ) )
180 if ( wp_delete_post( $id ) )
186 check_ajax_referer( "{$action}_$id" );
187 if ( !current_user_can( 'delete_page', $id ) )
190 if ( !get_page( $id ) )
193 if ( wp_delete_post( $id ) )
199 if ( !$comment = get_comment( $id ) )
202 if ( !current_user_can( 'edit_post', $comment->comment_post_ID ) )
204 if ( !current_user_can( 'moderate_comments' ) )
207 $current = wp_get_comment_status( $comment->comment_ID );
208 if ( $_POST['new'] == $current )
211 if ( in_array( $current, array( 'unapproved', 'spam' ) ) ) {
212 check_ajax_referer( "approve-comment_$id" );
213 if ( wp_set_comment_status( $comment->comment_ID, 'approve' ) )
216 check_ajax_referer( "unapprove-comment_$id" );
217 if ( wp_set_comment_status( $comment->comment_ID, 'hold' ) )
222 case 'add-category' : // On the Fly
223 check_ajax_referer( $action );
224 if ( !current_user_can( 'manage_categories' ) )
226 $names = explode(',', $_POST['newcat']);
227 if ( 0 > $parent = (int) $_POST['newcat_parent'] )
229 $post_category = isset($_POST['post_category'])? (array) $_POST['post_category'] : array();
230 $checked_categories = array_map( 'absint', (array) $post_category );
231 $popular_ids = isset( $_POST['popular_ids'] ) ?
232 array_map( 'absint', explode( ',', $_POST['popular_ids'] ) ) :
235 $x = new WP_Ajax_Response();
236 foreach ( $names as $cat_name ) {
237 $cat_name = trim($cat_name);
238 $category_nicename = sanitize_title($cat_name);
239 if ( '' === $category_nicename )
241 $cat_id = wp_create_category( $cat_name, $parent );
242 $checked_categories[] = $cat_id;
243 if ( $parent ) // Do these all at once in a second
245 $category = get_category( $cat_id );
247 wp_category_checklist( 0, $cat_id, $checked_categories, $popular_ids );
248 $data = ob_get_contents();
251 'what' => 'category',
257 if ( $parent ) { // Foncy - replace the parent and all its children
258 $parent = get_category( $parent );
260 dropdown_categories( 0, $parent );
261 $data = ob_get_contents();
264 'what' => 'category',
265 'id' => $parent->term_id,
266 'old_id' => $parent->term_id,
274 case 'add-link-category' : // On the Fly
275 check_ajax_referer( $action );
276 if ( !current_user_can( 'manage_categories' ) )
278 $names = explode(',', $_POST['newcat']);
279 $x = new WP_Ajax_Response();
280 foreach ( $names as $cat_name ) {
281 $cat_name = trim($cat_name);
282 $slug = sanitize_title($cat_name);
285 if ( !$cat_id = is_term( $cat_name, 'link_category' ) ) {
286 $cat_id = wp_insert_term( $cat_name, 'link_category' );
288 $cat_id = $cat_id['term_id'];
289 $cat_name = wp_specialchars(stripslashes($cat_name));
291 'what' => 'link-category',
293 '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>",
299 case 'add-cat' : // From Manage->Categories
300 check_ajax_referer( 'add-category' );
301 if ( !current_user_can( 'manage_categories' ) )
304 if ( '' === trim($_POST['cat_name']) ) {
305 $x = new WP_Ajax_Response( array(
307 'id' => new WP_Error( 'cat_name', __('You did not enter a category name.') )
312 if ( category_exists( trim( $_POST['cat_name'] ) ) ) {
313 $x = new WP_Ajax_Response( array(
315 'id' => new WP_Error( 'cat_exists', __('The category you are trying to create already exists.'), array( 'form-field' => 'cat_name' ) ),
320 $cat = wp_insert_category( $_POST, true );
322 if ( is_wp_error($cat) ) {
323 $x = new WP_Ajax_Response( array(
330 if ( !$cat || (!$cat = get_category( $cat )) )
334 $cat_full_name = $cat->name;
336 while ( $_cat->parent ) {
337 $_cat = get_category( $_cat->parent );
338 $cat_full_name = $_cat->name . ' — ' . $cat_full_name;
341 $cat_full_name = attribute_escape($cat_full_name);
343 $x = new WP_Ajax_Response( array(
345 'id' => $cat->term_id,
347 'data' => _cat_row( $cat, $level, $cat_full_name ),
348 'supplemental' => array('name' => $cat_full_name, 'show-link' => sprintf(__( 'Category <a href="#%s">%s</a> added' ), "cat-$cat->term_id", $cat_full_name))
352 case 'add-link-cat' : // From Blogroll -> Categories
353 check_ajax_referer( 'add-link-category' );
354 if ( !current_user_can( 'manage_categories' ) )
357 if ( '' === trim($_POST['name']) ) {
358 $x = new WP_Ajax_Response( array(
359 'what' => 'link-cat',
360 'id' => new WP_Error( 'name', __('You did not enter a category name.') )
365 $r = wp_insert_term($_POST['name'], 'link_category', $_POST );
366 if ( is_wp_error( $r ) ) {
367 $x = new WP_AJAX_Response( array(
368 'what' => 'link-cat',
374 extract($r, EXTR_SKIP);
376 if ( !$link_cat = link_cat_row( $term_id ) )
379 $x = new WP_Ajax_Response( array(
380 'what' => 'link-cat',
387 case 'add-tag' : // From Manage->Tags
388 check_ajax_referer( 'add-tag' );
389 if ( !current_user_can( 'manage_categories' ) )
392 if ( '' === trim($_POST['name']) ) {
393 $x = new WP_Ajax_Response( array(
395 'id' => new WP_Error( 'name', __('You did not enter a tag name.') )
400 $tag = wp_insert_term($_POST['name'], 'post_tag', $_POST );
402 if ( is_wp_error($tag) ) {
403 $x = new WP_Ajax_Response( array(
410 if ( !$tag || (!$tag = get_term( $tag['term_id'], 'post_tag' )) )
413 $tag_full_name = $tag->name;
414 $tag_full_name = attribute_escape($tag_full_name);
416 $x = new WP_Ajax_Response( array(
418 'id' => $tag->term_id,
420 'data' => _tag_row( $tag ),
421 'supplemental' => array('name' => $tag_full_name, 'show-link' => sprintf(__( 'Tag <a href="#%s">%s</a> added' ), "tag-$tag->term_id", $tag_full_name))
425 case 'get-tagcloud' :
426 if ( !current_user_can( 'manage_categories' ) )
429 $tags = get_tags( array( 'number' => 45, 'orderby' => 'count', 'order' => 'DESC' ) );
431 if ( empty( $tags ) )
432 die( __('No tags found!') );
434 if ( is_wp_error($tags) )
435 die($tags->get_error_message());
437 foreach ( $tags as $key => $tag ) {
438 $tags[ $key ]->link = '#';
439 $tags[ $key ]->id = $tag->term_id;
442 $return = wp_generate_tag_cloud( $tags );
444 if ( empty($return) )
452 check_ajax_referer( $action );
453 if ( !current_user_can( 'edit_post', $id ) )
455 $search = isset($_POST['s']) ? $_POST['s'] : false;
456 $start = isset($_POST['page']) ? intval($_POST['page']) * 25 - 1: 24;
457 $status = isset($_POST['comment_status']) ? $_POST['comment_status'] : false;
458 $mode = isset($_POST['mode']) ? $_POST['mode'] : 'detail';
459 $p = isset($_POST['p']) ? $_POST['p'] : 0;
460 $comment_type = isset($_POST['comment_type']) ? $_POST['comment_type'] : '';
461 list($comments, $total) = _wp_get_comment_list( $status, $search, $start, 1, $p, $comment_type );
463 if ( get_option('show_avatars') )
464 add_filter( 'comment_author', 'floated_admin_avatar' );
468 $x = new WP_Ajax_Response();
469 foreach ( (array) $comments as $comment ) {
470 get_comment( $comment );
472 _wp_comment_row( $comment->comment_ID, $mode, $status, true, true );
473 $comment_list_item = ob_get_contents();
477 'id' => $comment->comment_ID,
478 'data' => $comment_list_item
483 case 'get-comments' :
484 check_ajax_referer( $action );
486 $post_ID = (int) $_POST['post_ID'];
487 if ( !current_user_can( 'edit_post', $post_ID ) )
490 $start = isset($_POST['start']) ? intval($_POST['start']) : 0;
491 $num = isset($_POST['num']) ? intval($_POST['num']) : 10;
493 list($comments, $total) = _wp_get_comment_list( false, false, $start, $num, $post_ID );
498 $comment_list_item = '';
499 $x = new WP_Ajax_Response();
500 foreach ( (array) $comments as $comment ) {
501 get_comment( $comment );
503 _wp_comment_row( $comment->comment_ID, 'single', false, false );
504 $comment_list_item .= ob_get_contents();
508 'what' => 'comments',
509 'data' => $comment_list_item
513 case 'replyto-comment' :
514 check_ajax_referer( $action );
516 $comment_post_ID = (int) $_POST['comment_post_ID'];
517 if ( !current_user_can( 'edit_post', $comment_post_ID ) )
520 $status = $wpdb->get_var( $wpdb->prepare("SELECT post_status FROM $wpdb->posts WHERE ID = %d", $comment_post_ID) );
522 if ( empty($status) )
524 elseif ( in_array($status, array('draft', 'pending') ) )
525 die( __('Error: you are replying to a comment on a draft post.') );
527 $user = wp_get_current_user();
529 $comment_author = $wpdb->escape($user->display_name);
530 $comment_author_email = $wpdb->escape($user->user_email);
531 $comment_author_url = $wpdb->escape($user->user_url);
532 $comment_content = trim($_POST['content']);
533 if ( current_user_can('unfiltered_html') ) {
534 if ( wp_create_nonce('unfiltered-html-comment_' . $comment_post_ID) != $_POST['_wp_unfiltered_html_comment'] ) {
535 kses_remove_filters(); // start with a clean slate
536 kses_init_filters(); // set up the filters
540 die( __('Sorry, you must be logged in to reply to a comment.') );
543 if ( '' == $comment_content )
544 die( __('Error: please type a comment.') );
546 $comment_parent = absint($_POST['comment_ID']);
547 $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type', 'comment_parent', 'user_ID');
549 $comment_id = wp_new_comment( $commentdata );
550 $comment = get_comment($comment_id);
551 if ( ! $comment ) die('1');
553 $modes = array( 'single', 'detail', 'dashboard' );
554 $mode = isset($_POST['mode']) && in_array( $_POST['mode'], $modes ) ? $_POST['mode'] : 'detail';
555 $position = ( isset($_POST['position']) && (int) $_POST['position']) ? (int) $_POST['position'] : '-1';
556 $checkbox = ( isset($_POST['checkbox']) && true == $_POST['checkbox'] ) ? 1 : 0;
558 if ( get_option('show_avatars') && 'single' != $mode )
559 add_filter( 'comment_author', 'floated_admin_avatar' );
561 $x = new WP_Ajax_Response();
564 if ( 'dashboard' == $mode ) {
565 require_once( ABSPATH . 'wp-admin/includes/dashboard.php' );
566 _wp_dashboard_recent_comments_row( $comment, false );
568 _wp_comment_row( $comment->comment_ID, $mode, false, $checkbox );
570 $comment_list_item = ob_get_contents();
575 'id' => $comment->comment_ID,
576 'data' => $comment_list_item,
577 'position' => $position
582 case 'edit-comment' :
583 check_ajax_referer( 'replyto-comment' );
585 $comment_post_ID = (int) $_POST['comment_post_ID'];
586 if ( ! current_user_can( 'edit_post', $comment_post_ID ) )
589 if ( '' == $_POST['content'] )
590 die( __('Error: please type a comment.') );
592 $comment_id = (int) $_POST['comment_ID'];
593 $_POST['comment_status'] = $_POST['status'];
596 $mode = ( isset($_POST['mode']) && 'single' == $_POST['mode'] ) ? 'single' : 'detail';
597 $position = ( isset($_POST['position']) && (int) $_POST['position']) ? (int) $_POST['position'] : '-1';
598 $checkbox = ( isset($_POST['checkbox']) && true == $_POST['checkbox'] ) ? 1 : 0;
600 if ( get_option('show_avatars') && 'single' != $mode )
601 add_filter( 'comment_author', 'floated_admin_avatar' );
603 $x = new WP_Ajax_Response();
606 _wp_comment_row( $comment_id, $mode, true, $checkbox );
607 $comment_list_item = ob_get_contents();
611 'what' => 'edit_comment',
612 'id' => $comment->comment_ID,
613 'data' => $comment_list_item,
614 'position' => $position
620 check_ajax_referer( 'add-meta' );
622 $pid = (int) $_POST['post_id'];
623 if ( isset($_POST['metakeyselect']) || isset($_POST['metakeyinput']) ) {
624 if ( !current_user_can( 'edit_post', $pid ) )
626 if ( '#NONE#' == $_POST['metakeyselect'] && empty($_POST['metakeyinput']) )
629 $now = current_time('timestamp', 1);
630 if ( $pid = wp_insert_post( array(
631 'post_title' => sprintf('Draft created on %s at %s', date(get_option('date_format'), $now), date(get_option('time_format'), $now))
633 if ( is_wp_error( $pid ) ) {
634 $x = new WP_Ajax_Response( array(
640 $mid = add_meta( $pid );
644 } else if ( !$mid = add_meta( $pid ) ) {
648 $meta = get_post_meta_by_id( $mid );
649 $pid = (int) $meta->post_id;
650 $meta = get_object_vars( $meta );
651 $x = new WP_Ajax_Response( array(
654 'data' => _list_meta_row( $meta, $c ),
656 'supplemental' => array('postid' => $pid)
659 $mid = (int) array_pop(array_keys($_POST['meta']));
660 $key = $_POST['meta'][$mid]['key'];
661 $value = $_POST['meta'][$mid]['value'];
662 if ( !$meta = get_post_meta_by_id( $mid ) )
663 die('0'); // if meta doesn't exist
664 if ( !current_user_can( 'edit_post', $meta->post_id ) )
666 if ( !$u = update_meta( $mid, $key, $value ) )
667 die('1'); // We know meta exists; we also know it's unchanged (or DB error, in which case there are bigger problems).
668 $key = stripslashes($key);
669 $value = stripslashes($value);
670 $x = new WP_Ajax_Response( array(
672 'id' => $mid, 'old_id' => $mid,
673 'data' => _list_meta_row( array(
675 'meta_value' => $value,
679 'supplemental' => array('postid' => $meta->post_id)
685 check_ajax_referer( $action );
686 if ( !current_user_can('create_users') )
688 require_once(ABSPATH . WPINC . '/registration.php');
689 if ( !$user_id = add_user() )
691 elseif ( is_wp_error( $user_id ) ) {
692 $x = new WP_Ajax_Response( array(
698 $user_object = new WP_User( $user_id );
700 $x = new WP_Ajax_Response( array(
703 'data' => user_row( $user_object, '', $user_object->roles[0] ),
704 'supplemental' => array(
705 'show-link' => sprintf(__( 'User <a href="#%s">%s</a> added' ), "user-$user_id", $user_object->user_login),
706 'role' => $user_object->roles[0]
711 case 'autosave' : // The name of this action is hardcoded in edit_post()
712 define( 'DOING_AUTOSAVE', true );
714 $nonce_age = check_ajax_referer( 'autosave', 'autosavenonce' );
715 global $current_user;
717 $_POST['post_category'] = explode(",", $_POST['catslist']);
718 $_POST['tags_input'] = explode(",", $_POST['tags_input']);
719 if($_POST['post_type'] == 'page' || empty($_POST['post_category']))
720 unset($_POST['post_category']);
722 $do_autosave = (bool) $_POST['autosave'];
726 $message = sprintf( __('Draft Saved at %s.'), date_i18n( __('g:i:s a') ) );
728 $supplemental = array();
730 $id = $revision_id = 0;
731 if($_POST['post_ID'] < 0) {
732 $_POST['post_status'] = 'draft';
733 $_POST['temp_ID'] = $_POST['post_ID'];
734 if ( $do_autosave ) {
735 $id = wp_write_post();
739 $post_ID = (int) $_POST['post_ID'];
740 $_POST['ID'] = $post_ID;
741 $post = get_post($post_ID);
743 if ( $last = wp_check_post_lock( $post->ID ) ) {
744 $do_autosave = $do_lock = false;
746 $last_user = get_userdata( $last );
747 $last_user_name = $last_user ? $last_user->display_name : __( 'Someone' );
748 $data = new WP_Error( 'locked', sprintf(
749 $_POST['post_type'] == 'page' ? __( 'Autosave disabled: %s is currently editing this page.' ) : __( 'Autosave disabled: %s is currently editing this post.' ),
750 wp_specialchars( $last_user_name )
753 $supplemental['disable_autosave'] = 'disable';
756 if ( 'page' == $post->post_type ) {
757 if ( !current_user_can('edit_page', $post_ID) )
758 die(__('You are not allowed to edit this page.'));
760 if ( !current_user_can('edit_post', $post_ID) )
761 die(__('You are not allowed to edit this post.'));
764 if ( $do_autosave ) {
765 // Drafts are just overwritten by autosave
766 if ( 'draft' == $post->post_status ) {
768 } else { // Non drafts are not overwritten. The autosave is stored in a special post revision.
769 $revision_id = wp_create_post_autosave( $post->ID );
770 if ( is_wp_error($revision_id) )
781 if ( $do_lock && $id && is_numeric($id) )
782 wp_set_post_lock( $id );
784 if ( $nonce_age == 2 ) {
785 $supplemental['replace-autosavenonce'] = wp_create_nonce('autosave');
786 $supplemental['replace-getpermalinknonce'] = wp_create_nonce('getpermalink');
787 $supplemental['replace-samplepermalinknonce'] = wp_create_nonce('samplepermalink');
788 $supplemental['replace-closedpostboxesnonce'] = wp_create_nonce('closedpostboxes');
790 if ( $_POST['post_type'] == 'post' )
791 $supplemental['replace-_wpnonce'] = wp_create_nonce('update-post_' . $id);
792 elseif ( $_POST['post_type'] == 'page' )
793 $supplemental['replace-_wpnonce'] = wp_create_nonce('update-page_' . $id);
797 $x = new WP_Ajax_Response( array(
798 'what' => 'autosave',
800 'data' => $id ? $data : '',
801 'supplemental' => $supplemental
805 case 'autosave-generate-nonces' :
806 check_ajax_referer( 'autosave', 'autosavenonce' );
807 $ID = (int) $_POST['post_ID'];
808 if($_POST['post_type'] == 'post') {
809 if(current_user_can('edit_post', $ID))
810 die(wp_create_nonce('update-post_' . $ID));
812 if($_POST['post_type'] == 'page') {
813 if(current_user_can('edit_page', $ID)) {
814 die(wp_create_nonce('update-page_' . $ID));
819 case 'closed-postboxes' :
820 check_ajax_referer( 'closedpostboxes', 'closedpostboxesnonce' );
821 $closed = isset( $_POST['closed'] )? $_POST['closed'] : '';
822 $closed = explode( ',', $_POST['closed'] );
823 $hidden = isset( $_POST['hidden'] )? $_POST['hidden'] : '';
824 $hidden = explode( ',', $_POST['hidden'] );
825 $page = isset( $_POST['page'] )? $_POST['page'] : '';
826 if ( !preg_match( '/^[a-z-_]+$/', $page ) ) {
829 $current_user = wp_get_current_user();
830 if ( is_array($closed) )
831 update_usermeta($current_user->ID, 'closedpostboxes_'.$page, $closed);
832 if ( is_array($hidden) )
833 update_usermeta($current_user->ID, 'meta-box-hidden_'.$page, $hidden);
835 case 'hidden-columns' :
836 check_ajax_referer( 'hiddencolumns', 'hiddencolumnsnonce' );
837 $hidden = isset( $_POST['hidden'] )? $_POST['hidden'] : '';
838 $hidden = explode( ',', $_POST['hidden'] );
839 $page = isset( $_POST['page'] )? $_POST['page'] : '';
840 if ( !preg_match( '/^[a-z-_]+$/', $page ) ) {
843 $current_user = wp_get_current_user();
844 if ( is_array($hidden) )
845 update_usermeta($current_user->ID, "manage-$page-columns-hidden", $hidden);
847 case 'get-permalink':
848 check_ajax_referer( 'getpermalink', 'getpermalinknonce' );
849 $post_id = isset($_POST['post_id'])? intval($_POST['post_id']) : 0;
850 die(add_query_arg(array('preview' => 'true'), get_permalink($post_id)));
852 case 'sample-permalink':
853 check_ajax_referer( 'samplepermalink', 'samplepermalinknonce' );
854 $post_id = isset($_POST['post_id'])? intval($_POST['post_id']) : 0;
855 $title = isset($_POST['new_title'])? $_POST['new_title'] : '';
856 $slug = isset($_POST['new_slug'])? $_POST['new_slug'] : '';
857 die(get_sample_permalink_html($post_id, $title, $slug));
860 check_ajax_referer( 'inlineeditnonce', '_inline_edit' );
862 if ( ! isset($_POST['post_ID']) || ! ( $post_ID = (int) $_POST['post_ID'] ) )
865 if ( 'page' == $_POST['post_type'] ) {
866 if ( ! current_user_can( 'edit_page', $post_ID ) )
867 die( __('You are not allowed to edit this page.') );
869 if ( ! current_user_can( 'edit_post', $post_ID ) )
870 die( __('You are not allowed to edit this post.') );
873 if ( $last = wp_check_post_lock( $post_ID ) ) {
874 $last_user = get_userdata( $last );
875 $last_user_name = $last_user ? $last_user->display_name : __( 'Someone' );
876 printf( $_POST['post_type'] == 'page' ? __( 'Saving is disabled: %s is currently editing this page.' ) : __( 'Saving is disabled: %s is currently editing this post.' ), wp_specialchars( $last_user_name ) );
881 $post = get_post( $post_ID, ARRAY_A );
882 $data['content'] = $post['post_content'];
883 $data['excerpt'] = $post['post_excerpt'];
886 $data['user_ID'] = $GLOBALS['user_ID'];
888 if ( isset($data['post_parent']) )
889 $data['parent_id'] = $data['post_parent'];
892 if ( isset($data['keep_private']) && 'private' == $data['keep_private'] )
893 $data['post_status'] = 'private';
895 $data['post_status'] = $data['_status'];
897 if ( empty($data['comment_status']) )
898 $data['comment_status'] = 'closed';
899 if ( empty($data['ping_status']) )
900 $data['ping_status'] = 'closed';
907 if ( 'page' == $_POST['post_type'] ) {
908 $post[] = get_post($_POST['post_ID']);
910 } elseif ( 'post' == $_POST['post_type'] ) {
911 $mode = $_POST['post_view'];
912 $post[] = get_post($_POST['post_ID']);
918 case 'inline-save-tax':
919 check_ajax_referer( 'taxinlineeditnonce', '_inline_edit' );
921 if ( ! current_user_can('manage_categories') )
922 die( __('Cheatin’ uh?') );
924 if ( ! isset($_POST['tax_ID']) || ! ( $id = (int) $_POST['tax_ID'] ) )
927 switch ($_POST['tax_type']) {
930 $data['cat_ID'] = $id;
931 $data['cat_name'] = $_POST['name'];
932 $data['category_nicename'] = $_POST['slug'];
933 if ( isset($_POST['parent']) && (int) $_POST['parent'] > 0 )
934 $data['category_parent'] = $_POST['parent'];
936 $cat = get_category($id, ARRAY_A);
937 $data['category_description'] = $cat['category_description'];
939 $updated = wp_update_category($data);
941 if ( $updated && !is_wp_error($updated) )
942 echo _cat_row( $updated, 0 );
944 die( __('Category not updated.') );
948 $updated = wp_update_term($id, 'link_category', $_POST);
950 if ( $updated && !is_wp_error($updated) )
951 echo link_cat_row($updated['term_id']);
953 die( __('Category not updated.') );
957 $updated = wp_update_term($id, 'post_tag', $_POST);
958 if ( $updated && !is_wp_error($updated) ) {
959 $tag = get_term( $updated['term_id'], 'post_tag' );
960 if ( !$tag || is_wp_error( $tag ) )
961 die( __('Tag not updated.') );
965 die( __('Tag not updated.') );
973 case 'meta-box-order':
974 check_ajax_referer( 'meta-box-order' );
975 update_user_option( $GLOBALS['current_user']->ID, "meta-box-order_$_POST[page]", $_POST['order'] );
979 check_ajax_referer( 'find-posts' );
981 if ( empty($_POST['ps']) )
984 $what = isset($_POST['pages']) ? 'page' : 'post';
985 $s = stripslashes($_POST['ps']);
986 preg_match_all('/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $s, $matches);
987 $search_terms = array_map(create_function('$a', 'return trim($a, "\\"\'\\n\\r ");'), $matches[0]);
989 $searchand = $search = '';
990 foreach( (array) $search_terms as $term) {
991 $term = addslashes_gpc($term);
992 $search .= "{$searchand}(($wpdb->posts.post_title LIKE '%{$term}%') OR ($wpdb->posts.post_content LIKE '%{$term}%'))";
993 $searchand = ' AND ';
995 $term = $wpdb->escape($s);
996 if ( count($search_terms) > 1 && $search_terms[0] != $s )
997 $search .= " OR ($wpdb->posts.post_title LIKE '%{$term}%') OR ($wpdb->posts.post_content LIKE '%{$term}%')";
999 $posts = $wpdb->get_results( "SELECT ID, post_title, post_status, post_date FROM $wpdb->posts WHERE post_type = '$what' AND $search ORDER BY post_date_gmt DESC LIMIT 50" );
1002 exit( __('No posts found.') );
1004 $html = '<table class="widefat"><thead><tr><th class="found-radio"><br /></th><th>'.__('Title').'</th><th>'.__('Time').'</th><th>'.__('Status').'</th></tr></thead><tbody>';
1005 foreach ( $posts as $post ) {
1007 switch ( $post->post_status ) {
1010 $stat = __('Published');
1013 $stat = __('Scheduled');
1016 $stat = __('Pending Review');
1019 $stat = __('Unpublished');
1023 if ( '0000-00-00 00:00:00' == $post->post_date ) {
1026 $time = mysql2date(__('Y/m/d'), $post->post_date);
1029 $html .= '<tr class="found-posts"><td class="found-radio"><input type="radio" id="found-'.$post->ID.'" name="found_post_id" value="'.$post->ID.'"></td>';
1030 $html .= '<td><label for="found-'.$post->ID.'">'.wp_specialchars($post->post_title, true).'</label></td><td>'.wp_specialchars($time, true).'</td><td>'.wp_specialchars($stat, true).'</td></tr>'."\n\n";
1032 $html .= '</tbody></table>';
1034 $x = new WP_Ajax_Response();
1043 do_action( 'wp_ajax_' . $_POST['action'] );