Skip to content

Commit 4015d55

Browse files
committed
Show error if unable to fetch voices and display disabled voice selector if voices are not present.
1 parent 20c9456 commit 4015d55

File tree

3 files changed

+52
-39
lines changed

3 files changed

+52
-39
lines changed

includes/Classifai/Providers/ElevenLabs/ElevenLabs.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,18 @@ public function sanitize_api_key_settings( array $new_settings = [], array $sett
186186
if ( $this->feature_instance instanceof TextToSpeech ) {
187187
// Get the available voices.
188188
$voices = $this->get_voices( $new_settings[ static::ID ]['api_key'] ?? '' );
189-
if ( ! is_wp_error( $voices ) && ! empty( $voices ) ) {
190-
$new_settings[ static::ID ]['voices'] = $voices;
189+
190+
if ( is_wp_error( $voices ) ) {
191+
$new_settings[ static::ID ]['authenticated'] = false;
192+
$new_settings[ static::ID ]['voices'] = [];
193+
add_settings_error(
194+
'api_key',
195+
'classifai-elevenlabs-voices-error',
196+
$voices->get_error_message(),
197+
'error'
198+
);
191199
} else {
192-
$new_settings[ static::ID ]['voices'] = [];
200+
$new_settings[ static::ID ]['voices'] = $voices;
193201
}
194202
}
195203
}

includes/Classifai/Providers/ElevenLabs/TextToSpeech.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,12 @@ public function sanitize_settings( array $new_settings ): array {
9595
$new_settings[ static::ID ]['voice'] = sanitize_text_field( $new_settings[ static::ID ]['voice'] ?? $settings[ static::ID ]['voice'] );
9696

9797
// Ensure the model being saved is valid. If not valid or we don't have one, use the first model.
98-
if ( ! in_array( $new_settings[ static::ID ]['model'], array_column( $new_settings[ static::ID ]['models'], 'id' ), true ) ) {
98+
if ( ! empty( $new_settings[ static::ID ]['models'] ) && ! in_array( $new_settings[ static::ID ]['model'], array_column( $new_settings[ static::ID ]['models'], 'id' ), true ) ) {
9999
$new_settings[ static::ID ]['model'] = array_column( $new_settings[ static::ID ]['models'], 'id' )[0];
100100
}
101101

102102
// Ensure the voice being saved is valid. If not valid or we don't have one, use the first voice.
103-
if ( ! in_array( $new_settings[ static::ID ]['voice'], array_column( $new_settings[ static::ID ]['voices'], 'id' ), true ) ) {
103+
if ( ! empty( $new_settings[ static::ID ]['voices'] ) && ! in_array( $new_settings[ static::ID ]['voice'], array_column( $new_settings[ static::ID ]['voices'], 'id' ), true ) ) {
104104
$new_settings[ static::ID ]['voice'] = array_column( $new_settings[ static::ID ]['voices'], 'id' )[0];
105105
}
106106

src/js/settings/components/provider-settings/elevenlabs/text-to-speech.js

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ export const ElevenLabsTextToSpeechSettings = ( { isConfigured = false } ) => {
2929
);
3030
const { setProviderSettings } = useDispatch( STORE_NAME );
3131
const onChange = ( data ) => setProviderSettings( providerName, data );
32+
const voices = providerSettings.voices.length
33+
? providerSettings.voices
34+
: [
35+
{
36+
name: __( '-- Choose Voice --', 'classifai' ),
37+
id: '',
38+
},
39+
];
3240

3341
return (
3442
<>
@@ -44,40 +52,37 @@ export const ElevenLabsTextToSpeechSettings = ( { isConfigured = false } ) => {
4452
onChange={ ( value ) => onChange( { model: value } ) }
4553
modelsDocUrl="https://elevenlabs.io/docs/models"
4654
/>
47-
{ !! providerSettings.voices?.length && (
48-
<SettingsRow
49-
label={ __( 'Voice', 'classifai' ) }
50-
description={
51-
<>
52-
{ __(
53-
'Select the voice for the generated audio. You can find more details on voices',
54-
'classifai'
55-
) }{ ' ' }
56-
<a
57-
href="https://elevenlabs.io/app/voice-library"
58-
target="_blank"
59-
rel="noreferrer"
60-
>
61-
{ __( 'here', 'classifai' ) }
62-
</a>
63-
.
64-
</>
65-
}
66-
>
67-
<SelectControl
68-
id={ `${ providerName }_voice` }
69-
onChange={ ( value ) => onChange( { voice: value } ) }
70-
value={ providerSettings.voice || 'alloy' }
71-
options={ ( providerSettings.voices || [] ).map(
72-
( ele ) => ( {
73-
label: ele.name,
74-
value: ele.id,
75-
} )
76-
) }
77-
__nextHasNoMarginBottom
78-
/>
79-
</SettingsRow>
80-
) }
55+
<SettingsRow
56+
label={ __( 'Voice', 'classifai' ) }
57+
description={
58+
<>
59+
{ __(
60+
'Select the voice for the generated audio. You can find more details on voices',
61+
'classifai'
62+
) }{ ' ' }
63+
<a
64+
href="https://elevenlabs.io/app/voice-library"
65+
target="_blank"
66+
rel="noreferrer"
67+
>
68+
{ __( 'here', 'classifai' ) }
69+
</a>
70+
.
71+
</>
72+
}
73+
>
74+
<SelectControl
75+
id={ `${ providerName }_voice` }
76+
onChange={ ( value ) => onChange( { voice: value } ) }
77+
value={ providerSettings.voice || '' }
78+
options={ voices.map( ( ele ) => ( {
79+
label: ele.name,
80+
value: ele.id,
81+
} ) ) }
82+
disabled={ ! providerSettings.voices?.length }
83+
__nextHasNoMarginBottom
84+
/>
85+
</SettingsRow>
8186
</>
8287
);
8388
};

0 commit comments

Comments
 (0)