Skip to content

Commit 069e6d4

Browse files
committed
chore: revert __call() check in favor of constructor
1 parent 9773705 commit 069e6d4

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

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

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -297,35 +297,26 @@ public function __call( string $name, array $arguments ) {
297297

298298
// Check if the prompt should be prevented for is_supported* and generate_*/convert_text_to_speech* methods.
299299
if ( self::is_support_check_method( $name ) || self::is_generating_method( $name ) ) {
300-
// If AI is not supported, then there's no need to apply the filter as the prompt will be prevented anyway.
301-
$is_ai_disabled = ! wp_supports_ai();
302-
$prevent = $is_ai_disabled;
303-
if ( ! $prevent ) {
304-
/**
305-
* Filters whether to prevent the prompt from being executed.
306-
*
307-
* @since 7.0.0
308-
*
309-
* @param bool $prevent Whether to prevent the prompt. Default false.
310-
* @param WP_AI_Client_Prompt_Builder $builder A clone of the prompt builder instance (read-only).
311-
*/
312-
$prevent = (bool) apply_filters( 'wp_ai_client_prevent_prompt', false, clone $this );
313-
}
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 );
314309

315310
if ( $prevent ) {
316311
// For is_supported* methods, return false.
317312
if ( self::is_support_check_method( $name ) ) {
318313
return false;
319314
}
320315

321-
$error_message = $is_ai_disabled
322-
? __( 'AI features are not supported in this environment.' )
323-
: __( 'Prompt execution was prevented by a filter.' );
324-
325316
// For generate_* and convert_text_to_speech* methods, create a WP_Error.
326317
$this->error = new WP_Error(
327318
'prompt_prevented',
328-
$error_message,
319+
__( 'Prompt execution was prevented by a filter.' ),
329320
array(
330321
'exception_class' => 'WP_AI_Client_Prompt_Prevented',
331322
)

tests/phpunit/tests/ai-client/wpAiClientPromptBuilder.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2395,6 +2395,19 @@ public function test_using_ability_method_chaining() {
23952395
$this->assertEquals( 500, $config->getMaxTokens() );
23962396
}
23972397

2398+
/**
2399+
* Tests that is_supported returns false when prevent prompt filter returns true.
2400+
*
2401+
* @ticket 64591
2402+
*/
2403+
public function test_is_supported_returns_false_when_ai_not_supported() {
2404+
add_filter( 'wp_supports_ai', '__return_false' );
2405+
2406+
$builder = new WP_AI_Client_Prompt_Builder( AiClient::defaultRegistry(), 'Test prompt' );
2407+
2408+
$this->assertFalse( $builder->is_supported() );
2409+
}
2410+
23982411
/**
23992412
* Tests that is_supported returns false when prevent prompt filter returns true.
24002413
*
@@ -2407,7 +2420,6 @@ public function test_is_supported_returns_false_when_filter_prevents_prompt() {
24072420

24082421
$this->assertFalse( $builder->is_supported() );
24092422
}
2410-
24112423
/**
24122424
* Tests that generate_result returns WP_Error when prevent prompt filter returns true.
24132425
*

0 commit comments

Comments
 (0)