is_fatal() || get_option( self::OPTION ) === $error->code() ) {
return;
}
update_option( self::OPTION, $error->code(), false );
}
/**
* Clears the notice; called when the key or model is saved.
*/
public static function clear(): void {
delete_option( self::OPTION );
}
/**
* Shows the notice on Fitzaro's own settings tab only (NoticePlace).
*/
public static function render(): void {
$code = get_option( self::OPTION );
if ( ! is_string( $code ) || ! current_user_can( 'manage_woocommerce' ) ) {
return;
}
if ( NoticePlace::SETTINGS !== NoticePlace::current() ) {
return;
}
echo '
' . esc_html__( 'Fitzaro: try-ons are failing.', 'fitzaro' ) . ' '
. esc_html( self::message( $code, Settings::openai_model() ) ) . '
';
}
/**
* What the owner should know about a provider error code ('' = the key works).
*
* @param string $code ProviderError code, or ''.
* @param string $model Model name.
*/
public static function message( string $code, string $model ): string {
switch ( $code ) {
case '':
/* translators: %s: model name. */
return sprintf( __( 'The OpenAI key works with %s.', 'fitzaro' ), $model );
case ProviderError::INVALID_KEY:
return __( 'OpenAI rejected the API key. Check that it is complete and not revoked.', 'fitzaro' );
case ProviderError::NO_QUOTA:
return __( 'The OpenAI account has no credit left or reached its spending limit. Add credit under Billing on platform.openai.com.', 'fitzaro' );
case ProviderError::FORBIDDEN:
/* translators: %s: model name. */
return sprintf( __( 'This key may not use %s. Verify your organisation on platform.openai.com, allow the model for the project, or choose another model.', 'fitzaro' ), $model );
case ProviderError::CONNECTION:
return __( 'This server could not reach OpenAI. Your host may block outgoing HTTPS requests.', 'fitzaro' );
case ProviderError::RATE_LIMITED:
return __( 'OpenAI is rate limiting this key right now. The key itself is accepted.', 'fitzaro' );
default:
/* translators: %s: error code. */
return sprintf( __( 'OpenAI could not check the key (%s). Try again later.', 'fitzaro' ), $code );
}
}
}