]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/admin-ajax.php
Wordpress 2.7.1
[autoinstalls/wordpress.git] / wp-admin / admin-ajax.php
1 <?php
2 /**
3  * WordPress AJAX Process Execution.
4  *
5  * @package WordPress
6  * @subpackage Administration
7  */
8
9 /**
10  * Executing AJAX process.
11  *
12  * @since unknown
13  */
14 define('DOING_AJAX', true);
15 define('WP_ADMIN', true);
16
17 require_once('../wp-load.php');
18 require_once('includes/admin.php');
19
20 if ( ! is_user_logged_in() ) {
21
22         if ( $_POST['action'] == 'autosave' ) {
23                 $id = isset($_POST['post_ID'])? (int) $_POST['post_ID'] : 0;
24
25                 if ( ! $id )
26                         die('-1');
27
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(
30                                 'what' => 'autosave',
31                                 'id' => $id,
32                                 'data' => $message
33                         ) );
34                         $x->send();
35         }
36
37         die('-1');
38 }
39
40 if ( isset( $_GET['action'] ) ) :
41 switch ( $action = $_GET['action'] ) :
42 case 'ajax-tag-search' :
43         if ( !current_user_can( 'manage_categories' ) )
44                 die('-1');
45
46         $s = $_GET['q']; // is this slashed already?
47
48         if ( false !== strpos( $s, ',' ) ) {
49                 $s = explode( ',', $s );
50                 $s = $s[count( $s ) - 1];
51         }
52         $s = trim( $s );
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" );
57         die;
58         break;
59 default :
60         do_action( 'wp_ajax_' . $_GET['action'] );
61         die('0');
62         break;
63 endswitch;
64 endif;
65
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 ) )
71                 die('1');
72         if ( !current_user_can( 'edit_post', $comment->comment_post_ID ) )
73                 die('-1');
74
75         if ( isset($_POST['spam']) && 1 == $_POST['spam'] ) {
76                 if ( 'spam' == wp_get_comment_status( $comment->comment_ID ) )
77                         die('1');
78                 $r = wp_set_comment_status( $comment->comment_ID, 'spam' );
79         } else {
80                 $r = wp_delete_comment( $comment->comment_ID );
81         }
82
83         die( $r ? '1' : '0' );
84         break;
85 case 'delete-cat' :
86         check_ajax_referer( "delete-category_$id" );
87         if ( !current_user_can( 'manage_categories' ) )
88                 die('-1');
89
90         $cat = get_category( $id );
91         if ( !$cat || is_wp_error( $cat ) )
92                 die('1');
93
94         if ( wp_delete_category( $id ) )
95                 die('1');
96         else
97                 die('0');
98         break;
99 case 'delete-tag' :
100         check_ajax_referer( "delete-tag_$id" );
101         if ( !current_user_can( 'manage_categories' ) )
102                 die('-1');
103
104         $tag = get_term( $id, 'post_tag' );
105         if ( !$tag || is_wp_error( $tag ) )
106                 die('1');
107
108         if ( wp_delete_term($id, 'post_tag'))
109                 die('1');
110         else
111                 die('0');
112         break;
113 case 'delete-link-cat' :
114         check_ajax_referer( "delete-link-category_$id" );
115         if ( !current_user_can( 'manage_categories' ) )
116                 die('-1');
117
118         $cat = get_term( $id, 'link_category' );
119         if ( !$cat || is_wp_error( $cat ) )
120                 die('1');
121
122         $cat_name = get_term_field('name', $id, 'link_category');
123
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',
128                         'id' => $id,
129                         'data' => new WP_Error( 'default-link-cat', sprintf(__("Can&#8217;t delete the <strong>%s</strong> category: this is the default one"), $cat_name) )
130                 ) );
131                 $x->send();
132         }
133
134         $r = wp_delete_term($id, 'link_category');
135         if ( !$r )
136                 die('0');
137         if ( is_wp_error($r) ) {
138                 $x = new WP_AJAX_Response( array(
139                         'what' => 'link-cat',
140                         'id' => $id,
141                         'data' => $r
142                 ) );
143                 $x->send();
144         }
145         die('1');
146         break;
147 case 'delete-link' :
148         check_ajax_referer( "delete-bookmark_$id" );
149         if ( !current_user_can( 'manage_links' ) )
150                 die('-1');
151
152         $link = get_bookmark( $id );
153         if ( !$link || is_wp_error( $link ) )
154                 die('1');
155
156         if ( wp_delete_link( $id ) )
157                 die('1');
158         else
159                 die('0');
160         break;
161 case 'delete-meta' :
162         check_ajax_referer( "delete-meta_$id" );
163         if ( !$meta = get_post_meta_by_id( $id ) )
164                 die('1');
165
166         if ( !current_user_can( 'edit_post', $meta->post_id ) )
167                 die('-1');
168         if ( delete_meta( $meta->meta_id ) )
169                 die('1');
170         die('0');
171         break;
172 case 'delete-post' :
173         check_ajax_referer( "{$action}_$id" );
174         if ( !current_user_can( 'delete_post', $id ) )
175                 die('-1');
176
177         if ( !get_post( $id ) )
178                 die('1');
179
180         if ( wp_delete_post( $id ) )
181                 die('1');
182         else
183                 die('0');
184         break;
185 case 'delete-page' :
186         check_ajax_referer( "{$action}_$id" );
187         if ( !current_user_can( 'delete_page', $id ) )
188                 die('-1');
189
190         if ( !get_page( $id ) )
191                 die('1');
192
193         if ( wp_delete_post( $id ) )
194                 die('1');
195         else
196                 die('0');
197         break;
198 case 'dim-comment' :
199         if ( !$comment = get_comment( $id ) )
200                 die('0');
201
202         if ( !current_user_can( 'edit_post', $comment->comment_post_ID ) )
203                 die('-1');
204         if ( !current_user_can( 'moderate_comments' ) )
205                 die('-1');
206
207         $current = wp_get_comment_status( $comment->comment_ID );
208         if ( $_POST['new'] == $current )
209                 die('1');
210
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' ) )
214                         die('1');
215         } else {
216                 check_ajax_referer( "unapprove-comment_$id" );
217                 if ( wp_set_comment_status( $comment->comment_ID, 'hold' ) )
218                         die('1');
219         }
220         die('0');
221         break;
222 case 'add-category' : // On the Fly
223         check_ajax_referer( $action );
224         if ( !current_user_can( 'manage_categories' ) )
225                 die('-1');
226         $names = explode(',', $_POST['newcat']);
227         if ( 0 > $parent = (int) $_POST['newcat_parent'] )
228                 $parent = 0;
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'] ) ) :
233                         false;
234
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 )
240                         continue;
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
244                         continue;
245                 $category = get_category( $cat_id );
246                 ob_start();
247                         wp_category_checklist( 0, $cat_id, $checked_categories, $popular_ids );
248                 $data = ob_get_contents();
249                 ob_end_clean();
250                 $x->add( array(
251                         'what' => 'category',
252                         'id' => $cat_id,
253                         'data' => $data,
254                         'position' => -1
255                 ) );
256         }
257         if ( $parent ) { // Foncy - replace the parent and all its children
258                 $parent = get_category( $parent );
259                 ob_start();
260                         dropdown_categories( 0, $parent );
261                 $data = ob_get_contents();
262                 ob_end_clean();
263                 $x->add( array(
264                         'what' => 'category',
265                         'id' => $parent->term_id,
266                         'old_id' => $parent->term_id,
267                         'data' => $data,
268                         'position' => -1
269                 ) );
270
271         }
272         $x->send();
273         break;
274 case 'add-link-category' : // On the Fly
275         check_ajax_referer( $action );
276         if ( !current_user_can( 'manage_categories' ) )
277                 die('-1');
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);
283                 if ( '' === $slug )
284                         continue;
285                 if ( !$cat_id = is_term( $cat_name, 'link_category' ) ) {
286                         $cat_id = wp_insert_term( $cat_name, 'link_category' );
287                 }
288                 $cat_id = $cat_id['term_id'];
289                 $cat_name = wp_specialchars(stripslashes($cat_name));
290                 $x->add( array(
291                         'what' => 'link-category',
292                         'id' => $cat_id,
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>",
294                         'position' => -1
295                 ) );
296         }
297         $x->send();
298         break;
299 case 'add-cat' : // From Manage->Categories
300         check_ajax_referer( 'add-category' );
301         if ( !current_user_can( 'manage_categories' ) )
302                 die('-1');
303
304         if ( '' === trim($_POST['cat_name']) ) {
305                 $x = new WP_Ajax_Response( array(
306                         'what' => 'cat',
307                         'id' => new WP_Error( 'cat_name', __('You did not enter a category name.') )
308                 ) );
309                 $x->send();
310         }
311
312         if ( category_exists( trim( $_POST['cat_name'] ) ) ) {
313                 $x = new WP_Ajax_Response( array(
314                         'what' => 'cat',
315                         'id' => new WP_Error( 'cat_exists', __('The category you are trying to create already exists.'), array( 'form-field' => 'cat_name' ) ),
316                 ) );
317                 $x->send();
318         }
319
320         $cat = wp_insert_category( $_POST, true );
321
322         if ( is_wp_error($cat) ) {
323                 $x = new WP_Ajax_Response( array(
324                         'what' => 'cat',
325                         'id' => $cat
326                 ) );
327                 $x->send();
328         }
329
330         if ( !$cat || (!$cat = get_category( $cat )) )
331                 die('0');
332
333         $level = 0;
334         $cat_full_name = $cat->name;
335         $_cat = $cat;
336         while ( $_cat->parent ) {
337                 $_cat = get_category( $_cat->parent );
338                 $cat_full_name = $_cat->name . ' &#8212; ' . $cat_full_name;
339                 $level++;
340         }
341         $cat_full_name = attribute_escape($cat_full_name);
342
343         $x = new WP_Ajax_Response( array(
344                 'what' => 'cat',
345                 'id' => $cat->term_id,
346                 'position' => -1,
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))
349         ) );
350         $x->send();
351         break;
352 case 'add-link-cat' : // From Blogroll -> Categories
353         check_ajax_referer( 'add-link-category' );
354         if ( !current_user_can( 'manage_categories' ) )
355                 die('-1');
356
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.') )
361                 ) );
362                 $x->send();
363         }
364
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',
369                         'id' => $r
370                 ) );
371                 $x->send();
372         }
373
374         extract($r, EXTR_SKIP);
375
376         if ( !$link_cat = link_cat_row( $term_id ) )
377                 die('0');
378
379         $x = new WP_Ajax_Response( array(
380                 'what' => 'link-cat',
381                 'id' => $term_id,
382                 'position' => -1,
383                 'data' => $link_cat
384         ) );
385         $x->send();
386         break;
387 case 'add-tag' : // From Manage->Tags
388         check_ajax_referer( 'add-tag' );
389         if ( !current_user_can( 'manage_categories' ) )
390                 die('-1');
391
392         if ( '' === trim($_POST['name']) ) {
393                 $x = new WP_Ajax_Response( array(
394                         'what' => 'tag',
395                         'id' => new WP_Error( 'name', __('You did not enter a tag name.') )
396                 ) );
397                 $x->send();
398         }
399
400         $tag = wp_insert_term($_POST['name'], 'post_tag', $_POST );
401
402         if ( is_wp_error($tag) ) {
403                 $x = new WP_Ajax_Response( array(
404                         'what' => 'tag',
405                         'id' => $tag
406                 ) );
407                 $x->send();
408         }
409
410         if ( !$tag || (!$tag = get_term( $tag['term_id'], 'post_tag' )) )
411                 die('0');
412
413         $tag_full_name = $tag->name;
414         $tag_full_name = attribute_escape($tag_full_name);
415
416         $x = new WP_Ajax_Response( array(
417                 'what' => 'tag',
418                 'id' => $tag->term_id,
419                 'position' => '-1',
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))
422         ) );
423         $x->send();
424         break;
425 case 'get-tagcloud' :
426         if ( !current_user_can( 'manage_categories' ) )
427                 die('-1');
428
429         $tags = get_tags( array( 'number' => 45, 'orderby' => 'count', 'order' => 'DESC' ) );
430
431         if ( empty( $tags ) )
432                 die( __('No tags found!') );
433
434         if ( is_wp_error($tags) )
435                 die($tags->get_error_message());
436
437         foreach ( $tags as $key => $tag ) {
438                 $tags[ $key ]->link = '#';
439                 $tags[ $key ]->id = $tag->term_id;
440         }
441
442         $return = wp_generate_tag_cloud( $tags );
443
444         if ( empty($return) )
445                 die('0');
446
447         echo $return;
448
449         exit;
450         break;
451 case 'add-comment' :
452         check_ajax_referer( $action );
453         if ( !current_user_can( 'edit_post', $id ) )
454                 die('-1');
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 );
462
463         if ( get_option('show_avatars') )
464                 add_filter( 'comment_author', 'floated_admin_avatar' );
465
466         if ( !$comments )
467                 die('1');
468         $x = new WP_Ajax_Response();
469         foreach ( (array) $comments as $comment ) {
470                 get_comment( $comment );
471                 ob_start();
472                         _wp_comment_row( $comment->comment_ID, $mode, $status, true, true );
473                         $comment_list_item = ob_get_contents();
474                 ob_end_clean();
475                 $x->add( array(
476                         'what' => 'comment',
477                         'id' => $comment->comment_ID,
478                         'data' => $comment_list_item
479                 ) );
480         }
481         $x->send();
482         break;
483 case 'get-comments' :
484         check_ajax_referer( $action );
485
486         $post_ID = (int) $_POST['post_ID'];
487         if ( !current_user_can( 'edit_post', $post_ID ) )
488                 die('-1');
489
490         $start = isset($_POST['start']) ? intval($_POST['start']) : 0;
491         $num = isset($_POST['num']) ? intval($_POST['num']) : 10;
492
493         list($comments, $total) = _wp_get_comment_list( false, false, $start, $num, $post_ID );
494
495         if ( !$comments )
496                 die('1');
497
498         $comment_list_item = '';
499         $x = new WP_Ajax_Response();
500         foreach ( (array) $comments as $comment ) {
501                 get_comment( $comment );
502                 ob_start();
503                         _wp_comment_row( $comment->comment_ID, 'single', false, false );
504                         $comment_list_item .= ob_get_contents();
505                 ob_end_clean();
506         }
507         $x->add( array(
508                 'what' => 'comments',
509                 'data' => $comment_list_item
510         ) );
511         $x->send();
512         break;
513 case 'replyto-comment' :
514         check_ajax_referer( $action );
515
516         $comment_post_ID = (int) $_POST['comment_post_ID'];
517         if ( !current_user_can( 'edit_post', $comment_post_ID ) )
518                 die('-1');
519
520         $status = $wpdb->get_var( $wpdb->prepare("SELECT post_status FROM $wpdb->posts WHERE ID = %d", $comment_post_ID) );
521
522         if ( empty($status) )
523                 die('1');
524         elseif ( in_array($status, array('draft', 'pending') ) )
525                 die( __('Error: you are replying to a comment on a draft post.') );
526
527         $user = wp_get_current_user();
528         if ( $user->ID ) {
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
537                         }
538                 }
539         } else {
540                 die( __('Sorry, you must be logged in to reply to a comment.') );
541         }
542
543         if ( '' == $comment_content )
544                 die( __('Error: please type a comment.') );
545
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');
548
549         $comment_id = wp_new_comment( $commentdata );
550         $comment = get_comment($comment_id);
551         if ( ! $comment ) die('1');
552
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;
557
558         if ( get_option('show_avatars') && 'single' != $mode )
559                 add_filter( 'comment_author', 'floated_admin_avatar' );
560
561         $x = new WP_Ajax_Response();
562
563         ob_start();
564                 if ( 'dashboard' == $mode ) {
565                         require_once( ABSPATH . 'wp-admin/includes/dashboard.php' );
566                         _wp_dashboard_recent_comments_row( $comment, false );
567                 } else {
568                         _wp_comment_row( $comment->comment_ID, $mode, false, $checkbox );
569                 }
570                 $comment_list_item = ob_get_contents();
571         ob_end_clean();
572
573         $x->add( array(
574                 'what' => 'comment',
575                 'id' => $comment->comment_ID,
576                 'data' => $comment_list_item,
577                 'position' => $position
578         ));
579
580         $x->send();
581         break;
582 case 'edit-comment' :
583         check_ajax_referer( 'replyto-comment' );
584
585         $comment_post_ID = (int) $_POST['comment_post_ID'];
586         if ( ! current_user_can( 'edit_post', $comment_post_ID ) )
587                 die('-1');
588
589         if ( '' == $_POST['content'] )
590                 die( __('Error: please type a comment.') );
591
592         $comment_id = (int) $_POST['comment_ID'];
593         $_POST['comment_status'] = $_POST['status'];
594         edit_comment();
595
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;
599
600         if ( get_option('show_avatars') && 'single' != $mode )
601                 add_filter( 'comment_author', 'floated_admin_avatar' );
602
603         $x = new WP_Ajax_Response();
604
605         ob_start();
606                 _wp_comment_row( $comment_id, $mode, true, $checkbox );
607                 $comment_list_item = ob_get_contents();
608         ob_end_clean();
609
610         $x->add( array(
611                 'what' => 'edit_comment',
612                 'id' => $comment->comment_ID,
613                 'data' => $comment_list_item,
614                 'position' => $position
615         ));
616
617         $x->send();
618         break;
619 case 'add-meta' :
620         check_ajax_referer( 'add-meta' );
621         $c = 0;
622         $pid = (int) $_POST['post_id'];
623         if ( isset($_POST['metakeyselect']) || isset($_POST['metakeyinput']) ) {
624                 if ( !current_user_can( 'edit_post', $pid ) )
625                         die('-1');
626                 if ( '#NONE#' == $_POST['metakeyselect'] && empty($_POST['metakeyinput']) )
627                         die('1');
628                 if ( $pid < 0 ) {
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))
632                         ) ) ) {
633                                 if ( is_wp_error( $pid ) ) {
634                                         $x = new WP_Ajax_Response( array(
635                                                 'what' => 'meta',
636                                                 'data' => $pid
637                                         ) );
638                                         $x->send();
639                                 }
640                                 $mid = add_meta( $pid );
641                         } else {
642                                 die('0');
643                         }
644                 } else if ( !$mid = add_meta( $pid ) ) {
645                         die('0');
646                 }
647
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(
652                         'what' => 'meta',
653                         'id' => $mid,
654                         'data' => _list_meta_row( $meta, $c ),
655                         'position' => 1,
656                         'supplemental' => array('postid' => $pid)
657                 ) );
658         } else {
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 ) )
665                         die('-1');
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(
671                         'what' => 'meta',
672                         'id' => $mid, 'old_id' => $mid,
673                         'data' => _list_meta_row( array(
674                                 'meta_key' => $key,
675                                 'meta_value' => $value,
676                                 'meta_id' => $mid
677                         ), $c ),
678                         'position' => 0,
679                         'supplemental' => array('postid' => $meta->post_id)
680                 ) );
681         }
682         $x->send();
683         break;
684 case 'add-user' :
685         check_ajax_referer( $action );
686         if ( !current_user_can('create_users') )
687                 die('-1');
688         require_once(ABSPATH . WPINC . '/registration.php');
689         if ( !$user_id = add_user() )
690                 die('0');
691         elseif ( is_wp_error( $user_id ) ) {
692                 $x = new WP_Ajax_Response( array(
693                         'what' => 'user',
694                         'id' => $user_id
695                 ) );
696                 $x->send();
697         }
698         $user_object = new WP_User( $user_id );
699
700         $x = new WP_Ajax_Response( array(
701                 'what' => 'user',
702                 'id' => $user_id,
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]
707                 )
708         ) );
709         $x->send();
710         break;
711 case 'autosave' : // The name of this action is hardcoded in edit_post()
712         define( 'DOING_AUTOSAVE', true );
713
714         $nonce_age = check_ajax_referer( 'autosave', 'autosavenonce' );
715         global $current_user;
716
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']);
721
722         $do_autosave = (bool) $_POST['autosave'];
723         $do_lock = true;
724
725         $data = '';
726         $message = sprintf( __('Draft Saved at %s.'), date_i18n( __('g:i:s a') ) );
727
728         $supplemental = array();
729
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();
736                         $data = $message;
737                 }
738         } else {
739                 $post_ID = (int) $_POST['post_ID'];
740                 $_POST['ID'] = $post_ID;
741                 $post = get_post($post_ID);
742
743                 if ( $last = wp_check_post_lock( $post->ID ) ) {
744                         $do_autosave = $do_lock = false;
745
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 )
751                         ) );
752
753                         $supplemental['disable_autosave'] = 'disable';
754                 }
755
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.'));
759                 } else {
760                         if ( !current_user_can('edit_post', $post_ID) )
761                                 die(__('You are not allowed to edit this post.'));
762                 }
763
764                 if ( $do_autosave ) {
765                         // Drafts are just overwritten by autosave
766                         if ( 'draft' == $post->post_status ) {
767                                 $id = edit_post();
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) )
771                                         $id = $revision_id;
772                                 else
773                                         $id = $post->ID;
774                         }
775                         $data = $message;
776                 } else {
777                         $id = $post->ID;
778                 }
779         }
780
781         if ( $do_lock && $id && is_numeric($id) )
782                 wp_set_post_lock( $id );
783
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');
789                 if ( $id ) {
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);
794                 }
795         }
796
797         $x = new WP_Ajax_Response( array(
798                 'what' => 'autosave',
799                 'id' => $id,
800                 'data' => $id ? $data : '',
801                 'supplemental' => $supplemental
802         ) );
803         $x->send();
804         break;
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));
811         }
812         if($_POST['post_type'] == 'page') {
813                 if(current_user_can('edit_page', $ID)) {
814                         die(wp_create_nonce('update-page_' . $ID));
815                 }
816         }
817         die('0');
818 break;
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 ) ) {
827                 die(-1);
828         }
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);
834 break;
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 ) ) {
841                 die(-1);
842         }
843         $current_user = wp_get_current_user();
844         if ( is_array($hidden) )
845                 update_usermeta($current_user->ID, "manage-$page-columns-hidden", $hidden);
846 break;
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)));
851 break;
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));
858 break;
859 case 'inline-save':
860         check_ajax_referer( 'inlineeditnonce', '_inline_edit' );
861
862         if ( ! isset($_POST['post_ID']) || ! ( $post_ID = (int) $_POST['post_ID'] ) )
863                 exit;
864
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.') );
868         } else {
869                 if ( ! current_user_can( 'edit_post', $post_ID ) )
870                         die( __('You are not allowed to edit this post.') );
871         }
872
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 ) );
877                 exit;
878         }
879
880         $data = &$_POST;
881         $post = get_post( $post_ID, ARRAY_A );
882         $data['content'] = $post['post_content'];
883         $data['excerpt'] = $post['post_excerpt'];
884
885         // rename
886         $data['user_ID'] = $GLOBALS['user_ID'];
887
888         if ( isset($data['post_parent']) )
889                 $data['parent_id'] = $data['post_parent'];
890
891         // status
892         if ( isset($data['keep_private']) && 'private' == $data['keep_private'] )
893                 $data['post_status'] = 'private';
894         else
895                 $data['post_status'] = $data['_status'];
896
897         if ( empty($data['comment_status']) )
898                 $data['comment_status'] = 'closed';
899         if ( empty($data['ping_status']) )
900                 $data['ping_status'] = 'closed';
901
902         // update the post
903         $_POST = $data;
904         edit_post();
905
906         $post = array();
907         if ( 'page' == $_POST['post_type'] ) {
908                 $post[] = get_post($_POST['post_ID']);
909                 page_rows($post);
910         } elseif ( 'post' == $_POST['post_type'] ) {
911                 $mode = $_POST['post_view'];
912                 $post[] = get_post($_POST['post_ID']);
913                 post_rows($post);
914         }
915
916         exit;
917         break;
918 case 'inline-save-tax':
919         check_ajax_referer( 'taxinlineeditnonce', '_inline_edit' );
920
921         if ( ! current_user_can('manage_categories') )
922                 die( __('Cheatin&#8217; uh?') );
923
924         if ( ! isset($_POST['tax_ID']) || ! ( $id = (int) $_POST['tax_ID'] ) )
925                 die(-1);
926
927         switch ($_POST['tax_type']) {
928                 case 'cat' :
929                         $data = array();
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'];
935
936                         $cat = get_category($id, ARRAY_A);
937                         $data['category_description'] = $cat['category_description'];
938
939                         $updated = wp_update_category($data);
940
941                         if ( $updated && !is_wp_error($updated) )
942                                 echo _cat_row( $updated, 0 );
943                         else
944                                 die( __('Category not updated.') );
945
946                         break;
947                 case 'link-cat' :
948                         $updated = wp_update_term($id, 'link_category', $_POST);
949
950                         if ( $updated && !is_wp_error($updated) )
951                                 echo link_cat_row($updated['term_id']);
952                         else
953                                 die( __('Category not updated.') );
954
955                         break;
956                 case 'tag' :
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.') );
962
963                                 echo _tag_row($tag);
964                         } else {
965                                 die( __('Tag not updated.') );
966                         }
967
968                         break;
969         }
970
971         exit;
972         break;
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'] );
976         die('1');
977         break;
978 case 'find_posts':
979         check_ajax_referer( 'find-posts' );
980
981         if ( empty($_POST['ps']) )
982                 exit;
983
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]);
988
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 ';
994         }
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}%')";
998
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" );
1000
1001         if ( ! $posts )
1002                 exit( __('No posts found.') );
1003
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 ) {
1006
1007                 switch ( $post->post_status ) {
1008                         case 'publish' :
1009                         case 'private' :
1010                                 $stat = __('Published');
1011                                 break;
1012                         case 'future' :
1013                                 $stat = __('Scheduled');
1014                                 break;
1015                         case 'pending' :
1016                                 $stat = __('Pending Review');
1017                                 break;
1018                         case 'draft' :
1019                                 $stat = __('Unpublished');
1020                                 break;
1021                 }
1022
1023                 if ( '0000-00-00 00:00:00' == $post->post_date ) {
1024                         $time = '';
1025                 } else {
1026                         $time = mysql2date(__('Y/m/d'), $post->post_date);
1027                 }
1028
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";
1031         }
1032         $html .= '</tbody></table>';
1033
1034         $x = new WP_Ajax_Response();
1035         $x->add( array(
1036                 'what' => $what,
1037                 'data' => $html
1038         ));
1039         $x->send();
1040
1041         break;
1042 default :
1043         do_action( 'wp_ajax_' . $_POST['action'] );
1044         die('0');
1045         break;
1046 endswitch;
1047 ?>