PluginProbe
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin / 0.9.0
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin v0.9.0
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.0, at includes/ai-copilot/jobs.php

190 lines 6.1 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 three async job hooks that actually call OpenAI and
6 * store results. Jobs are scheduled by the WordPress hooks in
7 * `hooks.php` with a short delay so they run outside the HTTP
8 * request that triggered the save — keeping the editor snappy even
9 * when the OpenAI API is slow.
10 *
11 * Hook names:
12 * desktop_mode_ai_analyze_post ($post_id, $user_id)
13 * desktop_mode_ai_analyze_term ($term_id, $taxonomy, $user_id)
14 * desktop_mode_ai_analyze_comment ($comment_id, $user_id)
15 *
16 * @package WPDesktopMode
17 */
18
19 defined( 'ABSPATH' ) || exit;
20
21 // ---------------------------------------------------------------------------
22 // Job: post / page
23 // ---------------------------------------------------------------------------
24
25 /**
26 * Analyzes a post or page and stores the result in post meta.
27 *
28 * @since 0.14.0
29 *
30 * @param int $post_id The post ID.
31 * @param int $user_id The ID of the user whose API key to use.
32 */
33 function desktop_mode_ai_job_analyze_post( $post_id, $user_id ) {
34 $post_id = (int) $post_id;
35 $user_id = (int) $user_id;
36
37 if ( ! desktop_mode_ai_is_enabled( $user_id ) ) {
38 return;
39 }
40
41 $post = get_post( $post_id );
42 if ( ! $post instanceof WP_Post ) {
43 return;
44 }
45
46 /** @var array $supported_types Post types the copilot analyzes. */
47 $supported_types = (array) apply_filters(
48 'desktop_mode_ai_supported_post_types',
49 array( 'post', 'page' )
50 );
51 if ( ! in_array( $post->post_type, $supported_types, true ) ) {
52 return;
53 }
54
55 $api_key = desktop_mode_ai_get_api_key( $user_id );
56 $messages = desktop_mode_ai_messages_for_post( $post );
57 $schema = desktop_mode_ai_schema_content();
58
59 $result = desktop_mode_ai_provider_structured_request( $user_id, $api_key, $messages, $schema, 'content_analysis' );
60
61 if ( is_wp_error( $result ) ) {
62 // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
63 error_log( '[WP Desktop Mode AI] Post ' . $post_id . ' analysis failed: ' . $result->get_error_message() );
64 return;
65 }
66
67 desktop_mode_ai_save_meta( 'post', $post_id, $result );
68
69 /**
70 * Fires after a post or page has been successfully analyzed by the AI copilot.
71 *
72 * @since 0.14.0
73 *
74 * @param int $post_id The post ID.
75 * @param array $result The structured analysis result.
76 * @param WP_Post $post The post object.
77 */
78 do_action( 'desktop_mode_ai_post_analyzed', $post_id, $result, $post );
79 }
80 add_action( 'desktop_mode_ai_analyze_post', 'desktop_mode_ai_job_analyze_post', 10, 2 );
81
82 // ---------------------------------------------------------------------------
83 // Job: taxonomy term (category, tag, custom)
84 // ---------------------------------------------------------------------------
85
86 /**
87 * Analyzes a taxonomy term and stores the result in term meta.
88 *
89 * @since 0.14.0
90 *
91 * @param int $term_id The term ID.
92 * @param string $taxonomy The taxonomy slug.
93 * @param int $user_id The ID of the user whose API key to use.
94 */
95 function desktop_mode_ai_job_analyze_term( $term_id, $taxonomy, $user_id ) {
96 $term_id = (int) $term_id;
97 $user_id = (int) $user_id;
98
99 if ( ! desktop_mode_ai_is_enabled( $user_id ) ) {
100 return;
101 }
102
103 $term = get_term( $term_id, $taxonomy );
104 if ( ! $term instanceof WP_Term ) {
105 return;
106 }
107
108 $api_key = desktop_mode_ai_get_api_key( $user_id );
109 $messages = desktop_mode_ai_messages_for_term( $term );
110 $schema = desktop_mode_ai_schema_content();
111
112 $result = desktop_mode_ai_provider_structured_request( $user_id, $api_key, $messages, $schema, 'content_analysis' );
113
114 if ( is_wp_error( $result ) ) {
115 // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
116 error_log( '[WP Desktop Mode AI] Term ' . $term_id . ' (' . $taxonomy . ') analysis failed: ' . $result->get_error_message() );
117 return;
118 }
119
120 desktop_mode_ai_save_meta( 'term', $term_id, $result );
121
122 /**
123 * Fires after a taxonomy term has been successfully analyzed by the AI copilot.
124 *
125 * @since 0.14.0
126 *
127 * @param int $term_id The term ID.
128 * @param array $result The structured analysis result.
129 * @param WP_Term $term The term object.
130 */
131 do_action( 'desktop_mode_ai_term_analyzed', $term_id, $result, $term );
132 }
133 add_action( 'desktop_mode_ai_analyze_term', 'desktop_mode_ai_job_analyze_term', 10, 3 );
134
135 // ---------------------------------------------------------------------------
136 // Job: comment
137 // ---------------------------------------------------------------------------
138
139 /**
140 * Analyzes a comment and stores the result in comment meta.
141 *
142 * @since 0.14.0
143 *
144 * @param int $comment_id The comment ID.
145 * @param int $user_id The ID of the user whose API key to use.
146 */
147 function desktop_mode_ai_job_analyze_comment( $comment_id, $user_id ) {
148 $comment_id = (int) $comment_id;
149 $user_id = (int) $user_id;
150
151 if ( ! desktop_mode_ai_is_enabled( $user_id ) ) {
152 return;
153 }
154
155 $comment = get_comment( $comment_id );
156 if ( ! $comment instanceof WP_Comment ) {
157 return;
158 }
159
160 $api_key = desktop_mode_ai_get_api_key( $user_id );
161 $messages = desktop_mode_ai_messages_for_comment( $comment );
162 $schema = desktop_mode_ai_schema_comment();
163
164 $result = desktop_mode_ai_provider_structured_request( $user_id, $api_key, $messages, $schema, 'comment_analysis' );
165
166 if ( is_wp_error( $result ) ) {
167 // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
168 error_log( '[WP Desktop Mode AI] Comment ' . $comment_id . ' analysis failed: ' . $result->get_error_message() );
169 return;
170 }
171
172 desktop_mode_ai_save_meta( 'comment', $comment_id, $result );
173
174 /**
175 * Fires after a comment has been successfully analyzed by the AI copilot.
176 *
177 * The `$result` array always contains `topic`, `ai_summary`, `harmful`,
178 * and `spam`. Downstream plugins (e.g. a moderation helper) can act on
179 * `harmful` or `spam` here rather than polling meta.
180 *
181 * @since 0.14.0
182 *
183 * @param int $comment_id The comment ID.
184 * @param array $result The structured analysis result.
185 * @param WP_Comment $comment The comment object.
186 */
187 do_action( 'desktop_mode_ai_comment_analyzed', $comment_id, $result, $comment );
188 }
189 add_action( 'desktop_mode_ai_analyze_comment', 'desktop_mode_ai_job_analyze_comment', 10, 2 );
190