@@ -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 */
0 commit comments