is_check_enabled( 'link_density_check_enabled' ); } /** * Evaluate link density. * * @param SubmissionContext $context Normalized submission context. * @param SettingsSnapshot $settings Immutable settings snapshot. */ public function evaluate( SubmissionContext $context, SettingsSnapshot $settings ): CheckResult { $text = implode( ' ', $context->get_field_values() ); if ( '' === trim( $text ) ) { return CheckResult::pass(); } $link_count = $this->count_links( $text ); if ( $link_count < self::MINIMUM_LINKS ) { return CheckResult::pass(); } $token_count = $this->count_tokens( $text ); $density = $link_count / max( 1, $token_count ); if ( $density > $settings->get_link_density_threshold() ) { return CheckResult::suspicious( ReasonCodeRegistry::EXCESSIVE_LINKS ); } return CheckResult::pass(); } /** * Count URLs. * * @param string $text Text to inspect. */ private function count_links( string $text ): int { preg_match_all( '/(?:https?:\/\/|www\.)[^\s<>"\']+/i', $text, $matches ); return count( $matches[0] ); } /** * Count whitespace-delimited tokens. * * @param string $text Text to inspect. */ private function count_tokens( string $text ): int { $tokens = preg_split( '/\s+/', trim( $text ) ); if ( ! is_array( $tokens ) ) { return 0; } return count( array_filter( $tokens, static function ( string $token ): bool { return '' !== $token; } ) ); } }