forked from WordPress/ai
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.php
More file actions
322 lines (283 loc) · 7.69 KB
/
helpers.php
File metadata and controls
322 lines (283 loc) · 7.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php
/**
* Helper functions for the AI plugin.
*
* @package WordPress\AI
*/
declare( strict_types=1 );
namespace WordPress\AI;
use Throwable;
use WordPress\AI_Client\AI_Client;
/**
* Normalizes the content by cleaning it and removing unwanted HTML tags.
*
* @since 0.1.0
*
* @param string $content The content to normalize.
* @return string The normalized content.
*/
function normalize_content( string $content ): string {
/**
* Hook to filter content before cleaning it.
*
* @since 0.1.0
*
* @param string $post_content The post content.
*
* @return string The filtered Post content.
*/
$content = (string) apply_filters( 'ai_experiments_pre_normalize_content', $content );
// Strip HTML entities.
$content = preg_replace( '/&#?[a-z0-9]{2,8};/i', '', $content );
// Replace HTML linebreaks with newlines.
$content = preg_replace( '#<br\s?/?>#', "\n\n", (string) $content );
// Strip all HTML tags.
$content = wp_strip_all_tags( (string) $content );
// Remove unrendered shortcode tags.
$content = preg_replace( '#\[.+\](.+)\[/.+\]#', '$1', $content );
/**
* Filters the normalized content to allow for additional cleanup.
*
* @since 0.1.0
*
* @param string $content The normalized content.
*
* @return string The filtered normalized content.
*/
$content = (string) apply_filters( 'ai_experiments_normalize_content', (string) $content );
return trim( $content );
}
/**
* Returns the context for the given post ID.
*
* @since 0.1.0
*
* @param int $post_id The ID of the post to get the context for.
* @return array<string, string> The context for the given post ID.
*/
function get_post_context( int $post_id ): array {
$context = array();
// Get the post details using the get-post-details ability.
$details_ability = wp_get_ability( 'ai/get-post-details' );
if ( $details_ability ) {
$details = $details_ability->execute( array( 'post_id' => $post_id ) );
if ( is_array( $details ) ) {
$context = array_merge( $context, $details );
if ( isset( $context['content'] ) ) {
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
$context['content'] = normalize_content( (string) apply_filters( 'the_content', $context['content'] ) );
}
if ( isset( $context['title'] ) ) {
$context['current_title'] = $context['title'];
unset( $context['title'] );
}
if ( isset( $context['type'] ) ) {
$context['content_type'] = $context['type'];
unset( $context['type'] );
}
}
}
// Get the post terms using the get-terms ability.
$terms_ability = wp_get_ability( 'ai/get-post-terms' );
if ( $terms_ability ) {
$terms = $terms_ability->execute( array( 'post_id' => $post_id ) );
if ( $terms && ! is_wp_error( $terms ) ) {
$grouped_terms = array();
foreach ( $terms as $term ) {
$grouped_terms[ $term->taxonomy ][] = $term->name;
}
$context = array_merge(
$context,
array_map(
static fn( array $term_names ): string => implode( ', ', $term_names ),
$grouped_terms
)
);
}
}
return $context;
}
/**
* Returns the preferred models.
*
* @since 0.1.0
*
* @return array<int, array{string, string}> The preferred models.
*/
function get_preferred_models(): array {
$preferred_models = array(
array(
'anthropic',
'claude-haiku-4-5',
),
array(
'google',
'gemini-2.5-flash',
),
array(
'openai',
'gpt-4o-mini',
),
array(
'openai',
'gpt-4.1',
),
);
/**
* Filters the preferred models.
*
* @since 0.1.0
*
* @param array<int, array{string, string}> $preferred_models The preferred models.
* @return array<int, array{string, string}> The filtered preferred models.
*/
return (array) apply_filters( 'ai_experiments_preferred_models', $preferred_models );
}
/**
* Returns the preferred image models.
*
* @since x.x.x
*
* @return array<int, array{string, string}> The preferred image models.
*/
function get_preferred_image_models(): array {
$preferred_models = array(
array(
'google',
'gemini-3-pro-image-preview',
),
array(
'google',
'gemini-2.5-flash-image',
),
array(
'google',
'imagen-4.0-generate-001',
),
array(
'openai',
'gpt-image-1',
),
array(
'openai',
'dall-e-3',
),
);
/**
* Filters the preferred image models.
*
* @since x.x.x
*
* @param array<int, array{string, string}> $preferred_models The preferred image models.
* @return array<int, array{string, string}> The filtered preferred image models.
*/
return (array) apply_filters( 'ai_experiments_preferred_image_models', $preferred_models );
}
/**
* Checks if we have AI credentials set.
*
* @since 0.1.0
*
* @return bool True if we have AI credentials, false otherwise.
*/
function has_ai_credentials(): bool {
$credentials = get_option( 'wp_ai_client_provider_credentials', array() );
// If there are no credentials, return false.
if ( ! is_array( $credentials ) || empty( $credentials ) ) {
return false;
}
// If all of the AI keys are empty, return false; otherwise, return true.
return ! empty(
array_filter(
$credentials,
static function ( $api_key ): bool {
return is_string( $api_key ) && '' !== $api_key;
}
)
);
}
/**
* Checks if we have valid AI credentials.
*
* @since 0.1.0
*
* @return bool True if we have valid AI credentials, false otherwise.
*/
function has_valid_ai_credentials(): bool {
// If we have no AI credentials, return false.
if ( ! has_ai_credentials() ) {
return false;
}
/**
* Filters whether valid AI credentials are available.
*
* Allows overriding the credentials check, useful for testing.
*
* @since 0.1.0
*
* @param bool|null $has_valid_credentials Whether valid credentials are available. Return null to use default check.
* @return bool|null True if valid credentials are available, false otherwise, or null to use default check.
*/
$valid = apply_filters( 'ai_experiments_pre_has_valid_credentials_check', null );
if ( null !== $valid ) {
return (bool) $valid;
}
// See if we have credentials that give us access to generate text.
try {
return AI_Client::prompt( 'Test' )->is_supported_for_text_generation();
} catch ( Throwable $t ) {
return false;
}
}
/**
* Returns the AI icon as an inline SVG.
*
* @since 0.1.0
*
* @param string $width The width of the icon.
* @param string $height The height of the icon.
* @return string The AI icon SVG markup.
*/
function get_ai_icon_svg( string $width = '1em', string $height = '1em' ): string {
static $svg_content = null;
if ( null === $svg_content ) {
$svg_path = dirname( __DIR__ ) . '/assets/images/ai-icon.svg';
$svg_content = file_exists( $svg_path ) ? file_get_contents( $svg_path ) : '';
}
if ( empty( $svg_content ) ) {
return '';
}
// Add width and height attributes, and fill="currentColor" for theme compatibility.
return preg_replace(
'/<svg\b/',
sprintf( '<svg width="%s" height="%s" fill="currentColor"', esc_attr( $width ), esc_attr( $height ) ),
$svg_content,
1
);
}
/**
* Returns the AI icon as a base64 data URI for use in admin menu icons.
*
* @since 0.1.0
*
* @return string The base64-encoded data URI for the AI icon.
*/
function get_ai_icon_data_uri(): string {
static $data_uri = null;
if ( null === $data_uri ) {
$svg_path = dirname( __DIR__ ) . '/assets/images/ai-icon.svg';
if ( file_exists( $svg_path ) ) {
$svg_content = file_get_contents( $svg_path );
if ( false === $svg_content ) {
$data_uri = '';
return $data_uri;
}
// Replace currentColor with a neutral color for admin menu compatibility.
$svg_content = str_replace( 'fill="currentColor"', 'fill="black"', $svg_content );
$data_uri = 'data:image/svg+xml;base64,' . base64_encode( $svg_content );
} else {
$data_uri = '';
}
}
return $data_uri;
}