Skip to content

Commit c87de09

Browse files
committed
Fix: AI Check model preference not being applied
The model preference was not being applied due to two issues: 1. method_exists() check was failing because WP_AI_Client_Prompt_Builder uses magic __call() method. Changed to directly call the method with try-catch error handling instead. 2. normalize_model_preference() was returning a nested array [['provider', 'model']] instead of the expected ['provider', 'model'] format. Removed the extra array wrapper. This ensures that when users specify a model preference like 'openai::gpt-5.4-pro', it is correctly passed to the WordPress AI client instead of falling back to the default model. Fixes #1212
1 parent df70fd7 commit c87de09

1 file changed

Lines changed: 14 additions & 17 deletions

File tree

includes/Traits/AI_Check_Names.php

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -210,25 +210,22 @@ protected function apply_model_preference( $builder, $model_preference ) {
210210
return $builder;
211211
}
212212

213-
$method = null;
214-
if ( method_exists( $builder, 'using_model_preference' ) ) {
215-
$method = 'using_model_preference';
216-
} elseif ( method_exists( $builder, 'usingModelPreference' ) ) {
217-
$method = 'usingModelPreference';
218-
}
219-
220-
if ( ! $method ) {
221-
return $builder;
222-
}
223-
224213
$preference = $this->normalize_model_preference( $model_preference );
225-
$result = call_user_func( array( $builder, $method ), $preference );
226214

227-
if ( is_wp_error( $result ) ) {
228-
return $result;
215+
try {
216+
$result = $builder->using_model_preference( $preference );
217+
return $result ? $result : $builder;
218+
} catch ( \Exception $e ) {
219+
// If method doesn't exist or fails, return WP_Error.
220+
return new WP_Error(
221+
'model_preference_error',
222+
sprintf(
223+
/* translators: %s: Exception message */
224+
__( 'Failed to apply model preference: %s', 'plugin-check' ),
225+
$e->getMessage()
226+
)
227+
);
229228
}
230-
231-
return $result ? $result : $builder;
232229
}
233230

234231
/**
@@ -249,7 +246,7 @@ protected function normalize_model_preference( $model_preference ) {
249246
if ( false !== strpos( $trimmed, $separator ) ) {
250247
list( $provider, $model ) = array_map( 'trim', explode( $separator, $trimmed, 2 ) );
251248
if ( '' !== $provider && '' !== $model ) {
252-
return array( array( $provider, $model ) );
249+
return array( $provider, $model );
253250
}
254251
}
255252
}

0 commit comments

Comments
 (0)