Skip to content

Commit

Permalink
Merge pull request #2877 from dparker1005/halt-subs-migration-on-fail
Browse files Browse the repository at this point in the history
If a sub fails to migrate, throw an error and halt AJAX
  • Loading branch information
ideadude authored Mar 1, 2024
2 parents 513102f + dc5df76 commit 6d190eb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
14 changes: 14 additions & 0 deletions includes/updates/upgrade_3_0.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,20 @@ function pmpro_upgrade_3_0_ajax() {
);
$subscriptions = PMPro_Subscription::get_subscriptions( $subscription_search_param );

// Check if the migration was successful.
$failed_migrations = array();
foreach ( $subscriptions as $subscription ) {
// This is the same check we used to get subs to update. We want to avoid infinite loops.
if ( empty( $subscription->get_billing_amount() ) && empty( $subscription->get_cycle_number() ) ) {
$failed_migrations[] = $subscription->get_id();
}
}
if ( ! empty( $failed_migrations ) ) {
// If we have failed migrations, echo the error.
echo '[error] Failed to migrate subscriptions: ' . implode( ', ', $failed_migrations );
return;
}

// Get the number of subs with a billing amount or cycle number.
$migrated = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->pmpro_subscriptions} WHERE billing_amount > 0 OR cycle_number > 0" );

Expand Down
29 changes: 26 additions & 3 deletions js/updates.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ jQuery(document).ready(function() {
var $count = 0;
var $title = document.title;
var $cycles = ['|','/','-','\\'];
var $timeout = 30; // 30 seconds
var $error_count = 0;

//start updates and update status
if($status && $status.length > 0)
Expand All @@ -14,13 +16,34 @@ jQuery(document).ready(function() {
function pmpro_updates()
{
jQuery.ajax({
url: ajaxurl,type:'GET', timeout: 30000,
url: ajaxurl,type:'GET', timeout: $timeout * 1000,
dataType: 'html',
data: 'action=pmpro_updates',
error: function(xml){
alert('Error with update. Try refreshing.');
error: function( xml, status, error ) {
$error_count++;
// If we haven't failed 3 times, try again.
if ( $error_count < 3 ) {
$status.html($status.html() + ( status == 'timeout' ? 't ' : 'x ') );
// Wait for a second to let things settle and then try again.
setTimeout(function() { pmpro_updates();}, 1000);
} else {
// We have failed 3 times. Alert the user and stop the updates.
if ( status == 'timeout' ) {
if ( window.confirm( 'Timeout error after ' + $timeout + ' seconds. Would you like to try again with a ' + ( $timeout + 30 ) + ' second timeout?' ) ) {
$timeout = $timeout + 30;
pmpro_updates();
}
} else if ( status == 'error' && error ) {
// Likely the case with a PHP error.
alert( error + '. Try refreshing. If this error occurs again, check your PHP error logs or seek help on the PMPro member forums.');
} else if ( status == 'error' ) {
// Likely the case if the user tries to nagivate away from the update page.
alert( 'This update could not complete. Try refreshing. If this error occurs again, seek help on the PMPro member forums.');
}
}
},
success: function(responseHTML){
$error_count = 0;
if (responseHTML.indexOf('[error]') > -1)
{
alert('Error while running update: ' + responseHTML + ' Try refreshing. If this error occurs again, seek help on the PMPro member forums.');
Expand Down

0 comments on commit 6d190eb

Please sign in to comment.