window.wp = window.wp || {}; (function( $, wp, pagenow ) { wp.updates = {}; /** * 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, $adminBarUpdateCount = $( '#wp-admin-bar-updates .ab-label' ), $dashboardNavMenuUpdateCount = $( 'a[href="update-core.php"] .update-plugins' ), $pluginsMenuItem = $( '#menu-plugins' ); count = $adminBarUpdateCount.text(); count = parseInt( count, 10 ) - 1; if ( count < 0 || isNaN( count ) ) { return; } $( '#wp-admin-bar-updates .ab-item' ).removeAttr( 'title' ); $adminBarUpdateCount.text( count ); $dashboardNavMenuUpdateCount.each( function( index, elem ) { elem.className = elem.className.replace( /count-\d+/, 'count-' + count ); } ); $dashboardNavMenuUpdateCount.removeAttr( 'title' ); $dashboardNavMenuUpdateCount.find( '.update-count' ).text( count ); if ( 'plugin' === upgradeType ) { pluginCount = $pluginsMenuItem.find( '.plugin-count' ).eq(0).text(); pluginCount = parseInt( pluginCount, 10 ) - 1; if ( pluginCount < 0 || isNaN( pluginCount ) ) { return; } $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; 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.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.text( wp.updates.l10n.updateFailed ); wp.a11y.speak( wp.updates.l10n.updateFailed ); }; /** * 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( '