Basic building blocks for importing with AJAX
// make sure JS file is enqueued and localized
function wpstagecoach_import() {
// verify nonce
// grab the latest finished row from the database
// do the import
// save the latest finished row to the database
if( everything_went_smoothly() && we_still_have_more_to_do() ) {
$result = intval( latest finished row );
} elseif( everything_went_smoothly() && we_are_all_done() ) {
$result = 'finished';
} else {
$result = 'error';
}
die( $result );
}
add_action( 'stagecoach_ajax_wpstagecoach_import', 'wpstagecoach_import' );// The plugin JS file
$(document).ready(function() {
$( "#wpsc-import" ).submit(function( event ) {
event.preventDefault();
$( '#wpsc-import' ).hide();
do_the_import();
});
});
function do_the_import() {
var url = wpsc_ajax.ajaxurl;
var nonce = $( 'NONCE NAME' ).val();
var data = {
'action': 'wpstagecoach_import',
'nonce': nonce,
};
$.post(url, data, function (response) {
if( response ) {
if( 'error' == response ) {
$("#import_results").html('We encountered an error. Please contact support');
} else if( 'finished' == response ) {
var success = 'All done';
$("#import_results").html(success);
} else {
console.log('continue');
var total = 'Updating ' + response + ' of ' + $('#working-on').val() + ' database rows';
$("#import_results").html(total);
do_the_import();
}
}
});
}
/**
* The import page in the plugin
**/
// Form passes data about the last step finished, as well as whatever else you need
<form id="wpsc-import" action="<?php admin_url( 'THIS PAGE' ); ?>" method="post">
<?php wp_nonce_field( 'NONCE ACTION', 'NONCE NAME' ); ?>
<?php $current_step = $this->options['wpstagecoach_import_last_step'];
if( !isset( $current_step ) || '0' == $current_step ) {
$working_on = '1-100'; // or whatever interval you want
} else {
$next_step = intval( $current_step ) + 100;
$working_on = $current_step . '-' . $next_step;
} ?>
<input type="hidden" name="working-on" id="working-on" value="<?php echo $working_on; ?>">
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e( 'Import Now' ) ?>" name="wpsc-import-button" />
</p>
</form>
<p id="import_results"></p>