Skip to content

Commit

Permalink
Merge pull request #455 from 10up/fix/415
Browse files Browse the repository at this point in the history
Ensure we don't send unnecessary API requests to NLU
  • Loading branch information
dkotter authored May 18, 2023
2 parents 3a6dda5 + 4a55ebc commit 7a8d8cb
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 84 deletions.
14 changes: 0 additions & 14 deletions includes/Classifai/Providers/Azure/ComputerVision.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,6 @@ public function get_alt_text_settings() {
return $enabled_fields;
}

/**
* Can the functionality be initialized?
*
* @return bool
*/
public function can_register() {
$options = get_option( $this->get_option_name() );
if ( empty( $options ) || ( isset( $options['authenticated'] ) && false === $options['authenticated'] ) ) {
return false;
}

return true;
}

/**
* Register the functionality.
*/
Expand Down
14 changes: 0 additions & 14 deletions includes/Classifai/Providers/Azure/Personalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,6 @@ private function get_default_settings() {
];
}

/**
* Can the functionality be initialized?
*
* @return bool
*/
public function can_register() {
$options = get_option( $this->get_option_name() );
if ( empty( $options ) || ( isset( $options['authenticated'] ) && false === $options['authenticated'] ) ) {
return false;
}

return true;
}

/**
* Register the functionality.
*/
Expand Down
15 changes: 0 additions & 15 deletions includes/Classifai/Providers/OpenAI/ChatGPT.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,6 @@ public function __construct( $service ) {
);
}

/**
* Can the functionality be initialized?
*
* @return bool
*/
public function can_register() {
$settings = $this->get_settings();

if ( empty( $settings ) || ( isset( $settings['authenticated'] ) && false === $settings['authenticated'] ) ) {
return false;
}

return true;
}

/**
* Register what we need for the plugin.
*
Expand Down
15 changes: 0 additions & 15 deletions includes/Classifai/Providers/OpenAI/DallE.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,6 @@ public function __construct( $service ) {
);
}

/**
* Can the functionality be initialized?
*
* @return bool
*/
public function can_register() {
$settings = $this->get_settings();

if ( empty( $settings ) || ( isset( $settings['authenticated'] ) && false === $settings['authenticated'] ) ) {
return false;
}

return true;
}

/**
* Register what we need for the provider.
*
Expand Down
4 changes: 3 additions & 1 deletion includes/Classifai/Providers/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ public function get_onboarding_options() {
/**
* Can the Provider be initalized?
*/
abstract public function can_register();
public function can_register() {
return $this->is_configured();
}

/**
* Register the functionality for the Provider.
Expand Down
52 changes: 27 additions & 25 deletions includes/Classifai/Providers/Watson/NLU.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Classifai\Admin\PreviewClassifierData;
use Classifai\Providers\Provider;
use Classifai\Taxonomy\TaxonomyFactory;
use WP_Error;

use function Classifai\get_asset_info;
use function Classifai\get_post_types_for_language_settings;
Expand Down Expand Up @@ -116,19 +117,6 @@ public function reset_settings() {
update_option( $this->get_option_name(), $settings );
}

/**
* Can the functionality be initialized?
*
* @return bool
*/
public function can_register() {
if ( $this->nlu_authentication_check_failed( $this->get_settings() ) ) {
return false;
}

return true;
}

/**
* Register what we need for the plugin.
*/
Expand Down Expand Up @@ -600,17 +588,17 @@ public function get_supported_taxonomies() {
*
* @param array $settings The list of settings to be saved
*
* @return bool
* @return bool|WP_Error
*/
protected function nlu_authentication_check_failed( $settings ) {
protected function nlu_authentication_check( $settings ) {

// Check that we have credentials before hitting the API.
if ( ! isset( $settings['credentials'] )
|| empty( $settings['credentials']['watson_username'] )
|| empty( $settings['credentials']['watson_password'] )
|| empty( $settings['credentials']['watson_url'] )
) {
return true;
return new WP_Error( 'auth', esc_html__( 'Please enter your credentials.', 'classifai' ) );
}

$request = new \Classifai\Watson\APIRequest();
Expand All @@ -634,15 +622,13 @@ protected function nlu_authentication_check_failed( $settings ) {
];
$response = $request->post( $url, $options );

$is_error = is_wp_error( $response );
if ( ! $is_error ) {
if ( ! is_wp_error( $response ) ) {
update_option( 'classifai_configured', true );
return true;
} else {
delete_option( 'classifai_configured' );
return $response;
}

return $is_error;

}


Expand All @@ -654,14 +640,19 @@ protected function nlu_authentication_check_failed( $settings ) {
* @return array The sanitized settings to be saved.
*/
public function sanitize_settings( $settings ) {
$new_settings = $this->get_settings();
if ( $this->nlu_authentication_check_failed( $settings ) ) {
$new_settings = $this->get_settings();
$authenticated = $this->nlu_authentication_check( $settings );

if ( is_wp_error( $authenticated ) ) {
$new_settings['authenticated'] = false;
add_settings_error(
'credentials',
'classifai-auth',
esc_html__( 'IBM Watson NLU Authentication Failed. Please check credentials.', 'classifai' ),
$authenticated->get_error_message(),
'error'
);
} else {
$new_settings['authenticated'] = true;
}

if ( isset( $settings['credentials']['watson_url'] ) ) {
Expand Down Expand Up @@ -903,9 +894,20 @@ public function add_process_content_meta_to_rest_api() {
/**
* Returns whether the provider is configured or not.
*
* For backwards compat, we've maintained the use of the
* `classifai_configured` option. We default to looking for
* the `authenticated` setting though.
*
* @return bool
*/
public function is_configured() {
return get_option( 'classifai_configured', false );
$is_configured = parent::is_configured();

if ( ! $is_configured ) {
$is_configured = (bool) get_option( 'classifai_configured', false );
}

return $is_configured;
}

}

0 comments on commit 7a8d8cb

Please sign in to comment.