~katiekitty/+junk/wordpress-byet

« back to all changes in this revision

Viewing changes to wp-admin/admin-ajax.php

  • Committer: kserver
  • Date: 2010-05-15 01:16:36 UTC
  • Revision ID: kserver@kserver-desktop-20100515011636-mnr1j7t637suptdq
Wordpress 2.9.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
                        $x->send();
38
38
        }
39
39
 
40
 
        if ( !empty( $_POST['action']) )
41
 
                do_action( 'wp_ajax_nopriv_' . $_POST['action'] );
 
40
        if ( !empty( $_REQUEST['action']) )
 
41
                do_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] );
42
42
 
43
43
        die('-1');
44
44
}
91
91
                        echo $test_str;
92
92
                        die;
93
93
                 } elseif ( 2 == $_GET['test'] ) {
 
94
                        if ( !isset($_SERVER['HTTP_ACCEPT_ENCODING']) )
 
95
                                die('-1');
94
96
                        if ( false !== strpos( strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'deflate') && function_exists('gzdeflate') && ! $force_gzip ) {
95
97
                                header('Content-Encoding: deflate');
96
98
                                $out = gzdeflate( $test_str, 1 );
111
113
 
112
114
        die('0');
113
115
        break;
 
116
case 'imgedit-preview' :
 
117
        $post_id = intval($_GET['postid']);
 
118
        if ( empty($post_id) || !current_user_can('edit_post', $post_id) )
 
119
                die('-1');
 
120
 
 
121
        check_ajax_referer( "image_editor-$post_id" );
 
122
 
 
123
        include_once( ABSPATH . 'wp-admin/includes/image-edit.php' );
 
124
        if ( !stream_preview_image($post_id) )
 
125
                die('-1');
 
126
 
 
127
        die();
 
128
        break;
 
129
case 'oembed-cache' :
 
130
        $return = ( $wp_embed->cache_oembed( $_GET['post'] ) ) ? '1' : '0';
 
131
        die( $return );
 
132
        break;
114
133
default :
115
134
        do_action( 'wp_ajax_' . $_GET['action'] );
116
135
        die('0');
143
162
        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
144
163
                die( (string) time() );
145
164
 
 
165
        $post_id = 0;
146
166
        $status = 'total_comments'; // What type of comment count are we looking for?
147
167
        $parsed = parse_url( $url );
148
168
        if ( isset( $parsed['query'] ) ) {
149
169
                parse_str( $parsed['query'], $query_vars );
150
170
                if ( !empty( $query_vars['comment_status'] ) )
151
171
                        $status = $query_vars['comment_status'];
 
172
                if ( !empty( $query_vars['p'] ) )
 
173
                        $post_id = (int) $query_vars['p'];
152
174
        }
153
175
 
154
 
        $comment_count = wp_count_comments();
 
176
        $comment_count = wp_count_comments($post_id);
155
177
        $time = time(); // The time since the last comment count
156
178
 
157
179
        if ( isset( $comment_count->$status ) ) // We're looking for a known type of comment count
181
203
$id = isset($_POST['id'])? (int) $_POST['id'] : 0;
182
204
switch ( $action = $_POST['action'] ) :
183
205
case 'delete-comment' : // On success, die with time() instead of 1
 
206
        if ( !$comment = get_comment( $id ) )
 
207
                die( (string) time() );
 
208
        if ( !current_user_can( 'edit_post', $comment->comment_post_ID ) )
 
209
                die('-1');
 
210
 
184
211
        check_ajax_referer( "delete-comment_$id" );
185
 
        if ( !$comment = get_comment( $id ) )
186
 
                die( (string) time() );
187
 
        if ( !current_user_can( 'edit_post', $comment->comment_post_ID ) )
188
 
                die('-1');
 
212
        $status = wp_get_comment_status( $comment->comment_ID );
189
213
 
190
 
        if ( isset($_POST['spam']) && 1 == $_POST['spam'] ) {
191
 
                if ( 'spam' == wp_get_comment_status( $comment->comment_ID ) )
192
 
                        die( (string) time() );
193
 
                $r = wp_set_comment_status( $comment->comment_ID, 'spam' );
194
 
        } else {
 
214
        if ( isset($_POST['trash']) && 1 == $_POST['trash'] ) {
 
215
                if ( 'trash' == $status )
 
216
                        die( (string) time() );
 
217
                $r = wp_trash_comment( $comment->comment_ID );
 
218
        } elseif ( isset($_POST['untrash']) && 1 == $_POST['untrash'] ) {
 
219
                if ( 'trash' != $status )
 
220
                        die( (string) time() );
 
221
                $r = wp_untrash_comment( $comment->comment_ID );
 
222
        } elseif ( isset($_POST['spam']) && 1 == $_POST['spam'] ) {
 
223
                if ( 'spam' == $status )
 
224
                        die( (string) time() );
 
225
                $r = wp_spam_comment( $comment->comment_ID );
 
226
        } elseif ( isset($_POST['unspam']) && 1 == $_POST['unspam'] ) {
 
227
                if ( 'spam' != $status )
 
228
                        die( (string) time() );
 
229
                $r = wp_unspam_comment( $comment->comment_ID );
 
230
        } elseif ( isset($_POST['delete']) && 1 == $_POST['delete'] ) {
195
231
                $r = wp_delete_comment( $comment->comment_ID );
 
232
        } else {
 
233
                die('-1');
196
234
        }
 
235
 
197
236
        if ( $r ) // Decide if we need to send back '1' or a more complicated response including page links and comment counts
198
237
                _wp_ajax_delete_comment_response( $comment->comment_ID );
199
238
        die( '0' );
213
252
                die('0');
214
253
        break;
215
254
case 'delete-tag' :
216
 
        check_ajax_referer( "delete-tag_$id" );
 
255
        $tag_id = (int) $_POST['tag_ID'];
 
256
        check_ajax_referer( "delete-tag_$tag_id" );
217
257
        if ( !current_user_can( 'manage_categories' ) )
218
258
                die('-1');
219
259
 
220
 
        if ( !empty($_POST['taxonomy']) )
221
 
                $taxonomy = $_POST['taxonomy'];
222
 
        else
223
 
                $taxonomy = 'post_tag';
 
260
        $taxonomy = !empty($_POST['taxonomy']) ? $_POST['taxonomy'] : 'post_tag';
224
261
 
225
 
        $tag = get_term( $id, $taxonomy );
 
262
        $tag = get_term( $tag_id, $taxonomy );
226
263
        if ( !$tag || is_wp_error( $tag ) )
227
264
                die('1');
228
265
 
229
 
        if ( wp_delete_term($id, $taxonomy))
 
266
        if ( wp_delete_term($tag_id, $taxonomy))
230
267
                die('1');
231
268
        else
232
269
                die('0');
305
342
        else
306
343
                die('0');
307
344
        break;
 
345
case 'trash-post' :
 
346
case 'untrash-post' :
 
347
        check_ajax_referer( "{$action}_$id" );
 
348
        if ( !current_user_can( 'delete_post', $id ) )
 
349
                die('-1');
 
350
 
 
351
        if ( !get_post( $id ) )
 
352
                die('1');
 
353
 
 
354
        if ( 'trash-post' == $action )
 
355
                $done = wp_trash_post( $id );
 
356
        else
 
357
                $done = wp_untrash_post( $id );
 
358
 
 
359
        if ( $done )
 
360
                die('1');
 
361
 
 
362
        die('0');
 
363
        break;
308
364
case 'delete-page' :
309
365
        check_ajax_referer( "{$action}_$id" );
310
366
        if ( !current_user_can( 'delete_page', $id ) )
328
384
                $x->send();
329
385
        }
330
386
 
331
 
        if ( !current_user_can( 'edit_post', $comment->comment_post_ID ) )
332
 
                die('-1');
333
 
        if ( !current_user_can( 'moderate_comments' ) )
 
387
        if ( !current_user_can( 'edit_post', $comment->comment_post_ID ) && !current_user_can( 'moderate_comments' ) )
334
388
                die('-1');
335
389
 
336
390
        $current = wp_get_comment_status( $comment->comment_ID );
337
391
        if ( $_POST['new'] == $current )
338
392
                die( (string) time() );
339
393
 
340
 
        $r = 0;
341
 
        if ( in_array( $current, array( 'unapproved', 'spam' ) ) ) {
342
 
                check_ajax_referer( "approve-comment_$id" );
 
394
        check_ajax_referer( "approve-comment_$id" );
 
395
        if ( in_array( $current, array( 'unapproved', 'spam' ) ) )
343
396
                $result = wp_set_comment_status( $comment->comment_ID, 'approve', true );
344
 
        } else {
345
 
                check_ajax_referer( "unapprove-comment_$id" );
 
397
        else
346
398
                $result = wp_set_comment_status( $comment->comment_ID, 'hold', true );
347
 
        }
 
399
 
348
400
        if ( is_wp_error($result) ) {
349
401
                $x = new WP_Ajax_Response( array(
350
402
                        'what' => 'comment',
366
418
                $parent = 0;
367
419
        $post_category = isset($_POST['post_category'])? (array) $_POST['post_category'] : array();
368
420
        $checked_categories = array_map( 'absint', (array) $post_category );
369
 
        $popular_ids = isset( $_POST['popular_ids'] ) ?
370
 
                        array_map( 'absint', explode( ',', $_POST['popular_ids'] ) ) :
371
 
                        false;
 
421
        $popular_ids = wp_popular_terms_checklist('category', 0, 10, false);
372
422
 
373
 
        $x = new WP_Ajax_Response();
374
423
        foreach ( $names as $cat_name ) {
375
424
                $cat_name = trim($cat_name);
376
425
                $category_nicename = sanitize_title($cat_name);
385
434
                        wp_category_checklist( 0, $cat_id, $checked_categories, $popular_ids );
386
435
                $data = ob_get_contents();
387
436
                ob_end_clean();
388
 
                $x->add( array(
 
437
                $add = array(
389
438
                        'what' => 'category',
390
439
                        'id' => $cat_id,
391
 
                        'data' => $data,
 
440
                        'data' => str_replace( array("\n", "\t"), '', $data),
392
441
                        'position' => -1
393
 
                ) );
 
442
                );
394
443
        }
395
444
        if ( $parent ) { // Foncy - replace the parent and all its children
396
445
                $parent = get_category( $parent );
 
446
                $term_id = $parent->term_id;
 
447
 
 
448
                while ( $parent->parent ) { // get the top parent
 
449
                        $parent = &get_category( $parent->parent );
 
450
                        if ( is_wp_error( $parent ) )
 
451
                                break;
 
452
                        $term_id = $parent->term_id;
 
453
                }
 
454
 
397
455
                ob_start();
398
 
                        dropdown_categories( 0, $parent );
 
456
                        wp_category_checklist( 0, $term_id, $checked_categories, $popular_ids, null, false );
399
457
                $data = ob_get_contents();
400
458
                ob_end_clean();
401
 
                $x->add( array(
 
459
                $add = array(
402
460
                        'what' => 'category',
403
 
                        'id' => $parent->term_id,
404
 
                        'old_id' => $parent->term_id,
405
 
                        'data' => $data,
 
461
                        'id' => $term_id,
 
462
                        'data' => str_replace( array("\n", "\t"), '', $data),
406
463
                        'position' => -1
407
 
                ) );
408
 
 
 
464
                );
409
465
        }
 
466
 
 
467
        ob_start();
 
468
                wp_dropdown_categories( array( 'hide_empty' => 0, 'name' => 'newcat_parent', 'orderby' => 'name', 'hierarchical' => 1, 'show_option_none' => __('Parent category') ) );
 
469
        $sup = ob_get_contents();
 
470
        ob_end_clean();
 
471
        $add['supplemental'] = array( 'newcat_parent' => $sup );
 
472
 
 
473
        $x = new WP_Ajax_Response( $add );
410
474
        $x->send();
411
475
        break;
412
476
case 'add-link-category' : // On the Fly
527
591
        if ( !current_user_can( 'manage_categories' ) )
528
592
                die('-1');
529
593
 
530
 
        if ( '' === trim($_POST['name']) ) {
531
 
                $x = new WP_Ajax_Response( array(
532
 
                        'what' => 'tag',
533
 
                        'id' => new WP_Error( 'name', __('You did not enter a tag name.') )
534
 
                ) );
535
 
                $x->send();
536
 
        }
537
 
 
538
 
        if ( !empty($_POST['taxonomy']) )
539
 
                $taxonomy = $_POST['taxonomy'];
540
 
        else
541
 
                $taxonomy = 'post_tag';
542
 
 
543
 
        $tag = wp_insert_term($_POST['name'], $taxonomy, $_POST );
544
 
 
545
 
        if ( is_wp_error($tag) ) {
546
 
                $x = new WP_Ajax_Response( array(
547
 
                        'what' => 'tag',
548
 
                        'id' => $tag
549
 
                ) );
550
 
                $x->send();
551
 
        }
552
 
 
553
 
        if ( !$tag || (!$tag = get_term( $tag['term_id'], $taxonomy )) )
554
 
                die('0');
555
 
 
556
 
        $tag_full_name = $tag->name;
557
 
        $tag_full_name = esc_attr($tag_full_name);
558
 
 
559
 
        $x = new WP_Ajax_Response( array(
560
 
                'what' => 'tag',
561
 
                'id' => $tag->term_id,
562
 
                'position' => '-1',
563
 
                'data' => _tag_row( $tag, '', $taxonomy ),
564
 
                'supplemental' => array('name' => $tag_full_name, 'show-link' => sprintf(__( 'Tag <a href="#%s">%s</a> added' ), "tag-$tag->term_id", $tag_full_name))
565
 
        ) );
566
 
        $x->send();
 
594
        $taxonomy = !empty($_POST['taxonomy']) ? $_POST['taxonomy'] : 'post_tag';
 
595
        $tag = wp_insert_term($_POST['tag-name'], $taxonomy, $_POST );
 
596
 
 
597
        if ( !$tag || is_wp_error($tag) || (!$tag = get_term( $tag['term_id'], $taxonomy )) ) {
 
598
                echo '<div class="error"><p>' . __('An error has occured. Please reload the page and try again.') . '</p></div>';
 
599
                exit;
 
600
        }
 
601
 
 
602
        echo _tag_row( $tag, '', $taxonomy );
 
603
        exit;
567
604
        break;
568
605
case 'get-tagcloud' :
569
606
        if ( !current_user_can( 'edit_posts' ) )
599
636
        break;
600
637
case 'add-comment' :
601
638
        check_ajax_referer( $action );
602
 
        if ( !current_user_can( 'edit_post', $id ) )
 
639
        if ( !current_user_can( 'edit_posts' ) )
603
640
                die('-1');
604
641
        $search = isset($_POST['s']) ? $_POST['s'] : false;
605
642
        $status = isset($_POST['comment_status']) ? $_POST['comment_status'] : 'all';
674
711
 
675
712
        if ( empty($status) )
676
713
                die('1');
677
 
        elseif ( in_array($status, array('draft', 'pending') ) )
 
714
        elseif ( in_array($status, array('draft', 'pending', 'trash') ) )
678
715
                die( __('Error: you are replying to a comment on a draft post.') );
679
716
 
680
717
        $user = wp_get_current_user();
885
922
        $message = sprintf( __('Draft Saved at %s.'), date_i18n( $draft_saved_date_format ) );
886
923
 
887
924
        $supplemental = array();
 
925
        if ( isset($login_grace_period) )
 
926
                $supplemental['session_expired'] = add_query_arg( 'interim-login', 1, wp_login_url() );
888
927
 
889
928
        $id = $revision_id = 0;
890
929
        if($_POST['post_ID'] < 0) {
964
1003
case 'autosave-generate-nonces' :
965
1004
        check_ajax_referer( 'autosave', 'autosavenonce' );
966
1005
        $ID = (int) $_POST['post_ID'];
967
 
        if($_POST['post_type'] == 'post') {
968
 
                if(current_user_can('edit_post', $ID))
969
 
                        die(wp_create_nonce('update-post_' . $ID));
970
 
        }
971
 
        if($_POST['post_type'] == 'page') {
972
 
                if(current_user_can('edit_page', $ID)) {
973
 
                        die(wp_create_nonce('update-page_' . $ID));
974
 
                }
975
 
        }
 
1006
        $post_type = ( 'page' == $_POST['post_type'] ) ? 'page' : 'post';
 
1007
        if ( current_user_can( "edit_{$post_type}", $ID ) )
 
1008
                die( json_encode( array( 'updateNonce' => wp_create_nonce( "update-{$post_type}_{$ID}" ), 'deleteURL' => str_replace( '&amp;', '&', wp_nonce_url( admin_url( $post_type . '.php?action=trash&post=' . $ID ), "trash-{$post_type}_{$ID}" ) ) ) ) );
 
1009
        do_action('autosave_generate_nonces');
976
1010
        die('0');
977
1011
break;
978
1012
case 'closed-postboxes' :
993
1027
                update_usermeta($user->ID, 'closedpostboxes_'.$page, $closed);
994
1028
 
995
1029
        if ( is_array($hidden) ) {
996
 
                $hidden = array_diff( $hidden, array('submitdiv', 'pagesubmitdiv', 'linksubmitdiv') ); // postboxes that are always shown
 
1030
                $hidden = array_diff( $hidden, array('submitdiv', 'linksubmitdiv') ); // postboxes that are always shown
997
1031
                update_usermeta($user->ID, 'meta-box-hidden_'.$page, $hidden);
998
1032
        }
999
1033
 
1148
1182
 
1149
1183
                        break;
1150
1184
                case 'tag' :
1151
 
                        if ( !empty($_POST['taxonomy']) )
1152
 
                                $taxonomy = $_POST['taxonomy'];
1153
 
                        else
1154
 
                                $taxonomy = 'post_tag';
 
1185
                        $taxonomy = !empty($_POST['taxonomy']) ? $_POST['taxonomy'] : 'post_tag';
1155
1186
 
1156
1187
                        $tag = get_term( $id, $taxonomy );
1157
1188
                        $_POST['description'] = $tag->description;
1162
1193
                                if ( !$tag || is_wp_error( $tag ) )
1163
1194
                                        die( __('Tag not updated.') );
1164
1195
 
1165
 
                                echo _tag_row($tag);
 
1196
                                echo _tag_row($tag, '', $taxonomy);
1166
1197
                        } else {
1167
1198
                                die( __('Tag not updated.') );
1168
1199
                        }
1181
1212
        $what = isset($_POST['pages']) ? 'page' : 'post';
1182
1213
        $s = stripslashes($_POST['ps']);
1183
1214
        preg_match_all('/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $s, $matches);
1184
 
        $search_terms = array_map(create_function('$a', 'return trim($a, "\\"\'\\n\\r ");'), $matches[0]);
 
1215
        $search_terms = array_map('_search_terms_tidy', $matches[0]);
1185
1216
 
1186
1217
        $searchand = $search = '';
1187
 
        foreach( (array) $search_terms as $term) {
 
1218
        foreach ( (array) $search_terms as $term ) {
1188
1219
                $term = addslashes_gpc($term);
1189
1220
                $search .= "{$searchand}(($wpdb->posts.post_title LIKE '%{$term}%') OR ($wpdb->posts.post_content LIKE '%{$term}%'))";
1190
1221
                $searchand = ' AND ';
1193
1224
        if ( count($search_terms) > 1 && $search_terms[0] != $s )
1194
1225
                $search .= " OR ($wpdb->posts.post_title LIKE '%{$term}%') OR ($wpdb->posts.post_content LIKE '%{$term}%')";
1195
1226
 
1196
 
        $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" );
 
1227
        $posts = $wpdb->get_results( "SELECT ID, post_title, post_status, post_date FROM $wpdb->posts WHERE post_type = '$what' AND post_status IN ('draft', 'publish') AND ($search) ORDER BY post_date_gmt DESC LIMIT 50" );
1197
1228
 
1198
1229
        if ( ! $posts )
1199
1230
                exit( __('No posts found.') );
1200
1231
 
1201
 
        $html = '<table class="widefat" cellspacing="0"><thead><tr><th class="found-radio"><br /></th><th>'.__('Title').'</th><th>'.__('Time').'</th><th>'.__('Status').'</th></tr></thead><tbody>';
 
1232
        $html = '<table class="widefat" cellspacing="0"><thead><tr><th class="found-radio"><br /></th><th>'.__('Title').'</th><th>'.__('Date').'</th><th>'.__('Status').'</th></tr></thead><tbody>';
1202
1233
        foreach ( $posts as $post ) {
1203
1234
 
1204
1235
                switch ( $post->post_status ) {
1213
1244
                                $stat = __('Pending Review');
1214
1245
                                break;
1215
1246
                        case 'draft' :
1216
 
                                $stat = __('Unpublished');
 
1247
                                $stat = __('Draft');
1217
1248
                                break;
1218
1249
                }
1219
1250
 
1348
1379
 
1349
1380
        die();
1350
1381
        break;
 
1382
case 'image-editor':
 
1383
        $attachment_id = intval($_POST['postid']);
 
1384
        if ( empty($attachment_id) || !current_user_can('edit_post', $attachment_id) )
 
1385
                die('-1');
 
1386
 
 
1387
        check_ajax_referer( "image_editor-$attachment_id" );
 
1388
        include_once( ABSPATH . 'wp-admin/includes/image-edit.php' );
 
1389
 
 
1390
        $msg = false;
 
1391
        switch ( $_POST['do'] ) {
 
1392
                case 'save' :
 
1393
                        $msg = wp_save_image($attachment_id);
 
1394
                        $msg = json_encode($msg);
 
1395
                        die($msg);
 
1396
                        break;
 
1397
                case 'scale' :
 
1398
                        $msg = wp_save_image($attachment_id);
 
1399
                        break;
 
1400
                case 'restore' :
 
1401
                        $msg = wp_restore_image($attachment_id);
 
1402
                        break;
 
1403
        }
 
1404
 
 
1405
        wp_image_editor($attachment_id, $msg);
 
1406
        die();
 
1407
        break;
 
1408
case 'set-post-thumbnail':
 
1409
        $post_id = intval( $_POST['post_id'] );
 
1410
        if ( !current_user_can( 'edit_post', $post_id ) )
 
1411
                die( '-1' );
 
1412
        $thumbnail_id = intval( $_POST['thumbnail_id'] );
 
1413
 
 
1414
        if ( $thumbnail_id == '-1' ) {
 
1415
                delete_post_meta( $post_id, '_thumbnail_id' );
 
1416
                die( _wp_post_thumbnail_html() );
 
1417
        }
 
1418
 
 
1419
        if ( $thumbnail_id && get_post( $thumbnail_id ) ) {
 
1420
                $thumbnail_html = wp_get_attachment_image( $thumbnail_id, 'thumbnail' );
 
1421
                if ( !empty( $thumbnail_html ) ) {
 
1422
                        update_post_meta( $post_id, '_thumbnail_id', $thumbnail_id );
 
1423
                        die( _wp_post_thumbnail_html( $thumbnail_id ) );
 
1424
                }
 
1425
        }
 
1426
        die( '0' );
1351
1427
default :
1352
1428
        do_action( 'wp_ajax_' . $_POST['action'] );
1353
1429
        die('0');