Skip to content

Commit 0207e75

Browse files
committed
Merge branch 'develop' into feature/global-providers
2 parents ac6dfc1 + a58d566 commit 0207e75

File tree

8 files changed

+114
-189
lines changed

8 files changed

+114
-189
lines changed

includes/Classifai/Providers/AWS/AmazonPolly.php

Lines changed: 36 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -198,61 +198,44 @@ public function get_default_provider_settings(): array {
198198
* @return array
199199
*/
200200
public function sanitize_settings( array $new_settings ): array {
201-
$settings = $this->feature_instance->get_settings();
202-
$is_credentials_changed = false;
201+
$settings = $this->feature_instance->get_settings();
202+
$authenticated = $this->authenticate_credentials( $new_settings );
203203

204-
$new_settings[ static::ID ]['authenticated'] = $settings[ static::ID ]['authenticated'];
205-
$new_settings[ static::ID ]['voices'] = $settings[ static::ID ]['voices'];
206-
207-
if (
208-
! empty( $new_settings[ static::ID ]['access_key_id'] ) &&
209-
! empty( $new_settings[ static::ID ]['secret_access_key'] ) &&
210-
! empty( $new_settings[ static::ID ]['aws_region'] )
211-
) {
212-
$new_access_key_id = sanitize_text_field( $new_settings[ static::ID ]['access_key_id'] );
213-
$new_secret_access_key = sanitize_text_field( $new_settings[ static::ID ]['secret_access_key'] );
214-
$new_aws_region = sanitize_text_field( $new_settings[ static::ID ]['aws_region'] );
215-
216-
if (
217-
$new_access_key_id !== $settings[ static::ID ]['access_key_id'] ||
218-
$new_secret_access_key !== $settings[ static::ID ]['secret_access_key'] ||
219-
$new_aws_region !== $settings[ static::ID ]['aws_region']
220-
) {
221-
$is_credentials_changed = true;
222-
}
223-
224-
if ( $is_credentials_changed ) {
225-
$new_settings[ static::ID ]['access_key_id'] = $new_access_key_id;
226-
$new_settings[ static::ID ]['secret_access_key'] = $new_secret_access_key;
227-
$new_settings[ static::ID ]['aws_region'] = $new_aws_region;
228-
229-
// Connect to the service and get voices.
230-
$new_settings[ static::ID ]['voices'] = $this->connect_to_service(
231-
array(
232-
'access_key_id' => $new_access_key_id,
233-
'secret_access_key' => $new_secret_access_key,
234-
'aws_region' => $new_aws_region,
235-
)
236-
);
237-
238-
if ( ! empty( $new_settings[ static::ID ]['voices'] ) ) {
239-
$new_settings[ static::ID ]['authenticated'] = true;
240-
} else {
241-
$new_settings[ static::ID ]['voices'] = [];
242-
$new_settings[ static::ID ]['authenticated'] = false;
243-
}
244-
}
245-
} else {
246-
$new_settings[ static::ID ]['access_key_id'] = $settings[ static::ID ]['access_key_id'];
247-
$new_settings[ static::ID ]['secret_access_key'] = $settings[ static::ID ]['secret_access_key'];
248-
$new_settings[ static::ID ]['aws_region'] = $settings[ static::ID ]['aws_region'];
204+
if ( is_wp_error( $authenticated ) ) {
205+
$new_settings[ static::ID ]['authenticated'] = false;
249206

250207
add_settings_error(
251-
$this->feature_instance->get_option_name(),
252-
'classifai-ams-polly-auth-empty',
253-
esc_html__( 'One or more credentials required to connect to the Amazon Polly service is empty.', 'classifai' ),
208+
'api_key',
209+
'classifai-auth',
210+
$authenticated->get_error_message(),
254211
'error'
255212
);
213+
} else {
214+
$new_settings[ static::ID ]['authenticated'] = true;
215+
}
216+
217+
$new_access_key_id = sanitize_text_field( $new_settings[ static::ID ]['access_key_id'] ?? $settings[ static::ID ]['access_key_id'] );
218+
$new_secret_access_key = sanitize_text_field( $new_settings[ static::ID ]['secret_access_key'] ?? $settings[ static::ID ]['secret_access_key'] );
219+
$new_aws_region = sanitize_text_field( $new_settings[ static::ID ]['aws_region'] ?? $settings[ static::ID ]['aws_region'] );
220+
221+
$new_settings[ static::ID ]['access_key_id'] = $new_access_key_id;
222+
$new_settings[ static::ID ]['secret_access_key'] = $new_secret_access_key;
223+
$new_settings[ static::ID ]['aws_region'] = $new_aws_region;
224+
225+
// Connect to the service and get voices.
226+
$new_settings[ static::ID ]['voices'] = $this->connect_to_service(
227+
array(
228+
'access_key_id' => $new_access_key_id,
229+
'secret_access_key' => $new_secret_access_key,
230+
'aws_region' => $new_aws_region,
231+
)
232+
);
233+
234+
if ( ! empty( $new_settings[ static::ID ]['voices'] ) ) {
235+
$new_settings[ static::ID ]['authenticated'] = true;
236+
} else {
237+
$new_settings[ static::ID ]['voices'] = [];
238+
$new_settings[ static::ID ]['authenticated'] = false;
256239
}
257240

258241
$new_settings[ static::ID ]['voice'] = sanitize_text_field( $new_settings[ static::ID ]['voice'] ?? $settings[ static::ID ]['voice'] );
@@ -273,13 +256,14 @@ public function authenticate_credentials( array $settings = [] ) {
273256
/**
274257
* Filters the return value of the connect to services function.
275258
*
276-
* Returning a non-false value from the filter will short-circuit the describe voices request and return early with that value.
259+
* Returning a non-false value from the filter will short-circuit
260+
* the describe voices request and return early with that value.
277261
* This filter is useful for E2E tests.
278262
*
279263
* @since 3.1.0
280264
* @hook classifai_aws_polly_pre_connect_to_service
281265
*
282-
* @param bool $pre The value of pre connect to service. Default false. non-false value will short-circuit the describe voices request.
266+
* @param bool $pre The value of pre connect to service. Default false. A non-false value will short-circuit the describe voices request.
283267
*
284268
* @return bool|mixed The filtered value of connect to service.
285269
*/

includes/Classifai/Providers/Azure/ComputerVision.php

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -204,47 +204,31 @@ public function get_default_provider_settings(): array {
204204
}
205205

206206
/**
207-
* Sanitization
207+
* Sanitize the settings for this Provider.
208208
*
209209
* @param array $new_settings The settings being saved.
210210
* @return array|mixed
211211
*/
212212
public function sanitize_settings( array $new_settings ) {
213-
$settings = $this->feature_instance->get_settings();
213+
$settings = $this->feature_instance->get_settings();
214+
$authenticated = $this->authenticate_credentials( $new_settings );
214215

215-
if ( ! empty( $new_settings[ static::ID ]['endpoint_url'] ) && ! empty( $new_settings[ static::ID ]['api_key'] ) ) {
216-
$new_settings[ static::ID ]['authenticated'] = $settings[ static::ID ]['authenticated'];
217-
$new_settings[ static::ID ]['endpoint_url'] = esc_url_raw( $new_settings[ static::ID ]['endpoint_url'] ?? $settings[ static::ID ]['endpoint_url'] );
218-
$new_settings[ static::ID ]['api_key'] = sanitize_text_field( $new_settings[ static::ID ]['api_key'] ?? $settings[ static::ID ]['api_key'] );
216+
if ( is_wp_error( $authenticated ) ) {
217+
$new_settings[ static::ID ]['authenticated'] = false;
219218

220-
$is_authenticated = $new_settings[ static::ID ]['authenticated'];
221-
$is_endpoint_same = $new_settings[ static::ID ]['endpoint_url'] === $settings[ static::ID ]['endpoint_url'];
222-
$is_api_key_same = $new_settings[ static::ID ]['api_key'] === $settings[ static::ID ]['api_key'];
223-
224-
if ( ! ( $is_authenticated && $is_endpoint_same && $is_api_key_same ) ) {
225-
$auth_check = $this->authenticate_credentials( $new_settings );
226-
227-
if ( is_wp_error( $auth_check ) ) {
228-
$new_settings[ static::ID ]['authenticated'] = false;
229-
230-
$error_message = $auth_check->get_error_message();
231-
232-
// Add an error message.
233-
add_settings_error(
234-
'api_key',
235-
'classifai-auth',
236-
$error_message,
237-
'error'
238-
);
239-
} else {
240-
$new_settings[ static::ID ]['authenticated'] = true;
241-
}
242-
}
219+
add_settings_error(
220+
'api_key',
221+
'classifai-auth',
222+
$authenticated->get_error_message(),
223+
'error'
224+
);
243225
} else {
244-
$new_settings[ static::ID ]['endpoint_url'] = $settings[ static::ID ]['endpoint_url'];
245-
$new_settings[ static::ID ]['api_key'] = $settings[ static::ID ]['api_key'];
226+
$new_settings[ static::ID ]['authenticated'] = true;
246227
}
247228

229+
$new_settings[ static::ID ]['endpoint_url'] = esc_url_raw( $new_settings[ static::ID ]['endpoint_url'] ?? $settings[ static::ID ]['endpoint_url'] );
230+
$new_settings[ static::ID ]['api_key'] = sanitize_text_field( $new_settings[ static::ID ]['api_key'] ?? $settings[ static::ID ]['api_key'] );
231+
248232
if ( $this->feature_instance instanceof DescriptiveTextGenerator ) {
249233
$new_settings[ static::ID ]['descriptive_confidence_threshold'] = floatval( $new_settings[ static::ID ]['descriptive_confidence_threshold'] ?? $settings[ static::ID ]['descriptive_confidence_threshold'] );
250234
}

includes/Classifai/Providers/Azure/OpenAI.php

Lines changed: 17 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -200,47 +200,26 @@ public function get_default_provider_settings(): array {
200200
* @return array
201201
*/
202202
public function sanitize_settings( array $new_settings ): array {
203-
$settings = $this->feature_instance->get_settings();
204-
205-
if (
206-
! empty( $new_settings[ static::ID ]['endpoint_url'] ) &&
207-
! empty( $new_settings[ static::ID ]['api_key'] ) &&
208-
! empty( $new_settings[ static::ID ]['deployment'] )
209-
) {
210-
$new_settings[ static::ID ]['authenticated'] = $settings[ static::ID ]['authenticated'];
211-
$new_settings[ static::ID ]['endpoint_url'] = esc_url_raw( $new_settings[ static::ID ]['endpoint_url'] ?? $settings[ static::ID ]['endpoint_url'] );
212-
$new_settings[ static::ID ]['api_key'] = sanitize_text_field( $new_settings[ static::ID ]['api_key'] ?? $settings[ static::ID ]['api_key'] );
213-
$new_settings[ static::ID ]['deployment'] = sanitize_text_field( $new_settings[ static::ID ]['deployment'] ?? $settings[ static::ID ]['deployment'] );
214-
215-
$is_authenticated = $new_settings[ static::ID ]['authenticated'];
216-
$is_endpoint_same = $new_settings[ static::ID ]['endpoint_url'] === $settings[ static::ID ]['endpoint_url'];
217-
$is_api_key_same = $new_settings[ static::ID ]['api_key'] === $settings[ static::ID ]['api_key'];
218-
$is_deployment_same = $new_settings[ static::ID ]['deployment'] === $settings[ static::ID ]['deployment'];
219-
220-
if ( ! ( $is_authenticated && $is_endpoint_same && $is_api_key_same && $is_deployment_same ) ) {
221-
$auth_check = $this->authenticate_credentials( $new_settings );
222-
223-
if ( is_wp_error( $auth_check ) ) {
224-
$new_settings[ static::ID ]['authenticated'] = false;
225-
$error_message = $auth_check->get_error_message();
226-
227-
// Add an error message.
228-
add_settings_error(
229-
'api_key',
230-
'classifai-auth',
231-
$error_message,
232-
'error'
233-
);
234-
} else {
235-
$new_settings[ static::ID ]['authenticated'] = true;
236-
}
237-
}
203+
$settings = $this->feature_instance->get_settings();
204+
$authenticated = $this->authenticate_credentials( $new_settings );
205+
206+
if ( is_wp_error( $authenticated ) ) {
207+
$new_settings[ static::ID ]['authenticated'] = false;
208+
209+
add_settings_error(
210+
'api_key',
211+
'classifai-auth',
212+
$authenticated->get_error_message(),
213+
'error'
214+
);
238215
} else {
239-
$new_settings[ static::ID ]['endpoint_url'] = $settings[ static::ID ]['endpoint_url'];
240-
$new_settings[ static::ID ]['api_key'] = $settings[ static::ID ]['api_key'];
241-
$new_settings[ static::ID ]['deployment'] = $settings[ static::ID ]['deployment'];
216+
$new_settings[ static::ID ]['authenticated'] = true;
242217
}
243218

219+
$new_settings[ static::ID ]['endpoint_url'] = esc_url_raw( $new_settings[ static::ID ]['endpoint_url'] ?? $settings[ static::ID ]['endpoint_url'] );
220+
$new_settings[ static::ID ]['api_key'] = sanitize_text_field( $new_settings[ static::ID ]['api_key'] ?? $settings[ static::ID ]['api_key'] );
221+
$new_settings[ static::ID ]['deployment'] = sanitize_text_field( $new_settings[ static::ID ]['deployment'] ?? $settings[ static::ID ]['deployment'] );
222+
244223
switch ( $this->feature_instance::ID ) {
245224
case ContentResizing::ID:
246225
case TitleGeneration::ID:

includes/Classifai/Providers/Azure/Speech.php

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -132,49 +132,40 @@ public function get_default_provider_settings(): array {
132132
* @return array
133133
*/
134134
public function sanitize_settings( array $new_settings ): array {
135-
$settings = $this->feature_instance->get_settings();
136-
$is_credentials_changed = false;
135+
$settings = $this->feature_instance->get_settings();
136+
$authenticated = $this->authenticate_credentials( $new_settings );
137137

138-
$new_settings[ static::ID ]['authenticated'] = $settings[ static::ID ]['authenticated'];
139-
$new_settings[ static::ID ]['voices'] = $settings[ static::ID ]['voices'];
140-
141-
if ( ! empty( $new_settings[ static::ID ]['endpoint_url'] ) && ! empty( $new_settings[ static::ID ]['api_key'] ) ) {
142-
$new_url = trailingslashit( esc_url_raw( $new_settings[ static::ID ]['endpoint_url'] ) );
143-
$new_key = sanitize_text_field( $new_settings[ static::ID ]['api_key'] );
144-
145-
if ( $new_url !== $settings[ static::ID ]['endpoint_url'] || $new_key !== $settings[ static::ID ]['api_key'] ) {
146-
$is_credentials_changed = true;
147-
}
148-
149-
if ( $is_credentials_changed ) {
150-
$new_settings[ static::ID ]['endpoint_url'] = $new_url;
151-
$new_settings[ static::ID ]['api_key'] = $new_key;
152-
153-
// Connect to the service and get voices.
154-
$new_settings[ static::ID ]['voices'] = $this->connect_to_service(
155-
array(
156-
'endpoint_url' => $new_url,
157-
'api_key' => $new_key,
158-
)
159-
);
160-
161-
if ( ! empty( $new_settings[ static::ID ]['voices'] ) ) {
162-
$new_settings[ static::ID ]['authenticated'] = true;
163-
} else {
164-
$new_settings[ static::ID ]['voices'] = [];
165-
$new_settings[ static::ID ]['authenticated'] = false;
166-
}
167-
}
168-
} else {
169-
$new_settings[ static::ID ]['endpoint_url'] = $settings[ static::ID ]['endpoint_url'];
170-
$new_settings[ static::ID ]['api_key'] = $settings[ static::ID ]['api_key'];
138+
if ( is_wp_error( $authenticated ) ) {
139+
$new_settings[ static::ID ]['authenticated'] = false;
171140

172141
add_settings_error(
173-
$this->feature_instance->get_option_name(),
174-
'classifai-azure-text-to-speech-auth-empty',
175-
esc_html__( 'One or more credentials required to connect to the Azure Text to Speech service is empty.', 'classifai' ),
142+
'api_key',
143+
'classifai-auth',
144+
$authenticated->get_error_message(),
176145
'error'
177146
);
147+
} else {
148+
$new_settings[ static::ID ]['authenticated'] = true;
149+
}
150+
151+
$new_settings[ static::ID ]['voices'] = $settings[ static::ID ]['voices'];
152+
153+
$new_settings[ static::ID ]['endpoint_url'] = trailingslashit( esc_url_raw( $new_settings[ static::ID ]['endpoint_url'] ?? $settings[ static::ID ]['endpoint_url'] ) );
154+
$new_settings[ static::ID ]['api_key'] = sanitize_text_field( $new_settings[ static::ID ]['api_key'] ?? $settings[ static::ID ]['api_key'] );
155+
156+
// Connect to the service and get voices.
157+
$new_settings[ static::ID ]['voices'] = $this->connect_to_service(
158+
array(
159+
'endpoint_url' => $new_settings[ static::ID ]['endpoint_url'],
160+
'api_key' => $new_settings[ static::ID ]['api_key'],
161+
)
162+
);
163+
164+
if ( ! empty( $new_settings[ static::ID ]['voices'] ) ) {
165+
$new_settings[ static::ID ]['authenticated'] = true;
166+
} else {
167+
$new_settings[ static::ID ]['voices'] = [];
168+
$new_settings[ static::ID ]['authenticated'] = false;
178169
}
179170

180171
$new_settings[ static::ID ]['voice'] = sanitize_text_field( $new_settings[ static::ID ]['voice'] ?? $settings[ static::ID ]['voice'] );

includes/Classifai/Providers/ElevenLabs/ElevenLabs.php

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -163,22 +163,28 @@ public function request( string $url, string $api_key = '', string $type = 'post
163163
* @return array
164164
*/
165165
public function sanitize_api_key_settings( array $new_settings = [], array $settings = [] ): array {
166-
$models = $this->get_models( $new_settings[ static::ID ]['api_key'] ?? '' );
167-
168-
$new_settings[ static::ID ]['authenticated'] = $settings[ static::ID ]['authenticated'];
169-
$new_settings[ static::ID ]['models'] = $settings[ static::ID ]['models'];
166+
$authenticated = $this->authenticate_credentials( $new_settings );
170167

171-
if ( is_wp_error( $models ) ) {
168+
if ( is_wp_error( $authenticated ) ) {
172169
$new_settings[ static::ID ]['authenticated'] = false;
173-
$new_settings[ static::ID ]['models'] = [];
174-
$error_message = $models->get_error_message();
175170

176171
add_settings_error(
177172
'api_key',
178173
'classifai-auth',
179-
$error_message,
174+
$authenticated->get_error_message(),
180175
'error'
181176
);
177+
} else {
178+
$new_settings[ static::ID ]['authenticated'] = true;
179+
}
180+
181+
$models = $this->get_models( $new_settings[ static::ID ]['api_key'] ?? '' );
182+
183+
$new_settings[ static::ID ]['models'] = $settings[ static::ID ]['models'];
184+
185+
if ( is_wp_error( $models ) ) {
186+
$new_settings[ static::ID ]['authenticated'] = false;
187+
$new_settings[ static::ID ]['models'] = [];
182188
} else {
183189
$new_settings[ static::ID ]['authenticated'] = true;
184190
$new_settings[ static::ID ]['models'] = $models;
@@ -229,11 +235,6 @@ public function authenticate_credentials( array $settings = [] ) {
229235
* @return array|WP_Error
230236
*/
231237
protected function get_models( string $api_key = '' ) {
232-
// Check that we have credentials before hitting the API.
233-
if ( empty( $api_key ) ) {
234-
return new WP_Error( 'auth', esc_html__( 'Please enter your ElevenLabs API key.', 'classifai' ) );
235-
}
236-
237238
$response = $this->request( $this->get_api_url( $this->model_path ), $api_key, 'get', [ 'use_vip' => true ] );
238239

239240
if ( is_wp_error( $response ) ) {
@@ -270,11 +271,6 @@ function ( $model ) {
270271
* @return array|WP_Error
271272
*/
272273
protected function get_voices( string $api_key = '' ) {
273-
// Check that we have credentials before hitting the API.
274-
if ( empty( $api_key ) ) {
275-
return new WP_Error( 'auth', esc_html__( 'Please enter your ElevenLabs API key.', 'classifai' ) );
276-
}
277-
278274
$response = $this->request( $this->get_api_url( 'voices?per_page=100' ), $api_key, 'get', [ 'use_vip' => true ] );
279275

280276
if ( is_wp_error( $response ) ) {

includes/Classifai/Providers/OpenAI/OpenAI.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ trait OpenAI {
2929
public function sanitize_api_key_settings( array $new_settings = [], array $settings = [] ): array {
3030
$authenticated = $this->authenticate_credentials( $new_settings );
3131

32-
$new_settings[ static::ID ]['authenticated'] = $settings[ static::ID ]['authenticated'];
33-
3432
if ( is_wp_error( $authenticated ) ) {
3533
$new_settings[ static::ID ]['authenticated'] = false;
3634
$error_message = $authenticated->get_error_message();

includes/Classifai/Providers/Provider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ public function get_credentials( array $settings = [] ): array {
203203
*
204204
* Possible hook names include:
205205
* - `classifai_provider_credentials_azure_openai`
206+
* - `classifai_provider_credentials_azure_openai_embeddings`
206207
* - `classifai_provider_credentials_openai_chatgpt`
207208
* - `classifai_provider_credentials_openai_embeddings`
208209
* - `classifai_provider_credentials_openai_moderation`

0 commit comments

Comments
 (0)