]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/admin-ajax.php
4485935147ed90a97b3f8cb89a57f5c84e5a2551
[autoinstalls/wordpress.git] / wp-admin / admin-ajax.php
1 <?php
2 define('DOING_AJAX', true);
3
4 require_once('../wp-load.php');
5 require_once('includes/admin.php');
6
7 if ( !is_user_logged_in() )
8         die('-1');
9
10 if ( isset($_GET['action']) && 'ajax-tag-search' == $_GET['action'] ) {
11         if ( !current_user_can( 'manage_categories' ) )
12                 die('-1');
13
14         $s = $_GET['q']; // is this slashed already?
15
16         if ( strstr( $s, ',' ) ) { 
17                 $s = explode( ',', $s ); 
18                 $s = $s[count( $s ) - 1]; 
19         }
20         $s = trim( $s );
21         if ( strlen( $s ) < 2 )
22          die; // require 2 chars for matching
23         $results = $wpdb->get_col( "SELECT name FROM $wpdb->terms WHERE name LIKE ('%". $s . "%')" );
24         echo join( $results, "\n" );
25         die;
26 }
27
28 $id = isset($_POST['id'])? (int) $_POST['id'] : 0;
29 switch ( $action = $_POST['action'] ) :
30 case 'delete-comment' :
31         check_ajax_referer( "delete-comment_$id" );
32         if ( !$comment = get_comment( $id ) )
33                 die('1');
34         if ( !current_user_can( 'edit_post', $comment->comment_post_ID ) )
35                 die('-1');
36
37         if ( isset($_POST['spam']) && 1 == $_POST['spam'] ) {
38                 if ( 'spam' == wp_get_comment_status( $comment->comment_ID ) )
39                         die('1');
40                 $r = wp_set_comment_status( $comment->comment_ID, 'spam' );
41         } else {
42                 $r = wp_delete_comment( $comment->comment_ID );
43         }
44
45         die( $r ? '1' : '0' );
46         break;
47 case 'delete-cat' :
48         check_ajax_referer( "delete-category_$id" );
49         if ( !current_user_can( 'manage_categories' ) )
50                 die('-1');
51
52         $cat = get_category( $id );
53         if ( !$cat || is_wp_error( $cat ) )
54                 die('1');
55
56         if ( wp_delete_category( $id ) )
57                 die('1');
58         else
59                 die('0');
60         break;
61 case 'delete-tag' :
62         check_ajax_referer( "delete-tag_$id" );
63         if ( !current_user_can( 'manage_categories' ) )
64                 die('-1');
65
66         $tag = get_term( $id, 'post_tag' );
67         if ( !$tag || is_wp_error( $tag ) )
68                 die('1');
69
70         if ( wp_delete_term($id, 'post_tag'))
71                 die('1');
72         else
73                 die('0');
74         break;
75 case 'delete-link-cat' :
76         check_ajax_referer( "delete-link-category_$id" );
77         if ( !current_user_can( 'manage_categories' ) )
78                 die('-1');
79
80         $cat = get_term( $id, 'link_category' );
81         if ( !$cat || is_wp_error( $cat ) )
82                 die('1');
83
84         $cat_name = get_term_field('name', $id, 'link_category');
85
86         // Don't delete the default cats.
87         if ( $id == get_option('default_link_category') ) {
88                 $x = new WP_AJAX_Response( array(
89                         'what' => 'link-cat',
90                         'id' => $id,
91                         'data' => new WP_Error( 'default-link-cat', sprintf(__("Can&#8217;t delete the <strong>%s</strong> category: this is the default one"), $cat_name) )
92                 ) );
93                 $x->send();
94         }
95
96         $r = wp_delete_term($id, 'link_category');
97         if ( !$r )
98                 die('0');
99         if ( is_wp_error($r) ) {
100                 $x = new WP_AJAX_Response( array(
101                         'what' => 'link-cat',
102                         'id' => $id,
103                         'data' => $r
104                 ) );
105                 $x->send();
106         }
107         die('1');
108         break;
109 case 'delete-link' :
110         check_ajax_referer( "delete-bookmark_$id" );
111         if ( !current_user_can( 'manage_links' ) )
112                 die('-1');
113
114         $link = get_bookmark( $id );
115         if ( !$link || is_wp_error( $link ) )
116                 die('1');
117
118         if ( wp_delete_link( $id ) )
119                 die('1');
120         else
121                 die('0');
122         break;
123 case 'delete-meta' :
124         check_ajax_referer( "delete-meta_$id" );
125         if ( !$meta = get_post_meta_by_id( $id ) )
126                 die('1');
127
128         if ( !current_user_can( 'edit_post', $meta->post_id ) )
129                 die('-1');
130         if ( delete_meta( $meta->meta_id ) )
131                 die('1');
132         die('0');
133         break;
134 case 'delete-post' :
135         check_ajax_referer( "{$action}_$id" );
136         if ( !current_user_can( 'delete_post', $id ) )
137                 die('-1');
138
139         if ( !get_post( $id ) )
140                 die('1');
141
142         if ( wp_delete_post( $id ) )
143                 die('1');
144         else
145                 die('0');
146         break;
147 case 'delete-page' :
148         check_ajax_referer( "{$action}_$id" );
149         if ( !current_user_can( 'delete_page', $id ) )
150                 die('-1');
151
152         if ( !get_page( $id ) )
153                 die('1');
154
155         if ( wp_delete_post( $id ) )
156                 die('1');
157         else
158                 die('0');
159         break;
160 case 'dim-comment' :
161         if ( !$comment = get_comment( $id ) )
162                 die('0');
163
164         if ( !current_user_can( 'edit_post', $comment->comment_post_ID ) )
165                 die('-1');
166         if ( !current_user_can( 'moderate_comments' ) )
167                 die('-1');
168
169         $current = wp_get_comment_status( $comment->comment_ID );
170         if ( $_POST['new'] == $current )
171                 die('1');
172
173         if ( 'unapproved' == $current ) {
174                 check_ajax_referer( "approve-comment_$id" );
175                 if ( wp_set_comment_status( $comment->comment_ID, 'approve' ) )
176                         die('1');
177         } else {
178                 check_ajax_referer( "unapprove-comment_$id" );
179                 if ( wp_set_comment_status( $comment->comment_ID, 'hold' ) )
180                         die('1');
181         }
182         die('0');
183         break;
184 case 'add-category' : // On the Fly
185         check_ajax_referer( $action );
186         if ( !current_user_can( 'manage_categories' ) )
187                 die('-1');
188         $names = explode(',', $_POST['newcat']);
189         if ( 0 > $parent = (int) $_POST['newcat_parent'] )
190                 $parent = 0;
191         $post_category = isset($_POST['post_category'])? (array) $_POST['post_category'] : array();
192         $checked_categories = array_map( 'absint', (array) $post_category );
193         $popular_ids = isset( $_POST['popular_ids'] ) ?
194                         array_map( 'absint', explode( ',', $_POST['popular_ids'] ) ) :
195                         false;
196
197         $x = new WP_Ajax_Response();
198         foreach ( $names as $cat_name ) {
199                 $cat_name = trim($cat_name);
200                 $category_nicename = sanitize_title($cat_name);
201                 if ( '' === $category_nicename )
202                         continue;
203                 $cat_id = wp_create_category( $cat_name, $parent );
204                 $checked_categories[] = $cat_id;
205                 if ( $parent ) // Do these all at once in a second
206                         continue;
207                 $category = get_category( $cat_id );
208                 ob_start();
209                         wp_category_checklist( 0, $cat_id, $checked_categories, $popular_ids );
210                 $data = ob_get_contents();
211                 ob_end_clean();
212                 $x->add( array(
213                         'what' => 'category',
214                         'id' => $cat_id,
215                         'data' => $data,
216                         'position' => -1
217                 ) );
218         }
219         if ( $parent ) { // Foncy - replace the parent and all its children
220                 $parent = get_category( $parent );
221                 ob_start();
222                         dropdown_categories( 0, $parent );
223                 $data = ob_get_contents();
224                 ob_end_clean();
225                 $x->add( array(
226                         'what' => 'category',
227                         'id' => $parent->term_id,
228                         'old_id' => $parent->term_id,
229                         'data' => $data,
230                         'position' => -1
231                 ) );
232
233         }
234         $x->send();
235         break;
236 case 'add-link-category' : // On the Fly
237         check_ajax_referer( $action );
238         if ( !current_user_can( 'manage_categories' ) )
239                 die('-1');
240         $names = explode(',', $_POST['newcat']);
241         $x = new WP_Ajax_Response();
242         foreach ( $names as $cat_name ) {
243                 $cat_name = trim($cat_name);
244                 $slug = sanitize_title($cat_name);
245                 if ( '' === $slug )
246                         continue;
247                 if ( !$cat_id = is_term( $cat_name, 'link_category' ) ) {
248                         $cat_id = wp_insert_term( $cat_name, 'link_category' );
249                 }
250                 $cat_id = $cat_id['term_id'];
251                 $cat_name = wp_specialchars(stripslashes($cat_name));
252                 $x->add( array(
253                         'what' => 'link-category',
254                         'id' => $cat_id,
255                         'data' => "<li id='link-category-$cat_id'><label for='in-link-category-$cat_id' class='selectit'><input value='$cat_id' type='checkbox' checked='checked' name='link_category[]' id='in-link-category-$cat_id'/> $cat_name</label></li>",
256                         'position' => -1
257                 ) );
258         }
259         $x->send();
260         break;
261 case 'add-cat' : // From Manage->Categories
262         check_ajax_referer( 'add-category' );
263         if ( !current_user_can( 'manage_categories' ) )
264                 die('-1');
265
266         if ( '' === trim($_POST['cat_name']) ) {
267                 $x = new WP_Ajax_Response( array(
268                         'what' => 'cat',
269                         'id' => new WP_Error( 'cat_name', __('You did not enter a category name.') )
270                 ) );
271                 $x->send();
272         }
273
274         if ( category_exists( trim( $_POST['cat_name'] ) ) ) {
275                 $x = new WP_Ajax_Response( array(
276                         'what' => 'cat',
277                         'id' => new WP_Error( 'cat_exists', __('The category you are trying to create already exists.'), array( 'form-field' => 'cat_name' ) ),
278                 ) );
279                 $x->send();
280         }
281         
282         $cat = wp_insert_category( $_POST, true );
283
284         if ( is_wp_error($cat) ) {
285                 $x = new WP_Ajax_Response( array(
286                         'what' => 'cat',
287                         'id' => $cat
288                 ) );
289                 $x->send();
290         }
291
292         if ( !$cat || (!$cat = get_category( $cat )) )
293                 die('0');
294
295         $level = 0;
296         $cat_full_name = $cat->name;
297         $_cat = $cat;
298         while ( $_cat->parent ) {
299                 $_cat = get_category( $_cat->parent );
300                 $cat_full_name = $_cat->name . ' &#8212; ' . $cat_full_name;
301                 $level++;
302         }
303         $cat_full_name = attribute_escape($cat_full_name);
304
305         $x = new WP_Ajax_Response( array(
306                 'what' => 'cat',
307                 'id' => $cat->term_id,
308                 'data' => _cat_row( $cat, $level, $cat_full_name ),
309                 'supplemental' => array('name' => $cat_full_name, 'show-link' => sprintf(__( 'Category <a href="#%s">%s</a> added' ), "cat-$cat->term_id", $cat_full_name))
310         ) );
311         $x->send();
312         break;
313 case 'add-link-cat' : // From Blogroll -> Categories
314         check_ajax_referer( 'add-link-category' );
315         if ( !current_user_can( 'manage_categories' ) )
316                 die('-1');
317
318         if ( '' === trim($_POST['name']) ) {
319                 $x = new WP_Ajax_Response( array(
320                         'what' => 'link-cat',
321                         'id' => new WP_Error( 'name', __('You did not enter a category name.') )
322                 ) );
323                 $x->send();
324         }
325
326         $r = wp_insert_term($_POST['name'], 'link_category', $_POST );
327         if ( is_wp_error( $r ) ) {
328                 $x = new WP_AJAX_Response( array(
329                         'what' => 'link-cat',
330                         'id' => $r
331                 ) );
332                 $x->send();
333         }
334
335         extract($r, EXTR_SKIP);
336
337         if ( !$link_cat = link_cat_row( $term_id ) )
338                 die('0');
339
340         $x = new WP_Ajax_Response( array(
341                 'what' => 'link-cat',
342                 'id' => $term_id,
343                 'data' => $link_cat
344         ) );
345         $x->send();
346         break;
347 case 'add-tag' : // From Manage->Tags
348         check_ajax_referer( 'add-tag' );
349         if ( !current_user_can( 'manage_categories' ) )
350                 die('-1');
351
352         if ( '' === trim($_POST['name']) ) {
353                 $x = new WP_Ajax_Response( array(
354                         'what' => 'tag',
355                         'id' => new WP_Error( 'name', __('You did not enter a tag name.') )
356                 ) );
357                 $x->send();
358         }
359
360         $tag = wp_insert_term($_POST['name'], 'post_tag', $_POST );
361
362         if ( is_wp_error($tag) ) {
363                 $x = new WP_Ajax_Response( array(
364                         'what' => 'tag',
365                         'id' => $tag
366                 ) );
367                 $x->send();
368         }
369
370         if ( !$tag || (!$tag = get_term( $tag['term_id'], 'post_tag' )) )
371                 die('0');
372
373         $tag_full_name = $tag->name;
374         $tag_full_name = attribute_escape($tag_full_name);
375
376         $x = new WP_Ajax_Response( array(
377                 'what' => 'tag',
378                 'id' => $tag->term_id,
379                 'data' => _tag_row( $tag ),
380                 'supplemental' => array('name' => $tag_full_name, 'show-link' => sprintf(__( 'Tag <a href="#%s">%s</a> added' ), "tag-$tag->term_id", $tag_full_name))
381         ) );
382         $x->send();
383         break;
384 case 'add-comment' :
385         check_ajax_referer( $action );
386         if ( !current_user_can( 'edit_post', $id ) )
387                 die('-1');
388         $search = isset($_POST['s']) ? $_POST['s'] : false;
389         $start = isset($_POST['page']) ? intval($_POST['page']) * 25 - 1: 24;
390         $status = isset($_POST['comment_status']) ? $_POST['comment_status'] : false;
391         $mode = isset($_POST['mode']) ? $_POST['mode'] : 'detail';
392
393         list($comments, $total) = _wp_get_comment_list( $status, $search, $start, 1 );
394
395         if ( get_option('show_avatars') )
396                 add_filter( 'comment_author', 'floated_admin_avatar' );
397
398         if ( !$comments )
399                 die('1');
400         $x = new WP_Ajax_Response();
401         foreach ( (array) $comments as $comment ) {
402                 get_comment( $comment );
403                 ob_start();
404                         _wp_comment_row( $comment->comment_ID, $mode, $status );
405                         $comment_list_item = ob_get_contents();
406                 ob_end_clean();
407                 $x->add( array(
408                         'what' => 'comment',
409                         'id' => $comment->comment_ID,
410                         'data' => $comment_list_item
411                 ) );
412         }
413         $x->send();
414         break;
415 case 'add-meta' :
416         check_ajax_referer( 'add-meta' );
417         $c = 0;
418         $pid = (int) $_POST['post_id'];
419         if ( isset($_POST['metakeyselect']) || isset($_POST['metakeyinput']) ) {
420                 if ( !current_user_can( 'edit_post', $pid ) )
421                         die('-1');
422                 if ( '#NONE#' == $_POST['metakeyselect'] && empty($_POST['metakeyinput']) )
423                         die('1');
424                 if ( $pid < 0 ) {
425                         $now = current_time('timestamp', 1);
426                         if ( $pid = wp_insert_post( array(
427                                 'post_title' => sprintf('Draft created on %s at %s', date(get_option('date_format'), $now), date(get_option('time_format'), $now))
428                         ) ) ) {
429                                 if ( is_wp_error( $pid ) ) {
430                                         $x = new WP_Ajax_Response( array(
431                                                 'what' => 'meta',
432                                                 'data' => $pid
433                                         ) );
434                                         $x->send();
435                                 }
436                                 $mid = add_meta( $pid );
437                         } else {
438                                 die('0');
439                         }
440                 } else if ( !$mid = add_meta( $pid ) ) {
441                         die('0');
442                 }
443
444                 $meta = get_post_meta_by_id( $mid );
445                 $pid = (int) $meta->post_id;
446                 $meta = get_object_vars( $meta );
447                 $x = new WP_Ajax_Response( array(
448                         'what' => 'meta',
449                         'id' => $mid,
450                         'data' => _list_meta_row( $meta, $c ),
451                         'position' => 1,
452                         'supplemental' => array('postid' => $pid)
453                 ) );
454         } else {
455                 $mid = (int) array_pop(array_keys($_POST['meta']));
456                 $key = $_POST['meta'][$mid]['key'];
457                 $value = $_POST['meta'][$mid]['value'];
458                 if ( !$meta = get_post_meta_by_id( $mid ) )
459                         die('0'); // if meta doesn't exist
460                 if ( !current_user_can( 'edit_post', $meta->post_id ) )
461                         die('-1');
462                 if ( !$u = update_meta( $mid, $key, $value ) )
463                         die('1'); // We know meta exists; we also know it's unchanged (or DB error, in which case there are bigger problems).
464                 $key = stripslashes($key);
465                 $value = stripslashes($value);
466                 $x = new WP_Ajax_Response( array(
467                         'what' => 'meta',
468                         'id' => $mid, 'old_id' => $mid,
469                         'data' => _list_meta_row( array(
470                                 'meta_key' => $key,
471                                 'meta_value' => $value,
472                                 'meta_id' => $mid
473                         ), $c ),
474                         'position' => 0,
475                         'supplemental' => array('postid' => $meta->post_id)
476                 ) );
477         }
478         $x->send();
479         break;
480 case 'add-user' :
481         check_ajax_referer( $action );
482         if ( !current_user_can('create_users') )
483                 die('-1');
484         require_once(ABSPATH . WPINC . '/registration.php');
485         if ( !$user_id = add_user() )
486                 die('0');
487         elseif ( is_wp_error( $user_id ) ) {
488                 $x = new WP_Ajax_Response( array(
489                         'what' => 'user',
490                         'id' => $user_id
491                 ) );
492                 $x->send();
493         }
494         $user_object = new WP_User( $user_id );
495
496         $x = new WP_Ajax_Response( array(
497                 'what' => 'user',
498                 'id' => $user_id,
499                 'data' => user_row( $user_object, '', $user_object->roles[0] ),
500                 'supplemental' => array(
501                         'show-link' => sprintf(__( 'User <a href="#%s">%s</a> added' ), "user-$user_id", $user_object->user_login),
502                         'role' => $user_object->roles[0]
503                 )
504         ) );
505         $x->send();
506         break;
507 case 'autosave' : // The name of this action is hardcoded in edit_post()
508         define( 'DOING_AUTOSAVE', true );
509
510         $nonce_age = check_ajax_referer( 'autosave', 'autosavenonce' );
511         global $current_user;
512
513         $_POST['post_category'] = explode(",", $_POST['catslist']);
514         $_POST['tags_input'] = explode(",", $_POST['tags_input']);
515         if($_POST['post_type'] == 'page' || empty($_POST['post_category']))
516                 unset($_POST['post_category']);
517
518         $do_autosave = (bool) $_POST['autosave'];
519         $do_lock = true;
520
521         $data = '';
522         $message = sprintf( __('Draft Saved at %s.'), date( __('g:i:s a'), current_time( 'timestamp', true ) ) );
523
524         $supplemental = array();
525
526         $id = $revision_id = 0;
527         if($_POST['post_ID'] < 0) {
528                 $_POST['post_status'] = 'draft';
529                 $_POST['temp_ID'] = $_POST['post_ID'];
530                 if ( $do_autosave ) {
531                         $id = wp_write_post();
532                         $data = $message;
533                 }
534         } else {
535                 $post_ID = (int) $_POST['post_ID'];
536                 $_POST['ID'] = $post_ID;
537                 $post = get_post($post_ID);
538
539                 if ( $last = wp_check_post_lock( $post->ID ) ) {
540                         $do_autosave = $do_lock = false;
541
542                         $last_user = get_userdata( $last );
543                         $last_user_name = $last_user ? $last_user->display_name : __( 'Someone' );
544                         $data = new WP_Error( 'locked', sprintf(
545                                 $_POST['post_type'] == 'page' ? __( 'Autosave disabled: %s is currently editing this page.' ) : __( 'Autosave disabled: %s is currently editing this post.' ),
546                                 wp_specialchars( $last_user_name )
547                         ) );
548
549                         $supplemental['disable_autosave'] = 'disable';
550                 }
551
552                 if ( 'page' == $post->post_type ) {
553                         if ( !current_user_can('edit_page', $post_ID) )
554                                 die(__('You are not allowed to edit this page.'));
555                 } else {
556                         if ( !current_user_can('edit_post', $post_ID) )
557                                 die(__('You are not allowed to edit this post.'));
558                 }
559
560                 if ( $do_autosave ) {
561                         // Drafts are just overwritten by autosave
562                         if ( 'draft' == $post->post_status ) {
563                                 $id = edit_post();
564                         } else { // Non drafts are not overwritten.  The autosave is stored in a special post revision.
565                                 $revision_id = wp_create_post_autosave( $post->ID );
566                                 if ( is_wp_error($revision_id) )
567                                         $id = $revision_id;
568                                 else
569                                         $id = $post->ID;
570                         }
571                         $data = $message;
572                 } else {
573                         $id = $post->ID;
574                 }
575         }
576
577         if ( $do_lock && $id && is_numeric($id) )
578                 wp_set_post_lock( $id );
579
580         if ( $nonce_age == 2 ) {
581                 $supplemental['replace-autosavenonce'] = wp_create_nonce('autosave');
582                 $supplemental['replace-getpermalinknonce'] = wp_create_nonce('getpermalink');
583                 $supplemental['replace-samplepermalinknonce'] = wp_create_nonce('samplepermalink');
584                 $supplemental['replace-closedpostboxesnonce'] = wp_create_nonce('closedpostboxes');
585                 if ( $id ) {
586                         if ( $_POST['post_type'] == 'post' )
587                                 $supplemental['replace-_wpnonce'] = wp_create_nonce('update-post_' . $id);
588                         elseif ( $_POST['post_type'] == 'page' )
589                                 $supplemental['replace-_wpnonce'] = wp_create_nonce('update-page_' . $id);
590                 }
591         }
592
593         $x = new WP_Ajax_Response( array(
594                 'what' => 'autosave',
595                 'id' => $id,
596                 'data' => $id ? $data : '',
597                 'supplemental' => $supplemental
598         ) );
599         $x->send();
600         break;
601 case 'autosave-generate-nonces' :
602         check_ajax_referer( 'autosave', 'autosavenonce' );
603         $ID = (int) $_POST['post_ID'];
604         if($_POST['post_type'] == 'post') {
605                 if(current_user_can('edit_post', $ID))
606                         die(wp_create_nonce('update-post_' . $ID));
607         }
608         if($_POST['post_type'] == 'page') {
609                 if(current_user_can('edit_page', $ID)) {
610                         die(wp_create_nonce('update-page_' . $ID));
611                 }
612         }
613         die('0');
614 break;
615 case 'closed-postboxes' :
616         check_ajax_referer( 'closedpostboxes', 'closedpostboxesnonce' );
617         $closed = isset( $_POST['closed'] )? $_POST['closed'] : '';
618         $closed = explode( ',', $_POST['closed'] );
619         $page = isset( $_POST['page'] )? $_POST['page'] : '';
620         if ( !preg_match( '/^[a-z-]+$/', $page ) ) {
621                 die(-1);
622         }
623         if (!is_array($closed)) break;
624         $current_user = wp_get_current_user();
625         update_usermeta($current_user->ID, 'closedpostboxes_'.$page, $closed);
626 break;
627 case 'get-permalink':
628         check_ajax_referer( 'getpermalink', 'getpermalinknonce' );
629         $post_id = isset($_POST['post_id'])? intval($_POST['post_id']) : 0;
630         die(add_query_arg(array('preview' => 'true'), get_permalink($post_id)));
631 break;
632 case 'sample-permalink':
633         check_ajax_referer( 'samplepermalink', 'samplepermalinknonce' );
634         $post_id = isset($_POST['post_id'])? intval($_POST['post_id']) : 0;
635         $title = isset($_POST['new_title'])? $_POST['new_title'] : '';
636         $slug = isset($_POST['new_slug'])? $_POST['new_slug'] : '';
637         die(get_sample_permalink_html($post_id, $title, $slug));
638 break;
639 default :
640         do_action( 'wp_ajax_' . $_POST['action'] );
641         die('0');
642         break;
643 endswitch;
644 ?>