4646 *
4747 * @since 7.0.0
4848 *
49+ * @phpstan-import-type Prompt from PromptBuilder
50+ *
4951 * @method self with_text(string $text) Adds text to the current message.
5052 * @method self with_file($file, ?string $mimeType = null) Adds a file to the current message.
5153 * @method self with_function_response(FunctionResponse $functionResponse) Adds a function response to the current message.
@@ -170,14 +172,14 @@ class WP_AI_Client_Prompt_Builder {
170172 *
171173 * @since 7.0.0
172174 *
173- * @param ProviderRegistry $registry The provider registry for finding suitable models.
174- * @param string|MessagePart|Message|array|list<string|MessagePart|array>|list<Message>|null $prompt Optional. Initial prompt content.
175- * A string for simple text prompts,
176- * a MessagePart or Message object for
177- * structured content, an array for a
178- * message array shape, or a list of
179- * parts or messages for multi-turn
180- * conversations. Default null.
175+ * @param ProviderRegistry $registry The provider registry for finding suitable models.
176+ * @param Prompt $prompt Optional. Initial prompt content.
177+ * A string for simple text prompts,
178+ * a MessagePart or Message object for
179+ * structured content, an array for a
180+ * message array shape, or a list of
181+ * parts or messages for multi-turn
182+ * conversations. Default null.
181183 */
182184 public function __construct ( ProviderRegistry $ registry , $ prompt = null ) {
183185 try {
@@ -289,26 +291,35 @@ public function __call( string $name, array $arguments ) {
289291
290292 // Check if the prompt should be prevented for is_supported* and generate_*/convert_text_to_speech* methods.
291293 if ( self ::is_support_check_method ( $ name ) || self ::is_generating_method ( $ name ) ) {
292- /**
293- * Filters whether to prevent the prompt from being executed.
294- *
295- * @since 7.0.0
296- *
297- * @param bool $prevent Whether to prevent the prompt. Default false.
298- * @param WP_AI_Client_Prompt_Builder $builder A clone of the prompt builder instance (read-only).
299- */
300- $ prevent = (bool ) apply_filters ( 'wp_ai_client_prevent_prompt ' , false , clone $ this );
294+ // If AI is not supported, then there's no need to apply the filter as the prompt will be prevented anyway.
295+ $ is_ai_disabled = ! wp_supports_ai ();
296+ $ prevent = $ is_ai_disabled ;
297+ if ( ! $ prevent ) {
298+ /**
299+ * Filters whether to prevent the prompt from being executed.
300+ *
301+ * @since 7.0.0
302+ *
303+ * @param bool $prevent Whether to prevent the prompt. Default false.
304+ * @param WP_AI_Client_Prompt_Builder $builder A clone of the prompt builder instance (read-only).
305+ */
306+ $ prevent = (bool ) apply_filters ( 'wp_ai_client_prevent_prompt ' , false , clone $ this );
307+ }
301308
302309 if ( $ prevent ) {
303310 // For is_supported* methods, return false.
304311 if ( self ::is_support_check_method ( $ name ) ) {
305312 return false ;
306313 }
307314
315+ $ error_message = $ is_ai_disabled
316+ ? __ ( 'AI features are not supported in this environment. ' )
317+ : __ ( 'Prompt execution was prevented by a filter. ' );
318+
308319 // For generate_* and convert_text_to_speech* methods, create a WP_Error.
309320 $ this ->error = new WP_Error (
310321 'prompt_prevented ' ,
311- __ ( ' Prompt execution was prevented by a filter. ' ) ,
322+ $ error_message ,
312323 array (
313324 'status ' => 503 ,
314325 )
@@ -423,7 +434,8 @@ private static function is_generating_method( string $name ): bool {
423434 protected function get_builder_callable ( string $ name ): callable {
424435 $ camel_case_name = $ this ->snake_to_camel_case ( $ name );
425436
426- if ( ! is_callable ( array ( $ this ->builder , $ camel_case_name ) ) ) {
437+ $ method = array ( $ this ->builder , $ camel_case_name );
438+ if ( ! is_callable ( $ method ) ) {
427439 throw new BadMethodCallException (
428440 sprintf (
429441 /* translators: 1: Method name. 2: Class name. */
@@ -434,7 +446,7 @@ protected function get_builder_callable( string $name ): callable {
434446 );
435447 }
436448
437- return array ( $ this -> builder , $ camel_case_name ) ;
449+ return $ method ;
438450 }
439451
440452 /**
0 commit comments