+
+/**
+ * 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() {
+ $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' );
+
+ $current = get_site_transient( 'update_plugins' );
+ if ( empty( $current ) ) {
+ wp_update_plugins();
+ }
+
+ $upgrader = new Plugin_Upgrader( new Automatic_Upgrader_Skin() );
+ $result = $upgrader->bulk_upgrade( array( $plugin ) );
+
+ if ( is_array( $result ) ) {
+ $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_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
+
+ 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.' );
+ 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();
+}