2 window.wp = window.wp || {};
4 (function( $, wp, pagenow ) {
8 * User nonce for ajax calls.
14 wp.updates.ajaxNonce = window._wpUpdatesSettings.ajax_nonce;
23 wp.updates.l10n = window._wpUpdatesSettings.l10n;
26 * Whether filesystem credentials need to be requested from the user.
32 wp.updates.shouldRequestFilesystemCredentials = null;
35 * Filesystem credentials to be packaged along with the request.
41 wp.updates.filesystemCredentials = {
55 * Flag if we're waiting for an update to complete.
61 wp.updates.updateLock = false;
64 * * Flag if we've done an update successfully.
70 wp.updates.updateDoneSuccessfully = false;
73 * If the user tries to update a plugin while an update is
74 * already happening, it can be placed in this queue to perform later.
80 wp.updates.updateQueue = [];
83 * Store a jQuery reference to return focus to when exiting the request credentials modal.
89 wp.updates.$elToReturnFocusToFromCredentialsModal = null;
92 * Decrement update counts throughout the various menus.
96 * @param {string} upgradeType
98 wp.updates.decrementCount = function( upgradeType ) {
101 $adminBarUpdateCount = $( '#wp-admin-bar-updates .ab-label' ),
102 $dashboardNavMenuUpdateCount = $( 'a[href="update-core.php"] .update-plugins' ),
103 $pluginsMenuItem = $( '#menu-plugins' );
106 count = $adminBarUpdateCount.text();
107 count = parseInt( count, 10 ) - 1;
108 if ( count < 0 || isNaN( count ) ) {
111 $( '#wp-admin-bar-updates .ab-item' ).removeAttr( 'title' );
112 $adminBarUpdateCount.text( count );
115 $dashboardNavMenuUpdateCount.each( function( index, elem ) {
116 elem.className = elem.className.replace( /count-\d+/, 'count-' + count );
118 $dashboardNavMenuUpdateCount.removeAttr( 'title' );
119 $dashboardNavMenuUpdateCount.find( '.update-count' ).text( count );
121 if ( 'plugin' === upgradeType ) {
122 pluginCount = $pluginsMenuItem.find( '.plugin-count' ).eq(0).text();
123 pluginCount = parseInt( pluginCount, 10 ) - 1;
124 if ( pluginCount < 0 || isNaN( pluginCount ) ) {
127 $pluginsMenuItem.find( '.plugin-count' ).text( pluginCount );
128 $pluginsMenuItem.find( '.update-plugins' ).each( function( index, elem ) {
129 elem.className = elem.className.replace( /count-\d+/, 'count-' + pluginCount );
132 if (pluginCount > 0 ) {
133 $( '.subsubsub .upgrade .count' ).text( '(' + pluginCount + ')' );
135 $( '.subsubsub .upgrade' ).remove();
141 * Send an Ajax request to the server to update a plugin.
145 * @param {string} plugin
146 * @param {string} slug
148 wp.updates.updatePlugin = function( plugin, slug ) {
150 $card = $( '.plugin-card-' + slug );
152 if ( 'plugins' === pagenow || 'plugins-network' === pagenow ) {
153 $message = $( '[data-slug="' + slug + '"]' ).next().find( '.update-message' );
154 } else if ( 'plugin-install' === pagenow ) {
155 $message = $card.find( '.update-now' );
156 name = $message.data( 'name' );
157 $message.attr( 'aria-label', wp.updates.l10n.updatingLabel.replace( '%s', name ) );
158 // Remove previous error messages, if any.
159 $card.removeClass( 'plugin-card-update-failed' ).find( '.notice.notice-error' ).remove();
162 $message.addClass( 'updating-message' );
163 if ( $message.html() !== wp.updates.l10n.updating ){
164 $message.data( 'originaltext', $message.html() );
167 $message.text( wp.updates.l10n.updating );
168 wp.a11y.speak( wp.updates.l10n.updatingMsg );
170 if ( wp.updates.updateLock ) {
171 wp.updates.updateQueue.push( {
172 type: 'update-plugin',
181 wp.updates.updateLock = true;
184 _ajax_nonce: wp.updates.ajaxNonce,
187 username: wp.updates.filesystemCredentials.ftp.username,
188 password: wp.updates.filesystemCredentials.ftp.password,
189 hostname: wp.updates.filesystemCredentials.ftp.hostname,
190 connection_type: wp.updates.filesystemCredentials.ftp.connectionType,
191 public_key: wp.updates.filesystemCredentials.ssh.publicKey,
192 private_key: wp.updates.filesystemCredentials.ssh.privateKey
195 wp.ajax.post( 'update-plugin', data )
196 .done( wp.updates.updateSuccess )
197 .fail( wp.updates.updateError );
201 * On a successful plugin update, update the UI with the result.
205 * @param {object} response
207 wp.updates.updateSuccess = function( response ) {
208 var $updateMessage, name, $pluginRow, newText;
209 if ( 'plugins' === pagenow || 'plugins-network' === pagenow ) {
210 $pluginRow = $( '[data-slug="' + response.slug + '"]' ).first();
211 $updateMessage = $pluginRow.next().find( '.update-message' );
212 $pluginRow.addClass( 'updated' ).removeClass( 'update' );
214 // Update the version number in the row.
215 newText = $pluginRow.find('.plugin-version-author-uri').html().replace( response.oldVersion, response.newVersion );
216 $pluginRow.find('.plugin-version-author-uri').html( newText );
218 // Add updated class to update message parent tr
219 $pluginRow.next().addClass( 'updated' );
220 } else if ( 'plugin-install' === pagenow ) {
221 $updateMessage = $( '.plugin-card-' + response.slug ).find( '.update-now' );
222 $updateMessage.addClass( 'button-disabled' );
223 name = $updateMessage.data( 'name' );
224 $updateMessage.attr( 'aria-label', wp.updates.l10n.updatedLabel.replace( '%s', name ) );
227 $updateMessage.removeClass( 'updating-message' ).addClass( 'updated-message' );
228 $updateMessage.text( wp.updates.l10n.updated );
229 wp.a11y.speak( wp.updates.l10n.updatedMsg );
231 wp.updates.decrementCount( 'plugin' );
233 wp.updates.updateDoneSuccessfully = true;
236 * The lock can be released since the update was successful,
237 * and any other updates can commence.
239 wp.updates.updateLock = false;
241 $(document).trigger( 'wp-plugin-update-success', response );
243 wp.updates.queueChecker();
248 * On a plugin update error, update the UI appropriately.
252 * @param {object} response
254 wp.updates.updateError = function( response ) {
255 var $card = $( '.plugin-card-' + response.slug ),
261 wp.updates.updateDoneSuccessfully = false;
263 if ( response.errorCode && response.errorCode == 'unable_to_connect_to_filesystem' && wp.updates.shouldRequestFilesystemCredentials ) {
264 wp.updates.credentialError( response, 'update-plugin' );
268 error_message = wp.updates.l10n.updateFailed.replace( '%s', response.error );
270 if ( 'plugins' === pagenow || 'plugins-network' === pagenow ) {
271 $message = $( '[data-slug="' + response.slug + '"]' ).next().find( '.update-message' );
272 $message.html( error_message ).removeClass( 'updating-message' );
273 } else if ( 'plugin-install' === pagenow ) {
274 $button = $card.find( '.update-now' );
275 name = $button.data( 'name' );
278 .addClass( 'plugin-card-update-failed' )
279 .append( '<div class="notice notice-error is-dismissible"><p>' + error_message + '</p></div>' );
282 .attr( 'aria-label', wp.updates.l10n.updateFailedLabel.replace( '%s', name ) )
283 .html( wp.updates.l10n.updateFailedShort ).removeClass( 'updating-message' );
285 $card.on( 'click', '.notice.is-dismissible .notice-dismiss', function() {
286 // Use same delay as the total duration of the notice fadeTo + slideUp animation.
287 setTimeout( function() {
289 .removeClass( 'plugin-card-update-failed' )
290 .find( '.column-name a' ).focus();
295 wp.a11y.speak( error_message, 'assertive' );
298 * The lock can be released since this failure was
299 * after the credentials form.
301 wp.updates.updateLock = false;
303 $(document).trigger( 'wp-plugin-update-error', response );
305 wp.updates.queueChecker();
309 * Show an error message in the request for credentials form.
311 * @param {string} message
314 wp.updates.showErrorInCredentialsForm = function( message ) {
315 var $modal = $( '.notification-dialog' );
317 // Remove any existing error.
318 $modal.find( '.error' ).remove();
320 $modal.find( 'h3' ).after( '<div class="error">' + message + '</div>' );
324 * Events that need to happen when there is a credential error
328 wp.updates.credentialError = function( response, type ) {
329 wp.updates.updateQueue.push( {
332 // Not cool that we're depending on response for this data.
333 // This would feel more whole in a view all tied together.
334 plugin: response.plugin,
338 wp.updates.showErrorInCredentialsForm( response.error );
339 wp.updates.requestFilesystemCredentials();
343 * If an update job has been placed in the queue, queueChecker pulls it out and runs it.
347 wp.updates.queueChecker = function() {
348 if ( wp.updates.updateLock || wp.updates.updateQueue.length <= 0 ) {
352 var job = wp.updates.updateQueue.shift();
354 wp.updates.updatePlugin( job.data.plugin, job.data.slug );
359 * Request the users filesystem credentials if we don't have them already.
363 wp.updates.requestFilesystemCredentials = function( event ) {
364 if ( wp.updates.updateDoneSuccessfully === false ) {
366 * For the plugin install screen, return the focus to the install button
367 * after exiting the credentials request modal.
369 if ( 'plugin-install' === pagenow && event ) {
370 wp.updates.$elToReturnFocusToFromCredentialsModal = $( event.target );
373 wp.updates.updateLock = true;
375 wp.updates.requestForCredentialsModalOpen();
380 * Keydown handler for the request for credentials modal.
382 * Close the modal when the escape key is pressed.
383 * Constrain keyboard navigation to inside the modal.
387 wp.updates.keydown = function( event ) {
388 if ( 27 === event.keyCode ) {
389 wp.updates.requestForCredentialsModalCancel();
390 } else if ( 9 === event.keyCode ) {
391 // #upgrade button must always be the last focusable element in the dialog.
392 if ( event.target.id === 'upgrade' && ! event.shiftKey ) {
393 $( '#hostname' ).focus();
394 event.preventDefault();
395 } else if ( event.target.id === 'hostname' && event.shiftKey ) {
396 $( '#upgrade' ).focus();
397 event.preventDefault();
403 * Open the request for credentials modal.
407 wp.updates.requestForCredentialsModalOpen = function() {
408 var $modal = $( '#request-filesystem-credentials-dialog' );
409 $( 'body' ).addClass( 'modal-open' );
412 $modal.find( 'input:enabled:first' ).focus();
413 $modal.keydown( wp.updates.keydown );
417 * Close the request for credentials modal.
421 wp.updates.requestForCredentialsModalClose = function() {
422 $( '#request-filesystem-credentials-dialog' ).hide();
423 $( 'body' ).removeClass( 'modal-open' );
424 wp.updates.$elToReturnFocusToFromCredentialsModal.focus();
428 * The steps that need to happen when the modal is canceled out
432 wp.updates.requestForCredentialsModalCancel = function() {
433 // no updateLock and no updateQueue means we already have cleared things up
436 if( wp.updates.updateLock === false && wp.updates.updateQueue.length === 0 ){
440 slug = wp.updates.updateQueue[0].data.slug,
442 // remove the lock, and clear the queue
443 wp.updates.updateLock = false;
444 wp.updates.updateQueue = [];
446 wp.updates.requestForCredentialsModalClose();
447 if ( 'plugins' === pagenow || 'plugins-network' === pagenow ) {
448 $message = $( '[data-slug="' + slug + '"]' ).next().find( '.update-message' );
449 } else if ( 'plugin-install' === pagenow ) {
450 $message = $( '.plugin-card-' + slug ).find( '.update-now' );
453 $message.removeClass( 'updating-message' );
454 $message.html( $message.data( 'originaltext' ) );
455 wp.a11y.speak( wp.updates.l10n.updateCancel );
458 * Potentially add an AYS to a user attempting to leave the page
460 * If an update is on-going and a user attempts to leave the page,
461 * open an "Are you sure?" alert.
466 wp.updates.beforeunload = function() {
467 if ( wp.updates.updateLock ) {
468 return wp.updates.l10n.beforeunload;
473 $( document ).ready( function() {
475 * Check whether a user needs to submit filesystem credentials based on whether
476 * the form was output on the page server-side.
478 * @see {wp_print_request_filesystem_credentials_modal() in PHP}
480 wp.updates.shouldRequestFilesystemCredentials = ( $( '#request-filesystem-credentials-dialog' ).length <= 0 ) ? false : true;
482 // File system credentials form submit noop-er / handler.
483 $( '#request-filesystem-credentials-dialog form' ).on( 'submit', function() {
484 // Persist the credentials input by the user for the duration of the page load.
485 wp.updates.filesystemCredentials.ftp.hostname = $('#hostname').val();
486 wp.updates.filesystemCredentials.ftp.username = $('#username').val();
487 wp.updates.filesystemCredentials.ftp.password = $('#password').val();
488 wp.updates.filesystemCredentials.ftp.connectionType = $('input[name="connection_type"]:checked').val();
489 wp.updates.filesystemCredentials.ssh.publicKey = $('#public_key').val();
490 wp.updates.filesystemCredentials.ssh.privateKey = $('#private_key').val();
492 wp.updates.requestForCredentialsModalClose();
494 // Unlock and invoke the queue.
495 wp.updates.updateLock = false;
496 wp.updates.queueChecker();
501 // Close the request credentials modal when
502 $( '#request-filesystem-credentials-dialog [data-js-action="close"], .notification-dialog-background' ).on( 'click', function() {
503 wp.updates.requestForCredentialsModalCancel();
506 // Hide SSH fields when not selected
507 $( '#request-filesystem-credentials-dialog input[name="connection_type"]' ).on( 'change', function() {
508 $( this ).parents( 'form' ).find( '#private_key, #public_key' ).parents( 'label' ).toggle( ( 'ssh' == $( this ).val() ) );
511 // Click handler for plugin updates in List Table view.
512 $( '.plugin-update-tr' ).on( 'click', '.update-link', function( e ) {
514 if ( wp.updates.shouldRequestFilesystemCredentials && ! wp.updates.updateLock ) {
515 wp.updates.requestFilesystemCredentials( e );
517 var updateRow = $( e.target ).parents( '.plugin-update-tr' );
518 // Return the user to the input box of the plugin's table row after closing the modal.
519 wp.updates.$elToReturnFocusToFromCredentialsModal = $( '#' + updateRow.data( 'slug' ) ).find( '.check-column input' );
520 wp.updates.updatePlugin( updateRow.data( 'plugin' ), updateRow.data( 'slug' ) );
523 $( '.plugin-card' ).on( 'click', '.update-now', function( e ) {
525 var $button = $( e.target );
527 if ( wp.updates.shouldRequestFilesystemCredentials && ! wp.updates.updateLock ) {
528 wp.updates.requestFilesystemCredentials( e );
531 wp.updates.updatePlugin( $button.data( 'plugin' ), $button.data( 'slug' ) );
534 $( '#plugin_update_from_iframe' ).on( 'click' , function( e ) {
537 target = window.parent == window ? null : window.parent,
538 $.support.postMessage = !! window.postMessage;
540 if ( $.support.postMessage === false || target === null || window.parent.location.pathname.indexOf( 'update-core.php' ) !== -1 )
546 'action' : 'updatePlugin',
547 'slug' : $(this).data('slug')
550 target.postMessage( JSON.stringify( data ), window.location.origin );
555 $( window ).on( 'message', function( e ) {
556 var event = e.originalEvent,
558 loc = document.location,
559 expectedOrigin = loc.protocol + '//' + loc.hostname;
561 if ( event.origin !== expectedOrigin ) {
565 message = $.parseJSON( event.data );
567 if ( typeof message.action === 'undefined' ) {
571 switch (message.action){
572 case 'decrementUpdateCount' :
573 wp.updates.decrementCount( message.upgradeType );
575 case 'updatePlugin' :
577 if ( 'plugins' === pagenow || 'plugins-network' === pagenow ) {
578 // Return the user to the input box of the plugin's table row after closing the modal.
579 $( '#' + message.slug ).find( '.check-column input' ).focus();
580 // trigger the update
581 $( '.plugin-update-tr[data-slug="' + message.slug + '"]' ).find( '.update-link' ).trigger( 'click' );
582 } else if ( 'plugin-install' === pagenow ) {
583 $( '.plugin-card-' + message.slug ).find( '.column-name a' ).focus();
584 $( '.plugin-card-' + message.slug ).find( '[data-slug="' + message.slug + '"]' ).trigger( 'click' );
591 $( window ).on( 'beforeunload', wp.updates.beforeunload );
593 })( jQuery, window.wp, window.pagenow, window.ajaxurl );