Skip to content

Commit 4f43aad

Browse files
committed
chore: move support check to __call()
1 parent 069e6d4 commit 4f43aad

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

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

Lines changed: 19 additions & 15 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 );
@@ -297,26 +292,35 @@ public function __call( string $name, array $arguments ) {
297292

298293
// Check if the prompt should be prevented for is_supported* and generate_*/convert_text_to_speech* methods.
299294
if ( self::is_support_check_method( $name ) || self::is_generating_method( $name ) ) {
300-
/**
301-
* Filters whether to prevent the prompt from being executed.
302-
*
303-
* @since 7.0.0
304-
*
305-
* @param bool $prevent Whether to prevent the prompt. Default false.
306-
* @param WP_AI_Client_Prompt_Builder $builder A clone of the prompt builder instance (read-only).
307-
*/
308-
$prevent = (bool) apply_filters( 'wp_ai_client_prevent_prompt', false, clone $this );
295+
// If AI is not supported, then there's no need to apply the filter as the prompt will be prevented anyway.
296+
$is_ai_disabled = ! wp_supports_ai();
297+
$prevent = $is_ai_disabled;
298+
if ( ! $prevent ) {
299+
/**
300+
* Filters whether to prevent the prompt from being executed.
301+
*
302+
* @since 7.0.0
303+
*
304+
* @param bool $prevent Whether to prevent the prompt. Default false.
305+
* @param WP_AI_Client_Prompt_Builder $builder A clone of the prompt builder instance (read-only).
306+
*/
307+
$prevent = (bool) apply_filters( 'wp_ai_client_prevent_prompt', false, clone $this );
308+
}
309309

310310
if ( $prevent ) {
311311
// For is_supported* methods, return false.
312312
if ( self::is_support_check_method( $name ) ) {
313313
return false;
314314
}
315315

316+
$error_message = $is_ai_disabled
317+
? __( 'AI features are not supported in this environment.' )
318+
: __( 'Prompt execution was prevented by a filter.' );
319+
316320
// For generate_* and convert_text_to_speech* methods, create a WP_Error.
317321
$this->error = new WP_Error(
318322
'prompt_prevented',
319-
__( 'Prompt execution was prevented by a filter.' ),
323+
$error_message,
320324
array(
321325
'exception_class' => 'WP_AI_Client_Prompt_Prevented',
322326
)

0 commit comments

Comments
 (0)