Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 38 additions & 33 deletions includes/admin/class-error-log.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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' ),
Expand Down Expand Up @@ -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.
Expand All @@ -414,25 +439,13 @@ 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,
)
);
}
} 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(),
Expand Down Expand Up @@ -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 );
}

/**
Expand Down Expand Up @@ -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' );

Expand All @@ -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();
17 changes: 1 addition & 16 deletions includes/admin/js/error-log.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
Expand Down
Loading