2015-04-07 05:09:26 +02:00
|
|
|
/* global tb_remove */
|
2014-02-26 07:55:14 +01:00
|
|
|
window.wp = window.wp || {};
|
|
|
|
|
2015-02-11 06:06:28 +01:00
|
|
|
(function( $, wp, pagenow ) {
|
2014-02-26 07:55:14 +01:00
|
|
|
wp.updates = {};
|
|
|
|
|
|
|
|
/**
|
2015-02-05 05:19:23 +01:00
|
|
|
* 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;
|
|
|
|
|
2015-03-18 04:18:27 +01:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2015-04-02 07:31:26 +02:00
|
|
|
* @since 4.2.0
|
2015-03-18 04:18:27 +01:00
|
|
|
*
|
|
|
|
* @var object
|
|
|
|
*/
|
|
|
|
wp.updates.filesystemCredentials = {
|
|
|
|
ftp: {
|
|
|
|
host: null,
|
|
|
|
username: null,
|
|
|
|
password: null,
|
|
|
|
connectionType: null
|
|
|
|
},
|
|
|
|
ssh: {
|
2017-05-16 16:54:03 +02:00
|
|
|
publicKey: '',
|
|
|
|
privateKey: ''
|
|
|
|
},
|
|
|
|
fsNonce: ''
|
2015-03-18 04:18:27 +01:00
|
|
|
};
|
|
|
|
|
2015-02-05 05:19:23 +01:00
|
|
|
/**
|
2015-03-26 03:30:28 +01:00
|
|
|
* Flag if we're waiting for an update to complete.
|
2015-02-05 05:19:23 +01:00
|
|
|
*
|
|
|
|
* @since 4.2.0
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
wp.updates.updateLock = false;
|
|
|
|
|
2015-03-18 04:18:27 +01:00
|
|
|
/**
|
2015-03-26 03:30:28 +01:00
|
|
|
* * Flag if we've done an update successfully.
|
2015-03-18 04:18:27 +01:00
|
|
|
*
|
|
|
|
* @since 4.2.0
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
wp.updates.updateDoneSuccessfully = false;
|
|
|
|
|
2015-02-05 05:19:23 +01:00
|
|
|
/**
|
2015-03-26 03:30:28 +01:00
|
|
|
* If the user tries to update a plugin while an update is
|
2015-02-05 05:19:23 +01:00
|
|
|
* already happening, it can be placed in this queue to perform later.
|
|
|
|
*
|
|
|
|
* @since 4.2.0
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
wp.updates.updateQueue = [];
|
|
|
|
|
2015-04-01 04:48:28 +02:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
2015-02-05 05:19:23 +01:00
|
|
|
/**
|
|
|
|
* Decrement update counts throughout the various menus.
|
|
|
|
*
|
|
|
|
* @since 3.9.0
|
2014-02-26 07:55:14 +01:00
|
|
|
*
|
2015-08-18 23:47:25 +02:00
|
|
|
* @param {string} upgradeType
|
2014-02-26 07:55:14 +01:00
|
|
|
*/
|
|
|
|
wp.updates.decrementCount = function( upgradeType ) {
|
2015-02-05 05:19:23 +01:00
|
|
|
var count,
|
2015-04-02 07:31:26 +02:00
|
|
|
pluginCount,
|
|
|
|
$adminBarUpdateCount = $( '#wp-admin-bar-updates .ab-label' ),
|
|
|
|
$dashboardNavMenuUpdateCount = $( 'a[href="update-core.php"] .update-plugins' ),
|
|
|
|
$pluginsMenuItem = $( '#menu-plugins' );
|
2014-02-26 07:55:14 +01:00
|
|
|
|
2015-02-05 05:19:23 +01:00
|
|
|
|
|
|
|
count = $adminBarUpdateCount.text();
|
2014-02-26 07:55:14 +01:00
|
|
|
count = parseInt( count, 10 ) - 1;
|
2014-10-18 21:55:18 +02:00
|
|
|
if ( count < 0 || isNaN( count ) ) {
|
2014-02-26 07:55:14 +01:00
|
|
|
return;
|
|
|
|
}
|
2014-02-26 08:37:14 +01:00
|
|
|
$( '#wp-admin-bar-updates .ab-item' ).removeAttr( 'title' );
|
2015-02-05 05:19:23 +01:00
|
|
|
$adminBarUpdateCount.text( count );
|
|
|
|
|
2014-02-26 07:55:14 +01:00
|
|
|
|
2015-02-05 05:19:23 +01:00
|
|
|
$dashboardNavMenuUpdateCount.each( function( index, elem ) {
|
2014-02-26 07:55:14 +01:00
|
|
|
elem.className = elem.className.replace( /count-\d+/, 'count-' + count );
|
|
|
|
} );
|
2015-02-05 05:19:23 +01:00
|
|
|
$dashboardNavMenuUpdateCount.removeAttr( 'title' );
|
|
|
|
$dashboardNavMenuUpdateCount.find( '.update-count' ).text( count );
|
2014-02-26 07:55:14 +01:00
|
|
|
|
|
|
|
if ( 'plugin' === upgradeType ) {
|
2015-02-05 05:19:23 +01:00
|
|
|
pluginCount = $pluginsMenuItem.find( '.plugin-count' ).eq(0).text();
|
2014-02-26 07:55:14 +01:00
|
|
|
pluginCount = parseInt( pluginCount, 10 ) - 1;
|
2014-10-18 21:55:18 +02:00
|
|
|
if ( pluginCount < 0 || isNaN( pluginCount ) ) {
|
2014-02-26 07:55:14 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-02-05 05:19:23 +01:00
|
|
|
$pluginsMenuItem.find( '.plugin-count' ).text( pluginCount );
|
|
|
|
$pluginsMenuItem.find( '.update-plugins' ).each( function( index, elem ) {
|
2014-02-26 07:55:14 +01:00
|
|
|
elem.className = elem.className.replace( /count-\d+/, 'count-' + pluginCount );
|
|
|
|
} );
|
2015-02-05 05:19:23 +01:00
|
|
|
|
|
|
|
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 ) {
|
2015-11-18 21:10:26 +01:00
|
|
|
var $message, name,
|
|
|
|
$card = $( '.plugin-card-' + slug );
|
|
|
|
|
2015-02-05 05:19:23 +01:00
|
|
|
if ( 'plugins' === pagenow || 'plugins-network' === pagenow ) {
|
2016-01-08 18:48:28 +01:00
|
|
|
$message = $( '[data-plugin="' + plugin + '"]' ).next().find( '.update-message' );
|
2015-02-05 05:19:23 +01:00
|
|
|
} else if ( 'plugin-install' === pagenow ) {
|
2015-11-18 21:10:26 +01:00
|
|
|
$message = $card.find( '.update-now' );
|
2015-04-02 07:31:26 +02:00
|
|
|
name = $message.data( 'name' );
|
|
|
|
$message.attr( 'aria-label', wp.updates.l10n.updatingLabel.replace( '%s', name ) );
|
2015-11-18 21:10:26 +01:00
|
|
|
// Remove previous error messages, if any.
|
|
|
|
$card.removeClass( 'plugin-card-update-failed' ).find( '.notice.notice-error' ).remove();
|
2015-02-05 05:19:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$message.addClass( 'updating-message' );
|
2015-04-01 04:48:28 +02:00
|
|
|
if ( $message.html() !== wp.updates.l10n.updating ){
|
|
|
|
$message.data( 'originaltext', $message.html() );
|
|
|
|
}
|
|
|
|
|
2015-02-05 05:19:23 +01:00
|
|
|
$message.text( wp.updates.l10n.updating );
|
2015-03-01 20:11:27 +01:00
|
|
|
wp.a11y.speak( wp.updates.l10n.updatingMsg );
|
2015-02-05 05:19:23 +01:00
|
|
|
|
|
|
|
if ( wp.updates.updateLock ) {
|
|
|
|
wp.updates.updateQueue.push( {
|
|
|
|
type: 'update-plugin',
|
|
|
|
data: {
|
|
|
|
plugin: plugin,
|
|
|
|
slug: slug
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
return;
|
2014-02-26 07:55:14 +01:00
|
|
|
}
|
2015-02-05 05:19:23 +01:00
|
|
|
|
|
|
|
wp.updates.updateLock = true;
|
|
|
|
|
|
|
|
var data = {
|
2015-03-18 04:18:27 +01:00
|
|
|
_ajax_nonce: wp.updates.ajaxNonce,
|
|
|
|
plugin: plugin,
|
|
|
|
slug: slug,
|
2017-05-16 16:54:03 +02:00
|
|
|
_fs_nonce: wp.updates.filesystemCredentials.fsNonce,
|
2015-03-18 04:18:27 +01:00
|
|
|
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
|
2015-02-05 05:19:23 +01:00
|
|
|
};
|
|
|
|
|
2015-02-11 06:06:28 +01:00
|
|
|
wp.ajax.post( 'update-plugin', data )
|
|
|
|
.done( wp.updates.updateSuccess )
|
2015-03-18 04:18:27 +01:00
|
|
|
.fail( wp.updates.updateError );
|
2015-02-05 20:52:22 +01:00
|
|
|
};
|
2015-02-05 05:19:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* On a successful plugin update, update the UI with the result.
|
|
|
|
*
|
|
|
|
* @since 4.2.0
|
|
|
|
*
|
|
|
|
* @param {object} response
|
|
|
|
*/
|
|
|
|
wp.updates.updateSuccess = function( response ) {
|
2015-04-02 07:31:26 +02:00
|
|
|
var $updateMessage, name, $pluginRow, newText;
|
2015-02-05 05:19:23 +01:00
|
|
|
if ( 'plugins' === pagenow || 'plugins-network' === pagenow ) {
|
2016-01-08 18:48:28 +01:00
|
|
|
$pluginRow = $( '[data-plugin="' + response.plugin + '"]' ).first();
|
2015-03-19 06:26:28 +01:00
|
|
|
$updateMessage = $pluginRow.next().find( '.update-message' );
|
|
|
|
$pluginRow.addClass( 'updated' ).removeClass( 'update' );
|
|
|
|
|
2015-03-18 04:18:27 +01:00
|
|
|
// Update the version number in the row.
|
2015-04-02 07:31:26 +02:00
|
|
|
newText = $pluginRow.find('.plugin-version-author-uri').html().replace( response.oldVersion, response.newVersion );
|
2015-03-19 06:26:28 +01:00
|
|
|
$pluginRow.find('.plugin-version-author-uri').html( newText );
|
2015-04-01 23:31:26 +02:00
|
|
|
|
|
|
|
// Add updated class to update message parent tr
|
|
|
|
$pluginRow.next().addClass( 'updated' );
|
2015-02-05 05:19:23 +01:00
|
|
|
} else if ( 'plugin-install' === pagenow ) {
|
2015-03-19 06:26:28 +01:00
|
|
|
$updateMessage = $( '.plugin-card-' + response.slug ).find( '.update-now' );
|
|
|
|
$updateMessage.addClass( 'button-disabled' );
|
2015-04-02 07:31:26 +02:00
|
|
|
name = $updateMessage.data( 'name' );
|
|
|
|
$updateMessage.attr( 'aria-label', wp.updates.l10n.updatedLabel.replace( '%s', name ) );
|
2015-02-05 05:19:23 +01:00
|
|
|
}
|
|
|
|
|
2015-03-19 06:26:28 +01:00
|
|
|
$updateMessage.removeClass( 'updating-message' ).addClass( 'updated-message' );
|
|
|
|
$updateMessage.text( wp.updates.l10n.updated );
|
2015-03-01 20:11:27 +01:00
|
|
|
wp.a11y.speak( wp.updates.l10n.updatedMsg );
|
2015-02-05 05:19:23 +01:00
|
|
|
|
|
|
|
wp.updates.decrementCount( 'plugin' );
|
2015-03-18 04:18:27 +01:00
|
|
|
|
|
|
|
wp.updates.updateDoneSuccessfully = true;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The lock can be released since the update was successful,
|
|
|
|
* and any other updates can commence.
|
|
|
|
*/
|
|
|
|
wp.updates.updateLock = false;
|
2015-04-15 16:37:27 +02:00
|
|
|
|
|
|
|
$(document).trigger( 'wp-plugin-update-success', response );
|
2015-04-22 18:57:26 +02:00
|
|
|
|
2015-03-18 04:18:27 +01:00
|
|
|
wp.updates.queueChecker();
|
2015-02-05 05:19:23 +01:00
|
|
|
};
|
|
|
|
|
2015-04-01 23:31:26 +02:00
|
|
|
|
2015-02-05 05:19:23 +01:00
|
|
|
/**
|
|
|
|
* On a plugin update error, update the UI appropriately.
|
|
|
|
*
|
|
|
|
* @since 4.2.0
|
|
|
|
*
|
|
|
|
* @param {object} response
|
|
|
|
*/
|
|
|
|
wp.updates.updateError = function( response ) {
|
2015-11-18 21:10:26 +01:00
|
|
|
var $card = $( '.plugin-card-' + response.slug ),
|
|
|
|
$message,
|
|
|
|
$button,
|
|
|
|
name,
|
|
|
|
error_message;
|
|
|
|
|
2015-03-18 04:18:27 +01:00
|
|
|
wp.updates.updateDoneSuccessfully = false;
|
2015-11-18 21:10:26 +01:00
|
|
|
|
2015-06-15 04:48:28 +02:00
|
|
|
if ( response.errorCode && response.errorCode == 'unable_to_connect_to_filesystem' && wp.updates.shouldRequestFilesystemCredentials ) {
|
2015-03-18 04:18:27 +01:00
|
|
|
wp.updates.credentialError( response, 'update-plugin' );
|
|
|
|
return;
|
|
|
|
}
|
2015-11-18 21:10:26 +01:00
|
|
|
|
|
|
|
error_message = wp.updates.l10n.updateFailed.replace( '%s', response.error );
|
|
|
|
|
2015-02-05 05:19:23 +01:00
|
|
|
if ( 'plugins' === pagenow || 'plugins-network' === pagenow ) {
|
2016-01-08 18:48:28 +01:00
|
|
|
$message = $( '[data-plugin="' + response.plugin + '"]' ).next().find( '.update-message' );
|
2015-11-18 21:10:26 +01:00
|
|
|
$message.html( error_message ).removeClass( 'updating-message' );
|
2015-02-05 05:19:23 +01:00
|
|
|
} else if ( 'plugin-install' === pagenow ) {
|
2015-11-18 21:10:26 +01:00
|
|
|
$button = $card.find( '.update-now' );
|
|
|
|
name = $button.data( 'name' );
|
|
|
|
|
|
|
|
$card
|
|
|
|
.addClass( 'plugin-card-update-failed' )
|
|
|
|
.append( '<div class="notice notice-error is-dismissible"><p>' + error_message + '</p></div>' );
|
|
|
|
|
|
|
|
$button
|
|
|
|
.attr( 'aria-label', wp.updates.l10n.updateFailedLabel.replace( '%s', name ) )
|
|
|
|
.html( wp.updates.l10n.updateFailedShort ).removeClass( 'updating-message' );
|
|
|
|
|
|
|
|
$card.on( 'click', '.notice.is-dismissible .notice-dismiss', function() {
|
|
|
|
// Use same delay as the total duration of the notice fadeTo + slideUp animation.
|
|
|
|
setTimeout( function() {
|
|
|
|
$card
|
|
|
|
.removeClass( 'plugin-card-update-failed' )
|
|
|
|
.find( '.column-name a' ).focus();
|
|
|
|
}, 200 );
|
|
|
|
});
|
2015-02-05 05:19:23 +01:00
|
|
|
}
|
2015-11-18 21:10:26 +01:00
|
|
|
|
|
|
|
wp.a11y.speak( error_message, 'assertive' );
|
2015-03-18 04:18:27 +01:00
|
|
|
|
2015-06-15 06:28:25 +02:00
|
|
|
/*
|
|
|
|
* The lock can be released since this failure was
|
|
|
|
* after the credentials form.
|
|
|
|
*/
|
|
|
|
wp.updates.updateLock = false;
|
|
|
|
|
2015-04-07 05:12:26 +02:00
|
|
|
$(document).trigger( 'wp-plugin-update-error', response );
|
2015-06-15 06:28:25 +02:00
|
|
|
|
|
|
|
wp.updates.queueChecker();
|
2015-02-05 05:19:23 +01:00
|
|
|
};
|
|
|
|
|
2015-02-11 06:06:28 +01:00
|
|
|
/**
|
2015-03-18 04:18:27 +01:00
|
|
|
* Show an error message in the request for credentials form.
|
2015-02-11 06:06:28 +01:00
|
|
|
*
|
2015-03-18 04:18:27 +01:00
|
|
|
* @param {string} message
|
2015-02-11 06:06:28 +01:00
|
|
|
* @since 4.2.0
|
|
|
|
*/
|
2015-03-18 04:18:27 +01:00
|
|
|
wp.updates.showErrorInCredentialsForm = function( message ) {
|
2015-04-01 04:48:28 +02:00
|
|
|
var $modal = $( '.notification-dialog' );
|
2015-03-18 04:18:27 +01:00
|
|
|
|
2015-04-01 04:48:28 +02:00
|
|
|
// Remove any existing error.
|
|
|
|
$modal.find( '.error' ).remove();
|
2015-02-11 06:06:28 +01:00
|
|
|
|
2015-04-01 04:48:28 +02:00
|
|
|
$modal.find( 'h3' ).after( '<div class="error">' + message + '</div>' );
|
2015-03-18 04:18:27 +01:00
|
|
|
};
|
2015-02-11 06:06:28 +01:00
|
|
|
|
2015-03-18 04:18:27 +01:00
|
|
|
/**
|
|
|
|
* Events that need to happen when there is a credential error
|
|
|
|
*
|
|
|
|
* @since 4.2.0
|
|
|
|
*/
|
|
|
|
wp.updates.credentialError = function( response, type ) {
|
|
|
|
wp.updates.updateQueue.push( {
|
|
|
|
'type': type,
|
|
|
|
'data': {
|
|
|
|
// Not cool that we're depending on response for this data.
|
|
|
|
// This would feel more whole in a view all tied together.
|
|
|
|
plugin: response.plugin,
|
|
|
|
slug: response.slug
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
wp.updates.showErrorInCredentialsForm( response.error );
|
|
|
|
wp.updates.requestFilesystemCredentials();
|
|
|
|
};
|
2015-02-05 05:19:23 +01:00
|
|
|
|
|
|
|
/**
|
2015-03-26 03:30:28 +01:00
|
|
|
* If an update job has been placed in the queue, queueChecker pulls it out and runs it.
|
2015-02-05 05:19:23 +01:00
|
|
|
*
|
|
|
|
* @since 4.2.0
|
|
|
|
*/
|
|
|
|
wp.updates.queueChecker = function() {
|
|
|
|
if ( wp.updates.updateLock || wp.updates.updateQueue.length <= 0 ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var job = wp.updates.updateQueue.shift();
|
|
|
|
|
2015-03-26 17:30:27 +01:00
|
|
|
wp.updates.updatePlugin( job.data.plugin, job.data.slug );
|
2015-02-05 05:19:23 +01:00
|
|
|
};
|
|
|
|
|
2015-03-18 04:18:27 +01:00
|
|
|
|
|
|
|
/**
|
2015-04-01 04:48:28 +02:00
|
|
|
* Request the users filesystem credentials if we don't have them already.
|
2015-03-18 04:18:27 +01:00
|
|
|
*
|
|
|
|
* @since 4.2.0
|
|
|
|
*/
|
2015-04-01 04:48:28 +02:00
|
|
|
wp.updates.requestFilesystemCredentials = function( event ) {
|
2015-03-18 04:18:27 +01:00
|
|
|
if ( wp.updates.updateDoneSuccessfully === false ) {
|
2015-04-01 04:48:28 +02:00
|
|
|
/*
|
|
|
|
* For the plugin install screen, return the focus to the install button
|
|
|
|
* after exiting the credentials request modal.
|
|
|
|
*/
|
|
|
|
if ( 'plugin-install' === pagenow && event ) {
|
|
|
|
wp.updates.$elToReturnFocusToFromCredentialsModal = $( event.target );
|
|
|
|
}
|
|
|
|
|
2015-03-18 04:18:27 +01:00
|
|
|
wp.updates.updateLock = true;
|
2015-04-01 04:48:28 +02:00
|
|
|
|
|
|
|
wp.updates.requestForCredentialsModalOpen();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Keydown handler for the request for credentials modal.
|
|
|
|
*
|
|
|
|
* Close the modal when the escape key is pressed.
|
|
|
|
* Constrain keyboard navigation to inside the modal.
|
|
|
|
*
|
|
|
|
* @since 4.2.0
|
|
|
|
*/
|
|
|
|
wp.updates.keydown = function( event ) {
|
|
|
|
if ( 27 === event.keyCode ) {
|
|
|
|
wp.updates.requestForCredentialsModalCancel();
|
|
|
|
} else if ( 9 === event.keyCode ) {
|
|
|
|
// #upgrade button must always be the last focusable element in the dialog.
|
|
|
|
if ( event.target.id === 'upgrade' && ! event.shiftKey ) {
|
|
|
|
$( '#hostname' ).focus();
|
|
|
|
event.preventDefault();
|
|
|
|
} else if ( event.target.id === 'hostname' && event.shiftKey ) {
|
|
|
|
$( '#upgrade' ).focus();
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
2015-03-18 04:18:27 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-04-01 04:48:28 +02:00
|
|
|
/**
|
|
|
|
* Open the request for credentials modal.
|
|
|
|
*
|
|
|
|
* @since 4.2.0
|
|
|
|
*/
|
|
|
|
wp.updates.requestForCredentialsModalOpen = function() {
|
|
|
|
var $modal = $( '#request-filesystem-credentials-dialog' );
|
|
|
|
$( 'body' ).addClass( 'modal-open' );
|
|
|
|
$modal.show();
|
|
|
|
|
2015-04-22 18:57:26 +02:00
|
|
|
$modal.find( 'input:enabled:first' ).focus();
|
2015-04-01 04:48:28 +02:00
|
|
|
$modal.keydown( wp.updates.keydown );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Close the request for credentials modal.
|
|
|
|
*
|
|
|
|
* @since 4.2.0
|
|
|
|
*/
|
|
|
|
wp.updates.requestForCredentialsModalClose = function() {
|
|
|
|
$( '#request-filesystem-credentials-dialog' ).hide();
|
|
|
|
$( 'body' ).removeClass( 'modal-open' );
|
|
|
|
wp.updates.$elToReturnFocusToFromCredentialsModal.focus();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The steps that need to happen when the modal is canceled out
|
|
|
|
*
|
|
|
|
* @since 4.2.0
|
|
|
|
*/
|
|
|
|
wp.updates.requestForCredentialsModalCancel = function() {
|
|
|
|
// no updateLock and no updateQueue means we already have cleared things up
|
2016-01-08 18:48:28 +01:00
|
|
|
var data, $message;
|
2015-04-01 04:48:28 +02:00
|
|
|
|
|
|
|
if( wp.updates.updateLock === false && wp.updates.updateQueue.length === 0 ){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-08 18:48:28 +01:00
|
|
|
data = wp.updates.updateQueue[0].data;
|
2015-04-01 04:48:28 +02:00
|
|
|
|
|
|
|
// remove the lock, and clear the queue
|
|
|
|
wp.updates.updateLock = false;
|
|
|
|
wp.updates.updateQueue = [];
|
|
|
|
|
|
|
|
wp.updates.requestForCredentialsModalClose();
|
|
|
|
if ( 'plugins' === pagenow || 'plugins-network' === pagenow ) {
|
2016-01-08 18:48:28 +01:00
|
|
|
$message = $( '[data-plugin="' + data.plugin + '"]' ).next().find( '.update-message' );
|
2015-04-01 04:48:28 +02:00
|
|
|
} else if ( 'plugin-install' === pagenow ) {
|
2016-01-08 18:48:28 +01:00
|
|
|
$message = $( '.plugin-card-' + data.slug ).find( '.update-now' );
|
2015-04-01 04:48:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$message.removeClass( 'updating-message' );
|
|
|
|
$message.html( $message.data( 'originaltext' ) );
|
|
|
|
wp.a11y.speak( wp.updates.l10n.updateCancel );
|
|
|
|
};
|
2015-04-14 18:42:26 +02:00
|
|
|
/**
|
|
|
|
* Potentially add an AYS to a user attempting to leave the page
|
|
|
|
*
|
|
|
|
* If an update is on-going and a user attempts to leave the page,
|
|
|
|
* open an "Are you sure?" alert.
|
|
|
|
*
|
|
|
|
* @since 4.2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
wp.updates.beforeunload = function() {
|
|
|
|
if ( wp.updates.updateLock ) {
|
|
|
|
return wp.updates.l10n.beforeunload;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-04-01 04:48:28 +02:00
|
|
|
|
2015-02-05 05:19:23 +01:00
|
|
|
$( document ).ready( function() {
|
2015-03-18 04:18:27 +01:00
|
|
|
/*
|
|
|
|
* Check whether a user needs to submit filesystem credentials based on whether
|
|
|
|
* the form was output on the page server-side.
|
|
|
|
*
|
|
|
|
* @see {wp_print_request_filesystem_credentials_modal() in PHP}
|
|
|
|
*/
|
|
|
|
wp.updates.shouldRequestFilesystemCredentials = ( $( '#request-filesystem-credentials-dialog' ).length <= 0 ) ? false : true;
|
|
|
|
|
|
|
|
// File system credentials form submit noop-er / handler.
|
|
|
|
$( '#request-filesystem-credentials-dialog form' ).on( 'submit', function() {
|
|
|
|
// Persist the credentials input by the user for the duration of the page load.
|
2017-05-16 16:54:03 +02:00
|
|
|
wp.updates.filesystemCredentials.fsNonce = $( '#_fs_nonce' ).val();
|
2015-03-18 04:18:27 +01:00
|
|
|
wp.updates.filesystemCredentials.ftp.hostname = $('#hostname').val();
|
|
|
|
wp.updates.filesystemCredentials.ftp.username = $('#username').val();
|
|
|
|
wp.updates.filesystemCredentials.ftp.password = $('#password').val();
|
|
|
|
wp.updates.filesystemCredentials.ftp.connectionType = $('input[name="connection_type"]:checked').val();
|
|
|
|
wp.updates.filesystemCredentials.ssh.publicKey = $('#public_key').val();
|
|
|
|
wp.updates.filesystemCredentials.ssh.privateKey = $('#private_key').val();
|
|
|
|
|
2015-04-01 04:48:28 +02:00
|
|
|
wp.updates.requestForCredentialsModalClose();
|
2015-03-18 04:18:27 +01:00
|
|
|
|
|
|
|
// Unlock and invoke the queue.
|
|
|
|
wp.updates.updateLock = false;
|
|
|
|
wp.updates.queueChecker();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2015-04-01 04:48:28 +02:00
|
|
|
// Close the request credentials modal when
|
|
|
|
$( '#request-filesystem-credentials-dialog [data-js-action="close"], .notification-dialog-background' ).on( 'click', function() {
|
|
|
|
wp.updates.requestForCredentialsModalCancel();
|
|
|
|
});
|
|
|
|
|
2015-06-15 05:20:28 +02:00
|
|
|
// Hide SSH fields when not selected
|
|
|
|
$( '#request-filesystem-credentials-dialog input[name="connection_type"]' ).on( 'change', function() {
|
|
|
|
$( this ).parents( 'form' ).find( '#private_key, #public_key' ).parents( 'label' ).toggle( ( 'ssh' == $( this ).val() ) );
|
|
|
|
}).change();
|
|
|
|
|
2015-03-18 04:18:27 +01:00
|
|
|
// Click handler for plugin updates in List Table view.
|
2015-04-01 04:48:28 +02:00
|
|
|
$( '.plugin-update-tr' ).on( 'click', '.update-link', function( e ) {
|
2015-02-05 05:19:23 +01:00
|
|
|
e.preventDefault();
|
2015-03-18 04:18:27 +01:00
|
|
|
if ( wp.updates.shouldRequestFilesystemCredentials && ! wp.updates.updateLock ) {
|
2015-04-01 04:48:28 +02:00
|
|
|
wp.updates.requestFilesystemCredentials( e );
|
2015-03-18 04:18:27 +01:00
|
|
|
}
|
2015-04-01 04:48:28 +02:00
|
|
|
var updateRow = $( e.target ).parents( '.plugin-update-tr' );
|
|
|
|
// Return the user to the input box of the plugin's table row after closing the modal.
|
2016-01-08 18:48:28 +01:00
|
|
|
wp.updates.$elToReturnFocusToFromCredentialsModal = updateRow.prev().find( '.check-column input' );
|
2015-04-01 04:48:28 +02:00
|
|
|
wp.updates.updatePlugin( updateRow.data( 'plugin' ), updateRow.data( 'slug' ) );
|
2015-02-05 05:19:23 +01:00
|
|
|
} );
|
|
|
|
|
2015-04-01 04:48:28 +02:00
|
|
|
$( '.plugin-card' ).on( 'click', '.update-now', function( e ) {
|
2015-02-05 05:19:23 +01:00
|
|
|
e.preventDefault();
|
2015-02-05 20:52:22 +01:00
|
|
|
var $button = $( e.target );
|
2015-04-01 04:48:28 +02:00
|
|
|
|
2016-02-17 20:42:26 +01:00
|
|
|
// Do nothing while updating and when the button is disabled.
|
|
|
|
if ( $button.hasClass( 'updating-message' ) || $button.hasClass( 'button-disabled' ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-01 04:48:28 +02:00
|
|
|
if ( wp.updates.shouldRequestFilesystemCredentials && ! wp.updates.updateLock ) {
|
|
|
|
wp.updates.requestFilesystemCredentials( e );
|
|
|
|
}
|
|
|
|
|
2015-02-05 05:19:23 +01:00
|
|
|
wp.updates.updatePlugin( $button.data( 'plugin' ), $button.data( 'slug' ) );
|
|
|
|
} );
|
|
|
|
|
2015-04-07 05:09:26 +02:00
|
|
|
$( '#plugin_update_from_iframe' ).on( 'click' , function( e ) {
|
2016-01-08 18:48:28 +01:00
|
|
|
var target, job;
|
2015-04-07 05:09:26 +02:00
|
|
|
|
|
|
|
target = window.parent == window ? null : window.parent,
|
|
|
|
$.support.postMessage = !! window.postMessage;
|
|
|
|
|
2015-04-08 19:38:27 +02:00
|
|
|
if ( $.support.postMessage === false || target === null || window.parent.location.pathname.indexOf( 'update-core.php' ) !== -1 )
|
2015-04-07 05:09:26 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
2016-01-08 18:48:28 +01:00
|
|
|
job = {
|
|
|
|
action: 'updatePlugin',
|
|
|
|
type: 'update-plugin',
|
|
|
|
data: {
|
|
|
|
plugin: $( this ).data( 'plugin' ),
|
|
|
|
slug: $( this ).data( 'slug' )
|
|
|
|
}
|
2015-04-07 05:09:26 +02:00
|
|
|
};
|
|
|
|
|
2016-01-08 18:48:28 +01:00
|
|
|
target.postMessage( JSON.stringify( job ), window.location.origin );
|
2015-04-07 05:09:26 +02:00
|
|
|
});
|
|
|
|
|
2015-02-05 05:19:23 +01:00
|
|
|
} );
|
|
|
|
|
2014-02-26 07:55:14 +01:00
|
|
|
$( window ).on( 'message', function( e ) {
|
|
|
|
var event = e.originalEvent,
|
|
|
|
message,
|
|
|
|
loc = document.location,
|
|
|
|
expectedOrigin = loc.protocol + '//' + loc.hostname;
|
|
|
|
|
|
|
|
if ( event.origin !== expectedOrigin ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
message = $.parseJSON( event.data );
|
|
|
|
|
2015-04-07 05:09:26 +02:00
|
|
|
if ( typeof message.action === 'undefined' ) {
|
2014-02-26 07:55:14 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-07 05:09:26 +02:00
|
|
|
switch (message.action){
|
|
|
|
case 'decrementUpdateCount' :
|
|
|
|
wp.updates.decrementCount( message.upgradeType );
|
|
|
|
break;
|
|
|
|
case 'updatePlugin' :
|
|
|
|
tb_remove();
|
2016-01-08 18:48:28 +01:00
|
|
|
|
|
|
|
wp.updates.updateQueue.push( message );
|
|
|
|
wp.updates.queueChecker();
|
2015-04-07 05:09:26 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-02-26 07:55:14 +01:00
|
|
|
} );
|
|
|
|
|
2015-04-14 18:42:26 +02:00
|
|
|
$( window ).on( 'beforeunload', wp.updates.beforeunload );
|
2015-04-06 03:00:26 +02:00
|
|
|
|
2015-02-05 20:52:22 +01:00
|
|
|
})( jQuery, window.wp, window.pagenow, window.ajaxurl );
|