2 define('DOING_AJAX', true);
4 require_once('../wp-config.php');
5 require_once('includes/admin.php');
7 if ( !is_user_logged_in() )
10 if ( isset($_GET['action']) && 'ajax-tag-search' == $_GET['action'] ) {
11 if ( !current_user_can( 'manage_categories' ) )
14 $s = $_GET['q']; // is this slashed already?
16 if ( strstr( $s, ',' ) )
17 die; // it's a multiple tag insert, we won't find anything
18 $results = $wpdb->get_col( "SELECT name FROM $wpdb->terms WHERE name LIKE ('%$s%')" );
19 echo join( $results, "\n" );
23 $id = isset($_POST['id'])? (int) $_POST['id'] : 0;
24 switch ( $action = $_POST['action'] ) :
25 case 'delete-comment' :
26 check_ajax_referer( "delete-comment_$id" );
27 if ( !$comment = get_comment( $id ) )
29 if ( !current_user_can( 'edit_post', $comment->comment_post_ID ) )
32 if ( isset($_POST['spam']) && 1 == $_POST['spam'] )
33 $r = wp_set_comment_status( $comment->comment_ID, 'spam' );
35 $r = wp_delete_comment( $comment->comment_ID );
37 die( $r ? '1' : '0' );
40 check_ajax_referer( "delete-category_$id" );
41 if ( !current_user_can( 'manage_categories' ) )
44 if ( wp_delete_category( $id ) )
49 check_ajax_referer( "delete-tag_$id" );
50 if ( !current_user_can( 'manage_categories' ) )
53 if ( wp_delete_term($id, 'post_tag'))
57 case 'delete-link-cat' :
58 check_ajax_referer( "delete-link-category_$id" );
59 if ( !current_user_can( 'manage_categories' ) )
62 $cat_name = get_term_field('name', $id, 'link_category');
64 // Don't delete the default cats.
65 if ( $id == get_option('default_link_category') ) {
66 $x = new WP_AJAX_Response( array(
69 'data' => new WP_Error( 'default-link-cat', sprintf(__("Can’t delete the <strong>%s</strong> category: this is the default one"), $cat_name) )
74 $r = wp_delete_term($id, 'link_category');
77 if ( is_wp_error($r) ) {
78 $x = new WP_AJAX_Response( array(
88 check_ajax_referer( "delete-bookmark_$id" );
89 if ( !current_user_can( 'manage_links' ) )
92 if ( wp_delete_link( $id ) )
97 check_ajax_referer( "delete-meta_$id" );
98 if ( !$meta = get_post_meta_by_id( $id ) )
100 if ( !current_user_can( 'edit_post', $meta->post_id ) )
102 if ( delete_meta( $meta->meta_id ) )
107 check_ajax_referer( "{$action}_$id" );
108 if ( !current_user_can( 'delete_post', $id ) )
111 if ( wp_delete_post( $id ) )
117 check_ajax_referer( "{$action}_$id" );
118 if ( !current_user_can( 'delete_page', $id ) )
121 if ( wp_delete_post( $id ) )
126 if ( !$comment = get_comment( $id ) )
128 if ( !current_user_can( 'edit_post', $comment->comment_post_ID ) )
130 if ( !current_user_can( 'moderate_comments' ) )
133 if ( 'unapproved' == wp_get_comment_status($comment->comment_ID) ) {
134 check_ajax_referer( "approve-comment_$id" );
135 if ( wp_set_comment_status( $comment->comment_ID, 'approve' ) )
138 check_ajax_referer( "unapprove-comment_$id" );
139 if ( wp_set_comment_status( $comment->comment_ID, 'hold' ) )
144 case 'add-category' : // On the Fly
145 check_ajax_referer( $action );
146 if ( !current_user_can( 'manage_categories' ) )
148 $names = explode(',', $_POST['newcat']);
149 if ( 0 > $parent = (int) $_POST['newcat_parent'] )
151 $post_category = isset($_POST['post_category'])? (array) $_POST['post_category'] : array();
152 $checked_categories = array_map( 'absint', (array) $post_category );
154 $x = new WP_Ajax_Response();
155 foreach ( $names as $cat_name ) {
156 $cat_name = trim($cat_name);
157 $category_nicename = sanitize_title($cat_name);
158 if ( '' === $category_nicename )
160 $cat_id = wp_create_category( $cat_name, $parent );
161 $checked_categories[] = $cat_id;
162 if ( $parent ) // Do these all at once in a second
164 $category = get_category( $cat_id );
166 wp_category_checklist( 0, $cat_id, $checked_categories );
167 $data = ob_get_contents();
170 'what' => 'category',
176 if ( $parent ) { // Foncy - replace the parent and all its children
177 $parent = get_category( $parent );
179 dropdown_categories( 0, $parent );
180 $data = ob_get_contents();
183 'what' => 'category',
184 'id' => $parent->term_id,
185 'old_id' => $parent->term_id,
193 case 'add-link-category' : // On the Fly
194 check_ajax_referer( $action );
195 if ( !current_user_can( 'manage_categories' ) )
197 $names = explode(',', $_POST['newcat']);
198 $x = new WP_Ajax_Response();
199 foreach ( $names as $cat_name ) {
200 $cat_name = trim($cat_name);
201 $slug = sanitize_title($cat_name);
204 if ( !$cat_id = is_term( $cat_name, 'link_category' ) ) {
205 $cat_id = wp_insert_term( $cat_name, 'link_category' );
207 $cat_id = $cat_id['term_id'];
208 $cat_name = wp_specialchars(stripslashes($cat_name));
210 'what' => 'link-category',
212 '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>",
218 case 'add-cat' : // From Manage->Categories
219 check_ajax_referer( 'add-category' );
220 if ( !current_user_can( 'manage_categories' ) )
223 if ( '' === trim($_POST['cat_name']) ) {
224 $x = new WP_Ajax_Response( array(
226 'id' => new WP_Error( 'cat_name', __('You did not enter a category name.') )
231 if ( category_exists( trim( $_POST['cat_name'] ) ) ) {
232 $x = new WP_Ajax_Response( array(
234 'id' => new WP_Error( 'cat_exists', __('The category you are trying to create already exists.'), array( 'form-field' => 'cat_name' ) ),
239 $cat = wp_insert_category( $_POST, true );
241 if ( is_wp_error($cat) ) {
242 $x = new WP_Ajax_Response( array(
249 if ( !$cat || (!$cat = get_category( $cat )) )
253 $cat_full_name = $cat->name;
255 while ( $_cat->parent ) {
256 $_cat = get_category( $_cat->parent );
257 $cat_full_name = $_cat->name . ' — ' . $cat_full_name;
260 $cat_full_name = attribute_escape($cat_full_name);
262 $x = new WP_Ajax_Response( array(
264 'id' => $cat->term_id,
265 'data' => _cat_row( $cat, $level, $cat_full_name ),
266 'supplemental' => array('name' => $cat_full_name, 'show-link' => sprintf(__( 'Category <a href="#%s">%s</a> added' ), "cat-$cat->term_id", $cat_full_name))
270 case 'add-link-cat' : // From Blogroll -> Categories
271 check_ajax_referer( 'add-link-category' );
272 if ( !current_user_can( 'manage_categories' ) )
275 if ( '' === trim($_POST['name']) ) {
276 $x = new WP_Ajax_Response( array(
277 'what' => 'link-cat',
278 'id' => new WP_Error( 'name', __('You did not enter a category name.') )
283 $r = wp_insert_term($_POST['name'], 'link_category', $_POST );
284 if ( is_wp_error( $r ) ) {
285 $x = new WP_AJAX_Response( array(
286 'what' => 'link-cat',
292 extract($r, EXTR_SKIP);
294 if ( !$link_cat = link_cat_row( $term_id ) )
297 $x = new WP_Ajax_Response( array(
298 'what' => 'link-cat',
304 case 'add-tag' : // From Manage->Tags
305 check_ajax_referer( 'add-tag' );
306 if ( !current_user_can( 'manage_categories' ) )
309 if ( '' === trim($_POST['name']) ) {
310 $x = new WP_Ajax_Response( array(
312 'id' => new WP_Error( 'name', __('You did not enter a tag name.') )
317 $tag = wp_insert_term($_POST['name'], 'post_tag', $_POST );
319 if ( is_wp_error($tag) ) {
320 $x = new WP_Ajax_Response( array(
327 if ( !$tag || (!$tag = get_term( $tag['term_id'], 'post_tag' )) )
330 $tag_full_name = $tag->name;
331 $tag_full_name = attribute_escape($tag_full_name);
333 $x = new WP_Ajax_Response( array(
335 'id' => $tag->term_id,
336 'data' => _tag_row( $tag ),
337 'supplemental' => array('name' => $tag_full_name, 'show-link' => sprintf(__( 'Tag <a href="#%s">%s</a> added' ), "tag-$tag->term_id", $tag_full_name))
342 check_ajax_referer( $action );
343 if ( !current_user_can( 'edit_post', $id ) )
345 $search = isset($_POST['s']) ? $_POST['s'] : false;
346 $start = isset($_POST['page']) ? intval($_POST['page']) * 25 - 1: 24;
347 $status = isset($_POST['comment_status']) ? $_POST['comment_status'] : false;
348 $mode = isset($_POST['mode']) ? $_POST['mode'] : 'detail';
350 list($comments, $total) = _wp_get_comment_list( $status, $search, $start, 1 );
354 $x = new WP_Ajax_Response();
355 foreach ( (array) $comments as $comment ) {
356 get_comment( $comment );
358 _wp_comment_row( $comment->comment_ID, $mode, false );
359 $comment_list_item = ob_get_contents();
363 'id' => $comment->comment_ID,
364 'data' => $comment_list_item
370 check_ajax_referer( 'add-meta' );
372 $pid = (int) $_POST['post_id'];
373 if ( isset($_POST['metakeyselect']) || isset($_POST['metakeyinput']) ) {
374 if ( !current_user_can( 'edit_post', $pid ) )
376 if ( '#NONE#' == $_POST['metakeyselect'] && empty($_POST['metakeyinput']) )
379 $now = current_time('timestamp', 1);
380 if ( $pid = wp_insert_post( array(
381 'post_title' => sprintf('Draft created on %s at %s', date(get_option('date_format'), $now), date(get_option('time_format'), $now))
383 if ( is_wp_error( $pid ) ) {
384 $x = new WP_Ajax_Response( array(
390 $mid = add_meta( $pid );
394 } else if ( !$mid = add_meta( $pid ) ) {
398 $meta = get_post_meta_by_id( $mid );
399 $pid = (int) $meta->post_id;
400 $meta = get_object_vars( $meta );
401 $x = new WP_Ajax_Response( array(
404 'data' => _list_meta_row( $meta, $c ),
406 'supplemental' => array('postid' => $pid)
409 $mid = (int) array_pop(array_keys($_POST['meta']));
410 $key = $_POST['meta'][$mid]['key'];
411 $value = $_POST['meta'][$mid]['value'];
412 if ( !$meta = get_post_meta_by_id( $mid ) )
413 die('0'); // if meta doesn't exist
414 if ( !current_user_can( 'edit_post', $meta->post_id ) )
416 if ( !$u = update_meta( $mid, $key, $value ) )
417 die('1'); // We know meta exists; we also know it's unchanged (or DB error, in which case there are bigger problems).
418 $key = stripslashes($key);
419 $value = stripslashes($value);
420 $x = new WP_Ajax_Response( array(
422 'id' => $mid, 'old_id' => $mid,
423 'data' => _list_meta_row( array(
425 'meta_value' => $value,
429 'supplemental' => array('postid' => $meta->post_id)
435 check_ajax_referer( $action );
436 if ( !current_user_can('create_users') )
438 require_once(ABSPATH . WPINC . '/registration.php');
439 if ( !$user_id = add_user() )
441 elseif ( is_wp_error( $user_id ) ) {
442 $x = new WP_Ajax_Response( array(
448 $user_object = new WP_User( $user_id );
450 $x = new WP_Ajax_Response( array(
453 'data' => user_row( $user_object, '', $user_object->roles[0] ),
454 'supplemental' => array(
455 'show-link' => sprintf(__( 'User <a href="#%s">%s</a> added' ), "user-$user_id", $user_object->user_login),
456 'role' => $user_object->roles[0]
461 case 'autosave' : // The name of this action is hardcoded in edit_post()
462 $nonce_age = check_ajax_referer( 'autosave', 'autosavenonce');
463 global $current_user;
465 $_POST['post_status'] = 'draft';
466 $_POST['post_category'] = explode(",", $_POST['catslist']);
467 $_POST['tags_input'] = explode(",", $_POST['tags_input']);
468 if($_POST['post_type'] == 'page' || empty($_POST['post_category']))
469 unset($_POST['post_category']);
471 $do_autosave = (bool) $_POST['autosave'];
475 $message = sprintf( __('Draft Saved at %s.'), date( __('g:i:s a'), current_time( 'timestamp', true ) ) );
477 $supplemental = array();
480 if($_POST['post_ID'] < 0) {
481 $_POST['temp_ID'] = $_POST['post_ID'];
482 if ( $do_autosave ) {
483 $id = wp_write_post();
487 $post_ID = (int) $_POST['post_ID'];
488 $_POST['ID'] = $post_ID;
489 $post = get_post($post_ID);
491 if ( $last = wp_check_post_lock( $post->ID ) ) {
492 $do_autosave = $do_lock = false;
494 $last_user = get_userdata( $last );
495 $last_user_name = $last_user ? $last_user->display_name : __( 'Someone' );
496 $data = new WP_Error( 'locked', sprintf(
497 $_POST['post_type'] == 'page' ? __( 'Autosave disabled: %s is currently editing this page.' ) : __( 'Autosave disabled: %s is currently editing this post.' ),
498 wp_specialchars( $last_user_name )
501 $supplemental['disable_autosave'] = 'disable';
504 if ( 'page' == $post->post_type ) {
505 if ( !current_user_can('edit_page', $post_ID) )
506 die(__('You are not allowed to edit this page.'));
508 if ( !current_user_can('edit_post', $post_ID) )
509 die(__('You are not allowed to edit this post.'));
511 if ( $do_autosave ) {
519 if ( $do_lock && $id && is_numeric($id) )
520 wp_set_post_lock( $id );
522 if ( $nonce_age == 2 ) {
523 $supplemental['replace-autosavenonce'] = wp_create_nonce('autosave');
524 $supplemental['replace-getpermalinknonce'] = wp_create_nonce('getpermalink');
525 $supplemental['replace-samplepermalinknonce'] = wp_create_nonce('samplepermalink');
526 $supplemental['replace-closedpostboxesnonce'] = wp_create_nonce('closedpostboxes');
528 if ( $_POST['post_type'] == 'post' )
529 $supplemental['replace-_wpnonce'] = wp_create_nonce('update-post_' . $id);
530 elseif ( $_POST['post_type'] == 'page' )
531 $supplemental['replace-_wpnonce'] = wp_create_nonce('update-page_' . $id);
535 $x = new WP_Ajax_Response( array(
536 'what' => 'autosave',
538 'data' => $id ? $data : '',
539 'supplemental' => $supplemental
543 case 'autosave-generate-nonces' :
544 check_ajax_referer( 'autosave', 'autosavenonce' );
545 $ID = (int) $_POST['post_ID'];
546 if($_POST['post_type'] == 'post') {
547 if(current_user_can('edit_post', $ID))
548 die(wp_create_nonce('update-post_' . $ID));
550 if($_POST['post_type'] == 'page') {
551 if(current_user_can('edit_page', $ID)) {
552 die(wp_create_nonce('update-page_' . $ID));
557 case 'closed-postboxes' :
558 check_ajax_referer( 'closedpostboxes', 'closedpostboxesnonce' );
559 $closed = isset( $_POST['closed'] )? $_POST['closed'] : '';
560 $closed = explode( ',', $_POST['closed'] );
561 $page = isset( $_POST['page'] )? $_POST['page'] : '';
562 if ( !preg_match( '/^[a-z-]+$/', $page ) ) {
565 if (!is_array($closed)) break;
566 $current_user = wp_get_current_user();
567 update_usermeta($current_user->ID, 'closedpostboxes_'.$page, $closed);
569 case 'get-permalink':
570 check_ajax_referer( 'getpermalink', 'getpermalinknonce' );
571 $post_id = isset($_POST['post_id'])? intval($_POST['post_id']) : 0;
572 die(add_query_arg(array('preview' => 'true'), get_permalink($post_id)));
574 case 'sample-permalink':
575 check_ajax_referer( 'samplepermalink', 'samplepermalinknonce' );
576 $post_id = isset($_POST['post_id'])? intval($_POST['post_id']) : 0;
577 $title = isset($_POST['new_title'])? $_POST['new_title'] : '';
578 $slug = isset($_POST['new_slug'])? $_POST['new_slug'] : '';
579 die(get_sample_permalink_html($post_id, $title, $slug));
582 do_action( 'wp_ajax_' . $_POST['action'] );