diff --git a/includes/admin/class-error-log.php b/includes/admin/class-error-log.php index 8906e6a..c45526e 100644 --- a/includes/admin/class-error-log.php +++ b/includes/admin/class-error-log.php @@ -37,11 +37,38 @@ public function __construct() { add_action( 'wp_ajax_formscrm_resend_entry', array( $this, 'ajax_resend_entry' ) ); add_action( 'wp_ajax_formscrm_delete_log', array( $this, 'ajax_delete_log' ) ); add_action( 'wp_ajax_formscrm_clear_all_logs', array( $this, 'ajax_clear_all_logs' ) ); + add_action( 'wp_ajax_formscrm_export_csv', array( $this, 'ajax_export_csv' ) ); - // Hook for automatic retry cron. + // Hook for automatic retry via Action Scheduler. add_action( 'formscrm_retry_failed_entry', array( $this, 'retry_failed_entry' ), 10, 1 ); } + /** + * Schedule retry using Action Scheduler or WP-Cron fallback + * + * @param int $log_id Log ID to retry. + * @return void + */ + private function schedule_action_scheduler_retry( $log_id ) { + $retry_delay = HOUR_IN_SECONDS; + $timestamp = time() + $retry_delay; + + // Try Action Scheduler first. + if ( function_exists( 'as_schedule_single_action' ) ) { + try { + as_schedule_single_action( $timestamp, 'formscrm_retry_failed_entry', array( $log_id ) ); + return; + } catch ( Exception $e ) { + // Fallback to WP-Cron. + wp_schedule_single_event( $timestamp, 'formscrm_retry_failed_entry', array( $log_id ) ); + return; + } + } + + // Fallback to WP-Cron if Action Scheduler not available. + wp_schedule_single_event( $timestamp, 'formscrm_retry_failed_entry', array( $log_id ) ); + } + /** * Check database version and create/update table if needed * @@ -128,8 +155,8 @@ public function insert_log( $crm, $error, $data, $url = '', $json = '', $form_in if ( $result ) { $log_id = $wpdb->insert_id; - // Schedule automatic retry in 1 hour. - $this->schedule_retry( $log_id ); + // Schedule automatic retry using Action Scheduler (if available). + $this->schedule_action_scheduler_retry( $log_id ); return $log_id; } @@ -358,7 +385,6 @@ public function ajax_resend_entry() { $settings = formscrm_get_crm_settings( $log->form_type ); if ( empty( $settings ) ) { - formscrm_debug_message( 'ERROR: CRM settings not found for form type: ' . $log->form_type ); wp_send_json_error( array( 'message' => __( 'CRM settings not found. Please configure the CRM connection in FormsCRM settings.', 'formscrm' ), @@ -396,11 +422,10 @@ public function ajax_resend_entry() { wp_send_json_error( array( 'message' => $error_msg ) ); } - $this->increment_resend_attempts( $log_id ); try { - $response = $api_class->create_entry( $settings, $lead_data ); + $response = $api_class->create_entry( $settings, $lead_data, $log_id ); - if ( isset( $response['success'] ) && $response['success'] ) { + if ( isset( $response['status'] ) && 'ok' === strtolower( $response['status'] ) ) { $this->update_status( $log_id, 'success' ); // Clear any scheduled retries. @@ -414,12 +439,6 @@ public function ajax_resend_entry() { } else { $error_message = isset( $response['message'] ) ? $response['message'] : __( 'Unknown error occurred', 'formscrm' ); - // Schedule next retry if we haven't reached max attempts. - $log = $this->get_log( $log_id ); - if ( $log && $log->resend_attempts < 3 ) { - $this->schedule_retry( $log_id ); - } - wp_send_json_error( array( 'message' => $error_message, @@ -427,12 +446,6 @@ public function ajax_resend_entry() { ); } } catch ( Exception $e ) { - // Schedule next retry if we haven't reached max attempts. - $log = $this->get_log( $log_id ); - if ( $log && $log->resend_attempts < 3 ) { - $this->schedule_retry( $log_id ); - } - wp_send_json_error( array( 'message' => $e->getMessage(), @@ -503,14 +516,8 @@ private function schedule_retry( $log_id ) { return; } - // Schedule retry in 1 hour. - $timestamp = time() + HOUR_IN_SECONDS; - - // Clear any existing scheduled retry for this log. - wp_clear_scheduled_hook( 'formscrm_retry_failed_entry', array( $log_id ) ); - - // Schedule new retry. - wp_schedule_single_event( $timestamp, 'formscrm_retry_failed_entry', array( $log_id ) ); + // Use Action Scheduler (same as initial schedule). + $this->schedule_action_scheduler_retry( $log_id ); } /** @@ -567,9 +574,9 @@ public function retry_failed_entry( $log_id ) { $this->increment_resend_attempts( $log_id ); try { - $response = $api_class->create_entry( $settings, $lead_data ); + $response = $api_class->create_entry( $settings, $lead_data, $log_id ); - if ( isset( $response['success'] ) && $response['success'] ) { + if ( isset( $response['status'] ) && 'ok' === strtolower( $response['status'] ) ) { // Success - update status. $this->update_status( $log_id, 'success' ); @@ -594,7 +601,5 @@ public function retry_failed_entry( $log_id ) { } // Initialize error log. -if ( is_admin() ) { - global $formscrm_error_log; - $formscrm_error_log = new FORMSCRM_Error_Log(); -} +global $formscrm_error_log; +$formscrm_error_log = new FORMSCRM_Error_Log(); diff --git a/includes/admin/js/error-log.js b/includes/admin/js/error-log.js index 6d31457..2312bcd 100644 --- a/includes/admin/js/error-log.js +++ b/includes/admin/js/error-log.js @@ -46,22 +46,7 @@ success: function(response) { if (response.success) { alert(response.data.message || 'Entry resent successfully'); - - // Update status in table. - const $row = $button.closest('tr'); - $row.find('.fcrm-status') - .removeClass('fcrm-status-error') - .addClass('fcrm-status-success') - .css({ - 'background': '#e8f5e9', - 'color': '#2e7d32' - }) - .text(formscrmErrorLog.successText || 'Success'); - - // Update attempts count. - const $attemptsCell = $row.find('td').eq(5); - const currentAttempts = parseInt($attemptsCell.text()); - $attemptsCell.text(currentAttempts + 1); + location.reload(); } else { alert('Error: ' + (response.data.message || 'Failed to resend entry')); }