| 1 |
<?php |
| 2 |
/** |
| 3 |
* Desktop Mode — AI Copilot WP-Cron jobs. |
| 4 |
* |
| 5 |
* Registers the async comment-analysis hook that calls OpenAI and stores |
| 6 |
* the spam/harmful verdict. The job is scheduled by the comment hook in |
| 7 |
* `hooks.php` with a short delay so it runs outside the HTTP request that |
| 8 |
* triggered the comment — keeping moderation snappy even when the OpenAI |
| 9 |
* API is slow. |
| 10 |
* |
| 11 |
* Comment analysis is the only auto-analysis the copilot performs; posts, |
| 12 |
* pages, and terms are no longer analyzed. |
| 13 |
* |
| 14 |
* Hook name: |
| 15 |
* desktop_mode_ai_analyze_comment ($comment_id, $user_id) |
| 16 |
* |
| 17 |
* @package WPDesktopMode |
| 18 |
*/ |
| 19 |
|
| 20 |
defined( 'ABSPATH' ) || exit; |
| 21 |
|
| 22 |
// --------------------------------------------------------------------------- |
| 23 |
// Job: comment |
| 24 |
// --------------------------------------------------------------------------- |
| 25 |
|
| 26 |
/** |
| 27 |
* Analyzes a comment and stores the result in comment meta. |
| 28 |
* |
| 29 |
* @since 0.14.0 |
| 30 |
* |
| 31 |
* @param int $comment_id The comment ID. |
| 32 |
* @param int $user_id The ID of the user whose API key to use. |
| 33 |
*/ |
| 34 |
function desktop_mode_ai_job_analyze_comment( $comment_id, $user_id ) { |
| 35 |
$comment_id = (int) $comment_id; |
| 36 |
$user_id = (int) $user_id; |
| 37 |
|
| 38 |
if ( ! desktop_mode_ai_is_enabled( $user_id ) ) { |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
$comment = get_comment( $comment_id ); |
| 43 |
if ( ! $comment instanceof WP_Comment ) { |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
$api_key = desktop_mode_ai_get_api_key( $user_id ); |
| 48 |
$messages = desktop_mode_ai_messages_for_comment( $comment ); |
| 49 |
$schema = desktop_mode_ai_schema_comment(); |
| 50 |
|
| 51 |
$result = desktop_mode_ai_provider_structured_request( $user_id, $api_key, $messages, $schema, 'comment_analysis' ); |
| 52 |
|
| 53 |
if ( is_wp_error( $result ) ) { |
| 54 |
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 55 |
error_log( '[WP Desktop Mode AI] Comment ' . $comment_id . ' analysis failed: ' . $result->get_error_message() ); |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
desktop_mode_ai_save_meta( 'comment', $comment_id, $result ); |
| 60 |
|
| 61 |
/** |
| 62 |
* Fires after a comment has been successfully analyzed by the AI copilot. |
| 63 |
* |
| 64 |
* The `$result` array always contains `topic`, `ai_summary`, `harmful`, |
| 65 |
* and `spam`. Downstream plugins (e.g. a moderation helper) can act on |
| 66 |
* `harmful` or `spam` here rather than polling meta. |
| 67 |
* |
| 68 |
* @since 0.14.0 |
| 69 |
* |
| 70 |
* @param int $comment_id The comment ID. |
| 71 |
* @param array $result The structured analysis result. |
| 72 |
* @param WP_Comment $comment The comment object. |
| 73 |
*/ |
| 74 |
do_action( 'desktop_mode_ai_comment_analyzed', $comment_id, $result, $comment ); |
| 75 |
} |
| 76 |
add_action( 'desktop_mode_ai_analyze_comment', 'desktop_mode_ai_job_analyze_comment', 10, 2 ); |
| 77 |
|