event_store = $event_store; $this->settings_repository = $settings_repository; $this->reason_code_registry = $reason_code_registry instanceof ReasonCodeRegistry ? $reason_code_registry : new ReasonCodeRegistry(); $this->retention_service = $retention_service instanceof EventRetentionService ? $retention_service : new EventRetentionService(); } /** * Get recent events allowed by the current logging settings. * * @param int $limit Limit. * * @return array}> */ public function get_recent_events( int $limit = 5 ): array { $settings = $this->settings_repository->get_settings(); $actions = array( SpamEvent::ACTION_BLOCKED, SpamEvent::ACTION_SUSPICIOUS ); if ( ! empty( $settings['log_allowed_events'] ) ) { $actions[] = SpamEvent::ACTION_ALLOWED; } $events = $this->retention_service->filter_events( $this->event_store->get_events(), (int) $settings['event_retention_days'], (int) $settings['max_event_records'] ); $limit = max( 1, min( 20, $limit ) ); $rows = array(); foreach ( $events as $event ) { if ( ! in_array( $event->get_action(), $actions, true ) ) { continue; } $rows[] = array( 'created_at' => $event->get_created_at(), 'source_key' => $event->get_source_key(), 'action' => $event->get_action(), 'score' => $event->get_score(), 'reason_labels' => $this->get_reason_labels( $event->get_reason_codes(), $event->get_action() ), ); if ( count( $rows ) >= $limit ) { break; } } return $rows; } /** * Get safe reason labels. * * @param array $reason_codes Reason codes. * @param string $action Event action. * * @return array */ private function get_reason_labels( array $reason_codes, string $action ): array { $labels = array(); foreach ( $reason_codes as $reason_code ) { $labels[] = $this->reason_code_registry->get_label( $reason_code ); } if ( array() === $labels ) { if ( SpamEvent::ACTION_ALLOWED === $action ) { $labels[] = function_exists( '__' ) ? __( 'No spam signals detected.', '2o2code-spamless-forms' ) : 'No spam signals detected.'; } else { $labels[] = function_exists( '__' ) ? __( 'Spam signal detected.', '2o2code-spamless-forms' ) : 'Spam signal detected.'; } } return $labels; } }