PluginProbe
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin / 0.9.2
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin v0.9.2
1.1.10 1.1.9 1.1.8 1.1.7 1.1.6 1.1.5 1.1.4 1.1.3 1.1.2 1.1.1 1.1.0 1.0.1 1.0.0 0.9.8 0.9.7 0.9.6 0.9.4 0.9.5 0.9.3 0.9.2 0.9.1 0.9.0 0.8.9 0.8.8 0.8.7 All 34 releases
desktop-mode / includes / ai-copilot / jobs.php

jobs.php in OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin 0.9.2, at includes/ai-copilot/jobs.php

77 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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