From d2a18169708aef007b5688491e27460b68c1b65d Mon Sep 17 00:00:00 2001 From: Dovid Levine Date: Fri, 13 Mar 2026 01:22:01 +0200 Subject: [PATCH] try: check for wp_supports_ai in `WP_AI_Client_Prompt_Builder::__call()` --- .../ai-client/class-wp-ai-client-prompt-builder.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/ai-client/class-wp-ai-client-prompt-builder.php b/src/wp-includes/ai-client/class-wp-ai-client-prompt-builder.php index f3bfe87a00687..730143be50233 100644 --- a/src/wp-includes/ai-client/class-wp-ai-client-prompt-builder.php +++ b/src/wp-includes/ai-client/class-wp-ai-client-prompt-builder.php @@ -178,11 +178,6 @@ class WP_AI_Client_Prompt_Builder { */ public function __construct( ProviderRegistry $registry, $prompt = null ) { try { - if ( ! wp_supports_ai() ) { - // The catch block will convert this to a WP_Error. - throw new \RuntimeException( __( 'AI features are not supported in this environment.' ) ); - } - $this->builder = new PromptBuilder( $registry, $prompt ); } catch ( Exception $e ) { $this->builder = new PromptBuilder( $registry ); @@ -281,6 +276,14 @@ public function using_abilities( ...$abilities ): self { * @return mixed The result of the method call. */ public function __call( string $name, array $arguments ) { + // Check whether AI is supported before proceeding with any method call. + if ( null === $this->error && ! wp_supports_ai() ) { + $this->error = new WP_Error( + 'ai_not_supported', + __( 'AI features are not supported in this environment.' ) + ); + } + /* * If an error occurred in a previous method call, either return the error for terminate methods, * or return the same instance for other methods to maintain the fluent interface.