]> scripts.mit.edu Git - autoinstalls/wordpress.git/blobdiff - wp-admin/includes/ajax-actions.php
WordPress 4.2.5-scripts
[autoinstalls/wordpress.git] / wp-admin / includes / ajax-actions.php
index ff2db70f9ca8a3ca4e2a6b350c88fdf418824e92..696b4328c7ce97281747ea659dcee74738dc24c0 100644 (file)
@@ -103,17 +103,20 @@ function wp_ajax_fetch_list() {
  * @since 3.1.0
  */
 function wp_ajax_ajax_tag_search() {
-       if ( isset( $_GET['tax'] ) ) {
-               $taxonomy = sanitize_key( $_GET['tax'] );
-               $tax = get_taxonomy( $taxonomy );
-               if ( ! $tax )
-                       wp_die( 0 );
-               if ( ! current_user_can( $tax->cap->assign_terms ) )
-                       wp_die( -1 );
-       } else {
+       if ( ! isset( $_GET['tax'] ) ) {
                wp_die( 0 );
        }
 
+       $taxonomy = sanitize_key( $_GET['tax'] );
+       $tax = get_taxonomy( $taxonomy );
+       if ( ! $tax ) {
+               wp_die( 0 );
+       }
+
+       if ( ! current_user_can( $tax->cap->assign_terms ) ) {
+               wp_die( -1 );
+       }
+
        $s = wp_unslash( $_GET['q'] );
 
        $comma = _x( ',', 'tag delimiter' );
@@ -169,7 +172,7 @@ function wp_ajax_wp_compression_test() {
                header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' );
                header( 'Cache-Control: no-cache, must-revalidate, max-age=0' );
                header( 'Pragma: no-cache' );
-               header('Content-Type: application/x-javascript; charset=UTF-8');
+               header('Content-Type: application/javascript; charset=UTF-8');
                $force_gzip = ( defined('ENFORCE_GZIP') && ENFORCE_GZIP );
                $test_str = '"wpCompressionTest Lorem ipsum dolor sit amet consectetuer mollis sapien urna ut a. Eu nonummy condimentum fringilla tempor pretium platea vel nibh netus Maecenas. Hac molestie amet justo quis pellentesque est ultrices interdum nibh Morbi. Cras mattis pretium Phasellus ante ipsum ipsum ut sociis Suspendisse Lorem. Ante et non molestie. Porta urna Vestibulum egestas id congue nibh eu risus gravida sit. Ac augue auctor Ut et non a elit massa id sodales. Elit eu Nulla at nibh adipiscing mattis lacus mauris at tempus. Netus nibh quis suscipit nec feugiat eget sed lorem et urna. Pellentesque lacus at ut massa consectetuer ligula ut auctor semper Pellentesque. Ut metus massa nibh quam Curabitur molestie nec mauris congue. Volutpat molestie elit justo facilisis neque ac risus Ut nascetur tristique. Vitae sit lorem tellus et quis Phasellus lacus tincidunt nunc Fusce. Pharetra wisi Suspendisse mus sagittis libero lacinia Integer consequat ac Phasellus. Et urna ac cursus tortor aliquam Aliquam amet tellus volutpat Vestibulum. Justo interdum condimentum In augue congue tellus sollicitudin Quisque quis nibh."';
 
@@ -286,7 +289,7 @@ function wp_ajax_autocomplete_user() {
                );
        }
 
-       wp_die( json_encode( $return ) );
+       wp_die( wp_json_encode( $return ) );
 }
 
 /**
@@ -376,7 +379,7 @@ function _wp_ajax_delete_comment_response( $comment_id, $delta = -1 ) {
                // Here for completeness - not used.
                'id' => $comment_id,
                'supplemental' => array(
-                       'total_items_i18n' => sprintf( _n( '1 item', '%s items', $total ), number_format_i18n( $total ) ),
+                       'total_items_i18n' => sprintf( _n( '%s item', '%s items', $total ), number_format_i18n( $total ) ),
                        'total_pages' => ceil( $total / $per_page ),
                        'total_pages_i18n' => number_format_i18n( ceil( $total / $per_page ) ),
                        'total' => $total,
@@ -419,10 +422,11 @@ function _wp_ajax_add_hierarchical_term() {
                        continue;
                if ( !$cat_id = term_exists( $cat_name, $taxonomy->name, $parent ) )
                        $cat_id = wp_insert_term( $cat_name, $taxonomy->name, array( 'parent' => $parent ) );
-               if ( is_wp_error( $cat_id ) )
+               if ( is_wp_error( $cat_id ) ) {
                        continue;
-               else if ( is_array( $cat_id ) )
+               } elseif ( is_array( $cat_id ) ) {
                        $cat_id = $cat_id['term_id'];
+               }
                $checked_categories[] = $cat_id;
                if ( $parent ) // Do these all at once in a second
                        continue;
@@ -738,10 +742,11 @@ function wp_ajax_add_link_category( $action ) {
                        continue;
                if ( !$cat_id = term_exists( $cat_name, 'link_category' ) )
                        $cat_id = wp_insert_term( $cat_name, 'link_category' );
-               if ( is_wp_error( $cat_id ) )
+               if ( is_wp_error( $cat_id ) ) {
                        continue;
-               else if ( is_array( $cat_id ) )
+               } elseif ( is_array( $cat_id ) ) {
                        $cat_id = $cat_id['term_id'];
+               }
                $cat_name = esc_html( $cat_name );
                $x->add( array(
                        'what' => 'link-category',
@@ -788,7 +793,7 @@ function wp_ajax_add_tag() {
 
        $level = 0;
        if ( is_taxonomy_hierarchical($taxonomy) ) {
-               $level = count( get_ancestors( $tag->term_id, $taxonomy ) );
+               $level = count( get_ancestors( $tag->term_id, $taxonomy, 'taxonomy' ) );
                ob_start();
                $wp_list_table->single_row( $tag, $level );
                $noparents = ob_get_clean();
@@ -816,17 +821,20 @@ function wp_ajax_add_tag() {
  * @since 3.1.0
  */
 function wp_ajax_get_tagcloud() {
-       if ( isset( $_POST['tax'] ) ) {
-               $taxonomy = sanitize_key( $_POST['tax'] );
-               $tax = get_taxonomy( $taxonomy );
-               if ( ! $tax )
-                       wp_die( 0 );
-               if ( ! current_user_can( $tax->cap->assign_terms ) )
-                       wp_die( -1 );
-       } else {
+       if ( ! isset( $_POST['tax'] ) ) {
                wp_die( 0 );
        }
 
+       $taxonomy = sanitize_key( $_POST['tax'] );
+       $tax = get_taxonomy( $taxonomy );
+       if ( ! $tax ) {
+               wp_die( 0 );
+       }
+
+       if ( ! current_user_can( $tax->cap->assign_terms ) ) {
+               wp_die( -1 );
+       }
+
        $tags = get_terms( $taxonomy, array( 'number' => 45, 'orderby' => 'count', 'order' => 'DESC' ) );
 
        if ( empty( $tags ) )
@@ -935,7 +943,8 @@ function wp_ajax_replyto_comment( $action ) {
                $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']);
+               $comment_content      = trim( $_POST['content'] );
+               $comment_type         = isset( $_POST['comment_type'] ) ? trim( $_POST['comment_type'] ) : '';
                if ( current_user_can( 'unfiltered_html' ) ) {
                        if ( ! isset( $_POST['_wp_unfiltered_html_comment'] ) )
                                $_POST['_wp_unfiltered_html_comment'] = '';
@@ -1082,7 +1091,7 @@ function wp_ajax_add_menu_item() {
                        }
 
                        $_menu_items = array_map( 'wp_setup_nav_menu_item', array( $_object ) );
-                       $_menu_item = array_shift( $_menu_items );
+                       $_menu_item = reset( $_menu_items );
 
                        // Restore the missing menu item properties
                        $menu_item_data['menu-item-description'] = $_menu_item->description;
@@ -1141,6 +1150,8 @@ function wp_ajax_add_meta() {
                        wp_die( -1 );
                if ( isset($_POST['metakeyselect']) && '#NONE#' == $_POST['metakeyselect'] && empty($_POST['metakeyinput']) )
                        wp_die( 1 );
+
+               // If the post is an autodraft, save the post as a draft and then attempt to save the meta.
                if ( $post->post_status == 'auto-draft' ) {
                        $save_POST = $_POST; // Backup $_POST
                        $_POST = array(); // Make it empty for edit_post()
@@ -1165,7 +1176,7 @@ function wp_ajax_add_meta() {
                        } else {
                                wp_die( 0 );
                        }
-               } else if ( !$mid = add_meta( $pid ) ) {
+               } elseif ( ! $mid = add_meta( $pid ) ) {
                        wp_die( __( 'Please provide a custom field value.' ) );
                }
 
@@ -1364,7 +1375,7 @@ function wp_ajax_menu_get_metabox() {
 
                $markup = ob_get_clean();
 
-               echo json_encode(array(
+               echo wp_json_encode(array(
                        'replace-id' => $type . '-' . $item->name,
                        'markup' => $markup,
                ));
@@ -1393,7 +1404,7 @@ function wp_ajax_wp_link_ajax() {
        if ( ! isset( $results ) )
                wp_die( 0 );
 
-       echo json_encode( $results );
+       echo wp_json_encode( $results );
        echo "\n";
 
        wp_die();
@@ -1485,7 +1496,7 @@ function wp_ajax_sample_permalink() {
 }
 
 /**
- * Ajax handler for quick edit saving for a post.
+ * Ajax handler for Quick Edit saving a post from a list table.
  *
  * @since 3.1.0
  */
@@ -1529,16 +1540,29 @@ function wp_ajax_inline_save() {
                $data['parent_id'] = $data['post_parent'];
 
        // Status.
-       if ( isset($data['keep_private']) && 'private' == $data['keep_private'] )
+       if ( isset( $data['keep_private'] ) && 'private' == $data['keep_private'] ) {
+               $data['visibility']  = 'private';
                $data['post_status'] = 'private';
-       else
+       } else {
                $data['post_status'] = $data['_status'];
+       }
 
        if ( empty($data['comment_status']) )
                $data['comment_status'] = 'closed';
        if ( empty($data['ping_status']) )
                $data['ping_status'] = 'closed';
 
+       // Exclude terms from taxonomies that are not supposed to appear in Quick Edit.
+       if ( ! empty( $data['tax_input'] ) ) {
+               foreach ( $data['tax_input'] as $taxonomy => $terms ) {
+                       $tax_object = get_taxonomy( $taxonomy );
+                       /** This filter is documented in wp-admin/includes/class-wp-posts-list-table.php */
+                       if ( ! apply_filters( 'quick_edit_show_taxonomy', $tax_object->show_in_quick_edit, $taxonomy, $post['post_type'] ) ) {
+                               unset( $data['tax_input'][ $taxonomy ] );
+                       }
+               }
+       }
+
        // 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';
@@ -1616,7 +1640,9 @@ function wp_ajax_inline_save_tax() {
 }
 
 /**
- * Ajax handler for finding posts.
+ * Ajax handler for querying posts for the Find Posts modal.
+ *
+ * @see window.findPosts
  *
  * @since 3.1.0
  */
@@ -1769,7 +1795,7 @@ function wp_ajax_save_widget() {
                if ( !$multi_number )
                        wp_die( $error );
 
-               $_POST['widget-' . $id_base] = array( $multi_number => array_shift($settings) );
+               $_POST[ 'widget-' . $id_base ] = array( $multi_number => reset( $settings ) );
                $widget_id = $id_base . '-' . $multi_number;
                $sidebar[] = $widget_id;
        }
@@ -1821,14 +1847,37 @@ function wp_ajax_update_widget() {
  */
 function wp_ajax_upload_attachment() {
        check_ajax_referer( 'media-form' );
+       /*
+        * This function does not use wp_send_json_success() / wp_send_json_error()
+        * as the html4 Plupload handler requires a text/html content-type for older IE.
+        * See https://core.trac.wordpress.org/ticket/31037
+        */
+
+       if ( ! current_user_can( 'upload_files' ) ) {
+               echo wp_json_encode( array(
+                       'success' => false,
+                       'data'    => array(
+                               'message'  => __( "You don't have permission to upload files." ),
+                               'filename' => $_FILES['async-upload']['name'],
+                       )
+               ) );
 
-       if ( ! current_user_can( 'upload_files' ) )
                wp_die();
+       }
 
        if ( isset( $_REQUEST['post_id'] ) ) {
                $post_id = $_REQUEST['post_id'];
-               if ( ! current_user_can( 'edit_post', $post_id ) )
+               if ( ! current_user_can( 'edit_post', $post_id ) ) {
+                       echo wp_json_encode( array(
+                               'success' => false,
+                               'data'    => array(
+                                       'message'  => __( "You don't have permission to attach files to this post." ),
+                                       'filename' => $_FILES['async-upload']['name'],
+                               )
+                       ) );
+
                        wp_die();
+               }
        } else {
                $post_id = null;
        }
@@ -1837,9 +1886,9 @@ function wp_ajax_upload_attachment() {
 
        // If the context is custom header or background, make sure the uploaded file is an image.
        if ( isset( $post_data['context'] ) && in_array( $post_data['context'], array( 'custom-header', 'custom-background' ) ) ) {
-               $wp_filetype = wp_check_filetype_and_ext( $_FILES['async-upload']['tmp_name'], $_FILES['async-upload']['name'], false );
+               $wp_filetype = wp_check_filetype_and_ext( $_FILES['async-upload']['tmp_name'], $_FILES['async-upload']['name'] );
                if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) ) {
-                       echo json_encode( array(
+                       echo wp_json_encode( array(
                                'success' => false,
                                'data'    => array(
                                        'message'  => __( 'The uploaded file is not a valid image. Please try again.' ),
@@ -1854,7 +1903,7 @@ function wp_ajax_upload_attachment() {
        $attachment_id = media_handle_upload( 'async-upload', $post_id, $post_data );
 
        if ( is_wp_error( $attachment_id ) ) {
-               echo json_encode( array(
+               echo wp_json_encode( array(
                        'success' => false,
                        'data'    => array(
                                'message'  => $attachment_id->get_error_message(),
@@ -1876,7 +1925,7 @@ function wp_ajax_upload_attachment() {
        if ( ! $attachment = wp_prepare_attachment_for_js( $attachment_id ) )
                wp_die();
 
-       echo json_encode( array(
+       echo wp_json_encode( array(
                'success' => true,
                'data'    => $attachment,
        ) );
@@ -1901,7 +1950,7 @@ function wp_ajax_image_editor() {
        switch ( $_POST['do'] ) {
                case 'save' :
                        $msg = wp_save_image($attachment_id);
-                       $msg = json_encode($msg);
+                       $msg = wp_json_encode($msg);
                        wp_die( $msg );
                        break;
                case 'scale' :
@@ -2146,7 +2195,7 @@ function wp_ajax_get_attachment() {
 }
 
 /**
- * Ajax handler for querying for attachments.
+ * Ajax handler for querying attachments.
  *
  * @since 3.5.0
  */
@@ -2155,11 +2204,17 @@ function wp_ajax_query_attachments() {
                wp_send_json_error();
 
        $query = isset( $_REQUEST['query'] ) ? (array) $_REQUEST['query'] : array();
-       $query = array_intersect_key( $query, array_flip( array(
+       $keys = array(
                's', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type',
                'post_parent', 'post__in', 'post__not_in', 'year', 'monthnum'
-       ) ) );
+       );
+       foreach ( get_taxonomies_for_attachments( 'objects' ) as $t ) {
+               if ( $t->query_var && isset( $query[ $t->query_var ] ) ) {
+                       $keys[] = $t->query_var;
+               }
+       }
 
+       $query = array_intersect_key( $query, array_flip( $keys ) );
        $query['post_type'] = 'attachment';
        if ( MEDIA_TRASH
                && ! empty( $_REQUEST['query']['post_status'] )
@@ -2192,7 +2247,7 @@ function wp_ajax_query_attachments() {
 }
 
 /**
- * Ajax handler for saving attachment attributes.
+ * Ajax handler for updating attachment attributes.
  *
  * @since 3.5.0
  */
@@ -2214,6 +2269,9 @@ function wp_ajax_save_attachment() {
        if ( 'attachment' != $post['post_type'] )
                wp_send_json_error();
 
+       if ( isset( $changes['parent'] ) )
+               $post['post_parent'] = $changes['parent'];
+
        if ( isset( $changes['title'] ) )
                $post['post_title'] = $changes['title'];
 
@@ -2234,7 +2292,7 @@ function wp_ajax_save_attachment() {
                }
        }
 
-       if ( 0 === strpos( $post['post_mime_type'], 'audio/' ) ) {
+       if ( wp_attachment_is( 'audio', $post['ID'] ) ) {
                $changed = false;
                $id3data = wp_get_attachment_metadata( $post['ID'] );
                if ( ! is_array( $id3data ) ) {
@@ -2389,10 +2447,16 @@ function wp_ajax_send_attachment_to_editor() {
                $align = isset( $attachment['align'] ) ? $attachment['align'] : 'none';
                $size = isset( $attachment['image-size'] ) ? $attachment['image-size'] : 'medium';
                $alt = isset( $attachment['image_alt'] ) ? $attachment['image_alt'] : '';
+
+               // No whitespace-only captions.
                $caption = isset( $attachment['post_excerpt'] ) ? $attachment['post_excerpt'] : '';
+               if ( '' === trim( $caption ) ) {
+                       $caption = '';
+               }
+
                $title = ''; // We no longer insert title tags into <img> 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 )  ) {
+       } elseif ( wp_attachment_is( 'video', $post ) || wp_attachment_is( 'audio', $post )  ) {
                $html = stripslashes_deep( $_POST['html'] );
        }
 
@@ -2428,8 +2492,8 @@ function wp_ajax_send_link_to_editor() {
        if ( ! $src = esc_url_raw( $src ) )
                wp_send_json_error();
 
-       if ( ! $title = trim( wp_unslash( $_POST['title'] ) ) )
-               $title = wp_basename( $src );
+       if ( ! $link_text = trim( wp_unslash( $_POST['link_text'] ) ) )
+               $link_text = wp_basename( $src );
 
        $post = get_post( isset( $_POST['post_id'] ) ? $_POST['post_id'] : 0 );
 
@@ -2442,8 +2506,8 @@ function wp_ajax_send_link_to_editor() {
        if ( $check_embed !== $fallback ) {
                // TinyMCE view for [embed] will parse this
                $html = '[embed]' . $src . '[/embed]';
-       } elseif ( $title ) {
-               $html = '<a href="' . esc_url( $src ) . '">' . $title . '</a>';
+       } elseif ( $link_text ) {
+               $html = '<a href="' . esc_url( $src ) . '">' . $link_text . '</a>';
        } else {
                $html = '';
        }
@@ -2455,7 +2519,7 @@ function wp_ajax_send_link_to_editor() {
                        $type = $ext_type;
 
        /** This filter is documented in wp-admin/includes/media.php */
-       $html = apply_filters( $type . '_send_to_editor_url', $html, $src, $title );
+       $html = apply_filters( $type . '_send_to_editor_url', $html, $src, $link_text );
 
        wp_send_json_success( $html );
 }
@@ -2577,8 +2641,13 @@ function wp_ajax_save_user_color_scheme() {
                wp_send_json_error();
        }
 
+       $previous_color_scheme = get_user_meta( get_current_user_id(), 'admin_color', true );
        update_user_meta( get_current_user_id(), 'admin_color', $color_scheme );
-       wp_send_json_success();
+
+       wp_send_json_success( array(
+               'previousScheme' => 'admin-color-' . $previous_color_scheme,
+               'currentScheme'  => 'admin-color-' . $color_scheme
+       ) );
 }
 
 /**
@@ -2648,6 +2717,7 @@ function wp_ajax_parse_embed() {
 
        $shortcode = wp_unslash( $_POST['shortcode'] );
        $url = str_replace( '[embed]', '', str_replace( '[/embed]', '', $shortcode ) );
+
        $parsed = false;
        setup_postdata( $post );
 
@@ -2701,7 +2771,7 @@ function wp_ajax_parse_embed() {
                // Admin is ssl and the embed is not. Iframes, scripts, and other "active content" will be blocked.
                wp_send_json_error( array(
                        'type' => 'not-ssl',
-                       'message' => sprintf( __( 'Preview not available. %s cannot be embedded securely.' ), '<code>' . esc_html( $url ) . '</code>' ),
+                       'message' => __( 'This preview is unavailable in the editor.' ),
                ) );
        }
 
@@ -2713,18 +2783,28 @@ function wp_ajax_parse_embed() {
 function wp_ajax_parse_media_shortcode() {
        global $post, $wp_scripts;
 
-       if ( ! $post = get_post( (int) $_POST['post_ID'] ) ) {
+       if ( empty( $_POST['shortcode'] ) ) {
                wp_send_json_error();
        }
 
-       if ( empty( $_POST['shortcode'] ) || ! current_user_can( 'edit_post', $post->ID ) ) {
-               wp_send_json_error();
+       $shortcode = wp_unslash( $_POST['shortcode'] );
+
+       if ( ! empty( $_POST['post_ID'] ) ) {
+               $post = get_post( (int) $_POST['post_ID'] );
        }
 
-       setup_postdata( $post );
-       $shortcode = do_shortcode( wp_unslash( $_POST['shortcode'] ) );
+       // the embed shortcode requires a post
+       if ( ! $post || ! current_user_can( 'edit_post', $post->ID ) ) {
+               if ( 'embed' === $shortcode ) {
+                       wp_send_json_error();
+               }
+       } else {
+               setup_postdata( $post );
+       }
+
+       $parsed = do_shortcode( $shortcode  );
 
-       if ( empty( $shortcode ) ) {
+       if ( empty( $parsed ) ) {
                wp_send_json_error( array(
                        'type' => 'no-items',
                        'message' => __( 'No items found.' ),
@@ -2744,14 +2824,14 @@ function wp_ajax_parse_media_shortcode() {
 
        ob_start();
 
-       echo $shortcode;
+       echo $parsed;
 
        if ( 'playlist' === $_REQUEST['type'] ) {
                wp_underscore_playlist_templates();
 
                wp_print_scripts( 'wp-playlist' );
        } else {
-               wp_print_scripts( 'wp-mediaelement' );
+               wp_print_scripts( array( 'froogaloop', 'wp-mediaelement' ) );
        }
 
        wp_send_json_success( array(
@@ -2759,3 +2839,151 @@ function wp_ajax_parse_media_shortcode() {
                'body' => ob_get_clean()
        ) );
 }
+
+/**
+ * AJAX handler for destroying multiple open sessions for a user.
+ *
+ * @since 4.1.0
+ */
+function wp_ajax_destroy_sessions() {
+
+       $user = get_userdata( (int) $_POST['user_id'] );
+       if ( $user ) {
+               if ( ! current_user_can( 'edit_user', $user->ID ) ) {
+                       $user = false;
+               } elseif ( ! wp_verify_nonce( $_POST['nonce'], 'update-user_' . $user->ID ) ) {
+                       $user = false;
+               }
+       }
+
+       if ( ! $user ) {
+               wp_send_json_error( array(
+                       'message' => __( 'Could not log out user sessions. Please try again.' ),
+               ) );
+       }
+
+       $sessions = WP_Session_Tokens::get_instance( $user->ID );
+
+       if ( $user->ID === get_current_user_id() ) {
+               $sessions->destroy_others( wp_get_session_token() );
+               $message = __( 'You are now logged out everywhere else.' );
+       } else {
+               $sessions->destroy_all();
+               /* translators: 1: User's display name. */
+               $message = sprintf( __( '%s has been logged out.' ), $user->display_name );
+       }
+
+       wp_send_json_success( array( 'message' => $message ) );
+}
+
+
+/**
+ * AJAX handler for updating a plugin.
+ *
+ * @since 4.2.0
+ *
+ * @see Plugin_Upgrader
+ */
+function wp_ajax_update_plugin() {
+       global $wp_filesystem;
+
+       $plugin = urldecode( $_POST['plugin'] );
+
+       $status = array(
+               'update'     => 'plugin',
+               'plugin'     => $plugin,
+               'slug'       => sanitize_key( $_POST['slug'] ),
+               'oldVersion' => '',
+               'newVersion' => '',
+       );
+
+       $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
+       if ( $plugin_data['Version'] ) {
+               $status['oldVersion'] = sprintf( __( 'Version %s' ), $plugin_data['Version'] );
+       }
+
+       if ( ! current_user_can( 'update_plugins' ) ) {
+               $status['error'] = __( 'You do not have sufficient permissions to update plugins on this site.' );
+               wp_send_json_error( $status );
+       }
+
+       check_ajax_referer( 'updates' );
+
+       include_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' );
+
+       wp_update_plugins();
+
+       $skin = new Automatic_Upgrader_Skin();
+       $upgrader = new Plugin_Upgrader( $skin );
+       $result = $upgrader->bulk_upgrade( array( $plugin ) );
+
+       if ( is_array( $result ) && empty( $result[$plugin] ) && is_wp_error( $skin->result ) ) {
+               $result = $skin->result;
+       }
+
+       if ( is_array( $result ) && !empty( $result[ $plugin ] ) ) {
+               $plugin_update_data = current( $result );
+
+               /*
+                * If the `update_plugins` site transient is empty (e.g. when you update
+                * two plugins in quick succession before the transient repopulates),
+                * this may be the return.
+                *
+                * Preferably something can be done to ensure `update_plugins` isn't empty.
+                * For now, surface some sort of error here.
+                */
+               if ( $plugin_update_data === true ) {
+                       wp_send_json_error( $status );
+               }
+
+               $plugin_data = get_plugins( '/' . $result[ $plugin ]['destination_name'] );
+               $plugin_data = reset( $plugin_data );
+
+               if ( $plugin_data['Version'] ) {
+                       $status['newVersion'] = sprintf( __( 'Version %s' ), $plugin_data['Version'] );
+               }
+
+               wp_send_json_success( $status );
+       } else if ( is_wp_error( $result ) ) {
+               $status['error'] = $result->get_error_message();
+               wp_send_json_error( $status );
+
+       } else if ( is_bool( $result ) && ! $result ) {
+               $status['errorCode'] = 'unable_to_connect_to_filesystem';
+               $status['error'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.' );
+
+               // Pass through the error from WP_Filesystem if one was raised
+               if ( is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) {
+                       $status['error'] = $wp_filesystem->errors->get_error_message();
+               }
+
+               wp_send_json_error( $status );
+
+       }
+}
+
+/**
+ * AJAX handler for saving a post from Press This.
+ *
+ * @since 4.2.0
+ */
+function wp_ajax_press_this_save_post() {
+       if ( empty( $GLOBALS['wp_press_this'] ) ) {
+               include( ABSPATH . 'wp-admin/includes/class-wp-press-this.php' );
+       }
+
+       $GLOBALS['wp_press_this']->save_post();
+}
+
+/**
+ * AJAX handler for creating new category from Press This.
+ *
+ * @since 4.2.0
+ */
+function wp_ajax_press_this_add_category() {
+       if ( empty( $GLOBALS['wp_press_this'] ) ) {
+               include( ABSPATH . 'wp-admin/includes/class-wp-press-this.php' );
+       }
+
+       $GLOBALS['wp_press_this']->add_category();
+}