Skip to content
This repository has been archived by the owner on Feb 22, 2022. It is now read-only.

Commit

Permalink
Merge pull request #90 from ronalfy/add/ajax-errors
Browse files Browse the repository at this point in the history
Add Better Handling to Ajax Errors
  • Loading branch information
pbiron authored Apr 22, 2020
2 parents 4223e42 + 8f320e2 commit 2d36c0a
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 44 deletions.
13 changes: 7 additions & 6 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,13 @@ function insert_into_template(positioning_text, added_text, insert_before) {
'wp-autoupdates',
'wp_autoupdates',
array(
'enable' => __( 'Enable auto-updates', 'wp-autoupdates' ),
'enabling' => __( 'Enabling auto-updates...', 'wp-autoupdates' ),
'disable' => __( 'Disable auto-updates', 'wp-autoupdates' ),
'disabling' => __( 'Disabling auto-updates...', 'wp-autoupdates' ),
'auto_enabled' => __( 'Auto-updates enabled', 'wp-autoupdates' ),
'auto_disabled' => __( 'Auto-updates disabled', 'wp-autoupdates' ),
'enable' => __( 'Enable auto-updates', 'wp-autoupdates' ),
'enabling' => __( 'Enabling auto-updates...', 'wp-autoupdates' ),
'disable' => __( 'Disable auto-updates', 'wp-autoupdates' ),
'disabling' => __( 'Disabling auto-updates...', 'wp-autoupdates' ),
'auto_enabled' => __( 'Auto-updates enabled', 'wp-autoupdates' ),
'auto_disabled' => __( 'Auto-updates disabled', 'wp-autoupdates' ),
'auto_update_error' => __( 'The request could not be completed', 'wp-autoupdates' ),
)
);
}
Expand Down
142 changes: 104 additions & 38 deletions js/wp-autoupdates.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
jQuery(function ($) {

function add_error_notice( html, error ) {
html += '<div class="notice error"><p><strong>' + error + '</strong></p></div>';
return html;
}
// Disable auto-updates for a plugin.
$('.autoupdates_column').on('click', 'a.plugin-autoupdate-disable', function (e) {
e.preventDefault();
var $anchor = $( this );
$anchor.blur();
var href = wpAjax.unserialize($anchor.attr( 'href' ) );
var $parent = $anchor.parents( '.autoupdates_column' );
// Clear errors
$parent.find( '.notice' ).remove();
var html = $parent.html();

// Show loading status.
$anchor.html( '<span class="dashicons dashicons-update spin"></span> ' + wp_autoupdates.disabling );
Expand All @@ -23,15 +31,22 @@ jQuery(function ($) {
}
)
.done(function (response) {
$( '.autoupdate_enabled span' ).html( response.data.enabled_count );
$( '.autoupdate_disabled span' ).html( response.data.disabled_count );
$parent.html( response.data.return_html );
$parent.find('.plugin-autoupdate-enable').focus();
wp.a11y.speak( wp_autoupdates.auto_disabled, 'polite' );
if ( response.success ) {
$( '.autoupdate_enabled span' ).html( response.data.enabled_count );
$( '.autoupdate_disabled span' ).html( response.data.disabled_count );
$parent.html( response.data.return_html );
$parent.find('.plugin-autoupdate-enable').focus();
wp.a11y.speak( wp_autoupdates.auto_disabled, 'polite' );
} else {
var errorHTML = add_error_notice( html, response.data.error );
wp.a11y.speak( response.data.error, 'polite' );
$parent.html( errorHTML );
}
})
.fail(function (response) {
// todo - Better error handling.
alert( response.data.error );
var errorHTML = add_error_notice( html, wp_autoupdates.auto_update_error );
wp.a11y.speak( wp_autoupdates.auto_update_error, 'polite' );
$parent.html( errorHTML );
})
.always(function (response) {
});
Expand All @@ -43,6 +58,9 @@ jQuery(function ($) {
$anchor.blur();
var href = wpAjax.unserialize( $anchor.attr( 'href' ) );
var $parent = $anchor.parents( '.autoupdates_column' );
// Clear errors
$parent.find( '.notice' ).remove();
var html = $parent.html();

// Show loading status.
$anchor.addClass( 'spin' ).find( '.plugin-autoupdate-label' ).html( '<span class="dashicons dashicons-update spin"></span> ' + wp_autoupdates.enabling );
Expand All @@ -60,15 +78,22 @@ jQuery(function ($) {
}
)
.done(function (response) {
$( '.autoupdate_enabled span' ).html( response.data.enabled_count );
$( '.autoupdate_disabled span' ).html( response.data.disabled_count );
$parent.html( response.data.return_html );
$parent.find('.plugin-autoupdate-disable').focus();
wp.a11y.speak( wp_autoupdates.auto_enabled, 'polite' );
if ( response.success ) {
$( '.autoupdate_enabled span' ).html( response.data.enabled_count );
$( '.autoupdate_disabled span' ).html( response.data.disabled_count );
$parent.html( response.data.return_html );
$parent.find('.plugin-autoupdate-disable').focus();
wp.a11y.speak( wp_autoupdates.auto_enabled, 'polite' );
} else {
var errorHTML = add_error_notice( html, response.data.error );
wp.a11y.speak( response.data.error, 'polite' );
$parent.html( errorHTML );
}
})
.fail(function (response) {
// todo - Better error handling.
alert( response.data.error );
var errorHTML = add_error_notice( html, wp_autoupdates.auto_update_error );
wp.a11y.speak( wp_autoupdates.auto_update_error, 'polite' );
$parent.html( errorHTML );
})
.always(function (response) {
});
Expand All @@ -80,6 +105,9 @@ jQuery(function ($) {
$anchor.blur();
var href = wpAjax.unserialize($anchor.attr( 'href' ) );
var $parent = $anchor.parents( '.autoupdates_column' );
// Clear errors
$parent.find( '.notice' ).remove();
var html = $parent.html();

// Show loading status.
$anchor.html( '<span class="dashicons dashicons-update spin"></span> ' + wp_autoupdates.disabling );
Expand All @@ -97,15 +125,22 @@ jQuery(function ($) {
}
)
.done(function (response) {
$( '.autoupdate_enabled span' ).html( response.data.enabled_count );
$( '.autoupdate_disabled span' ).html( response.data.disabled_count );
$parent.html( response.data.return_html );
$parent.find('.theme-autoupdate-enable').focus();
wp.a11y.speak( wp_autoupdates.auto_disabled, 'polite' );
if ( response.success ) {
$( '.autoupdate_enabled span' ).html( response.data.enabled_count );
$( '.autoupdate_disabled span' ).html( response.data.disabled_count );
$parent.html( response.data.return_html );
$parent.find('.theme-autoupdate-enable').focus();
wp.a11y.speak( wp_autoupdates.auto_disabled, 'polite' );
} else {
var errorHTML = add_error_notice( html, response.data.error );
wp.a11y.speak( response.data.error, 'polite' );
$parent.html( errorHTML );
}
})
.fail(function (response) {
// todo - Better error handling.
alert( response.data.error );
var errorHTML = add_error_notice( html, wp_autoupdates.auto_update_error );
wp.a11y.speak( wp_autoupdates.auto_update_error, 'polite' );
$parent.html( errorHTML );
})
.always(function (response) {
});
Expand All @@ -117,6 +152,9 @@ jQuery(function ($) {
$anchor.blur();
var href = wpAjax.unserialize( $anchor.attr( 'href' ) );
var $parent = $anchor.parents( '.autoupdates_column' );
// Clear errors
$parent.find( '.notice' ).remove();
var html = $parent.html();

// Show loading status.
$anchor.addClass( 'spin' ).find( '.theme-autoupdate-label' ).html( '<span class="dashicons dashicons-update spin"></span> ' + wp_autoupdates.enabling );
Expand All @@ -134,15 +172,22 @@ jQuery(function ($) {
}
)
.done(function (response) {
$( '.autoupdate_enabled span' ).html( response.data.enabled_count );
$( '.autoupdate_disabled span' ).html( response.data.disabled_count );
$parent.html( response.data.return_html );
$parent.find('.theme-autoupdate-disable').focus();
wp.a11y.speak( wp_autoupdates.auto_enabled, 'polite' );
if ( response.success ) {
$( '.autoupdate_enabled span' ).html( response.data.enabled_count );
$( '.autoupdate_disabled span' ).html( response.data.disabled_count );
$parent.html( response.data.return_html );
$parent.find('.theme-autoupdate-disable').focus();
wp.a11y.speak( wp_autoupdates.auto_enabled, 'polite' );
} else {
var errorHTML = add_error_notice( html, response.data.error );
wp.a11y.speak( response.data.error, 'polite' );
$parent.html( errorHTML );
}
})
.fail(function (response) {
// todo - Better error handling.
alert( response.data.error );
var errorHTML = add_error_notice( html, wp_autoupdates.auto_update_error );
wp.a11y.speak( wp_autoupdates.auto_update_error, 'polite' );
$parent.html( errorHTML );
})
.always(function (response) {
});
Expand All @@ -154,6 +199,9 @@ jQuery(function ($) {
$anchor.blur();
var href = wpAjax.unserialize($anchor.attr( 'href' ) );
var $parent = $anchor.parents( '.theme-autoupdate' );
// Clear errors
$parent.find( '.notice' ).remove();
var html = $parent.html();

// Show loading status.
$anchor.html( '<span class="dashicons dashicons-update spin"></span> ' + wp_autoupdates.disabling );
Expand All @@ -171,13 +219,20 @@ jQuery(function ($) {
}
)
.done(function (response) {
$parent.html( response.data.return_html );
$parent.find('.theme-autoupdate-enable').focus();
wp.a11y.speak( wp_autoupdates.auto_disabled, 'polite' );
if ( response.success ) {
$parent.html( response.data.return_html );
$parent.find('.theme-autoupdate-enable').focus();
wp.a11y.speak( wp_autoupdates.auto_disabled, 'polite' );
} else {
var errorHTML = add_error_notice( html, response.data.error );
wp.a11y.speak( response.data.error, 'polite' );
$parent.html( errorHTML );
}
})
.fail(function (response) {
// todo - Better error handling.
alert( response.data.error );
var errorHTML = add_error_notice( html, wp_autoupdates.auto_update_error );
wp.a11y.speak( wp_autoupdates.auto_update_error, 'polite' );
$parent.html( errorHTML );
})
.always(function (response) {
});
Expand All @@ -189,6 +244,9 @@ jQuery(function ($) {
$anchor.blur();
var href = wpAjax.unserialize( $anchor.attr( 'href' ) );
var $parent = $anchor.parents( '.theme-autoupdate' );
// Clear errors
$parent.find( '.notice' ).remove();
var html = $parent.html();

// Show loading status.
$parent.find( '.theme-autoupdate-label' ).html( '<span class="dashicons dashicons-update spin"></span> ' + wp_autoupdates.enabling );
Expand All @@ -206,13 +264,21 @@ jQuery(function ($) {
}
)
.done(function (response) {
$parent.html( response.data.return_html );
$parent.find('.theme-autoupdate-disable').focus();
wp.a11y.speak( wp_autoupdates.auto_enabled, 'polite' );
if ( response.success ) {
$parent.html( response.data.return_html );
$parent.find('.theme-autoupdate-disable').focus();
wp.a11y.speak( wp_autoupdates.auto_enabled, 'polite' );
} else {
var errorHTML = add_error_notice( html, response.data.error );
wp.a11y.speak( response.data.error, 'polite' );
$parent.html( errorHTML );
}

})
.fail(function (response) {
// todo - Better error handling.
alert( response.data.error );
var errorHTML = add_error_notice( html, wp_autoupdates.auto_update_error );
wp.a11y.speak( wp_autoupdates.auto_update_error, 'polite' );
$parent.html( errorHTML );
})
.always(function (response) {
});
Expand Down

0 comments on commit 2d36c0a

Please sign in to comment.