Skip to content

Commit 6269f89

Browse files
committed
Add audit logging for webhook errors and fix retry limit comparison
- Log warning when no Kit forms are configured for event type - Log error when queued webhook payload is missing - Fix retry comparison: change >= to > for correct max attempts enforcement
1 parent 827231f commit 6269f89

1 file changed

Lines changed: 19 additions & 1 deletion

File tree

includes/class-webhook-handler.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,19 @@ public function process_webhook( string $input ) {
251251

252252
$api_result = $this->subscribe_to_forms( $active_form_ids, $email, $first_name, $kit_fields, $active_tag_ids );
253253

254+
if ( is_null( $api_result ) ) {
255+
Audit_Log::add(
256+
'webhook_no_forms',
257+
array(
258+
'event_type' => $event_type,
259+
'plugin_id' => (string) $plugin_id,
260+
'email' => $email,
261+
),
262+
'warning'
263+
);
264+
return new \WP_Error( 'no_forms', __( 'No Kit forms configured for this event type.', 'freemkit' ) );
265+
}
266+
254267
if ( is_wp_error( $api_result ) ) {
255268
if ( defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) {
256269
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
@@ -948,6 +961,11 @@ public function queue_webhook_event( string $input ) {
948961
public function process_queued_webhook( string $event_key ): void {
949962
$payload = get_transient( self::QUEUE_PREFIX . $event_key );
950963
if ( ! is_array( $payload ) || empty( $payload['input'] ) ) {
964+
Audit_Log::add(
965+
'webhook_payload_missing',
966+
array( 'event_key' => $event_key ),
967+
'error'
968+
);
951969
return;
952970
}
953971

@@ -961,7 +979,7 @@ public function process_queued_webhook( string $event_key ): void {
961979
$max_attempts = (int) apply_filters( 'freemkit_webhook_max_retries', 3, $event_key );
962980
$next_attempts = $attempts + 1;
963981

964-
if ( $next_attempts >= $max_attempts ) {
982+
if ( $next_attempts > $max_attempts ) {
965983
if ( defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) {
966984
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
967985
error_log( sprintf( '[FreemKit] Webhook dropped after retries (%s): %s', $event_key, $result->get_error_message() ) );

0 commit comments

Comments
 (0)