Skip to content

Instantly share code, notes, and snippets.

@aaronjorbin
Created March 12, 2015 03:20
Show Gist options
  • Save aaronjorbin/a19fc1480660e1b4fc1c to your computer and use it in GitHub Desktop.
Save aaronjorbin/a19fc1480660e1b4fc1c to your computer and use it in GitHub Desktop.
Index: src/wp-admin/css/common.css
===================================================================
--- src/wp-admin/css/common.css (revision 31694)
+++ src/wp-admin/css/common.css (working copy)
@@ -1942,6 +1942,10 @@
display: block;
}
+#request-filesystem-credentials-dialog {
+ display: none;
+}
+
/* - Only used once or twice in all of WP - deprecate for global style
------------------------------------------------------------------------------*/
td.media-icon {
Index: src/wp-admin/includes/ajax-actions.php
===================================================================
--- src/wp-admin/includes/ajax-actions.php (revision 31694)
+++ src/wp-admin/includes/ajax-actions.php (working copy)
@@ -2954,17 +2954,16 @@
$upgrader = new Plugin_Upgrader( new Automatic_Upgrader_Skin() );
$result = $upgrader->bulk_upgrade( array( $plugin ) );
-
if ( is_array( $result ) ) {
- $result = $result[ $plugin ];
- }
-
- if ( is_wp_error( $result ) ) {
+ 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 );
}
-
- wp_send_json_success( $status );
}
/**
Index: src/wp-admin/includes/class-wp-upgrader-skins.php
===================================================================
--- src/wp-admin/includes/class-wp-upgrader-skins.php (revision 31694)
+++ src/wp-admin/includes/class-wp-upgrader-skins.php (working copy)
@@ -714,6 +714,7 @@
if ( $context ) {
$this->options['context'] = $context;
}
+
// TODO: fix up request_filesystem_credentials(), or split it, to allow us to request a no-output version
// This will output a credentials form in event of failure, We don't want that, so just hide with a buffer
ob_start();
Index: src/wp-admin/includes/class-wp-upgrader.php
===================================================================
--- src/wp-admin/includes/class-wp-upgrader.php (revision 31694)
+++ src/wp-admin/includes/class-wp-upgrader.php (working copy)
@@ -258,6 +258,7 @@
//Clean up contents of upgrade directory beforehand.
$upgrade_files = $wp_filesystem->dirlist($upgrade_folder);
+
if ( !empty($upgrade_files) ) {
foreach ( $upgrade_files as $file )
$wp_filesystem->delete($upgrade_folder . $file['name'], true);
@@ -579,6 +580,7 @@
//Unzips the file into a temporary directory
$working_dir = $this->unpack_package( $download, $delete_package );
+
if ( is_wp_error($working_dir) ) {
$this->skin->error($working_dir);
$this->skin->after();
Index: src/wp-admin/js/updates.js
===================================================================
--- src/wp-admin/js/updates.js (revision 31694)
+++ src/wp-admin/js/updates.js (working copy)
@@ -22,6 +22,35 @@
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 = window._wpUpdatesSettings.requestFilesystemCredentials;
+
+ /**
+ * 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 install/update to complete.
*
* @since 4.2.0
@@ -124,8 +153,14 @@
var data = {
'_ajax_nonce': wp.updates.ajaxNonce,
- 'plugin': plugin,
- 'slug': slug
+ '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 )
@@ -168,6 +203,19 @@
*/
wp.updates.updateError = function( response ) {
var $message;
+ if ( response.errorCode && response.errorCode == 'unable_to_connect_to_filesystem' ) {
+ wp.updates.updateQueue.push( {
+ type: 'update-plugin',
+ 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.requestFilesystemCredentials();
+ return;
+ }
if ( 'plugins' === pagenow || 'plugins-network' === pagenow ) {
$message = $( '#' + response.slug ).next().find( '.update-message' );
} else if ( 'plugin-install' === pagenow ) {
@@ -282,9 +330,38 @@
}
};
+ wp.updates.requestFilesystemCredentials = function() {
+ wp.updates.updateLock = true;
+ $('#request-filesystem-credentials-dialog').show();
+ };
+
+ // Bind various click handlers.
$( document ).ready( function() {
+ // 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.
+ 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();
+
+ $('#request-filesystem-credentials-dialog').hide();
+
+ // Unlock and invoke the queue.
+ wp.updates.updateLock = false;
+ wp.updates.queueChecker();
+
+ return false;
+ });
+
+ // Click handler for plugin updates in List Table view.
$( '.plugin-update-tr .update-link' ).on( 'click', function( e ) {
e.preventDefault();
+ if ( wp.updates.shouldRequestFilesystemCredentials ) {
+ wp.updates.requestFilesystemCredentials();
+ }
var $row = $( e.target ).parents( '.plugin-update-tr' );
wp.updates.updatePlugin( $row.data( 'plugin' ), $row.data( 'slug' ) );
} );
@@ -343,4 +420,4 @@
} );
-})( jQuery, window.wp, window.pagenow, window.ajaxurl );
+})( jQuery, window.wp, window.pagenow, window.ajaxurl );
\ No newline at end of file
Index: src/wp-admin/plugins.php
===================================================================
--- src/wp-admin/plugins.php (revision 31694)
+++ src/wp-admin/plugins.php (working copy)
@@ -475,5 +475,11 @@
</div>
+<div id="request-filesystem-credentials-dialog" class="notification-dialog-wrap">
+ <div class="notification-dialog-background"></div>
+ <div class="notification-dialog">
+ <?php request_filesystem_credentials( site_url() ); ?>
+ </div>
+</div>
<?php
include(ABSPATH . 'wp-admin/admin-footer.php');
Index: src/wp-includes/script-loader.php
===================================================================
--- src/wp-includes/script-loader.php (revision 31694)
+++ src/wp-includes/script-loader.php (working copy)
@@ -518,8 +518,20 @@
) );
$scripts->add( 'updates', "/wp-admin/js/updates$suffix.js", array( 'jquery', 'wp-util', 'wp-a11y' ) );
+
+ /*
+ * Determine whether the user will need to enter filesystem credentials
+ * on the front-end.
+ */
+ $filesystem_method = get_filesystem_method();
+ ob_start();
+ $filesystem_credentials_are_stored = request_filesystem_credentials( self_admin_url() );
+ ob_end_clean();
+ $request_filesystem_credentials = ( $filesystem_method != 'direct' && ! $filesystem_credentials_are_stored ) ? 1 : 0;
+
did_action( 'init' ) && $scripts->localize( 'updates', '_wpUpdatesSettings', array(
'ajax_nonce' => wp_create_nonce( 'updates' ),
+ 'requestFilesystemCredentials' => $request_filesystem_credentials,
'l10n' => array(
'updating' => __( 'Updating...' ),
'updated' => __( 'Updated!' ),
@@ -532,7 +544,7 @@
'installingMsg' => __( 'Installing... please wait.' ),
'updatedMsg' => __( 'Update completed successfully.' ),
'installedMsg' => __( 'Installation completed successfully.' ),
- )
+ ),
) );
$scripts->add( 'farbtastic', '/wp-admin/js/farbtastic.js', array('jquery'), '1.2' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment