10
10
* Executing AJAX process.
14
14
define('DOING_AJAX', true);
15
15
define('WP_ADMIN', true);
17
if ( ! isset( $_REQUEST['action'] ) )
17
20
require_once('../wp-load.php');
19
if ( ! isset( $_REQUEST['action'] ) )
22
22
require_once('./includes/admin.php');
23
23
@header('Content-Type: text/html; charset=' . get_option('blog_charset'));
24
24
send_nosniff_header();
51
51
if ( isset( $_GET['action'] ) ) :
52
52
switch ( $action = $_GET['action'] ) :
55
$list_class = $_GET['list_args']['class'];
56
check_ajax_referer( "fetch-list-$list_class", '_ajax_fetch_list_nonce' );
58
$current_screen = (object) $_GET['list_args']['screen'];
59
//TODO fix this in a better way see #15336
60
$current_screen->is_network = 'false' === $current_screen->is_network ? false : true;
61
$current_screen->is_user = 'false' === $current_screen->is_user ? false : true;
63
define( 'WP_NETWORK_ADMIN', $current_screen->is_network );
64
define( 'WP_USER_ADMIN', $current_screen->is_user );
66
$wp_list_table = _get_list_table( $list_class );
67
if ( ! $wp_list_table )
70
if ( ! $wp_list_table->ajax_user_can() )
73
$wp_list_table->ajax_response();
53
77
case 'ajax-tag-search' :
54
if ( !current_user_can( 'edit_posts' ) )
57
$s = $_GET['q']; // is this slashed already?
59
if ( isset($_GET['tax']) )
60
$taxonomy = sanitize_title($_GET['tax']);
78
if ( isset( $_GET['tax'] ) ) {
79
$taxonomy = sanitize_key( $_GET['tax'] );
80
$tax = get_taxonomy( $taxonomy );
83
if ( ! current_user_can( $tax->cap->assign_terms ) )
89
$s = stripslashes( $_GET['q'] );
64
91
if ( false !== strpos( $s, ',' ) ) {
65
92
$s = explode( ',', $s );
171
198
if ( !$total || !$per_page || !$page || !$url )
172
199
die( (string) time() );
174
if ( --$total < 0 ) // Take the total from POST and decrement it (since we just deleted one)
177
if ( 0 != $total % $per_page && 1 != mt_rand( 1, $per_page ) ) // Only do the expensive stuff on a page-break, and about 1 other time per page
178
die( (string) time() );
181
$status = 'total_comments'; // What type of comment count are we looking for?
182
$parsed = parse_url( $url );
183
if ( isset( $parsed['query'] ) ) {
184
parse_str( $parsed['query'], $query_vars );
185
if ( !empty( $query_vars['comment_status'] ) )
186
$status = $query_vars['comment_status'];
187
if ( !empty( $query_vars['p'] ) )
188
$post_id = (int) $query_vars['p'];
205
// Only do the expensive stuff on a page-break, and about 1 other time per page
206
if ( 0 == $total % $per_page || 1 == mt_rand( 1, $per_page ) ) {
208
$status = 'total_comments'; // What type of comment count are we looking for?
209
$parsed = parse_url( $url );
210
if ( isset( $parsed['query'] ) ) {
211
parse_str( $parsed['query'], $query_vars );
212
if ( !empty( $query_vars['comment_status'] ) )
213
$status = $query_vars['comment_status'];
214
if ( !empty( $query_vars['p'] ) )
215
$post_id = (int) $query_vars['p'];
218
$comment_count = wp_count_comments($post_id);
220
if ( isset( $comment_count->$status ) ) // We're looking for a known type of comment count
221
$total = $comment_count->$status;
222
// else use the decremented value from above
191
$comment_count = wp_count_comments($post_id);
192
225
$time = time(); // The time since the last comment count
194
if ( isset( $comment_count->$status ) ) // We're looking for a known type of comment count
195
$total = $comment_count->$status;
196
// else use the decremented value from above
198
$page_links = paginate_links( array(
199
'base' => add_query_arg( 'apage', '%#%', $url ),
201
'prev_text' => __('«'),
202
'next_text' => __('»'),
203
'total' => ceil($total / $per_page),
206
227
$x = new WP_Ajax_Response( array(
207
228
'what' => 'comment',
208
229
'id' => $comment_id, // here for completeness - not used
209
230
'supplemental' => array(
210
'pageLinks' => $page_links,
231
'total_items_i18n' => sprintf( _n( '1 item', '%s items', $total ), number_format_i18n( $total ) ),
232
'total_pages' => ceil( $total / $per_page ),
233
'total_pages_i18n' => number_format_i18n( ceil( $total / $per_page ) ),
211
234
'total' => $total,
352
case 'delete-link-cat' :
353
check_ajax_referer( "delete-link-category_$id" );
354
if ( !current_user_can( 'manage_categories' ) )
357
$cat = get_term( $id, 'link_category' );
358
if ( !$cat || is_wp_error( $cat ) )
361
$cat_name = get_term_field('name', $id, 'link_category');
363
$default = get_option('default_link_category');
365
// Don't delete the default cats.
366
if ( $id == $default ) {
367
$x = new WP_AJAX_Response( array(
368
'what' => 'link-cat',
370
'data' => new WP_Error( 'default-link-cat', sprintf(__("Can’t delete the <strong>%s</strong> category: this is the default one"), $cat_name) )
375
$r = wp_delete_term($id, 'link_category', array('default' => $default));
378
if ( is_wp_error($r) ) {
379
$x = new WP_AJAX_Response( array(
380
'what' => 'link-cat',
388
380
case 'delete-link' :
389
381
check_ajax_referer( "delete-bookmark_$id" );
390
382
if ( !current_user_can( 'manage_links' ) )
518
case 'add-link-cat' : // From Blogroll -> Categories
519
check_ajax_referer( 'add-link-category' );
520
if ( !current_user_can( 'manage_categories' ) )
523
if ( '' === trim($_POST['name']) ) {
524
$x = new WP_Ajax_Response( array(
525
'what' => 'link-cat',
526
'id' => new WP_Error( 'name', __('You did not enter a category name.') )
531
$r = wp_insert_term($_POST['name'], 'link_category', $_POST );
532
if ( is_wp_error( $r ) ) {
533
$x = new WP_AJAX_Response( array(
534
'what' => 'link-cat',
540
extract($r, EXTR_SKIP);
542
if ( !$link_cat = link_cat_row( $term_id ) )
545
$x = new WP_Ajax_Response( array(
546
'what' => 'link-cat',
553
case 'add-tag' : // From Manage->Tags
554
511
check_ajax_referer( 'add-tag' );
555
512
$post_type = !empty($_POST['post_type']) ? $_POST['post_type'] : 'post';
556
513
$taxonomy = !empty($_POST['taxonomy']) ? $_POST['taxonomy'] : 'post_tag';
557
514
$tax = get_taxonomy($taxonomy);
559
$x = new WP_Ajax_Response();
561
516
if ( !current_user_can( $tax->cap->edit_terms ) )
519
$x = new WP_Ajax_Response();
564
521
$tag = wp_insert_term($_POST['tag-name'], $taxonomy, $_POST );
566
523
if ( !$tag || is_wp_error($tag) || (!$tag = get_term( $tag['term_id'], $taxonomy )) ) {
567
$message = __('An error has occured. Please reload the page and try again.');
524
$message = __('An error has occurred. Please reload the page and try again.');
568
525
if ( is_wp_error($tag) && $tag->get_error_message() )
569
526
$message = $tag->get_error_message();
578
if ( isset($_POST['screen']) )
579
set_current_screen($_POST['screen']);
535
set_current_screen( $_POST['screen'] );
537
$wp_list_table = _get_list_table('WP_Terms_List_Table');
582
$tag_full_name = false;
583
$tag_full_name = $tag->name;
584
540
if ( is_taxonomy_hierarchical($taxonomy) ) {
586
while ( $_tag->parent ) {
587
$_tag = get_term( $_tag->parent, $taxonomy );
588
$tag_full_name = $_tag->name . ' — ' . $tag_full_name;
591
$noparents = _tag_row( $tag, $level, $taxonomy );
541
$level = count( get_ancestors( $tag->term_id, $taxonomy ) );
543
$wp_list_table->single_row( $tag, $level );
544
$noparents = ob_get_clean();
593
$tag->name = $tag_full_name;
594
$parents = _tag_row( $tag, 0, $taxonomy);
548
$wp_list_table->single_row( $tag );
549
$parents = ob_get_clean();
597
552
'what' => 'taxonomy',
601
556
'what' => 'term',
602
557
'position' => $level,
603
'supplemental' => get_term( $tag->term_id, $taxonomy, ARRAY_A ) //Refetch as $tag has been contaminated by the full name.
558
'supplemental' => (array) $tag
607
562
case 'get-tagcloud' :
608
if ( !current_user_can( 'edit_posts' ) )
611
if ( isset($_POST['tax']) )
612
$taxonomy = sanitize_title($_POST['tax']);
563
if ( isset( $_POST['tax'] ) ) {
564
$taxonomy = sanitize_key( $_POST['tax'] );
565
$tax = get_taxonomy( $taxonomy );
568
if ( ! current_user_can( $tax->cap->assign_terms ) )
616
574
$tags = get_terms( $taxonomy, array( 'number' => 45, 'orderby' => 'count', 'order' => 'DESC' ) );
618
if ( empty( $tags ) ) {
619
$tax = get_taxonomy( $taxonomy );
576
if ( empty( $tags ) )
620
577
die( isset( $tax->no_tagcloud ) ? $tax->no_tagcloud : __('No tags found!') );
623
if ( is_wp_error($tags) )
624
die($tags->get_error_message());
579
if ( is_wp_error( $tags ) )
580
die( $tags->get_error_message() );
626
582
foreach ( $tags as $key => $tag ) {
627
583
$tags[ $key ]->link = '#';
642
check_ajax_referer( $action );
643
if ( !current_user_can( 'edit_posts' ) )
645
$search = isset($_POST['s']) ? $_POST['s'] : false;
646
$status = isset($_POST['comment_status']) ? $_POST['comment_status'] : 'all';
647
$per_page = isset($_POST['per_page']) ? (int) $_POST['per_page'] + 8 : 28;
648
$start = isset($_POST['page']) ? ( intval($_POST['page']) * $per_page ) -1 : $per_page - 1;
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 );
657
if ( get_option('show_avatars') )
658
add_filter( 'comment_author', 'floated_admin_avatar' );
662
$x = new WP_Ajax_Response();
663
foreach ( (array) $comments as $comment ) {
664
get_comment( $comment );
666
_wp_comment_row( $comment->comment_ID, $mode, $status, true, true );
667
$comment_list_item = ob_get_contents();
671
'id' => $comment->comment_ID,
672
'data' => $comment_list_item
677
597
case 'get-comments' :
678
598
check_ajax_referer( $action );
680
$post_ID = (int) $_POST['post_ID'];
681
if ( !current_user_can( 'edit_post', $post_ID ) )
600
set_current_screen( 'edit-comments' );
602
$wp_list_table = _get_list_table('WP_Post_Comments_List_Table');
604
if ( !current_user_can( 'edit_post', $post_id ) )
684
$start = isset($_POST['start']) ? intval($_POST['start']) : 0;
685
$num = isset($_POST['num']) ? intval($_POST['num']) : 10;
687
list($comments, $total) = _wp_get_comment_list( false, false, $start, $num, $post_ID );
607
$wp_list_table->prepare_items();
609
if ( !$wp_list_table->has_items() )
692
612
$comment_list_item = '';
693
613
$x = new WP_Ajax_Response();
694
foreach ( (array) $comments as $comment ) {
614
foreach ( $wp_list_table->items as $comment ) {
695
615
get_comment( $comment );
697
_wp_comment_row( $comment->comment_ID, 'single', false, false );
617
$wp_list_table->single_row( $comment );
698
618
$comment_list_item .= ob_get_contents();
744
666
$comment = get_comment($comment_id);
745
667
if ( ! $comment ) die('1');
747
$modes = array( 'single', 'detail', 'dashboard' );
748
$mode = isset($_POST['mode']) && in_array( $_POST['mode'], $modes ) ? $_POST['mode'] : 'detail';
749
669
$position = ( isset($_POST['position']) && (int) $_POST['position']) ? (int) $_POST['position'] : '-1';
750
$checkbox = ( isset($_POST['checkbox']) && true == $_POST['checkbox'] ) ? 1 : 0;
752
if ( get_option('show_avatars') && 'single' != $mode )
753
add_filter( 'comment_author', 'floated_admin_avatar' );
755
671
$x = new WP_Ajax_Response();
758
if ( 'dashboard' == $mode ) {
674
if ( 'dashboard' == $_REQUEST['mode'] ) {
759
675
require_once( ABSPATH . 'wp-admin/includes/dashboard.php' );
760
_wp_dashboard_recent_comments_row( $comment, false );
676
_wp_dashboard_recent_comments_row( $comment );
762
_wp_comment_row( $comment->comment_ID, $mode, false, $checkbox );
678
if ( 'single' == $_REQUEST['mode'] ) {
679
$wp_list_table = _get_list_table('WP_Post_Comments_List_Table');
681
$wp_list_table = _get_list_table('WP_Comments_List_Table');
683
$wp_list_table->single_row( $comment );
764
685
$comment_list_item = ob_get_contents();
787
710
$_POST['comment_status'] = $_POST['status'];
790
$mode = ( isset($_POST['mode']) && 'single' == $_POST['mode'] ) ? 'single' : 'detail';
791
713
$position = ( isset($_POST['position']) && (int) $_POST['position']) ? (int) $_POST['position'] : '-1';
714
$comments_status = isset($_POST['comments_listing']) ? $_POST['comments_listing'] : '';
792
716
$checkbox = ( isset($_POST['checkbox']) && true == $_POST['checkbox'] ) ? 1 : 0;
793
$comments_listing = isset($_POST['comments_listing']) ? $_POST['comments_listing'] : '';
795
if ( get_option('show_avatars') && 'single' != $mode )
796
add_filter( 'comment_author', 'floated_admin_avatar' );
798
$x = new WP_Ajax_Response();
717
$wp_list_table = _get_list_table( $checkbox ? 'WP_Comments_List_Table' : 'WP_Post_Comments_List_Table' );
801
_wp_comment_row( $comment_id, $mode, $comments_listing, $checkbox );
720
$wp_list_table->single_row( get_comment( $comment_id ) );
802
721
$comment_list_item = ob_get_contents();
724
$x = new WP_Ajax_Response();
806
727
'what' => 'edit_comment',
807
728
'id' => $comment->comment_ID,
820
741
require_once ABSPATH . 'wp-admin/includes/nav-menu.php';
822
$item_ids = wp_save_nav_menu_items( 0, $_POST['menu-item'] );
743
// For performance reasons, we omit some object properties from the checklist.
744
// The following is a hacky way to restore them when adding non-custom items.
746
$menu_items_data = array();
747
foreach ( (array) $_POST['menu-item'] as $menu_item_data ) {
749
! empty( $menu_item_data['menu-item-type'] ) &&
750
'custom' != $menu_item_data['menu-item-type'] &&
751
! empty( $menu_item_data['menu-item-object-id'] )
753
switch( $menu_item_data['menu-item-type'] ) {
755
$_object = get_post( $menu_item_data['menu-item-object-id'] );
759
$_object = get_term( $menu_item_data['menu-item-object-id'], $menu_item_data['menu-item-object'] );
763
$_menu_items = array_map( 'wp_setup_nav_menu_item', array( $_object ) );
764
$_menu_item = array_shift( $_menu_items );
766
// Restore the missing menu item properties
767
$menu_item_data['menu-item-description'] = $_menu_item->description;
770
$menu_items_data[] = $menu_item_data;
773
$item_ids = wp_save_nav_menu_items( 0, $menu_items_data );
823
774
if ( is_wp_error( $item_ids ) )
942
892
$user_object = new WP_User( $user_id );
894
$wp_list_table = _get_list_table('WP_Users_List_Table');
944
896
$x = new WP_Ajax_Response( array(
945
897
'what' => 'user',
946
898
'id' => $user_id,
947
'data' => user_row( $user_object, '', $user_object->roles[0] ),
899
'data' => $wp_list_table->single_row( $user_object, '', $user_object->roles[0] ),
948
900
'supplemental' => array(
949
901
'show-link' => sprintf(__( 'User <a href="#%s">%s</a> added' ), "user-$user_id", $user_object->user_login),
950
902
'role' => $user_object->roles[0]
1093
case 'wp-link-ajax':
1094
require_once ABSPATH . 'wp-admin/includes/internal-linking.php';
1096
check_ajax_referer( 'internal-linking', '_ajax_linking_nonce' );
1100
if ( isset( $_POST['search'] ) )
1101
$args['s'] = stripslashes( $_POST['search'] );
1102
$args['pagenum'] = ! empty( $_POST['page'] ) ? absint( $_POST['page'] ) : 1;
1104
$results = wp_link_query( $args );
1106
if ( ! isset( $results ) )
1109
echo json_encode( $results );
1141
1114
case 'menu-locations-save':
1142
1115
if ( ! current_user_can( 'edit_theme_options' ) )
1231
1203
// update the post
1234
if ( in_array( $_POST['post_type'], get_post_types( array( 'show_ui' => true ) ) ) ) {
1236
$post[] = get_post($_POST['post_ID']);
1237
if ( is_post_type_hierarchical( $_POST['post_type'] ) ) {
1240
$mode = $_POST['post_view'];
1206
$wp_list_table = _get_list_table('WP_Posts_List_Table');
1208
$mode = $_POST['post_view'];
1209
$wp_list_table->display_rows( array( get_post( $_POST['post_ID'] ) ) );
1247
1213
case 'inline-save-tax':
1248
1214
check_ajax_referer( 'taxinlineeditnonce', '_inline_edit' );
1250
$taxonomy = !empty($_POST['taxonomy']) ? $_POST['taxonomy'] : false;
1252
die( __('Cheatin’ uh?') );
1253
$tax = get_taxonomy($taxonomy);
1216
$taxonomy = sanitize_key( $_POST['taxonomy'] );
1217
$tax = get_taxonomy( $taxonomy );
1255
1221
if ( ! current_user_can( $tax->cap->edit_terms ) )
1256
die( __('Cheatin’ uh?') );
1224
set_current_screen( 'edit-' . $taxonomy );
1226
$wp_list_table = _get_list_table('WP_Terms_List_Table');
1258
1228
if ( ! isset($_POST['tax_ID']) || ! ( $id = (int) $_POST['tax_ID'] ) )
1261
switch ($_POST['tax_type']) {
1263
$updated = wp_update_term($id, 'link_category', $_POST);
1265
if ( $updated && !is_wp_error($updated) )
1266
echo link_cat_row($updated['term_id']);
1268
die( __('Category not updated.') );
1272
$taxonomy = !empty($_POST['taxonomy']) ? $_POST['taxonomy'] : 'post_tag';
1274
$tag = get_term( $id, $taxonomy );
1275
$_POST['description'] = $tag->description;
1277
$updated = wp_update_term($id, $taxonomy, $_POST);
1278
if ( $updated && !is_wp_error($updated) ) {
1279
$tag = get_term( $updated['term_id'], $taxonomy );
1280
if ( !$tag || is_wp_error( $tag ) ) {
1281
if ( is_wp_error($tag) && $tag->get_error_message() )
1282
die( $tag->get_error_message() );
1283
die( __('Item not updated.') );
1286
set_current_screen( 'edit-' . $taxonomy );
1288
echo _tag_row($tag, 0, $taxonomy);
1290
if ( is_wp_error($updated) && $updated->get_error_message() )
1291
die( $updated->get_error_message() );
1292
die( __('Item not updated.') );
1231
$tag = get_term( $id, $taxonomy );
1232
$_POST['description'] = $tag->description;
1234
$updated = wp_update_term($id, $taxonomy, $_POST);
1235
if ( $updated && !is_wp_error($updated) ) {
1236
$tag = get_term( $updated['term_id'], $taxonomy );
1237
if ( !$tag || is_wp_error( $tag ) ) {
1238
if ( is_wp_error($tag) && $tag->get_error_message() )
1239
die( $tag->get_error_message() );
1240
die( __('Item not updated.') );
1243
echo $wp_list_table->single_row( $tag );
1245
if ( is_wp_error($updated) && $updated->get_error_message() )
1246
die( $updated->get_error_message() );
1247
die( __('Item not updated.') );
1315
1267
$searchand = $search = '';
1316
1268
foreach ( (array) $search_terms as $term ) {
1317
$term = addslashes_gpc($term);
1269
$term = esc_sql( like_escape( $term ) );
1318
1270
$search .= "{$searchand}(($wpdb->posts.post_title LIKE '%{$term}%') OR ($wpdb->posts.post_content LIKE '%{$term}%'))";
1319
1271
$searchand = ' AND ';
1321
$term = $wpdb->escape($s);
1273
$term = esc_sql( like_escape( $s ) );
1322
1274
if ( count($search_terms) > 1 && $search_terms[0] != $s )
1323
1275
$search .= " OR ($wpdb->posts.post_title LIKE '%{$term}%') OR ($wpdb->posts.post_content LIKE '%{$term}%')";
1371
case 'lj-importer' :
1372
check_ajax_referer( 'lj-api-import' );
1373
if ( !current_user_can( 'publish_posts' ) )
1375
if ( empty( $_POST['step'] ) )
1377
define('WP_IMPORTING', true);
1378
include( ABSPATH . 'wp-admin/import/livejournal.php' );
1379
$result = $lj_api_import->{ 'step' . ( (int) $_POST['step'] ) }();
1380
if ( is_wp_error( $result ) )
1381
echo $result->get_error_message();
1384
1323
case 'widgets-order' :
1385
1324
check_ajax_referer( 'save-sidebar-widgets', 'savewidgets' );
1518
1457
die( _wp_post_thumbnail_html() );
1521
if ( $thumbnail_id && get_post( $thumbnail_id ) ) {
1522
$thumbnail_html = wp_get_attachment_image( $thumbnail_id, 'thumbnail' );
1523
if ( !empty( $thumbnail_html ) ) {
1524
update_post_meta( $post_ID, '_thumbnail_id', $thumbnail_id );
1525
die( _wp_post_thumbnail_html( $thumbnail_id ) );
1460
if ( set_post_thumbnail( $post_ID, $thumbnail_id ) )
1461
die( _wp_post_thumbnail_html( $thumbnail_id ) );
1464
case 'date_format' :
1465
die( date_i18n( sanitize_option( 'date_format', $_POST['date'] ) ) );
1467
case 'time_format' :
1468
die( date_i18n( sanitize_option( 'time_format', $_POST['date'] ) ) );
1531
1471
do_action( 'wp_ajax_' . $_POST['action'] );