X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/6c8f14c09105d0afa4c1574215c59b5021040e76..42aebe6945a3a60c8f73853bea2c8b202d64a20b:/wp-admin/includes/ajax-actions.php diff --git a/wp-admin/includes/ajax-actions.php b/wp-admin/includes/ajax-actions.php index c39effe7..e6c28344 100644 --- a/wp-admin/includes/ajax-actions.php +++ b/wp-admin/includes/ajax-actions.php @@ -10,19 +10,61 @@ * No-privilege Ajax handlers. */ -function wp_ajax_nopriv_autosave() { - $id = isset( $_POST['post_ID'] ) ? (int) $_POST['post_ID'] : 0; +/** + * Heartbeat API (experimental) + * + * Runs when the user is not logged in. + */ +function wp_ajax_nopriv_heartbeat() { + $response = array(); - if ( ! $id ) - wp_die( -1 ); + // screen_id is the same as $current_screen->id and the JS global 'pagenow' + if ( ! empty($_POST['screen_id']) ) + $screen_id = sanitize_key($_POST['screen_id']); + else + $screen_id = 'front'; + + if ( ! empty($_POST['data']) ) { + $data = wp_unslash( (array) $_POST['data'] ); + + /** + * Filter Heartbeat AJAX response in no-privilege environments. + * + * @since 3.6.0 + * + * @param array|object $response The no-priv Heartbeat response object or array. + * @param array $data An array of data passed via $_POST. + * @param string $screen_id The screen id. + */ + $response = apply_filters( 'heartbeat_nopriv_received', $response, $data, $screen_id ); + } - $message = sprintf( __('ALERT: You are logged out! Could not save draft. Please log in again.'), wp_login_url() ); - $x = new WP_Ajax_Response( array( - 'what' => 'autosave', - 'id' => $id, - 'data' => $message - ) ); - $x->send(); + /** + * Filter Heartbeat AJAX response when no data is passed. + * + * @since 3.6.0 + * + * @param array|object $response The Heartbeat response object or array. + * @param string $screen_id The screen id. + */ + $response = apply_filters( 'heartbeat_nopriv_send', $response, $screen_id ); + + /** + * Fires when Heartbeat ticks in no-privilege environments. + * + * Allows the transport to be easily replaced with long-polling. + * + * @since 3.6.0 + * + * @param array|object $response The no-priv Heartbeat response. + * @param string $screen_id The screen id. + */ + do_action( 'heartbeat_nopriv_tick', $response, $screen_id ); + + // send the current time according to the server + $response['server_time'] = time(); + + wp_send_json($response); } /* @@ -59,7 +101,7 @@ function wp_ajax_ajax_tag_search() { wp_die( 0 ); } - $s = stripslashes( $_GET['q'] ); + $s = wp_unslash( $_GET['q'] ); $comma = _x( ',', 'tag delimiter' ); if ( ',' !== $comma ) @@ -72,7 +114,7 @@ function wp_ajax_ajax_tag_search() { if ( strlen( $s ) < 2 ) wp_die(); // require 2 chars for matching - $results = $wpdb->get_col( $wpdb->prepare( "SELECT t.name FROM $wpdb->term_taxonomy AS tt INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = %s AND t.name LIKE (%s)", $taxonomy, '%' . like_escape( $s ) . '%' ) ); + $results = get_terms( $taxonomy, array( 'name__like' => $s, 'fields' => 'names', 'hide_empty' => false ) ); echo join( $results, "\n" ); wp_die(); @@ -148,6 +190,7 @@ function wp_ajax_autocomplete_user() { if ( ! is_multisite() || ! current_user_can( 'promote_users' ) || wp_is_large_network( 'users' ) ) wp_die( -1 ); + /** This filter is documented in wp-admin/user-new.php */ if ( ! is_super_admin() && ! apply_filters( 'autocomplete_users_for_site_admins', false ) ) wp_die( -1 ); @@ -226,10 +269,11 @@ function wp_ajax_logged_in() { * @return die */ function _wp_ajax_delete_comment_response( $comment_id, $delta = -1 ) { - $total = (int) @$_POST['_total']; - $per_page = (int) @$_POST['_per_page']; - $page = (int) @$_POST['_page']; - $url = esc_url_raw( @$_POST['_url'] ); + $total = isset( $_POST['_total'] ) ? (int) $_POST['_total'] : 0; + $per_page = isset( $_POST['_per_page'] ) ? (int) $_POST['_per_page'] : 0; + $page = isset( $_POST['_page'] ) ? (int) $_POST['_page'] : 0; + $url = isset( $_POST['_url'] ) ? esc_url_raw( $_POST['_url'] ) : ''; + // JS didn't send us everything we need to know. Just die with success message if ( !$total || !$per_page || !$page || !$url ) wp_die( time() ); @@ -531,7 +575,7 @@ function wp_ajax_dim_comment() { wp_die( -1 ); $current = wp_get_comment_status( $comment->comment_ID ); - if ( $_POST['new'] == $current ) + if ( isset( $_POST['new'] ) && $_POST['new'] == $current ) wp_die( time() ); check_ajax_referer( "approve-comment_$id" ); @@ -559,7 +603,7 @@ function wp_ajax_add_link_category( $action ) { check_ajax_referer( $action ); if ( !current_user_can( 'manage_categories' ) ) wp_die( -1 ); - $names = explode(',', $_POST['newcat']); + $names = explode(',', wp_unslash( $_POST['newcat'] ) ); $x = new WP_Ajax_Response(); foreach ( $names as $cat_name ) { $cat_name = trim($cat_name); @@ -572,7 +616,7 @@ function wp_ajax_add_link_category( $action ) { continue; else if ( is_array( $cat_id ) ) $cat_id = $cat_id['term_id']; - $cat_name = esc_html(stripslashes($cat_name)); + $cat_name = esc_html( $cat_name ); $x->add( array( 'what' => 'link-category', 'id' => $cat_id, @@ -651,7 +695,7 @@ function wp_ajax_get_tagcloud() { $tags = get_terms( $taxonomy, array( 'number' => 45, 'orderby' => 'count', 'order' => 'DESC' ) ); if ( empty( $tags ) ) - wp_die( isset( $tax->no_tagcloud ) ? $tax->no_tagcloud : __('No tags found!') ); + wp_die( $tax->labels->not_found ); if ( is_wp_error( $tags ) ) wp_die( $tags->get_error_message() ); @@ -679,9 +723,18 @@ function wp_ajax_get_comments( $action ) { check_ajax_referer( $action ); + if ( empty( $post_id ) && ! empty( $_REQUEST['p'] ) ) { + $id = absint( $_REQUEST['p'] ); + if ( ! empty( $id ) ) + $post_id = $id; + } + + if ( empty( $post_id ) ) + wp_die( -1 ); + $wp_list_table = _get_list_table( 'WP_Post_Comments_List_Table', array( 'screen' => 'edit-comments' ) ); - if ( !current_user_can( 'edit_post', $post_id ) ) + if ( ! current_user_can( 'edit_post', $post_id ) ) wp_die( -1 ); $wp_list_table->prepare_items(); @@ -715,24 +768,29 @@ function wp_ajax_replyto_comment( $action ) { check_ajax_referer( $action, '_ajax_nonce-replyto-comment' ); $comment_post_ID = (int) $_POST['comment_post_ID']; - if ( !current_user_can( 'edit_post', $comment_post_ID ) ) + $post = get_post( $comment_post_ID ); + if ( ! $post ) wp_die( -1 ); - $status = $wpdb->get_var( $wpdb->prepare("SELECT post_status FROM $wpdb->posts WHERE ID = %d", $comment_post_ID) ); + if ( !current_user_can( 'edit_post', $comment_post_ID ) ) + wp_die( -1 ); - if ( empty($status) ) + if ( empty( $post->post_status ) ) wp_die( 1 ); - elseif ( in_array($status, array('draft', 'pending', 'trash') ) ) + elseif ( in_array($post->post_status, array('draft', 'pending', 'trash') ) ) wp_die( __('ERROR: you are replying to a comment on a draft post.') ); $user = wp_get_current_user(); if ( $user->exists() ) { $user_ID = $user->ID; - $comment_author = $wpdb->escape($user->display_name); - $comment_author_email = $wpdb->escape($user->user_email); - $comment_author_url = $wpdb->escape($user->user_url); + $comment_author = wp_slash( $user->display_name ); + $comment_author_email = wp_slash( $user->user_email ); + $comment_author_url = wp_slash( $user->user_url ); $comment_content = trim($_POST['content']); if ( current_user_can( 'unfiltered_html' ) ) { + if ( ! isset( $_POST['_wp_unfiltered_html_comment'] ) ) + $_POST['_wp_unfiltered_html_comment'] = ''; + if ( wp_create_nonce( 'unfiltered-html-comment' ) != $_POST['_wp_unfiltered_html_comment'] ) { kses_remove_filters(); // start with a clean slate kses_init_filters(); // set up the filters @@ -745,7 +803,9 @@ function wp_ajax_replyto_comment( $action ) { if ( '' == $comment_content ) wp_die( __( 'ERROR: please type a comment.' ) ); - $comment_parent = absint($_POST['comment_ID']); + $comment_parent = 0; + if ( isset( $_POST['comment_ID'] ) ) + $comment_parent = absint( $_POST['comment_ID'] ); $comment_auto_approved = false; $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type', 'comment_parent', 'user_ID'); @@ -766,19 +826,18 @@ function wp_ajax_replyto_comment( $action ) { $position = ( isset($_POST['position']) && (int) $_POST['position'] ) ? (int) $_POST['position'] : '-1'; ob_start(); - if ( 'dashboard' == $_REQUEST['mode'] ) { - require_once( ABSPATH . 'wp-admin/includes/dashboard.php' ); - _wp_dashboard_recent_comments_row( $comment ); + if ( isset( $_REQUEST['mode'] ) && 'dashboard' == $_REQUEST['mode'] ) { + require_once( ABSPATH . 'wp-admin/includes/dashboard.php' ); + _wp_dashboard_recent_comments_row( $comment ); + } else { + if ( isset( $_REQUEST['mode'] ) && 'single' == $_REQUEST['mode'] ) { + $wp_list_table = _get_list_table('WP_Post_Comments_List_Table', array( 'screen' => 'edit-comments' ) ); } else { - if ( 'single' == $_REQUEST['mode'] ) { - $wp_list_table = _get_list_table('WP_Post_Comments_List_Table', array( 'screen' => 'edit-comments' ) ); - } else { - $wp_list_table = _get_list_table('WP_Comments_List_Table', array( 'screen' => 'edit-comments' ) ); - } - $wp_list_table->single_row( $comment ); + $wp_list_table = _get_list_table('WP_Comments_List_Table', array( 'screen' => 'edit-comments' ) ); } - $comment_list_item = ob_get_contents(); - ob_end_clean(); + $wp_list_table->single_row( $comment ); + } + $comment_list_item = ob_get_clean(); $response = array( 'what' => 'comment', @@ -807,7 +866,8 @@ function wp_ajax_edit_comment() { if ( '' == $_POST['content'] ) wp_die( __( 'ERROR: please type a comment.' ) ); - $_POST['comment_status'] = $_POST['status']; + if ( isset( $_POST['status'] ) ) + $_POST['comment_status'] = $_POST['status']; edit_comment(); $position = ( isset($_POST['position']) && (int) $_POST['position']) ? (int) $_POST['position'] : '-1'; @@ -817,11 +877,12 @@ function wp_ajax_edit_comment() { $wp_list_table = _get_list_table( $checkbox ? 'WP_Comments_List_Table' : 'WP_Post_Comments_List_Table', array( 'screen' => 'edit-comments' ) ); $comment = get_comment( $comment_id ); + if ( empty( $comment->comment_ID ) ) + wp_die( -1 ); ob_start(); - $wp_list_table->single_row( $comment ); - $comment_list_item = ob_get_contents(); - ob_end_clean(); + $wp_list_table->single_row( $comment ); + $comment_list_item = ob_get_clean(); $x = new WP_Ajax_Response(); @@ -888,6 +949,14 @@ function wp_ajax_add_menu_item() { } } + /** + * Filter the Walker class used when adding nav menu items. + * + * @since 3.4.0 + * + * @param string $class The walker class to use. Default 'Walker_Nav_Menu_Edit'. + * @param int $menu_id The menu id, derived from $_POST['menu']. + */ $walker_class_name = apply_filters( 'wp_edit_nav_menu_walker', 'Walker_Nav_Menu_Edit', $_POST['menu'] ); if ( ! class_exists( $walker_class_name ) ) @@ -903,6 +972,7 @@ function wp_ajax_add_menu_item() { ); echo walk_nav_menu_tree( $menu_items, 0, (object) $args ); } + wp_die(); } function wp_ajax_add_meta() { @@ -924,7 +994,7 @@ function wp_ajax_add_meta() { $_POST['post_type'] = $post->post_type; $_POST['post_status'] = 'draft'; $now = current_time('timestamp', 1); - $_POST['post_title'] = sprintf('Draft created on %s at %s', date(get_option('date_format'), $now), date(get_option('time_format'), $now)); + $_POST['post_title'] = sprintf( __( 'Draft created on %1$s at %2$s' ), date( get_option( 'date_format' ), $now ), date( get_option( 'time_format' ), $now ) ); if ( $pid = edit_post() ) { if ( is_wp_error( $pid ) ) { @@ -956,8 +1026,8 @@ function wp_ajax_add_meta() { ) ); } else { // Update? $mid = (int) key( $_POST['meta'] ); - $key = stripslashes( $_POST['meta'][$mid]['key'] ); - $value = stripslashes( $_POST['meta'][$mid]['value'] ); + $key = wp_unslash( $_POST['meta'][$mid]['key'] ); + $value = wp_unslash( $_POST['meta'][$mid]['value'] ); if ( '' == trim($key) ) wp_die( __( 'Please provide a custom field name.' ) ); if ( '' == trim($value) ) @@ -1024,68 +1094,50 @@ function wp_ajax_add_user( $action ) { } function wp_ajax_autosave() { - global $login_grace_period; - define( 'DOING_AUTOSAVE', true ); - $nonce_age = check_ajax_referer( 'autosave', 'autosavenonce' ); - - $_POST['post_category'] = explode(",", $_POST['catslist']); - if ( $_POST['post_type'] == 'page' || empty($_POST['post_category']) ) - unset($_POST['post_category']); + check_ajax_referer( 'autosave', 'autosavenonce' ); - $do_autosave = (bool) $_POST['autosave']; - $do_lock = true; - - $data = $alert = ''; - /* translators: draft saved date format, see http://php.net/date */ - $draft_saved_date_format = __('g:i:s a'); - /* translators: %s: date and time */ - $message = sprintf( __('Draft saved at %s.'), date_i18n( $draft_saved_date_format ) ); + if ( ! empty( $_POST['catslist'] ) ) + $_POST['post_category'] = explode( ',', $_POST['catslist'] ); + if ( $_POST['post_type'] == 'page' || empty( $_POST['post_category'] ) ) + unset( $_POST['post_category'] ); + $data = ''; $supplemental = array(); - if ( isset($login_grace_period) ) - $alert .= sprintf( __('Your login has expired. Please open a new browser window and log in again. '), add_query_arg( 'interim-login', 1, wp_login_url() ) ); - $id = $revision_id = 0; - $post_ID = (int) $_POST['post_ID']; - $_POST['ID'] = $post_ID; - $post = get_post($post_ID); - if ( 'auto-draft' == $post->post_status ) - $_POST['post_status'] = 'draft'; + $post_id = (int) $_POST['post_id']; + $_POST['ID'] = $_POST['post_ID'] = $post_id; + $post = get_post( $post_id ); + if ( empty( $post->ID ) || ! current_user_can( 'edit_post', $post->ID ) ) + wp_die( __( 'You are not allowed to edit this post.' ) ); - if ( $last = wp_check_post_lock( $post->ID ) ) { - $do_autosave = $do_lock = false; + if ( 'page' == $post->post_type && ! current_user_can( 'edit_page', $post->ID ) ) + wp_die( __( 'You are not allowed to edit this page.' ) ); - $last_user = get_userdata( $last ); - $last_user_name = $last_user ? $last_user->display_name : __( 'Someone' ); - $data = __( 'Autosave disabled.' ); - - $supplemental['disable_autosave'] = 'disable'; - $alert .= sprintf( __( '%s is currently editing this article. If you update it, you will overwrite the changes.' ), esc_html( $last_user_name ) ); - } - - if ( 'page' == $post->post_type ) { - if ( !current_user_can('edit_page', $post_ID) ) - wp_die( __( 'You are not allowed to edit this page.' ) ); - } else { - if ( !current_user_can('edit_post', $post_ID) ) - wp_die( __( 'You are not allowed to edit this post.' ) ); - } + if ( 'auto-draft' == $post->post_status ) + $_POST['post_status'] = 'draft'; - if ( $do_autosave ) { - // Drafts and auto-drafts are just overwritten by autosave - if ( 'auto-draft' == $post->post_status || 'draft' == $post->post_status ) { + if ( ! empty( $_POST['autosave'] ) ) { + if ( ! wp_check_post_lock( $post->ID ) && get_current_user_id() == $post->post_author && ( 'auto-draft' == $post->post_status || 'draft' == $post->post_status ) ) { + // Drafts and auto-drafts are just overwritten by autosave for the same user if the post is not locked $id = edit_post(); - } else { // Non drafts are not overwritten. The autosave is stored in a special post revision. + } else { + // Non drafts or other users drafts are not overwritten. The autosave is stored in a special post revision for each user. $revision_id = wp_create_post_autosave( $post->ID ); if ( is_wp_error($revision_id) ) $id = $revision_id; else $id = $post->ID; } - $data = $message; + + if ( ! is_wp_error($id) ) { + /* translators: draft saved date format, see http://php.net/date */ + $draft_saved_date_format = __('g:i:s a'); + /* translators: %s: date and time */ + $data = sprintf( __('Draft saved at %s.'), date_i18n( $draft_saved_date_format ) ); + } } else { if ( ! empty( $_POST['auto_draft'] ) ) $id = 0; // This tells us it didn't actually save @@ -1093,32 +1145,11 @@ function wp_ajax_autosave() { $id = $post->ID; } - if ( $do_lock && empty( $_POST['auto_draft'] ) && $id && is_numeric( $id ) ) { - $lock_result = wp_set_post_lock( $id ); - $supplemental['active-post-lock'] = implode( ':', $lock_result ); - } - - if ( $nonce_age == 2 ) { - $supplemental['replace-autosavenonce'] = wp_create_nonce('autosave'); - $supplemental['replace-getpermalinknonce'] = wp_create_nonce('getpermalink'); - $supplemental['replace-samplepermalinknonce'] = wp_create_nonce('samplepermalink'); - $supplemental['replace-closedpostboxesnonce'] = wp_create_nonce('closedpostboxes'); - $supplemental['replace-_ajax_linking_nonce'] = wp_create_nonce( 'internal-linking' ); - if ( $id ) { - if ( $_POST['post_type'] == 'post' ) - $supplemental['replace-_wpnonce'] = wp_create_nonce('update-post_' . $id); - elseif ( $_POST['post_type'] == 'page' ) - $supplemental['replace-_wpnonce'] = wp_create_nonce('update-page_' . $id); - } - } - - if ( ! empty($alert) ) - $supplemental['alert'] = $alert; - + // @todo Consider exposing any errors, rather than having 'Saving draft...' $x = new WP_Ajax_Response( array( 'what' => 'autosave', 'id' => $id, - 'data' => $id ? $data : '', + 'data' => $data, 'supplemental' => $supplemental ) ); $x->send(); @@ -1197,7 +1228,15 @@ function wp_ajax_menu_get_metabox() { } if ( ! empty( $_POST['item-object'] ) && isset( $items[$_POST['item-object']] ) ) { - $item = apply_filters( 'nav_menu_meta_box_object', $items[ $_POST['item-object'] ] ); + $menus_meta_box_object = $items[ $_POST['item-object'] ]; + /** + * Filter a nav menu meta box object. + * + * @since 3.0.0 + * + * @param object $menus_meta_box_object A nav menu meta box object, such as Page, Post, Category, Tag, etc. + */ + $item = apply_filters( 'nav_menu_meta_box_object', $menus_meta_box_object ); ob_start(); call_user_func_array($callback, array( null, @@ -1226,7 +1265,7 @@ function wp_ajax_wp_link_ajax() { $args = array(); if ( isset( $_POST['search'] ) ) - $args['s'] = stripslashes( $_POST['search'] ); + $args['s'] = wp_unslash( $_POST['search'] ); $args['pagenum'] = ! empty( $_POST['page'] ) ? absint( $_POST['page'] ) : 1; require(ABSPATH . WPINC . '/class-wp-editor.php'); @@ -1327,13 +1366,13 @@ function wp_ajax_inline_save() { $data = &$_POST; $post = get_post( $post_ID, ARRAY_A ); - $post = add_magic_quotes($post); //since it is from db + $post = wp_slash($post); //since it is from db $data['content'] = $post['post_content']; $data['excerpt'] = $post['post_excerpt']; // rename - $data['user_ID'] = $GLOBALS['user_ID']; + $data['user_ID'] = get_current_user_id(); if ( isset($data['post_parent']) ) $data['parent_id'] = $data['post_parent']; @@ -1349,6 +1388,12 @@ function wp_ajax_inline_save() { if ( empty($data['ping_status']) ) $data['ping_status'] = 'closed'; + // Hack: wp_unique_post_slug() doesn't work for drafts, so we will fake that our post is published. + if ( ! empty( $data['post_name'] ) && in_array( $post['post_status'], array( 'draft', 'pending' ) ) ) { + $post['post_status'] = 'publish'; + $data['post_name'] = wp_unique_post_slug( $data['post_name'], $post['ID'], $post['post_status'], $post['post_type'], $post['post_parent'] ); + } + // update the post edit_post(); @@ -1412,7 +1457,7 @@ function wp_ajax_inline_save_tax() { $parent = $parent_tag->parent; $level++; } - echo $wp_list_table->single_row( $tag, $level ); + $wp_list_table->single_row( $tag, $level ); wp_die(); } @@ -1424,7 +1469,7 @@ function wp_ajax_find_posts() { $post_types = get_post_types( array( 'public' => true ), 'objects' ); unset( $post_types['attachment'] ); - $s = stripslashes( $_POST['ps'] ); + $s = wp_unslash( $_POST['ps'] ); $searchand = $search = ''; $args = array( 'post_type' => array_keys( $post_types ), @@ -1520,9 +1565,26 @@ function wp_ajax_save_widget() { unset( $_POST['savewidgets'], $_POST['action'] ); - do_action('load-widgets.php'); - do_action('widgets.php'); - do_action('sidebar_admin_setup'); + /** + * Fires early when editing the widgets displayed in sidebars. + * + * @since 2.8.0 + */ + do_action( 'load-widgets.php' ); + + /** + * Fires early when editing the widgets displayed in sidebars. + * + * @since 2.8.0 + */ + do_action( 'widgets.php' ); + + /** + * Fires early when editing the widgets displayed in sidebars. + * + * @since 2.2.0 + */ + do_action( 'sidebar_admin_setup' ); $id_base = $_POST['id_base']; $widget_id = $_POST['widget-id']; @@ -1776,7 +1838,14 @@ function wp_ajax_wp_remove_post_lock() { if ( $active_lock[1] != get_current_user_id() ) wp_die( 0 ); - $new_lock = ( time() - apply_filters( 'wp_check_post_lock_window', AUTOSAVE_INTERVAL * 2 ) + 5 ) . ':' . $active_lock[1]; + /** + * Filter the post lock window duration. + * + * @since 3.3.0 + * + * @param int $interval The interval in seconds the post lock duration should last, plus 5 seconds. Default 120. + */ + $new_lock = ( time() - apply_filters( 'wp_check_post_lock_window', 120 ) + 5 ) . ':' . $active_lock[1]; update_post_meta( $post_id, '_edit_lock', $new_lock, implode( ':', $active_lock ) ); wp_die( 1 ); } @@ -1847,6 +1916,14 @@ function wp_ajax_query_attachments() { if ( current_user_can( get_post_type_object( 'attachment' )->cap->read_private_posts ) ) $query['post_status'] .= ',private'; + /** + * Filter the arguments passed to WP_Query during an AJAX call for querying attachments. + * + * @since 3.7.0 + * + * @param array $query An array of query variables. @see WP_Query::parse_query() + */ + $query = apply_filters( 'ajax_query_attachments_args', $query ); $query = new WP_Query( $query ); $posts = array_map( 'wp_prepare_attachment_for_js', $query->posts ); @@ -1888,11 +1965,10 @@ function wp_ajax_save_attachment() { $post['post_content'] = $changes['description']; if ( isset( $changes['alt'] ) ) { - $alt = get_post_meta( $id, '_wp_attachment_image_alt', true ); - $new_alt = stripslashes( $changes['alt'] ); - if ( $alt != $new_alt ) { - $new_alt = wp_strip_all_tags( $new_alt, true ); - update_post_meta( $id, '_wp_attachment_image_alt', addslashes( $new_alt ) ); + $alt = wp_unslash( $changes['alt'] ); + if ( $alt != get_post_meta( $id, '_wp_attachment_image_alt', true ) ) { + $alt = wp_strip_all_tags( $alt, true ); + update_post_meta( $id, '_wp_attachment_image_alt', wp_slash( $alt ) ); } } @@ -1926,6 +2002,7 @@ function wp_ajax_save_attachment_compat() { if ( 'attachment' != $post['post_type'] ) wp_send_json_error(); + /** This filter is documented in wp-admin/includes/media.php */ $post = apply_filters( 'attachment_fields_to_save', $post, $attachment_data ); if ( isset( $post['errors'] ) ) { @@ -1989,7 +2066,7 @@ function wp_ajax_save_attachment_order() { function wp_ajax_send_attachment_to_editor() { check_ajax_referer( 'media-send-to-editor', 'nonce' ); - $attachment = stripslashes_deep( $_POST['attachment'] ); + $attachment = wp_unslash( $_POST['attachment'] ); $id = intval( $attachment['id'] ); @@ -2015,7 +2092,7 @@ function wp_ajax_send_attachment_to_editor() { $html = '' . $html . ''; } - remove_filter( 'media_send_to_editor', 'image_media_send_to_editor', 10, 3 ); + remove_filter( 'media_send_to_editor', 'image_media_send_to_editor' ); if ( 'image' === substr( $post->post_mime_type, 0, 5 ) ) { $align = isset( $attachment['align'] ) ? $attachment['align'] : 'none'; @@ -2024,8 +2101,11 @@ function wp_ajax_send_attachment_to_editor() { $caption = isset( $attachment['post_excerpt'] ) ? $attachment['post_excerpt'] : ''; $title = ''; // We no longer insert title tags into tags, as they are redundant. $html = get_image_send_to_editor( $id, $caption, $title, $align, $url, (bool) $rel, $size, $alt ); + } elseif ( 'video' === substr( $post->post_mime_type, 0, 5 ) || 'audio' === substr( $post->post_mime_type, 0, 5 ) ) { + $html = stripslashes_deep( $_POST['html'] ); } + /** This filter is documented in wp-admin/includes/media.php */ $html = apply_filters( 'media_send_to_editor', $html, $id, $attachment ); wp_send_json_success( $html ); @@ -2044,7 +2124,7 @@ function wp_ajax_send_attachment_to_editor() { function wp_ajax_send_link_to_editor() { check_ajax_referer( 'media-send-to-editor', 'nonce' ); - if ( ! $src = stripslashes( $_POST['src'] ) ) + if ( ! $src = wp_unslash( $_POST['src'] ) ) wp_send_json_error(); if ( ! strpos( $src, '://' ) ) @@ -2053,7 +2133,7 @@ function wp_ajax_send_link_to_editor() { if ( ! $src = esc_url_raw( $src ) ) wp_send_json_error(); - if ( ! $title = trim( stripslashes( $_POST['title'] ) ) ) + if ( ! $title = trim( wp_unslash( $_POST['title'] ) ) ) $title = wp_basename( $src ); $html = ''; @@ -2066,7 +2146,101 @@ function wp_ajax_send_link_to_editor() { && ( 'audio' == $ext_type || 'video' == $ext_type ) ) $type = $ext_type; + /** This filter is documented in wp-admin/includes/media.php */ $html = apply_filters( $type . '_send_to_editor_url', $html, $src, $title ); wp_send_json_success( $html ); } + +/** + * Heartbeat API (experimental) + * + * Runs when the user is logged in. + */ +function wp_ajax_heartbeat() { + if ( empty( $_POST['_nonce'] ) ) + wp_send_json_error(); + + $response = array(); + + if ( false === wp_verify_nonce( $_POST['_nonce'], 'heartbeat-nonce' ) ) { + // User is logged in but nonces have expired. + $response['nonces_expired'] = true; + wp_send_json($response); + } + + // screen_id is the same as $current_screen->id and the JS global 'pagenow' + if ( ! empty($_POST['screen_id']) ) + $screen_id = sanitize_key($_POST['screen_id']); + else + $screen_id = 'front'; + + if ( ! empty($_POST['data']) ) { + $data = (array) $_POST['data']; + + /** + * Filter the Heartbeat response received. + * + * @since 3.6.0 + * + * @param array|object $response The Heartbeat response object or array. + * @param array $data The $_POST data sent. + * @param string $screen_id The screen id. + */ + $response = apply_filters( 'heartbeat_received', $response, $data, $screen_id ); + } + + /** + * Filter the Heartbeat response sent. + * + * @since 3.6.0 + * + * @param array|object $response The Heartbeat response object or array. + * @param string $screen_id The screen id. + */ + $response = apply_filters( 'heartbeat_send', $response, $screen_id ); + + /** + * Fires when Heartbeat ticks in logged-in environments. + * + * Allows the transport to be easily replaced with long-polling. + * + * @since 3.6.0 + * + * @param array|object $response The Heartbeat response object or array. + * @param string $screen_id The screen id. + */ + do_action( 'heartbeat_tick', $response, $screen_id ); + + // Send the current time according to the server + $response['server_time'] = time(); + + wp_send_json($response); +} + +function wp_ajax_get_revision_diffs() { + require ABSPATH . 'wp-admin/includes/revision.php'; + + if ( ! $post = get_post( (int) $_REQUEST['post_id'] ) ) + wp_send_json_error(); + + if ( ! current_user_can( 'read_post', $post->ID ) ) + wp_send_json_error(); + + // Really just pre-loading the cache here. + if ( ! $revisions = wp_get_post_revisions( $post->ID, array( 'check_enabled' => false ) ) ) + wp_send_json_error(); + + $return = array(); + @set_time_limit( 0 ); + + foreach ( $_REQUEST['compare'] as $compare_key ) { + list( $compare_from, $compare_to ) = explode( ':', $compare_key ); // from:to + + $return[] = array( + 'id' => $compare_key, + 'fields' => wp_get_revision_ui_diff( $post, $compare_from, $compare_to ), + ); + } + wp_send_json_success( $return ); +}