Make WordPress Core

Ticket #12400: 12400.3.diff

File 12400.3.diff, 17.3 KB (added by sivel, 14 years ago)

New try at frontend ajax handler

  • wp-ajax.php

     
     1<?php
     2/**
     3 * WordPress Primary AJAX Process Execution.
     4 *
     5 * @package WordPress
     6 */
     7
     8/**
     9 * Executing AJAX process.
     10 *
     11 * @since 2.1.0
     12 */
     13define( 'DOING_AJAX', true );
     14
     15if ( ! isset( $_REQUEST['action'] ) )
     16        die( '-1' );
     17
     18if ( ! defined( 'ABSPATH' ) )
     19        require_once( dirname( __FILE__ ) . '/wp-load.php' );
     20
     21if ( defined( 'WP_ADMIN' ) && WP_ADMIN )
     22        require_once( ABSPATH . 'wp-admin/includes/admin.php' );
     23
     24@header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) );
     25send_nosniff_header();
     26
     27if ( defined( 'WP_ADMIN' ) && WP_ADMIN )
     28        do_action( 'admin_init' );
     29
     30if ( ! is_user_logged_in() ) {
     31        if ( !empty( $_REQUEST['action'] ) )
     32                do_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] );
     33        die('-1');
     34}
     35
     36if ( isset( $_REQUEST['action'] ) ) {
     37        do_action( 'wp_ajax_' . $_REQUEST['action'] );
     38        die( '0' );
     39} else {
     40        die( '1' );
     41}
     42?>
  • wp-admin/admin-ajax.php

     
    11<?php
    22/**
    3  * WordPress AJAX Process Execution.
     3 * WordPress AJAX Process Execution for admin.
    44 *
    55 * @package WordPress
    66 * @subpackage Administration
    77 */
    8 
    9 /**
    10  * Executing AJAX process.
    11  *
    12  * @since 2.1.0
    13  */
    14 define('DOING_AJAX', true);
    158define('WP_ADMIN', true);
    169
    17 if ( ! isset( $_REQUEST['action'] ) )
    18         die('-1');
     10if ( ! defined( 'ABSPATH' ) )
     11        require_once( '../wp-load.php' );
    1912
    20 require_once('../wp-load.php');
    21 
    22 require_once('./includes/admin.php');
    23 @header('Content-Type: text/html; charset=' . get_option('blog_charset'));
    24 send_nosniff_header();
    25 
    26 do_action('admin_init');
    27 
    2813if ( ! is_user_logged_in() ) {
     14        if ( isset( $_POST['action'] ) )
     15                add_action( 'wp_ajax_nopriv_autosave', 'ajax_logged_out_autosave' );
     16} else {
     17        if ( isset( $_GET['action'] ) ) {
     18                add_action( 'wp_ajax_fetch-list', 'ajax_fetch_list' );
     19                add_action( 'wp_ajax_ajax-tag-search', 'ajax_tag_search' );
     20                add_action( 'wp_ajax_wp-compression-test', 'ajax_wp_compression_test' );
     21                add_action( 'wp_ajax_imgedit-preview', 'ajax_imgedit_preview' );
     22                add_action( 'wp_ajax_menu-quick-search', 'ajax_menu_quick_search' );
     23                add_action( 'wp_ajax_oembed-cache', 'ajax_oembed_cache' );
     24        } else if ( isset( $_POST['action'] ) ) {
     25                add_action( 'wp_ajax_delete-comment', 'ajax_delete_comment' );
     26                add_action( 'wp_ajax_delete-tag', 'ajax_delete_tag' );
     27                add_action( 'wp_ajax_delete-link', 'ajax_delete_link' );
     28                add_action( 'wp_ajax_delete-meta', 'ajax_delete_meta' );
     29                add_action( 'wp_ajax_delete-post', 'ajax_delete_post' );
     30                add_action( 'wp_ajax_trash-post', 'ajax_trash_untrash_post' );
     31                add_action( 'wp_ajax_untrash-post', 'ajax_trash_untrash_post' );
     32                add_action( 'wp_ajax_delete-page', 'ajax_delete_page' );
     33                add_action( 'wp_ajax_dim-comment', 'ajax_dim_comment' );
     34                add_action( 'wp_ajax_add-link-category', 'ajax_add_link_category' );
     35                add_action( 'wp_ajax_add-tag', 'ajax_add_tag' );
     36                add_action( 'wp_ajax_get-tagcloud', 'ajax_get_tagcloud' );
     37                add_action( 'wp_ajax_get-comments', 'ajax_get_comments' );
     38                add_action( 'wp_ajax_replyto-comment', 'ajax_replyto_comment' );
     39                add_action( 'wp_ajax_edit-comment', 'ajax_edit_comment' );
     40                add_action( 'wp_ajax_add-menu-item', 'ajax_add_menu_item' );
     41                add_action( 'wp_ajax_add-meta', 'ajax_add_meta' );
     42                add_action( 'wp_ajax_add-user', 'ajax_add_user' );
     43                add_action( 'wp_ajax_autosave', 'ajax_autosave' );
     44                add_action( 'wp_ajax_closed-postboxes', 'ajax_closed_postboxes' );
     45                add_action( 'wp_ajax_hidden-columns', 'ajax_hidden_columns' );
     46                add_action( 'wp_ajax_menu-get-metabox', 'ajax_menu_get_metabox' );
     47                add_action( 'wp_ajax_menu-quick-search', 'ajax_menu_quick_search' );
     48                add_action( 'wp_ajax_wp-link-ajax', 'ajax_wp_link_ajax' );
     49                add_action( 'wp_ajax_menu-locations-save', 'ajax_menu_locations_save' );
     50                add_action( 'wp_ajax_meta-box-order', 'ajax_meta_box_order' );
     51                add_action( 'wp_ajax_get-permalink', 'ajax_get_permalink' );
     52                add_action( 'wp_ajax_sample-permalink', 'ajax_sample_permalink' );
     53                add_action( 'wp_ajax_inline-save', 'ajax_inline_save' );
     54                add_action( 'wp_ajax_inline-save-tax', 'ajax_inline_save_tax' );
     55                add_action( 'wp_ajax_find_posts', 'ajax_find_posts' );
     56                add_action( 'wp_ajax_widgets-order', 'ajax_widgets_order' );
     57                add_action( 'wp_ajax_save-widget', 'ajax_save_widget' );
     58                add_action( 'wp_ajax_image-editor', 'ajax_image_editor' );
     59                add_action( 'wp_ajax_set-post-thumbnail', 'ajax_set_post_thumbnail' );
     60                add_action( 'wp_ajax_date_format', 'ajax_date_format' );
     61                add_action( 'wp_ajax_time_format', 'ajax_time_format' );
     62        }
     63}
    2964
    30         if ( isset( $_POST['action'] ) && $_POST['action'] == 'autosave' ) {
    31                 $id = isset($_POST['post_ID'])? (int) $_POST['post_ID'] : 0;
     65function ajax_logged_out_autosave() {
     66        $id = isset($_POST['post_ID'])? (int) $_POST['post_ID'] : 0;
    3267
    33                 if ( ! $id )
    34                         die('-1');
     68        if ( ! $id )
     69                die('-1');
    3570
    36                 $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() );
    37                 $x = new WP_Ajax_Response( array(
    38                         'what' => 'autosave',
    39                         'id' => $id,
    40                         'data' => $message
    41                 ) );
    42                 $x->send();
    43         }
    44 
    45         if ( !empty( $_REQUEST['action'] ) )
    46                 do_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] );
    47 
    48         die('-1');
     71        $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() );
     72        $x = new WP_Ajax_Response( array(
     73                'what' => 'autosave',
     74                'id' => $id,
     75                'data' => $message
     76        ) );
     77        $x->send();
    4978}
    5079
    51 if ( isset( $_GET['action'] ) ) :
    52 switch ( $action = $_GET['action'] ) :
    53 case 'fetch-list' :
    54 
     80function ajax_fetch_list() {
    5581        $list_class = $_GET['list_args']['class'];
    5682        check_ajax_referer( "fetch-list-$list_class", '_ajax_fetch_list_nonce' );
    5783
     
    7399        $wp_list_table->ajax_response();
    74100
    75101        die( '0' );
    76         break;
    77 case 'ajax-tag-search' :
     102}
     103
     104function ajax_tag_search() {
    78105        if ( isset( $_GET['tax'] ) ) {
    79106                $taxonomy = sanitize_key( $_GET['tax'] );
    80107                $tax = get_taxonomy( $taxonomy );
     
    100127
    101128        echo join( $results, "\n" );
    102129        die;
    103         break;
    104 case 'wp-compression-test' :
     130}
     131
     132function wp_compression_test() {
    105133        if ( !current_user_can( 'manage_options' ) )
    106134                die('-1');
    107135
     
    144172        }
    145173
    146174        die('0');
    147         break;
    148 case 'imgedit-preview' :
     175}
     176
     177function ajax_imgedit_preview() {
    149178        $post_id = intval($_GET['postid']);
    150179        if ( empty($post_id) || !current_user_can('edit_post', $post_id) )
    151180                die('-1');
     
    157186                die('-1');
    158187
    159188        die();
    160         break;
    161 case 'menu-quick-search':
     189}
     190
     191function admin_menu_quick_search() {
    162192        if ( ! current_user_can( 'edit_theme_options' ) )
    163193                die('-1');
    164194
     
    167197        _wp_ajax_menu_quick_search( $_REQUEST );
    168198
    169199        exit;
    170         break;
    171 case 'oembed-cache' :
     200}
     201
     202function oembed_cache() {
    172203        $return = ( $wp_embed->cache_oembed( $_GET['post'] ) ) ? '1' : '0';
    173204        die( $return );
    174         break;
    175 default :
    176         do_action( 'wp_ajax_' . $_GET['action'] );
    177         die('0');
    178         break;
    179 endswitch;
    180 endif;
     205}
    181206
     207
    182208/**
    183209 * Sends back current comment total and new page links if they need to be updated.
    184210 *
     
    316342        $x->send();
    317343}
    318344
    319 $id = isset($_POST['id'])? (int) $_POST['id'] : 0;
    320 switch ( $action = $_POST['action'] ) :
    321 case 'delete-comment' : // On success, die with time() instead of 1
     345function ajax_delete_comment() { // On success, die with time() instead of 1
     346        $id = isset($_POST['id'])? (int) $_POST['id'] : 0;
    322347        if ( !$comment = get_comment( $id ) )
    323348                die( (string) time() );
    324349        if ( !current_user_can( 'edit_post', $comment->comment_post_ID ) )
     
    357382        if ( $r ) // Decide if we need to send back '1' or a more complicated response including page links and comment counts
    358383                _wp_ajax_delete_comment_response( $comment->comment_ID, $delta );
    359384        die( '0' );
    360         break;
    361 case 'delete-tag' :
     385}
     386
     387function ajax_delete_tag() {
    362388        $tag_id = (int) $_POST['tag_ID'];
    363389        check_ajax_referer( "delete-tag_$tag_id" );
    364390
     
    376402                die('1');
    377403        else
    378404                die('0');
    379         break;
    380 case 'delete-link' :
     405}
     406
     407function ajax_delete_link() {
     408        $id = isset($_POST['id'])? (int) $_POST['id'] : 0;
    381409        check_ajax_referer( "delete-bookmark_$id" );
    382410        if ( !current_user_can( 'manage_links' ) )
    383411                die('-1');
     
    390418                die('1');
    391419        else
    392420                die('0');
    393         break;
    394 case 'delete-meta' :
     421}
     422
     423function ajax_delete_meta() {
     424        $id = isset($_POST['id'])? (int) $_POST['id'] : 0;
    395425        check_ajax_referer( "delete-meta_$id" );
    396426        if ( !$meta = get_post_meta_by_id( $id ) )
    397427                die('1');
     
    401431        if ( delete_meta( $meta->meta_id ) )
    402432                die('1');
    403433        die('0');
    404         break;
    405 case 'delete-post' :
     434}
     435
     436function ajax_delete_post() {
     437        $id = isset($_POST['id'])? (int) $_POST['id'] : 0;
    406438        check_ajax_referer( "{$action}_$id" );
    407439        if ( !current_user_can( 'delete_post', $id ) )
    408440                die('-1');
     
    414446                die('1');
    415447        else
    416448                die('0');
    417         break;
    418 case 'trash-post' :
    419 case 'untrash-post' :
     449}
     450
     451function ajax_trash_untrash_post() {
     452        $id = isset($_POST['id'])? (int) $_POST['id'] : 0;
    420453        check_ajax_referer( "{$action}_$id" );
    421454        if ( !current_user_can( 'delete_post', $id ) )
    422455                die('-1');
     
    433466                die('1');
    434467
    435468        die('0');
    436         break;
    437 case 'delete-page' :
     469}
     470
     471function ajax_delete_page() {
     472        $id = isset($_POST['id'])? (int) $_POST['id'] : 0;
    438473        check_ajax_referer( "{$action}_$id" );
    439474        if ( !current_user_can( 'delete_page', $id ) )
    440475                die('-1');
     
    446481                die('1');
    447482        else
    448483                die('0');
    449         break;
    450 case 'dim-comment' : // On success, die with time() instead of 1
     484}
    451485
     486function ajax_dim_comment() { // On success, die with time() instead of 1
     487        $id = isset($_POST['id'])? (int) $_POST['id'] : 0;
    452488        if ( !$comment = get_comment( $id ) ) {
    453489                $x = new WP_Ajax_Response( array(
    454490                        'what' => 'comment',
     
    481517        // Decide if we need to send back '1' or a more complicated response including page links and comment counts
    482518        _wp_ajax_delete_comment_response( $comment->comment_ID );
    483519        die( '0' );
    484         break;
    485 case 'add-link-category' : // On the Fly
     520}
     521
     522function ajax_add_link_category() { // On the Fly
    486523        check_ajax_referer( $action );
    487524        if ( !current_user_can( 'manage_categories' ) )
    488525                die('-1');
     
    506543                ) );
    507544        }
    508545        $x->send();
    509         break;
    510 case 'add-tag' :
     546}
     547
     548function ajax_add_tag() {
    511549        check_ajax_referer( 'add-tag' );
    512550        $post_type = !empty($_POST['post_type']) ? $_POST['post_type'] : 'post';
    513551        $taxonomy = !empty($_POST['taxonomy']) ? $_POST['taxonomy'] : 'post_tag';
     
    558596                'supplemental' => (array) $tag
    559597                ) );
    560598        $x->send();
    561         break;
    562 case 'get-tagcloud' :
     599}
     600
     601function ajax_get_tagcloud() {
    563602        if ( isset( $_POST['tax'] ) ) {
    564603                $taxonomy = sanitize_key( $_POST['tax'] );
    565604                $tax = get_taxonomy( $taxonomy );
     
    593632        echo $return;
    594633
    595634        exit;
    596         break;
    597 case 'get-comments' :
     635}
     636
     637function ajax_get_comments() {
    598638        check_ajax_referer( $action );
    599639
    600640        set_current_screen( 'edit-comments' );
     
    623663                'data' => $comment_list_item
    624664        ) );
    625665        $x->send();
    626         break;
    627 case 'replyto-comment' :
     666}
     667
     668function ajax_replyto_comment() {
    628669        check_ajax_referer( $action, '_ajax_nonce-replyto-comment' );
    629670
    630671        set_current_screen( 'edit-comments' );
     
    693734        ));
    694735
    695736        $x->send();
    696         break;
    697 case 'edit-comment' :
     737}
     738
     739function ajax_edit_comment() {
    698740        check_ajax_referer( 'replyto-comment', '_ajax_nonce-replyto-comment' );
    699741
    700742        set_current_screen( 'edit-comments' );
     
    731773        ));
    732774
    733775        $x->send();
    734         break;
    735 case 'add-menu-item' :
     776}
     777
     778function ajax_add_menu_item() {
    736779        if ( ! current_user_can( 'edit_theme_options' ) )
    737780                die('-1');
    738781
     
    793836                );
    794837                echo walk_nav_menu_tree( $menu_items, 0, (object) $args );
    795838        }
    796         break;
    797 case 'add-meta' :
     839}
     840
     841function ajax_add_meta() {
    798842        check_ajax_referer( 'add-meta', '_ajax_nonce-add-meta' );
    799843        $c = 0;
    800844        $pid = (int) $_POST['post_id'];
     
    875919                ) );
    876920        }
    877921        $x->send();
    878         break;
    879 case 'add-user' :
     922}
     923
     924function ajax_add_user() {
    880925        check_ajax_referer( $action );
    881926        if ( !current_user_can('create_users') )
    882927                die('-1');
     
    903948                )
    904949        ) );
    905950        $x->send();
    906         break;
    907 case 'autosave' : // The name of this action is hardcoded in edit_post()
     951}
     952
     953function ajax_autosave() { // The name of this action is hardcoded in edit_post()
    908954        define( 'DOING_AUTOSAVE', true );
    909955
    910956        $nonce_age = check_ajax_referer( 'autosave', 'autosavenonce' );
     
    9981044                'supplemental' => $supplemental
    9991045        ) );
    10001046        $x->send();
    1001         break;
    1002 case 'closed-postboxes' :
     1047}
     1048
     1049function ajax_closed_postboxes() {
    10031050        check_ajax_referer( 'closedpostboxes', 'closedpostboxesnonce' );
    10041051        $closed = isset( $_POST['closed'] ) ? explode( ',', $_POST['closed']) : array();
    10051052        $closed = array_filter($closed);
     
    10241071        }
    10251072
    10261073        die('1');
    1027         break;
    1028 case 'hidden-columns' :
     1074}
     1075
     1076function ajax_hidden_columns() {
    10291077        check_ajax_referer( 'screen-options-nonce', 'screenoptionnonce' );
    10301078        $hidden = isset( $_POST['hidden'] ) ? $_POST['hidden'] : '';
    10311079        $hidden = explode( ',', $_POST['hidden'] );
     
    10411089                update_user_option($user->ID, "manage{$page}columnshidden", $hidden, true);
    10421090
    10431091        die('1');
    1044         break;
    1045 case 'menu-get-metabox' :
     1092}
     1093
     1094function ajax_menu_get_metabox() {
    10461095        if ( ! current_user_can( 'edit_theme_options' ) )
    10471096                die('-1');
    10481097
     
    10801129        }
    10811130
    10821131        exit;
    1083         break;
    1084 case 'menu-quick-search':
     1132}
     1133
     1134function ajax_menu_quick_search() {
    10851135        if ( ! current_user_can( 'edit_theme_options' ) )
    10861136                die('-1');
    10871137
     
    10901140        _wp_ajax_menu_quick_search( $_REQUEST );
    10911141
    10921142        exit;
    1093         break;
    1094 case 'wp-link-ajax':
     1143}
     1144
     1145function ajax_wp_link_ajax() {
    10951146        require_once ABSPATH . 'wp-admin/includes/internal-linking.php';
    10961147
    10971148        check_ajax_referer( 'internal-linking', '_ajax_linking_nonce' );
     
    11111162        echo "\n";
    11121163
    11131164        exit;
    1114         break;
    1115 case 'menu-locations-save':
     1165}
     1166
     1167function ajax_menu_locations_save() {
    11161168        if ( ! current_user_can( 'edit_theme_options' ) )
    11171169                die('-1');
    11181170        check_ajax_referer( 'add-menu_item', 'menu-settings-column-nonce' );
     
    11201172                die('0');
    11211173        set_theme_mod( 'nav_menu_locations', array_map( 'absint', $_POST['menu-locations'] ) );
    11221174        die('1');
    1123         break;
    1124 case 'meta-box-order':
     1175}
     1176
     1177function ajax_meta_box_order() {
    11251178        check_ajax_referer( 'meta-box-order' );
    11261179        $order = isset( $_POST['order'] ) ? (array) $_POST['order'] : false;
    11271180        $page_columns = isset( $_POST['page_columns'] ) ? (int) $_POST['page_columns'] : 0;
     
    11401193                update_user_option($user->ID, "screen_layout_$page", $page_columns, true);
    11411194
    11421195        die('1');
    1143         break;
    1144 case 'get-permalink':
     1196}
     1197
     1198function ajax_get_permalink() {
    11451199        check_ajax_referer( 'getpermalink', 'getpermalinknonce' );
    11461200        $post_id = isset($_POST['post_id'])? intval($_POST['post_id']) : 0;
    11471201        die(add_query_arg(array('preview' => 'true'), get_permalink($post_id)));
    1148 break;
    1149 case 'sample-permalink':
     1202}
     1203
     1204function ajax_sample_permalink() {
    11501205        check_ajax_referer( 'samplepermalink', 'samplepermalinknonce' );
    11511206        $post_id = isset($_POST['post_id'])? intval($_POST['post_id']) : 0;
    11521207        $title = isset($_POST['new_title'])? $_POST['new_title'] : '';
    11531208        $slug = isset($_POST['new_slug'])? $_POST['new_slug'] : null;
    11541209        die(get_sample_permalink_html($post_id, $title, $slug));
    1155 break;
    1156 case 'inline-save':
     1210}
     1211
     1212function ajax_inline_save() {
    11571213        check_ajax_referer( 'inlineeditnonce', '_inline_edit' );
    11581214
    11591215        if ( ! isset($_POST['post_ID']) || ! ( $post_ID = (int) $_POST['post_ID'] ) )
     
    12101266        $wp_list_table->display_rows( array( get_post( $_POST['post_ID'] ) ) );
    12111267
    12121268        exit;
    1213         break;
    1214 case 'inline-save-tax':
     1269}
     1270
     1271function ajax_inline_save_tax() {
    12151272        check_ajax_referer( 'taxinlineeditnonce', '_inline_edit' );
    12161273
    12171274        $taxonomy = sanitize_key( $_POST['taxonomy'] );
     
    12491306        }
    12501307
    12511308        exit;
    1252         break;
    1253 case 'find_posts':
     1309}
     1310
     1311function ajax_find_posts() {
    12541312        check_ajax_referer( 'find-posts' );
    12551313
    12561314        if ( empty($_POST['ps']) )
     
    13191377                'data' => $html
    13201378        ));
    13211379        $x->send();
     1380}
    13221381
    1323         break;
    1324 case 'widgets-order' :
     1382function ajax_widgets_order() {
    13251383        check_ajax_referer( 'save-sidebar-widgets', 'savewidgets' );
    13261384
    13271385        if ( !current_user_can('edit_theme_options') )
     
    13501408        }
    13511409
    13521410        die('-1');
    1353         break;
    1354 case 'save-widget' :
     1411}
     1412
     1413function ajax_save_widget() {
    13551414        check_ajax_referer( 'save-sidebar-widgets', 'savewidgets' );
    13561415
    13571416        if ( !current_user_can('edit_theme_options') || !isset($_POST['id_base']) )
     
    14181477                call_user_func_array( $form['callback'], $form['params'] );
    14191478
    14201479        die();
    1421         break;
    1422 case 'image-editor':
     1480}
     1481
     1482function ajax_image_editor() {
    14231483        $attachment_id = intval($_POST['postid']);
    14241484        if ( empty($attachment_id) || !current_user_can('edit_post', $attachment_id) )
    14251485                die('-1');
     
    14441504
    14451505        wp_image_editor($attachment_id, $msg);
    14461506        die();
    1447         break;
    1448 case 'set-post-thumbnail':
     1507}
     1508
     1509function ajax_set_post_thumbnail() {
    14491510        $post_ID = intval( $_POST['post_id'] );
    14501511        if ( !current_user_can( 'edit_post', $post_ID ) )
    14511512                die( '-1' );
     
    14611522        if ( set_post_thumbnail( $post_ID, $thumbnail_id ) )
    14621523                die( _wp_post_thumbnail_html( $thumbnail_id ) );
    14631524        die( '0' );
    1464         break;
    1465 case 'date_format' :
     1525}
     1526
     1527function ajax_date_format() {
    14661528        die( date_i18n( sanitize_option( 'date_format', $_POST['date'] ) ) );
    1467         break;
    1468 case 'time_format' :
     1529}
     1530
     1531function ajax_time_format() {
    14691532        die( date_i18n( sanitize_option( 'time_format', $_POST['date'] ) ) );
    1470         break;
    1471 default :
    1472         do_action( 'wp_ajax_' . $_POST['action'] );
    1473         die('0');
    1474         break;
    1475 endswitch;
     1533}
     1534
     1535include( '../wp-ajax.php' );
    14761536?>