@@ -116,74 +116,6 @@ function ( $key ) {
116
116
// Remove the values which keys start with 'otter_'.
117
117
$ body = array_diff_key ( $ body , $ otter_data );
118
118
119
-
120
- if ( isset ( $ otter_data ['otter_used_action ' ] ) && isset ( $ otter_data ['otter_user_content ' ] ) ) {
121
-
122
- $ action = $ otter_data ['otter_used_action ' ];
123
- $ user_content = $ otter_data ['otter_user_content ' ];
124
-
125
- $ usage = get_option ( 'themeisle_otter_ai_usage ' );
126
-
127
- if ( ! is_array ( $ usage ) ) {
128
- $ usage = array (
129
- 'usage_count ' => array (),
130
- 'prompts ' => array (),
131
- );
132
- }
133
-
134
- if ( ! is_array ( $ usage ['usage_count ' ] ) ) {
135
- $ usage ['usage_count ' ] = array ();
136
- }
137
-
138
- if ( ! is_array ( $ usage ['prompts ' ] ) ) {
139
- $ usage ['prompts ' ] = array ();
140
- }
141
-
142
- $ is_missing = true ;
143
-
144
- foreach ( $ usage ['usage_count ' ] as &$ u ) {
145
- if ( isset ( $ u ['key ' ] ) && $ u ['key ' ] === $ action ) {
146
- $ u ['value ' ]++;
147
- $ is_missing = false ;
148
- }
149
- }
150
-
151
- unset( $ u );
152
-
153
- if ( $ is_missing ) {
154
- $ usage ['usage_count ' ][] = array (
155
- 'key ' => $ action ,
156
- 'value ' => 1 ,
157
- );
158
- }
159
-
160
- $ is_missing = true ;
161
-
162
- foreach ( $ usage ['prompts ' ] as &$ u ) {
163
- if ( isset ( $ u ['key ' ] ) && $ u ['key ' ] === $ action ) {
164
- $ u ['values ' ][] = $ user_content ;
165
- $ is_missing = false ;
166
-
167
- // Keep only the last 10 prompts.
168
- if ( count ( $ u ['values ' ] ) > 10 ) {
169
- array_shift ( $ u ['values ' ] );
170
- }
171
- }
172
- }
173
-
174
- unset( $ u );
175
-
176
- if ( $ is_missing ) {
177
- $ usage ['prompts ' ][] = array (
178
- 'key ' => $ action ,
179
- 'values ' => array ( $ user_content ),
180
- );
181
- }
182
-
183
- update_option ( 'themeisle_otter_ai_usage ' , $ usage );
184
- }
185
-
186
-
187
119
$ response = wp_remote_post (
188
120
$ open_ai_endpoint ,
189
121
array (
@@ -343,6 +275,82 @@ public function check_prompt_structure( $response ) {
343
275
return true ;
344
276
}
345
277
278
+ /**
279
+ * Record prompt usage.
280
+ *
281
+ * @param array $otter_metadata The metadata from the prompt usage request.
282
+ * @return void
283
+ * @phpstan-ignore-next-line
284
+ */
285
+ private function record_prompt_usage ( $ otter_metadata ) {
286
+ if ( ! isset ( $ otter_metadata ['otter_used_action ' ] ) || ! isset ( $ otter_metadata ['otter_user_content ' ] ) ) {
287
+ return ;
288
+ }
289
+
290
+ $ action = $ otter_metadata ['otter_used_action ' ];
291
+ $ user_content = $ otter_metadata ['otter_user_content ' ];
292
+
293
+ $ usage = get_option ( 'themeisle_otter_ai_usage ' );
294
+
295
+ if ( ! is_array ( $ usage ) ) {
296
+ $ usage = array (
297
+ 'usage_count ' => array (),
298
+ 'prompts ' => array (),
299
+ );
300
+ }
301
+
302
+ if ( ! is_array ( $ usage ['usage_count ' ] ) ) {
303
+ $ usage ['usage_count ' ] = array ();
304
+ }
305
+
306
+ if ( ! is_array ( $ usage ['prompts ' ] ) ) {
307
+ $ usage ['prompts ' ] = array ();
308
+ }
309
+
310
+ $ is_missing = true ;
311
+
312
+ foreach ( $ usage ['usage_count ' ] as &$ u ) {
313
+ if ( isset ( $ u ['key ' ] ) && $ u ['key ' ] === $ action ) {
314
+ $ u ['value ' ]++;
315
+ $ is_missing = false ;
316
+ }
317
+ }
318
+
319
+ unset( $ u );
320
+
321
+ if ( $ is_missing ) {
322
+ $ usage ['usage_count ' ][] = array (
323
+ 'key ' => $ action ,
324
+ 'value ' => 1 ,
325
+ );
326
+ }
327
+
328
+ $ is_missing = true ;
329
+
330
+ foreach ( $ usage ['prompts ' ] as &$ u ) {
331
+ if ( isset ( $ u ['key ' ] ) && $ u ['key ' ] === $ action ) {
332
+ $ u ['values ' ][] = $ user_content ;
333
+ $ is_missing = false ;
334
+
335
+ // Keep only the last 10 prompts.
336
+ if ( count ( $ u ['values ' ] ) > 10 ) {
337
+ array_shift ( $ u ['values ' ] );
338
+ }
339
+ }
340
+ }
341
+
342
+ unset( $ u );
343
+
344
+ if ( $ is_missing ) {
345
+ $ usage ['prompts ' ][] = array (
346
+ 'key ' => $ action ,
347
+ 'values ' => array ( $ user_content ),
348
+ );
349
+ }
350
+
351
+ update_option ( 'themeisle_otter_ai_usage ' , $ usage );
352
+ }
353
+
346
354
347
355
/**
348
356
* The instance method for the static class.
0 commit comments