diff --git a/assets/js/gf-feed-forge-admin.js b/assets/js/gf-feed-forge-admin.js index d4579bb..224e6f4 100644 --- a/assets/js/gf-feed-forge-admin.js +++ b/assets/js/gf-feed-forge-admin.js @@ -4,6 +4,22 @@ jQuery(function($) { e.preventDefault(); gfffAbortQueue = true; displayMessage(GFFF_ADMIN.abortSuccessMsg, 'error', '#entry_list_form'); + + $.post(ajaxurl, { + action: 'gf_process_feeds', + gf_process_feeds: GFFF_ADMIN.nonce, + abort_processing: true + }).done(function(response) { + if (response.success) { + gfffAbortQueue = true; + $('input[name="feed_process"]').prop('disabled', false); + closeModal(false); + location.reload(); + } + }); + + // Hide the abort button after click + $(this).hide(); }); $('#doaction, #doaction2').click(function () { @@ -87,6 +103,12 @@ jQuery(function($) { total, }, function (response) { + if (gfffAbortQueue || (response.success && response.data.aborted)) { + $('input[name="feed_process"]').prop('disabled', false); + closeModal(false); + return; + } + if (gfffAbortQueue) { $('input[name="feed_process"]').prop('disabled', false); closeModal(false); diff --git a/class-gwiz-gf-feed-forge.php b/class-gwiz-gf-feed-forge.php index d80460e..7c1355d 100644 --- a/class-gwiz-gf-feed-forge.php +++ b/class-gwiz-gf-feed-forge.php @@ -29,6 +29,8 @@ class GWiz_GF_Feed_Forge extends GFAddOn { protected $_short_title = 'Feed Forge'; const TRANSIENT_CURRENT_BATCH_OPTION_NAMES = 'gfff_current_batch_option_names'; + const TRANSIENT_ABORT_FLAG = 'gfff_abort_processing'; + const OPTION_BATCH_NAMES = 'gfff_current_batches'; public static function get_instance() { if ( self::$instance === null ) { @@ -381,8 +383,42 @@ public static function registered_addons() { return apply_filters( 'gfff_registered_addons', $feed_addons ); } + public function abort_processing() { + $batch_names = get_option(self::OPTION_BATCH_NAMES, []); + + foreach ( $batch_names as $batch_name ) { + delete_site_option($batch_name); + } + + delete_option( self::OPTION_BATCH_NAMES ); + delete_transient( self::TRANSIENT_CURRENT_BATCH_OPTION_NAMES ); + set_transient( self::TRANSIENT_ABORT_FLAG, true, 10 ); + } + + public function should_abort() { + return get_transient( self::TRANSIENT_ABORT_FLAG ); + } + + public function track_batch( $batch_name ) { + $batch_names = get_option( self::OPTION_BATCH_NAMES, [] ); + $batch_names[] = $batch_name; + update_option( self::OPTION_BATCH_NAMES, array_unique( $batch_names ) ); + } + public function process_feeds() { check_admin_referer( 'gf_process_feeds', 'gf_process_feeds' ); + + // Handle abort request + if ( rgpost( 'abort_processing' )) { + $this->abort_processing(); + return; + } + + // Check if we should abort + if ( $this->should_abort() ) { + return; + } + $form_id = absint( rgpost( 'formId' ) ); $leads = rgpost( 'leadIds' ); $feeds = json_decode( rgpost( 'feeds' ) ); @@ -478,6 +514,8 @@ public function process_feeds() { $batch_option_names[] = $batch_option_name; + $this->track_batch( $batch_option_name ); + set_transient( self::TRANSIENT_CURRENT_BATCH_OPTION_NAMES, $batch_option_names, DAY_IN_SECONDS ); if ( $count >= $total ) {