wpmind/modules/media-intelligence/includes/NsfwDetector.php
LinuxJoy 7f8503e3ad feat: Media Intelligence 模块 — AI 图片 alt text/标题/描述自动生成
新增 Media Intelligence 模块 (v4.3),复用 chat API 多模态能力实现:
- wpmind_vision() 全局函数 + VisionHelper 静态类
- 上传时异步生成 alt text、标题、caption
- NSFW 内容检测(可选)
- 批量处理已有无 alt text 图片(防无限循环)
- 设置管理界面 + 统计面板

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-02-09 11:48:06 +08:00

71 lines
1.6 KiB
PHP

<?php
/**
* NSFW Detector
*
* Checks uploaded images for inappropriate content using the Vision API.
*
* @package WPMind\Modules\MediaIntelligence
* @since 1.0.0
*/
declare(strict_types=1);
namespace WPMind\Modules\MediaIntelligence;
/**
* Class NsfwDetector
*
* Flags uploaded images that contain NSFW content.
*/
class NsfwDetector {
/**
* Constructor — register WordPress hooks.
*/
public function __construct() {
// Check on upload (priority 15, before AltTextGenerator at 20).
add_action( 'add_attachment', [ $this, 'check_attachment' ], 15 );
}
/**
* Check an uploaded attachment for NSFW content.
*
* @param int $attachment_id Attachment post ID.
*/
public function check_attachment( int $attachment_id ): void {
if ( ! function_exists( 'wpmind_vision' ) ) {
return;
}
if ( ! wp_attachment_is_image( $attachment_id ) ) {
return;
}
$image_url = wp_get_attachment_url( $attachment_id );
if ( ! $image_url ) {
return;
}
$result = wpmind_vision( $image_url, 'Is this image NSFW? Reply only "safe" or "nsfw".', [
'max_tokens' => 10,
'temperature' => 0.1,
'context' => 'media_nsfw_check',
] );
if ( is_wp_error( $result ) ) {
return;
}
$verdict = strtolower( trim( $result['content'] ?? '' ) );
if ( str_contains( $verdict, 'nsfw' ) ) {
update_post_meta( $attachment_id, '_wpmind_nsfw_flag', '1' );
/**
* Fires when NSFW content is detected in an uploaded image.
*
* @param int $attachment_id The flagged attachment ID.
*/
do_action( 'wpmind_nsfw_detected', $attachment_id );
}
}
}