Skip to content

Commit d2a1816

Browse files
committed
try: check for wp_supports_ai in WP_AI_Client_Prompt_Builder::__call()
1 parent bb965aa commit d2a1816

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

src/wp-includes/ai-client/class-wp-ai-client-prompt-builder.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,6 @@ class WP_AI_Client_Prompt_Builder {
178178
*/
179179
public function __construct( ProviderRegistry $registry, $prompt = null ) {
180180
try {
181-
if ( ! wp_supports_ai() ) {
182-
// The catch block will convert this to a WP_Error.
183-
throw new \RuntimeException( __( 'AI features are not supported in this environment.' ) );
184-
}
185-
186181
$this->builder = new PromptBuilder( $registry, $prompt );
187182
} catch ( Exception $e ) {
188183
$this->builder = new PromptBuilder( $registry );
@@ -281,6 +276,14 @@ public function using_abilities( ...$abilities ): self {
281276
* @return mixed The result of the method call.
282277
*/
283278
public function __call( string $name, array $arguments ) {
279+
// Check whether AI is supported before proceeding with any method call.
280+
if ( null === $this->error && ! wp_supports_ai() ) {
281+
$this->error = new WP_Error(
282+
'ai_not_supported',
283+
__( 'AI features are not supported in this environment.' )
284+
);
285+
}
286+
284287
/*
285288
* If an error occurred in a previous method call, either return the error for terminate methods,
286289
* or return the same instance for other methods to maintain the fluent interface.

0 commit comments

Comments
 (0)