Skip to content
Browse files

Display error messages

If there is an error during plugin installation or activation, pop up an
alert box with the error message. Also adds a way to reset the plugin's
options for testing purposes.
  • Loading branch information...
1 parent 1d37aee commit a068115ed2b9cd38f5365a7279a4d1ae05d90c6e @Viper007Bond Viper007Bond committed Aug 21, 2012
Showing with 166 additions and 160 deletions.
  1. +154 −152 developer.js
  2. +12 −8 developer.php
View
306 developer.js
@@ -1,153 +1,155 @@
-function a8c_developer_lightbox() {
- (function($){
- function make_colorbox( href, transition ) {
- $.colorbox({
- inline: true,
- href: href,
- title: a8c_developer_i18n.lightbox_title,
- innerWidth: 500,
- maxHeight: '100%',
- transition: transition
- });
- }
-
- make_colorbox( '#a8c-developer-setup-dialog-step-1', 'none' );
-
- $('#a8c-developer-setup-dialog-step-1-form').submit( function(e) {
- var form = this;
-
- $('#a8c-developer-setup-dialog-step-1-submit').val( a8c_developer_i18n.saving );
-
- if ( 'yes' != a8c_developer_i18n.go_to_step_2 )
- return;
-
- e.preventDefault();
-
- $.post( ajaxurl, $(form).serialize() )
- .success( function( result ) {
- // If there was an error with the AJAX save, then do a normal POST
- if ( '-1' == result ) {
- location.href = 'options-general.php?page=' + a8c_developer_i18n.settings_slug + '&a8cdev_errorsaving=1';
- return;
- }
-
- // AJAX says no step 2 needed, so head to the settings page
- if ( 'redirect' == result ) {
- location.href = 'options-general.php?page=' + a8c_developer_i18n.settings_slug + '&updated=1';
- return;
- }
-
- // Display the AJAX reponse
- $('#a8c-developer-setup-dialog-step-2').html( result );
- make_colorbox( '#a8c-developer-setup-dialog-step-2' );
- })
- ;
- });
- })(jQuery);
-}
-
-function a8c_developer_bind_events() {
- (function($){
- $('.a8c-developer-button-install').click( function() {
- var button = this;
-
- $(button).html( a8c_developer_i18n.installing );
-
- $.post( ajaxurl, {
- 'action': 'a8c_developer_install_plugin',
- '_ajax_nonce': $(button).attr('data-nonce'),
- 'plugin_slug': $(button).attr('data-pluginslug')
- } )
- .success( function( result ) {
- if ( '1' == result ) {
- $(button).html( a8c_developer_i18n.installed );
- $(button).unbind('click').prop('disabled', true);
- } else {
- $(button).html( a8c_developer_i18n.error );
- }
- })
- .error( function() {
- $(button).html( a8c_developer_i18n.error );
- })
- ;
- });
-
- $('.a8c-developer-button-activate').click( function() {
- var button = this;
-
- $(button).html( a8c_developer_i18n.activating );
-
- $.post( ajaxurl, {
- 'action': 'a8c_developer_activate_plugin',
- '_ajax_nonce': $(button).attr('data-nonce'),
- 'path': $(button).attr('data-path')
- } )
- .success( function( result ) {
- if ( '1' == result ) {
- $(button).html( a8c_developer_i18n.activated );
- $(button).unbind('click').prop('disabled', true);
- } else {
- $(button).html( a8c_developer_i18n.error );
- }
- })
- .error( function() {
- $(button).html( a8c_developer_i18n.error );
- })
- ;
- });
- })(jQuery);
-}
-
-function a8c_developer_bind_settings_events() {
- (function($){
- $('.a8c-developer-button-install').click( function() {
- var button = this;
-
- $(button).html( a8c_developer_i18n.installing );
-
- $.post( ajaxurl, {
- 'action': 'a8c_developer_install_plugin',
- '_ajax_nonce': $(button).attr('data-nonce'),
- 'plugin_slug': $(button).attr('data-pluginslug')
- } )
- .success( function( result ) {
- if ( '1' == result ) {
- $(button).html( a8c_developer_i18n.INSTALLED );
- } else {
- $(button).html( a8c_developer_i18n.ERROR );
- }
- })
- .error( function() {
- $(button).html( a8c_developer_i18n.ERROR );
- })
- ;
-
- return false;
- });
-
- $('.a8c-developer-button-activate').click( function() {
- var button = this;
-
- $(button).html( a8c_developer_i18n.activating );
-
- $.post( ajaxurl, {
- 'action': 'a8c_developer_activate_plugin',
- '_ajax_nonce': $(button).attr('data-nonce'),
- 'path': $(button).attr('data-path')
- } )
- .success( function( result ) {
- if ( '1' == result ) {
- $(button).replaceWith("<span class='a8c-developer-active'>" + a8c_developer_i18n.ACTIVE + "</span>");
- } else {
- $(button).html( a8c_developer_i18n.ERROR );
- }
- })
- .error( function() {
- $(button).html( a8c_developer_i18n.ERROR );
- })
- ;
-
- return false;
- });
- })(jQuery);
+function a8c_developer_lightbox() {
+ (function($){
+ function make_colorbox( href, transition ) {
+ $.colorbox({
+ inline: true,
+ href: href,
+ title: a8c_developer_i18n.lightbox_title,
+ innerWidth: 500,
+ maxHeight: '100%',
+ transition: transition
+ });
+ }
+
+ make_colorbox( '#a8c-developer-setup-dialog-step-1', 'none' );
+
+ $('#a8c-developer-setup-dialog-step-1-form').submit( function(e) {
+ var form = this;
+
+ $('#a8c-developer-setup-dialog-step-1-submit').val( a8c_developer_i18n.saving );
+
+ if ( 'yes' != a8c_developer_i18n.go_to_step_2 )
+ return;
+
+ e.preventDefault();
+
+ $.post( ajaxurl, $(form).serialize() )
+ .success( function( result ) {
+ // If there was an error with the AJAX save, then do a normal POST
+ if ( '-1' == result ) {
+ location.href = 'options-general.php?page=' + a8c_developer_i18n.settings_slug + '&a8cdev_errorsaving=1';
+ return;
+ }
+
+ // AJAX says no step 2 needed, so head to the settings page
+ if ( 'redirect' == result ) {
+ location.href = 'options-general.php?page=' + a8c_developer_i18n.settings_slug + '&updated=1';
+ return;
+ }
+
+ // Display the AJAX reponse
+ $('#a8c-developer-setup-dialog-step-2').html( result );
+ make_colorbox( '#a8c-developer-setup-dialog-step-2' );
+ })
+ ;
+ });
+ })(jQuery);
+}
+
+function a8c_developer_bind_events() {
+ (function($){
+ $('.a8c-developer-button-install').click( function() {
+ var button = this;
+
+ $(button).html( a8c_developer_i18n.installing );
+
+ $.post( ajaxurl, {
+ 'action': 'a8c_developer_install_plugin',
+ '_ajax_nonce': $(button).attr('data-nonce'),
+ 'plugin_slug': $(button).attr('data-pluginslug')
+ } )
+ .success( function( result ) {
+ if ( '1' === result ) {
+ $(button).html( a8c_developer_i18n.installed );
+ $(button).unbind('click').prop('disabled', true);
+ } else {
+ alert( result );
+ $(button).html( a8c_developer_i18n.error );
+ }
+ })
+ .error( function() {
+ $(button).html( a8c_developer_i18n.error );
+ })
+ ;
+ });
+
+ $('.a8c-developer-button-activate').click( function() {
+ var button = this;
+
+ $(button).html( a8c_developer_i18n.activating );
+
+ $.post( ajaxurl, {
+ 'action': 'a8c_developer_activate_plugin',
+ '_ajax_nonce': $(button).attr('data-nonce'),
+ 'path': $(button).attr('data-path')
+ } )
+ .success( function( result ) {
+ if ( '1' === result ) {
+ $(button).html( a8c_developer_i18n.activated );
+ $(button).unbind('click').prop('disabled', true);
+ } else {
+ alert( result );
+ $(button).html( a8c_developer_i18n.error );
+ }
+ })
+ .error( function() {
+ $(button).html( a8c_developer_i18n.error );
+ })
+ ;
+ });
+ })(jQuery);
+}
+
+function a8c_developer_bind_settings_events() {
+ (function($){
+ $('.a8c-developer-button-install').click( function() {
+ var button = this;
+
+ $(button).html( a8c_developer_i18n.installing );
+
+ $.post( ajaxurl, {
+ 'action': 'a8c_developer_install_plugin',
+ '_ajax_nonce': $(button).attr('data-nonce'),
+ 'plugin_slug': $(button).attr('data-pluginslug')
+ } )
+ .success( function( result ) {
+ if ( '1' == result ) {
+ $(button).html( a8c_developer_i18n.INSTALLED );
+ } else {
+ $(button).html( a8c_developer_i18n.ERROR );
+ }
+ })
+ .error( function() {
+ $(button).html( a8c_developer_i18n.ERROR );
+ })
+ ;
+
+ return false;
+ });
+
+ $('.a8c-developer-button-activate').click( function() {
+ var button = this;
+
+ $(button).html( a8c_developer_i18n.activating );
+
+ $.post( ajaxurl, {
+ 'action': 'a8c_developer_activate_plugin',
+ '_ajax_nonce': $(button).attr('data-nonce'),
+ 'path': $(button).attr('data-path')
+ } )
+ .success( function( result ) {
+ if ( '1' == result ) {
+ $(button).replaceWith("<span class='a8c-developer-active'>" + a8c_developer_i18n.ACTIVE + "</span>");
+ } else {
+ $(button).html( a8c_developer_i18n.ERROR );
+ }
+ })
+ .error( function() {
+ $(button).html( a8c_developer_i18n.ERROR );
+ })
+ ;
+
+ return false;
+ });
+ })(jQuery);
}
View
20 developer.php
@@ -60,6 +60,10 @@ public function init() {
}
public function admin_init() {
+ if ( ! empty( $_GET['developer_plugin_reset'] ) && current_user_can( 'manage_options' ) ) {
+ delete_option( self::OPTION );
+ }
+
$this->recommended_plugins = array(
'debug-bar' => array(
'project_type' => 'all',
@@ -324,19 +328,19 @@ public function ajax_handler( $action ) {
case 'a8c_developer_install_plugin':
if ( empty( $_POST['plugin_slug'] ) )
- die( '-1' );
+ die( __( 'ERROR: No slug was passed to the AJAX callback.', 'a8c-developer' ) );
check_ajax_referer( 'a8c_developer_install_plugin_' . $_POST['plugin_slug'] );
if ( ! current_user_can( 'install_plugins' ) || ! current_user_can( 'activate_plugins' ) )
- die( '-1' );
+ die( __( 'ERROR: You lack permissions to install and/or activate plugins.', 'a8c-developer' ) );
include_once ( ABSPATH . 'wp-admin/includes/plugin-install.php' );
$api = plugins_api( 'plugin_information', array( 'slug' => $_POST['plugin_slug'], 'fields' => array( 'sections' => false ) ) );
if ( is_wp_error( $api ) )
- die( '-1' );
+ die( sprintf( __( 'ERROR: Error fetching plugin information: %s', 'a8c-developer' ), get_error_message( $api ) ) );
$upgrader = new Plugin_Upgrader( new Automattic_Developer_Empty_Upgrader_Skin( array(
'nonce' => 'install-plugin_' . $_POST['plugin_slug'],
@@ -347,28 +351,28 @@ public function ajax_handler( $action ) {
$install_result = $upgrader->install( $api->download_link );
if ( ! $install_result || is_wp_error( $install_result ) )
- die( '-1' );
+ die( sprintf( __( 'ERROR: Failed to install plugin: %s', 'a8c-developer' ), get_error_message( $api ) ) );
$activate_result = activate_plugin( $this->get_path_for_recommended_plugin( $_POST['plugin_slug'] ) );
if ( is_wp_error( $activate_result ) )
- die( '-1' );
+ die( sprintf( __( 'ERROR: Failed to activate plugin: %s', 'a8c-developer' ), get_error_message( $api ) ) );
exit( '1' );
case 'a8c_developer_activate_plugin':
if ( empty( $_POST['path'] ) )
- die( '-1' );
+ die( __( 'ERROR: No slug was passed to the AJAX callback.', 'a8c-developer' ) );
check_ajax_referer( 'a8c_developer_activate_plugin_' . $_POST['path'] );
if ( ! current_user_can( 'activate_plugins' ) )
- die( '-1' );
+ die( __( 'ERROR: You lack permissions to activate plugins.', 'a8c-developer' ) );
$activate_result = activate_plugin( $_POST['path'] );
if ( is_wp_error( $activate_result ) )
- die( '-1' );
+ die( sprintf( __( 'ERROR: Failed to activate plugin: %s', 'a8c-developer' ), get_error_message( $api ) ) );
exit( '1' );
}

4 comments on commit a068115

@Viper007Bond
Automattic member

GitHub is stupid when it comes to line endings and diffs.

The actual changes to developer.js where the addition of two alert( result ); and changing the if ( '1' === result ) { check to an exact match, i.e. three equal signs.

@ashfame

@Viper007Bond Your change must have involved conversions of tabs into spaces or vice-versa.

@Viper007Bond
Automattic member

@ashfame: Nope, it was tabs in both cases.

@ashfame

@Viper007Bond Ah yes, just checked out the diff of file before and after this commit, you are right. Very weird. Never saw that happening on Github before.

Please sign in to comment.
Something went wrong with that request. Please try again.