-
+ 15DBF0BC5478CE0BD9C2A705C7AB4A7701E13CC6590A8B47211F39E8A6BC6A8DAB0AC895B8AD360AAF9CA6727FE2CDEF554B21BE787B9A746A1FEB5EB28C531F
mp-wp/wp-admin/admin-ajax.php
(0 . 0)(1 . 1044)
198 <?php
199 /**
200 * WordPress AJAX Process Execution.
201 *
202 * @package WordPress
203 * @subpackage Administration
204 */
205
206 /**
207 * Executing AJAX process.
208 *
209 * @since unknown
210 */
211 define('DOING_AJAX', true);
212 define('WP_ADMIN', true);
213
214 require_once('../wp-load.php');
215 require_once('includes/admin.php');
216
217 if ( ! is_user_logged_in() ) {
218
219 if ( $_POST['action'] == 'autosave' ) {
220 $id = isset($_POST['post_ID'])? (int) $_POST['post_ID'] : 0;
221
222 if ( ! $id )
223 die('-1');
224
225 $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() );
226 $x = new WP_Ajax_Response( array(
227 'what' => 'autosave',
228 'id' => $id,
229 'data' => $message
230 ) );
231 $x->send();
232 }
233
234 die('-1');
235 }
236
237 if ( isset( $_GET['action'] ) ) :
238 switch ( $action = $_GET['action'] ) :
239 case 'ajax-tag-search' :
240 if ( !current_user_can( 'manage_categories' ) )
241 die('-1');
242
243 $s = $_GET['q']; // is this slashed already?
244
245 if ( false !== strpos( $s, ',' ) ) {
246 $s = explode( ',', $s );
247 $s = $s[count( $s ) - 1];
248 }
249 $s = trim( $s );
250 if ( strlen( $s ) < 2 )
251 die; // require 2 chars for matching
252 $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 . "%')" );
253 echo join( $results, "\n" );
254 die;
255 break;
256 default :
257 do_action( 'wp_ajax_' . $_GET['action'] );
258 die('0');
259 break;
260 endswitch;
261 endif;
262
263 $id = isset($_POST['id'])? (int) $_POST['id'] : 0;
264 switch ( $action = $_POST['action'] ) :
265 case 'delete-comment' :
266 check_ajax_referer( "delete-comment_$id" );
267 if ( !$comment = get_comment( $id ) )
268 die('1');
269 if ( !current_user_can( 'edit_post', $comment->comment_post_ID ) )
270 die('-1');
271
272 if ( isset($_POST['spam']) && 1 == $_POST['spam'] ) {
273 if ( 'spam' == wp_get_comment_status( $comment->comment_ID ) )
274 die('1');
275 $r = wp_set_comment_status( $comment->comment_ID, 'spam' );
276 } else {
277 $r = wp_delete_comment( $comment->comment_ID );
278 }
279
280 die( $r ? '1' : '0' );
281 break;
282 case 'delete-cat' :
283 check_ajax_referer( "delete-category_$id" );
284 if ( !current_user_can( 'manage_categories' ) )
285 die('-1');
286
287 $cat = get_category( $id );
288 if ( !$cat || is_wp_error( $cat ) )
289 die('1');
290
291 if ( wp_delete_category( $id ) )
292 die('1');
293 else
294 die('0');
295 break;
296 case 'delete-tag' :
297 check_ajax_referer( "delete-tag_$id" );
298 if ( !current_user_can( 'manage_categories' ) )
299 die('-1');
300
301 $tag = get_term( $id, 'post_tag' );
302 if ( !$tag || is_wp_error( $tag ) )
303 die('1');
304
305 if ( wp_delete_term($id, 'post_tag'))
306 die('1');
307 else
308 die('0');
309 break;
310 case 'delete-link-cat' :
311 check_ajax_referer( "delete-link-category_$id" );
312 if ( !current_user_can( 'manage_categories' ) )
313 die('-1');
314
315 $cat = get_term( $id, 'link_category' );
316 if ( !$cat || is_wp_error( $cat ) )
317 die('1');
318
319 $cat_name = get_term_field('name', $id, 'link_category');
320
321 // Don't delete the default cats.
322 if ( $id == get_option('default_link_category') ) {
323 $x = new WP_AJAX_Response( array(
324 'what' => 'link-cat',
325 'id' => $id,
326 'data' => new WP_Error( 'default-link-cat', sprintf(__("Can’t delete the <strong>%s</strong> category: this is the default one"), $cat_name) )
327 ) );
328 $x->send();
329 }
330
331 $r = wp_delete_term($id, 'link_category');
332 if ( !$r )
333 die('0');
334 if ( is_wp_error($r) ) {
335 $x = new WP_AJAX_Response( array(
336 'what' => 'link-cat',
337 'id' => $id,
338 'data' => $r
339 ) );
340 $x->send();
341 }
342 die('1');
343 break;
344 case 'delete-link' :
345 check_ajax_referer( "delete-bookmark_$id" );
346 if ( !current_user_can( 'manage_links' ) )
347 die('-1');
348
349 $link = get_bookmark( $id );
350 if ( !$link || is_wp_error( $link ) )
351 die('1');
352
353 if ( wp_delete_link( $id ) )
354 die('1');
355 else
356 die('0');
357 break;
358 case 'delete-meta' :
359 check_ajax_referer( "delete-meta_$id" );
360 if ( !$meta = get_post_meta_by_id( $id ) )
361 die('1');
362
363 if ( !current_user_can( 'edit_post', $meta->post_id ) )
364 die('-1');
365 if ( delete_meta( $meta->meta_id ) )
366 die('1');
367 die('0');
368 break;
369 case 'delete-post' :
370 check_ajax_referer( "{$action}_$id" );
371 if ( !current_user_can( 'delete_post', $id ) )
372 die('-1');
373
374 if ( !get_post( $id ) )
375 die('1');
376
377 if ( wp_delete_post( $id ) )
378 die('1');
379 else
380 die('0');
381 break;
382 case 'delete-page' :
383 check_ajax_referer( "{$action}_$id" );
384 if ( !current_user_can( 'delete_page', $id ) )
385 die('-1');
386
387 if ( !get_page( $id ) )
388 die('1');
389
390 if ( wp_delete_post( $id ) )
391 die('1');
392 else
393 die('0');
394 break;
395 case 'dim-comment' :
396 if ( !$comment = get_comment( $id ) )
397 die('0');
398
399 if ( !current_user_can( 'edit_post', $comment->comment_post_ID ) )
400 die('-1');
401 if ( !current_user_can( 'moderate_comments' ) )
402 die('-1');
403
404 $current = wp_get_comment_status( $comment->comment_ID );
405 if ( $_POST['new'] == $current )
406 die('1');
407
408 if ( in_array( $current, array( 'unapproved', 'spam' ) ) ) {
409 check_ajax_referer( "approve-comment_$id" );
410 if ( wp_set_comment_status( $comment->comment_ID, 'approve' ) )
411 die('1');
412 } else {
413 check_ajax_referer( "unapprove-comment_$id" );
414 if ( wp_set_comment_status( $comment->comment_ID, 'hold' ) )
415 die('1');
416 }
417 die('0');
418 break;
419 case 'add-category' : // On the Fly
420 check_ajax_referer( $action );
421 if ( !current_user_can( 'manage_categories' ) )
422 die('-1');
423 $names = explode(',', $_POST['newcat']);
424 if ( 0 > $parent = (int) $_POST['newcat_parent'] )
425 $parent = 0;
426 $post_category = isset($_POST['post_category'])? (array) $_POST['post_category'] : array();
427 $checked_categories = array_map( 'absint', (array) $post_category );
428 $popular_ids = isset( $_POST['popular_ids'] ) ?
429 array_map( 'absint', explode( ',', $_POST['popular_ids'] ) ) :
430 false;
431
432 $x = new WP_Ajax_Response();
433 foreach ( $names as $cat_name ) {
434 $cat_name = trim($cat_name);
435 $category_nicename = sanitize_title($cat_name);
436 if ( '' === $category_nicename )
437 continue;
438 $cat_id = wp_create_category( $cat_name, $parent );
439 $checked_categories[] = $cat_id;
440 if ( $parent ) // Do these all at once in a second
441 continue;
442 $category = get_category( $cat_id );
443 ob_start();
444 wp_category_checklist( 0, $cat_id, $checked_categories, $popular_ids );
445 $data = ob_get_contents();
446 ob_end_clean();
447 $x->add( array(
448 'what' => 'category',
449 'id' => $cat_id,
450 'data' => $data,
451 'position' => -1
452 ) );
453 }
454 if ( $parent ) { // Foncy - replace the parent and all its children
455 $parent = get_category( $parent );
456 ob_start();
457 dropdown_categories( 0, $parent );
458 $data = ob_get_contents();
459 ob_end_clean();
460 $x->add( array(
461 'what' => 'category',
462 'id' => $parent->term_id,
463 'old_id' => $parent->term_id,
464 'data' => $data,
465 'position' => -1
466 ) );
467
468 }
469 $x->send();
470 break;
471 case 'add-link-category' : // On the Fly
472 check_ajax_referer( $action );
473 if ( !current_user_can( 'manage_categories' ) )
474 die('-1');
475 $names = explode(',', $_POST['newcat']);
476 $x = new WP_Ajax_Response();
477 foreach ( $names as $cat_name ) {
478 $cat_name = trim($cat_name);
479 $slug = sanitize_title($cat_name);
480 if ( '' === $slug )
481 continue;
482 if ( !$cat_id = is_term( $cat_name, 'link_category' ) ) {
483 $cat_id = wp_insert_term( $cat_name, 'link_category' );
484 }
485 $cat_id = $cat_id['term_id'];
486 $cat_name = wp_specialchars(stripslashes($cat_name));
487 $x->add( array(
488 'what' => 'link-category',
489 'id' => $cat_id,
490 '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>",
491 'position' => -1
492 ) );
493 }
494 $x->send();
495 break;
496 case 'add-cat' : // From Manage->Categories
497 check_ajax_referer( 'add-category' );
498 if ( !current_user_can( 'manage_categories' ) )
499 die('-1');
500
501 if ( '' === trim($_POST['cat_name']) ) {
502 $x = new WP_Ajax_Response( array(
503 'what' => 'cat',
504 'id' => new WP_Error( 'cat_name', __('You did not enter a category name.') )
505 ) );
506 $x->send();
507 }
508
509 if ( category_exists( trim( $_POST['cat_name'] ) ) ) {
510 $x = new WP_Ajax_Response( array(
511 'what' => 'cat',
512 'id' => new WP_Error( 'cat_exists', __('The category you are trying to create already exists.'), array( 'form-field' => 'cat_name' ) ),
513 ) );
514 $x->send();
515 }
516
517 $cat = wp_insert_category( $_POST, true );
518
519 if ( is_wp_error($cat) ) {
520 $x = new WP_Ajax_Response( array(
521 'what' => 'cat',
522 'id' => $cat
523 ) );
524 $x->send();
525 }
526
527 if ( !$cat || (!$cat = get_category( $cat )) )
528 die('0');
529
530 $level = 0;
531 $cat_full_name = $cat->name;
532 $_cat = $cat;
533 while ( $_cat->parent ) {
534 $_cat = get_category( $_cat->parent );
535 $cat_full_name = $_cat->name . ' — ' . $cat_full_name;
536 $level++;
537 }
538 $cat_full_name = attribute_escape($cat_full_name);
539
540 $x = new WP_Ajax_Response( array(
541 'what' => 'cat',
542 'id' => $cat->term_id,
543 'position' => -1,
544 'data' => _cat_row( $cat, $level, $cat_full_name ),
545 'supplemental' => array('name' => $cat_full_name, 'show-link' => sprintf(__( 'Category <a href="#%s">%s</a> added' ), "cat-$cat->term_id", $cat_full_name))
546 ) );
547 $x->send();
548 break;
549 case 'add-link-cat' : // From Blogroll -> Categories
550 check_ajax_referer( 'add-link-category' );
551 if ( !current_user_can( 'manage_categories' ) )
552 die('-1');
553
554 if ( '' === trim($_POST['name']) ) {
555 $x = new WP_Ajax_Response( array(
556 'what' => 'link-cat',
557 'id' => new WP_Error( 'name', __('You did not enter a category name.') )
558 ) );
559 $x->send();
560 }
561
562 $r = wp_insert_term($_POST['name'], 'link_category', $_POST );
563 if ( is_wp_error( $r ) ) {
564 $x = new WP_AJAX_Response( array(
565 'what' => 'link-cat',
566 'id' => $r
567 ) );
568 $x->send();
569 }
570
571 extract($r, EXTR_SKIP);
572
573 if ( !$link_cat = link_cat_row( $term_id ) )
574 die('0');
575
576 $x = new WP_Ajax_Response( array(
577 'what' => 'link-cat',
578 'id' => $term_id,
579 'position' => -1,
580 'data' => $link_cat
581 ) );
582 $x->send();
583 break;
584 case 'add-tag' : // From Manage->Tags
585 check_ajax_referer( 'add-tag' );
586 if ( !current_user_can( 'manage_categories' ) )
587 die('-1');
588
589 if ( '' === trim($_POST['name']) ) {
590 $x = new WP_Ajax_Response( array(
591 'what' => 'tag',
592 'id' => new WP_Error( 'name', __('You did not enter a tag name.') )
593 ) );
594 $x->send();
595 }
596
597 $tag = wp_insert_term($_POST['name'], 'post_tag', $_POST );
598
599 if ( is_wp_error($tag) ) {
600 $x = new WP_Ajax_Response( array(
601 'what' => 'tag',
602 'id' => $tag
603 ) );
604 $x->send();
605 }
606
607 if ( !$tag || (!$tag = get_term( $tag['term_id'], 'post_tag' )) )
608 die('0');
609
610 $tag_full_name = $tag->name;
611 $tag_full_name = attribute_escape($tag_full_name);
612
613 $x = new WP_Ajax_Response( array(
614 'what' => 'tag',
615 'id' => $tag->term_id,
616 'position' => '-1',
617 'data' => _tag_row( $tag ),
618 'supplemental' => array('name' => $tag_full_name, 'show-link' => sprintf(__( 'Tag <a href="#%s">%s</a> added' ), "tag-$tag->term_id", $tag_full_name))
619 ) );
620 $x->send();
621 break;
622 case 'get-tagcloud' :
623 if ( !current_user_can( 'manage_categories' ) )
624 die('-1');
625
626 $tags = get_tags( array( 'number' => 45, 'orderby' => 'count', 'order' => 'DESC' ) );
627
628 if ( empty( $tags ) )
629 die( __('No tags found!') );
630
631 foreach ( $tags as $key => $tag ) {
632 $tags[ $key ]->link = '#';
633 $tags[ $key ]->id = $tag->term_id;
634 }
635
636 $return = wp_generate_tag_cloud( $tags );
637
638 if ( empty($return) )
639 die('0');
640
641 echo $return;
642
643 exit;
644 break;
645 case 'add-comment' :
646 check_ajax_referer( $action );
647 if ( !current_user_can( 'edit_post', $id ) )
648 die('-1');
649 $search = isset($_POST['s']) ? $_POST['s'] : false;
650 $start = isset($_POST['page']) ? intval($_POST['page']) * 25 - 1: 24;
651 $status = isset($_POST['comment_status']) ? $_POST['comment_status'] : false;
652 $mode = isset($_POST['mode']) ? $_POST['mode'] : 'detail';
653 $p = isset($_POST['p']) ? $_POST['p'] : 0;
654 $comment_type = isset($_POST['comment_type']) ? $_POST['comment_type'] : '';
655 list($comments, $total) = _wp_get_comment_list( $status, $search, $start, 1, $p, $comment_type );
656
657 if ( get_option('show_avatars') )
658 add_filter( 'comment_author', 'floated_admin_avatar' );
659
660 if ( !$comments )
661 die('1');
662 $x = new WP_Ajax_Response();
663 foreach ( (array) $comments as $comment ) {
664 get_comment( $comment );
665 ob_start();
666 _wp_comment_row( $comment->comment_ID, $mode, $status, true, true );
667 $comment_list_item = ob_get_contents();
668 ob_end_clean();
669 $x->add( array(
670 'what' => 'comment',
671 'id' => $comment->comment_ID,
672 'data' => $comment_list_item
673 ) );
674 }
675 $x->send();
676 break;
677 case 'get-comments' :
678 check_ajax_referer( $action );
679
680 $post_ID = (int) $_POST['post_ID'];
681 if ( !current_user_can( 'edit_post', $post_ID ) )
682 die('-1');
683
684 $start = isset($_POST['start']) ? intval($_POST['start']) : 0;
685 $num = isset($_POST['num']) ? intval($_POST['num']) : 10;
686
687 list($comments, $total) = _wp_get_comment_list( false, false, $start, $num, $post_ID );
688
689 if ( !$comments )
690 die('1');
691
692 $comment_list_item = '';
693 $x = new WP_Ajax_Response();
694 foreach ( (array) $comments as $comment ) {
695 get_comment( $comment );
696 ob_start();
697 _wp_comment_row( $comment->comment_ID, 'single', false, false );
698 $comment_list_item .= ob_get_contents();
699 ob_end_clean();
700 }
701 $x->add( array(
702 'what' => 'comments',
703 'data' => $comment_list_item
704 ) );
705 $x->send();
706 break;
707 case 'replyto-comment' :
708 check_ajax_referer( $action );
709
710 $comment_post_ID = (int) $_POST['comment_post_ID'];
711 if ( !current_user_can( 'edit_post', $comment_post_ID ) )
712 die('-1');
713
714 $status = $wpdb->get_var( $wpdb->prepare("SELECT post_status FROM $wpdb->posts WHERE ID = %d", $comment_post_ID) );
715
716 if ( empty($status) )
717 die('1');
718 elseif ( in_array($status, array('draft', 'pending') ) )
719 die( __('Error: you are replying to a comment on a draft post.') );
720
721 $user = wp_get_current_user();
722 if ( $user->ID ) {
723 $comment_author = $wpdb->escape($user->display_name);
724 $comment_author_email = $wpdb->escape($user->user_email);
725 $comment_author_url = $wpdb->escape($user->user_url);
726 $comment_content = trim($_POST['content']);
727 if ( current_user_can('unfiltered_html') ) {
728 if ( wp_create_nonce('unfiltered-html-comment_' . $comment_post_ID) != $_POST['_wp_unfiltered_html_comment'] ) {
729 kses_remove_filters(); // start with a clean slate
730 kses_init_filters(); // set up the filters
731 }
732 }
733 } else {
734 die( __('Sorry, you must be logged in to reply to a comment.') );
735 }
736
737 if ( '' == $comment_content )
738 die( __('Error: please type a comment.') );
739
740 $comment_parent = absint($_POST['comment_ID']);
741 $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type', 'comment_parent', 'user_ID');
742
743 $comment_id = wp_new_comment( $commentdata );
744 $comment = get_comment($comment_id);
745 if ( ! $comment ) die('1');
746
747 $modes = array( 'single', 'detail', 'dashboard' );
748 $mode = isset($_POST['mode']) && in_array( $_POST['mode'], $modes ) ? $_POST['mode'] : 'detail';
749 $position = ( isset($_POST['position']) && (int) $_POST['position']) ? (int) $_POST['position'] : '-1';
750 $checkbox = ( isset($_POST['checkbox']) && true == $_POST['checkbox'] ) ? 1 : 0;
751
752 if ( get_option('show_avatars') && 'single' != $mode )
753 add_filter( 'comment_author', 'floated_admin_avatar' );
754
755 $x = new WP_Ajax_Response();
756
757 ob_start();
758 if ( 'dashboard' == $mode ) {
759 require_once( ABSPATH . 'wp-admin/includes/dashboard.php' );
760 _wp_dashboard_recent_comments_row( $comment, false );
761 } else {
762 _wp_comment_row( $comment->comment_ID, $mode, false, $checkbox );
763 }
764 $comment_list_item = ob_get_contents();
765 ob_end_clean();
766
767 $x->add( array(
768 'what' => 'comment',
769 'id' => $comment->comment_ID,
770 'data' => $comment_list_item,
771 'position' => $position
772 ));
773
774 $x->send();
775 break;
776 case 'edit-comment' :
777 check_ajax_referer( 'replyto-comment' );
778
779 $comment_post_ID = (int) $_POST['comment_post_ID'];
780 if ( ! current_user_can( 'edit_post', $comment_post_ID ) )
781 die('-1');
782
783 if ( '' == $_POST['content'] )
784 die( __('Error: please type a comment.') );
785
786 $comment_id = (int) $_POST['comment_ID'];
787 $_POST['comment_status'] = $_POST['status'];
788 edit_comment();
789
790 $mode = ( isset($_POST['mode']) && 'single' == $_POST['mode'] ) ? 'single' : 'detail';
791 $position = ( isset($_POST['position']) && (int) $_POST['position']) ? (int) $_POST['position'] : '-1';
792 $checkbox = ( isset($_POST['checkbox']) && true == $_POST['checkbox'] ) ? 1 : 0;
793
794 if ( get_option('show_avatars') && 'single' != $mode )
795 add_filter( 'comment_author', 'floated_admin_avatar' );
796
797 $x = new WP_Ajax_Response();
798
799 ob_start();
800 _wp_comment_row( $comment_id, $mode, true, $checkbox );
801 $comment_list_item = ob_get_contents();
802 ob_end_clean();
803
804 $x->add( array(
805 'what' => 'edit_comment',
806 'id' => $comment->comment_ID,
807 'data' => $comment_list_item,
808 'position' => $position
809 ));
810
811 $x->send();
812 break;
813 case 'add-meta' :
814 check_ajax_referer( 'add-meta' );
815 $c = 0;
816 $pid = (int) $_POST['post_id'];
817 if ( isset($_POST['metakeyselect']) || isset($_POST['metakeyinput']) ) {
818 if ( !current_user_can( 'edit_post', $pid ) )
819 die('-1');
820 if ( '#NONE#' == $_POST['metakeyselect'] && empty($_POST['metakeyinput']) )
821 die('1');
822 if ( $pid < 0 ) {
823 $now = current_time('timestamp', 1);
824 if ( $pid = wp_insert_post( array(
825 'post_title' => sprintf('Draft created on %s at %s', date(get_option('date_format'), $now), date(get_option('time_format'), $now))
826 ) ) ) {
827 if ( is_wp_error( $pid ) ) {
828 $x = new WP_Ajax_Response( array(
829 'what' => 'meta',
830 'data' => $pid
831 ) );
832 $x->send();
833 }
834 $mid = add_meta( $pid );
835 } else {
836 die('0');
837 }
838 } else if ( !$mid = add_meta( $pid ) ) {
839 die('0');
840 }
841
842 $meta = get_post_meta_by_id( $mid );
843 $pid = (int) $meta->post_id;
844 $meta = get_object_vars( $meta );
845 $x = new WP_Ajax_Response( array(
846 'what' => 'meta',
847 'id' => $mid,
848 'data' => _list_meta_row( $meta, $c ),
849 'position' => 1,
850 'supplemental' => array('postid' => $pid)
851 ) );
852 } else {
853 $mid = (int) array_pop(array_keys($_POST['meta']));
854 $key = $_POST['meta'][$mid]['key'];
855 $value = $_POST['meta'][$mid]['value'];
856 if ( !$meta = get_post_meta_by_id( $mid ) )
857 die('0'); // if meta doesn't exist
858 if ( !current_user_can( 'edit_post', $meta->post_id ) )
859 die('-1');
860 if ( !$u = update_meta( $mid, $key, $value ) )
861 die('1'); // We know meta exists; we also know it's unchanged (or DB error, in which case there are bigger problems).
862 $key = stripslashes($key);
863 $value = stripslashes($value);
864 $x = new WP_Ajax_Response( array(
865 'what' => 'meta',
866 'id' => $mid, 'old_id' => $mid,
867 'data' => _list_meta_row( array(
868 'meta_key' => $key,
869 'meta_value' => $value,
870 'meta_id' => $mid
871 ), $c ),
872 'position' => 0,
873 'supplemental' => array('postid' => $meta->post_id)
874 ) );
875 }
876 $x->send();
877 break;
878 case 'add-user' :
879 check_ajax_referer( $action );
880 if ( !current_user_can('create_users') )
881 die('-1');
882 require_once(ABSPATH . WPINC . '/registration.php');
883 if ( !$user_id = add_user() )
884 die('0');
885 elseif ( is_wp_error( $user_id ) ) {
886 $x = new WP_Ajax_Response( array(
887 'what' => 'user',
888 'id' => $user_id
889 ) );
890 $x->send();
891 }
892 $user_object = new WP_User( $user_id );
893
894 $x = new WP_Ajax_Response( array(
895 'what' => 'user',
896 'id' => $user_id,
897 'data' => user_row( $user_object, '', $user_object->roles[0] ),
898 'supplemental' => array(
899 'show-link' => sprintf(__( 'User <a href="#%s">%s</a> added' ), "user-$user_id", $user_object->user_login),
900 'role' => $user_object->roles[0]
901 )
902 ) );
903 $x->send();
904 break;
905 case 'autosave' : // The name of this action is hardcoded in edit_post()
906 define( 'DOING_AUTOSAVE', true );
907
908 $nonce_age = check_ajax_referer( 'autosave', 'autosavenonce' );
909 global $current_user;
910
911 $_POST['post_category'] = explode(",", $_POST['catslist']);
912 $_POST['tags_input'] = explode(",", $_POST['tags_input']);
913 if($_POST['post_type'] == 'page' || empty($_POST['post_category']))
914 unset($_POST['post_category']);
915
916 $do_autosave = (bool) $_POST['autosave'];
917 $do_lock = true;
918
919 $data = '';
920 $message = sprintf( __('Draft Saved at %s.'), date( __('g:i:s a'), current_time( 'timestamp', true ) ) );
921
922 $supplemental = array();
923
924 $id = $revision_id = 0;
925 if($_POST['post_ID'] < 0) {
926 $_POST['post_status'] = 'draft';
927 $_POST['temp_ID'] = $_POST['post_ID'];
928 if ( $do_autosave ) {
929 $id = wp_write_post();
930 $data = $message;
931 }
932 } else {
933 $post_ID = (int) $_POST['post_ID'];
934 $_POST['ID'] = $post_ID;
935 $post = get_post($post_ID);
936
937 if ( $last = wp_check_post_lock( $post->ID ) ) {
938 $do_autosave = $do_lock = false;
939
940 $last_user = get_userdata( $last );
941 $last_user_name = $last_user ? $last_user->display_name : __( 'Someone' );
942 $data = new WP_Error( 'locked', sprintf(
943 $_POST['post_type'] == 'page' ? __( 'Autosave disabled: %s is currently editing this page.' ) : __( 'Autosave disabled: %s is currently editing this post.' ),
944 wp_specialchars( $last_user_name )
945 ) );
946
947 $supplemental['disable_autosave'] = 'disable';
948 }
949
950 if ( 'page' == $post->post_type ) {
951 if ( !current_user_can('edit_page', $post_ID) )
952 die(__('You are not allowed to edit this page.'));
953 } else {
954 if ( !current_user_can('edit_post', $post_ID) )
955 die(__('You are not allowed to edit this post.'));
956 }
957
958 if ( $do_autosave ) {
959 // Drafts are just overwritten by autosave
960 if ( 'draft' == $post->post_status ) {
961 $id = edit_post();
962 } else { // Non drafts are not overwritten. The autosave is stored in a special post revision.
963 $revision_id = wp_create_post_autosave( $post->ID );
964 if ( is_wp_error($revision_id) )
965 $id = $revision_id;
966 else
967 $id = $post->ID;
968 }
969 $data = $message;
970 } else {
971 $id = $post->ID;
972 }
973 }
974
975 if ( $do_lock && $id && is_numeric($id) )
976 wp_set_post_lock( $id );
977
978 if ( $nonce_age == 2 ) {
979 $supplemental['replace-autosavenonce'] = wp_create_nonce('autosave');
980 $supplemental['replace-getpermalinknonce'] = wp_create_nonce('getpermalink');
981 $supplemental['replace-samplepermalinknonce'] = wp_create_nonce('samplepermalink');
982 $supplemental['replace-closedpostboxesnonce'] = wp_create_nonce('closedpostboxes');
983 if ( $id ) {
984 if ( $_POST['post_type'] == 'post' )
985 $supplemental['replace-_wpnonce'] = wp_create_nonce('update-post_' . $id);
986 elseif ( $_POST['post_type'] == 'page' )
987 $supplemental['replace-_wpnonce'] = wp_create_nonce('update-page_' . $id);
988 }
989 }
990
991 $x = new WP_Ajax_Response( array(
992 'what' => 'autosave',
993 'id' => $id,
994 'data' => $id ? $data : '',
995 'supplemental' => $supplemental
996 ) );
997 $x->send();
998 break;
999 case 'autosave-generate-nonces' :
1000 check_ajax_referer( 'autosave', 'autosavenonce' );
1001 $ID = (int) $_POST['post_ID'];
1002 if($_POST['post_type'] == 'post') {
1003 if(current_user_can('edit_post', $ID))
1004 die(wp_create_nonce('update-post_' . $ID));
1005 }
1006 if($_POST['post_type'] == 'page') {
1007 if(current_user_can('edit_page', $ID)) {
1008 die(wp_create_nonce('update-page_' . $ID));
1009 }
1010 }
1011 die('0');
1012 break;
1013 case 'closed-postboxes' :
1014 check_ajax_referer( 'closedpostboxes', 'closedpostboxesnonce' );
1015 $closed = isset( $_POST['closed'] )? $_POST['closed'] : '';
1016 $closed = explode( ',', $_POST['closed'] );
1017 $hidden = isset( $_POST['hidden'] )? $_POST['hidden'] : '';
1018 $hidden = explode( ',', $_POST['hidden'] );
1019 $page = isset( $_POST['page'] )? $_POST['page'] : '';
1020 if ( !preg_match( '/^[a-z-_]+$/', $page ) ) {
1021 die(-1);
1022 }
1023 $current_user = wp_get_current_user();
1024 if ( is_array($closed) )
1025 update_usermeta($current_user->ID, 'closedpostboxes_'.$page, $closed);
1026 if ( is_array($hidden) )
1027 update_usermeta($current_user->ID, 'meta-box-hidden_'.$page, $hidden);
1028 break;
1029 case 'hidden-columns' :
1030 check_ajax_referer( 'hiddencolumns', 'hiddencolumnsnonce' );
1031 $hidden = isset( $_POST['hidden'] )? $_POST['hidden'] : '';
1032 $hidden = explode( ',', $_POST['hidden'] );
1033 $page = isset( $_POST['page'] )? $_POST['page'] : '';
1034 if ( !preg_match( '/^[a-z-_]+$/', $page ) ) {
1035 die(-1);
1036 }
1037 $current_user = wp_get_current_user();
1038 if ( is_array($hidden) )
1039 update_usermeta($current_user->ID, "manage-$page-columns-hidden", $hidden);
1040 break;
1041 case 'get-permalink':
1042 check_ajax_referer( 'getpermalink', 'getpermalinknonce' );
1043 $post_id = isset($_POST['post_id'])? intval($_POST['post_id']) : 0;
1044 die(add_query_arg(array('preview' => 'true'), get_permalink($post_id)));
1045 break;
1046 case 'sample-permalink':
1047 check_ajax_referer( 'samplepermalink', 'samplepermalinknonce' );
1048 $post_id = isset($_POST['post_id'])? intval($_POST['post_id']) : 0;
1049 $title = isset($_POST['new_title'])? $_POST['new_title'] : '';
1050 $slug = isset($_POST['new_slug'])? $_POST['new_slug'] : '';
1051 die(get_sample_permalink_html($post_id, $title, $slug));
1052 break;
1053 case 'inline-save':
1054 check_ajax_referer( 'inlineeditnonce', '_inline_edit' );
1055
1056 if ( ! isset($_POST['post_ID']) || ! ( $post_ID = (int) $_POST['post_ID'] ) )
1057 exit;
1058
1059 if ( 'page' == $_POST['post_type'] ) {
1060 if ( ! current_user_can( 'edit_page', $post_ID ) )
1061 die( __('You are not allowed to edit this page.') );
1062 } else {
1063 if ( ! current_user_can( 'edit_post', $post_ID ) )
1064 die( __('You are not allowed to edit this post.') );
1065 }
1066
1067 if ( $last = wp_check_post_lock( $post_ID ) ) {
1068 $last_user = get_userdata( $last );
1069 $last_user_name = $last_user ? $last_user->display_name : __( 'Someone' );
1070 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 ) );
1071 exit;
1072 }
1073
1074 $data = &$_POST;
1075 $post = get_post( $post_ID, ARRAY_A );
1076 $data['content'] = $post['post_content'];
1077 $data['excerpt'] = $post['post_excerpt'];
1078
1079 // rename
1080 $data['user_ID'] = $GLOBALS['user_ID'];
1081
1082 if ( isset($data['post_parent']) )
1083 $data['parent_id'] = $data['post_parent'];
1084
1085 // status
1086 if ( isset($data['keep_private']) && 'private' == $data['keep_private'] )
1087 $data['post_status'] = 'private';
1088 else
1089 $data['post_status'] = $data['_status'];
1090
1091 if ( empty($data['comment_status']) )
1092 $data['comment_status'] = 'closed';
1093 if ( empty($data['ping_status']) )
1094 $data['ping_status'] = 'closed';
1095
1096 // update the post
1097 $_POST = $data;
1098 edit_post();
1099
1100 $post = array();
1101 if ( 'page' == $_POST['post_type'] ) {
1102 $post[] = get_post($_POST['post_ID']);
1103 page_rows($post);
1104 } elseif ( 'post' == $_POST['post_type'] ) {
1105 $mode = $_POST['post_view'];
1106 $post[] = get_post($_POST['post_ID']);
1107 post_rows($post);
1108 }
1109
1110 exit;
1111 break;
1112 case 'inline-save-tax':
1113 check_ajax_referer( 'taxinlineeditnonce', '_inline_edit' );
1114
1115 if ( ! current_user_can('manage_categories') )
1116 die( __('Cheatin’ uh?') );
1117
1118 if ( ! isset($_POST['tax_ID']) || ! ( $id = (int) $_POST['tax_ID'] ) )
1119 die(-1);
1120
1121 switch ($_POST['tax_type']) {
1122 case 'cat' :
1123 $data = array();
1124 $data['cat_ID'] = $id;
1125 $data['cat_name'] = $_POST['name'];
1126 $data['category_nicename'] = $_POST['slug'];
1127 if ( isset($_POST['parent']) && (int) $_POST['parent'] > 0 )
1128 $data['category_parent'] = $_POST['parent'];
1129
1130 $cat = get_category($id, ARRAY_A);
1131 $data['category_description'] = $cat['category_description'];
1132
1133 $updated = wp_update_category($data);
1134
1135 if ( $updated && !is_wp_error($updated) )
1136 echo _cat_row( $updated, 0 );
1137 else
1138 die( __('Category not updated.') );
1139
1140 break;
1141 case 'link-cat' :
1142 $updated = wp_update_term($id, 'link_category', $_POST);
1143
1144 if ( $updated && !is_wp_error($updated) )
1145 echo link_cat_row($updated['term_id']);
1146 else
1147 die( __('Category not updated.') );
1148
1149 break;
1150 case 'tag' :
1151 $updated = wp_update_term($id, 'post_tag', $_POST);
1152 if ( $updated && !is_wp_error($updated) ) {
1153 $tag = get_term( $updated['term_id'], 'post_tag' );
1154 if ( !$tag || is_wp_error( $tag ) )
1155 die( __('Tag not updated.') );
1156
1157 echo _tag_row($tag);
1158 } else {
1159 die( __('Tag not updated.') );
1160 }
1161
1162 break;
1163 }
1164
1165 exit;
1166 break;
1167 case 'meta-box-order':
1168 check_ajax_referer( 'meta-box-order' );
1169 update_user_option( $GLOBALS['current_user']->ID, "meta-box-order_$_POST[page]", $_POST['order'] );
1170 die('1');
1171 break;
1172 case 'find_posts':
1173 check_ajax_referer( 'find-posts' );
1174
1175 if ( empty($_POST['ps']) )
1176 exit;
1177
1178 $what = isset($_POST['pages']) ? 'page' : 'post';
1179 $s = stripslashes($_POST['ps']);
1180 preg_match_all('/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $s, $matches);
1181 $search_terms = array_map(create_function('$a', 'return trim($a, "\\"\'\\n\\r ");'), $matches[0]);
1182
1183 $searchand = $search = '';
1184 foreach( (array) $search_terms as $term) {
1185 $term = addslashes_gpc($term);
1186 $search .= "{$searchand}(($wpdb->posts.post_title LIKE '%{$term}%') OR ($wpdb->posts.post_content LIKE '%{$term}%'))";
1187 $searchand = ' AND ';
1188 }
1189 $term = $wpdb->escape($s);
1190 if ( count($search_terms) > 1 && $search_terms[0] != $s )
1191 $search .= " OR ($wpdb->posts.post_title LIKE '%{$term}%') OR ($wpdb->posts.post_content LIKE '%{$term}%')";
1192
1193 $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" );
1194
1195 if ( ! $posts )
1196 exit( __('No posts found.') );
1197
1198 $html = '<table class="widefat"><thead><tr><th class="found-radio"><br /></th><th>'.__('Title').'</th><th>'.__('Time').'</th><th>'.__('Status').'</th></tr></thead><tbody>';
1199 foreach ( $posts as $post ) {
1200
1201 switch ( $post->post_status ) {
1202 case 'publish' :
1203 case 'private' :
1204 $stat = __('Published');
1205 break;
1206 case 'future' :
1207 $stat = __('Scheduled');
1208 break;
1209 case 'pending' :
1210 $stat = __('Pending Review');
1211 break;
1212 case 'draft' :
1213 $stat = __('Unpublished');
1214 break;
1215 }
1216
1217 if ( '0000-00-00 00:00:00' == $post->post_date ) {
1218 $time = '';
1219 } else {
1220 $time = mysql2date(__('Y/m/d'), $post->post_date);
1221 }
1222
1223 $html .= '<tr class="found-posts"><td class="found-radio"><input type="radio" id="found-'.$post->ID.'" name="found_post_id" value="'.$post->ID.'"></td>';
1224 $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";
1225 }
1226 $html .= '</tbody></table>';
1227
1228 $x = new WP_Ajax_Response();
1229 $x->add( array(
1230 'what' => $what,
1231 'data' => $html
1232 ));
1233 $x->send();
1234
1235 break;
1236 default :
1237 do_action( 'wp_ajax_' . $_POST['action'] );
1238 die('0');
1239 break;
1240 endswitch;
1241 ?>