X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/af50974463450c98503e763a7836a50e260461a9..refs/tags/wordpress-4.2.3:/wp-admin/js/updates.js diff --git a/wp-admin/js/updates.js b/wp-admin/js/updates.js index b03e4ba8..2ddfc85d 100644 --- a/wp-admin/js/updates.js +++ b/wp-admin/js/updates.js @@ -1,47 +1,529 @@ +/* global tb_remove */ window.wp = window.wp || {}; -(function( $, wp ) { - +(function( $, wp, pagenow ) { wp.updates = {}; /** - * Decrement update counts throughout the various menus + * User nonce for ajax calls. + * + * @since 4.2.0 + * + * @var string + */ + wp.updates.ajaxNonce = window._wpUpdatesSettings.ajax_nonce; + + /** + * Localized strings. + * + * @since 4.2.0 + * + * @var object + */ + wp.updates.l10n = window._wpUpdatesSettings.l10n; + + /** + * Whether filesystem credentials need to be requested from the user. + * + * @since 4.2.0 + * + * @var bool + */ + wp.updates.shouldRequestFilesystemCredentials = null; + + /** + * Filesystem credentials to be packaged along with the request. + * + * @since 4.2.0 + * + * @var object + */ + wp.updates.filesystemCredentials = { + ftp: { + host: null, + username: null, + password: null, + connectionType: null + }, + ssh: { + publicKey: null, + privateKey: null + } + }; + + /** + * Flag if we're waiting for an update to complete. + * + * @since 4.2.0 + * + * @var bool + */ + wp.updates.updateLock = false; + + /** + * * Flag if we've done an update successfully. + * + * @since 4.2.0 + * + * @var bool + */ + wp.updates.updateDoneSuccessfully = false; + + /** + * If the user tries to update a plugin while an update is + * already happening, it can be placed in this queue to perform later. + * + * @since 4.2.0 + * + * @var array + */ + wp.updates.updateQueue = []; + + /** + * Store a jQuery reference to return focus to when exiting the request credentials modal. + * + * @since 4.2.0 + * + * @var jQuery object + */ + wp.updates.$elToReturnFocusToFromCredentialsModal = null; + + /** + * Decrement update counts throughout the various menus. + * + * @since 3.9.0 * * @param {string} updateType */ wp.updates.decrementCount = function( upgradeType ) { - var count, pluginCount, $elem; + var count, + pluginCount, + $adminBarUpdateCount = $( '#wp-admin-bar-updates .ab-label' ), + $dashboardNavMenuUpdateCount = $( 'a[href="update-core.php"] .update-plugins' ), + $pluginsMenuItem = $( '#menu-plugins' ); + - $elem = $( '#wp-admin-bar-updates .ab-label' ); - count = $elem.text(); + count = $adminBarUpdateCount.text(); count = parseInt( count, 10 ) - 1; if ( count < 0 || isNaN( count ) ) { return; } $( '#wp-admin-bar-updates .ab-item' ).removeAttr( 'title' ); - $elem.text( count ); + $adminBarUpdateCount.text( count ); + - $elem = $( 'a[href="update-core.php"] .update-plugins' ); - $elem.each( function( index, elem ) { + $dashboardNavMenuUpdateCount.each( function( index, elem ) { elem.className = elem.className.replace( /count-\d+/, 'count-' + count ); } ); - $elem.removeAttr( 'title' ); - $elem.find( '.update-count' ).text( count ); + $dashboardNavMenuUpdateCount.removeAttr( 'title' ); + $dashboardNavMenuUpdateCount.find( '.update-count' ).text( count ); if ( 'plugin' === upgradeType ) { - $elem = $( '#menu-plugins' ); - pluginCount = $elem.find( '.plugin-count' ).eq(0).text(); + pluginCount = $pluginsMenuItem.find( '.plugin-count' ).eq(0).text(); pluginCount = parseInt( pluginCount, 10 ) - 1; if ( pluginCount < 0 || isNaN( pluginCount ) ) { return; } - $elem.find( '.plugin-count' ).text( pluginCount ); - $elem.find( '.update-plugins' ).each( function( index, elem ) { + $pluginsMenuItem.find( '.plugin-count' ).text( pluginCount ); + $pluginsMenuItem.find( '.update-plugins' ).each( function( index, elem ) { elem.className = elem.className.replace( /count-\d+/, 'count-' + pluginCount ); } ); + + if (pluginCount > 0 ) { + $( '.subsubsub .upgrade .count' ).text( '(' + pluginCount + ')' ); + } else { + $( '.subsubsub .upgrade' ).remove(); + } } }; + /** + * Send an Ajax request to the server to update a plugin. + * + * @since 4.2.0 + * + * @param {string} plugin + * @param {string} slug + */ + wp.updates.updatePlugin = function( plugin, slug ) { + var $message, name; + if ( 'plugins' === pagenow || 'plugins-network' === pagenow ) { + $message = $( '[data-slug="' + slug + '"]' ).next().find( '.update-message' ); + } else if ( 'plugin-install' === pagenow ) { + $message = $( '.plugin-card-' + slug ).find( '.update-now' ); + name = $message.data( 'name' ); + $message.attr( 'aria-label', wp.updates.l10n.updatingLabel.replace( '%s', name ) ); + } + + $message.addClass( 'updating-message' ); + if ( $message.html() !== wp.updates.l10n.updating ){ + $message.data( 'originaltext', $message.html() ); + } + + $message.text( wp.updates.l10n.updating ); + wp.a11y.speak( wp.updates.l10n.updatingMsg ); + + if ( wp.updates.updateLock ) { + wp.updates.updateQueue.push( { + type: 'update-plugin', + data: { + plugin: plugin, + slug: slug + } + } ); + return; + } + + wp.updates.updateLock = true; + + var data = { + _ajax_nonce: wp.updates.ajaxNonce, + plugin: plugin, + slug: slug, + username: wp.updates.filesystemCredentials.ftp.username, + password: wp.updates.filesystemCredentials.ftp.password, + hostname: wp.updates.filesystemCredentials.ftp.hostname, + connection_type: wp.updates.filesystemCredentials.ftp.connectionType, + public_key: wp.updates.filesystemCredentials.ssh.publicKey, + private_key: wp.updates.filesystemCredentials.ssh.privateKey + }; + + wp.ajax.post( 'update-plugin', data ) + .done( wp.updates.updateSuccess ) + .fail( wp.updates.updateError ); + }; + + /** + * On a successful plugin update, update the UI with the result. + * + * @since 4.2.0 + * + * @param {object} response + */ + wp.updates.updateSuccess = function( response ) { + var $updateMessage, name, $pluginRow, newText; + if ( 'plugins' === pagenow || 'plugins-network' === pagenow ) { + $pluginRow = $( '[data-slug="' + response.slug + '"]' ).first(); + $updateMessage = $pluginRow.next().find( '.update-message' ); + $pluginRow.addClass( 'updated' ).removeClass( 'update' ); + + // Update the version number in the row. + newText = $pluginRow.find('.plugin-version-author-uri').html().replace( response.oldVersion, response.newVersion ); + $pluginRow.find('.plugin-version-author-uri').html( newText ); + + // Add updated class to update message parent tr + $pluginRow.next().addClass( 'updated' ); + } else if ( 'plugin-install' === pagenow ) { + $updateMessage = $( '.plugin-card-' + response.slug ).find( '.update-now' ); + $updateMessage.addClass( 'button-disabled' ); + name = $updateMessage.data( 'name' ); + $updateMessage.attr( 'aria-label', wp.updates.l10n.updatedLabel.replace( '%s', name ) ); + } + + $updateMessage.removeClass( 'updating-message' ).addClass( 'updated-message' ); + $updateMessage.text( wp.updates.l10n.updated ); + wp.a11y.speak( wp.updates.l10n.updatedMsg ); + + wp.updates.decrementCount( 'plugin' ); + + wp.updates.updateDoneSuccessfully = true; + + /* + * The lock can be released since the update was successful, + * and any other updates can commence. + */ + wp.updates.updateLock = false; + + $(document).trigger( 'wp-plugin-update-success', response ); + + wp.updates.queueChecker(); + }; + + + /** + * On a plugin update error, update the UI appropriately. + * + * @since 4.2.0 + * + * @param {object} response + */ + wp.updates.updateError = function( response ) { + var $message, name; + wp.updates.updateDoneSuccessfully = false; + if ( response.errorCode && response.errorCode == 'unable_to_connect_to_filesystem' && wp.updates.shouldRequestFilesystemCredentials ) { + wp.updates.credentialError( response, 'update-plugin' ); + return; + } + if ( 'plugins' === pagenow || 'plugins-network' === pagenow ) { + $message = $( '[data-slug="' + response.slug + '"]' ).next().find( '.update-message' ); + } else if ( 'plugin-install' === pagenow ) { + $message = $( '.plugin-card-' + response.slug ).find( '.update-now' ); + + name = $message.data( 'name' ); + $message.attr( 'aria-label', wp.updates.l10n.updateFailedLabel.replace( '%s', name ) ); + } + $message.removeClass( 'updating-message' ); + $message.html( wp.updates.l10n.updateFailed + ': ' + response.error ); + wp.a11y.speak( wp.updates.l10n.updateFailed ); + + /* + * The lock can be released since this failure was + * after the credentials form. + */ + wp.updates.updateLock = false; + + $(document).trigger( 'wp-plugin-update-error', response ); + + wp.updates.queueChecker(); + }; + + /** + * Show an error message in the request for credentials form. + * + * @param {string} message + * @since 4.2.0 + */ + wp.updates.showErrorInCredentialsForm = function( message ) { + var $modal = $( '.notification-dialog' ); + + // Remove any existing error. + $modal.find( '.error' ).remove(); + + $modal.find( 'h3' ).after( '