Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upkeep/673: eliminate dead code #678

Merged
merged 11 commits into from
Feb 5, 2024
46 changes: 0 additions & 46 deletions includes/Classifai/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,52 +24,6 @@ function get_plugin() {
return Plugin::get_instance();
}

/**
* Returns the ClassifAI plugin's stored settings in the WP options.
*
* @param string $service The service to get settings from, defaults to the ServiceManager class.
* @param string $provider The provider service name to get settings from, defaults to the first one found.
* @return array The array of ClassifAi settings.
*/
function get_plugin_settings( string $service = '', string $provider = '' ): array {
$services = Plugin::$instance->services;
if ( empty( $services ) || empty( $services['service_manager'] ) || ! $services['service_manager'] instanceof ServicesManager ) {
return [];
}

/** @var ServicesManager $service_manager Instance of the services manager class. */
$service_manager = $services['service_manager'];
if ( empty( $service ) ) {
return $service_manager->get_settings();
}

if ( ! isset( $service_manager->service_classes[ $service ] ) || ! $service_manager->service_classes[ $service ] instanceof Service ) {
return [];
}

// Ensure we have at least one provider.
$providers = $service_manager->service_classes[ $service ]->provider_classes;

if ( empty( $providers ) ) {
return [];
}

// If we want settings for a specific provider, find the proper provider service.
if ( ! empty( $provider ) ) {
foreach ( $providers as $provider_class ) {
if ( $provider_class->provider_service_name === $provider ) {
return $provider_class->get_settings();
}
}

return [];
}

/** @var Provider $provider An instance or extension of the provider abstract class. */
$provider = $providers[0];
return $provider->get_settings();
}

/**
* Overwrites the ClassifAI plugin's stored settings. Expected format is,
*
Expand Down
18 changes: 0 additions & 18 deletions includes/Classifai/Providers/Azure/ComputerVision.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,9 @@ class ComputerVision extends Provider {
* @param \Classifai\Features\Feature $feature_instance The feature instance.
*/
public function __construct( $feature_instance = null ) {
parent::__construct(
'Microsoft Azure',
'AI Vision',
'computer_vision'
);

$this->feature_instance = $feature_instance;
}

/**
* Resets settings for the ComputerVision provider.
*/
public function reset_settings() {
update_option( $this->get_option_name(), $this->get_default_settings() );
}

/**
* Renders the provider fields.
*/
Expand Down Expand Up @@ -665,11 +652,6 @@ protected function prep_api_url( \Classifai\Features\Feature $feature = null ):
return $endpoint;
}

/**
* Setup fields
*/
public function setup_fields_sections() {}

/**
* Authenticates our credentials.
*
Expand Down
10 changes: 2 additions & 8 deletions includes/Classifai/Providers/Azure/Personalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@ class Personalizer extends Provider {
* @param \Classifai\Features\Feature $feature_instance The feature instance.
*/
public function __construct( $feature_instance = null ) {
parent::__construct(
'Microsoft Azure',
'AI Personalizer',
'personalizer'
);

$this->feature_instance = $feature_instance;

add_action( 'rest_api_init', [ $this, 'register_endpoints' ] );
Expand Down Expand Up @@ -161,13 +155,13 @@ public function sanitize_settings( array $new_settings ): array {
}

if ( ! empty( $settings_errors ) ) {
$registered_settings_errors = wp_list_pluck( get_settings_errors( $this->get_option_name() ), 'code' );
$registered_settings_errors = wp_list_pluck( get_settings_errors( $this->feature_instance->get_option_name() ), 'code' );

foreach ( $settings_errors as $code => $message ) {

if ( ! in_array( $code, $registered_settings_errors, true ) ) {
add_settings_error(
$this->get_option_name(),
$this->feature_instance->get_option_name(),
$code,
esc_html( $message ),
'error'
Expand Down
16 changes: 2 additions & 14 deletions includes/Classifai/Providers/Azure/Speech.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,11 @@ class Speech extends Provider {
* @param \Classifai\Features\Feature $feature_instance The feature instance.
*/
public function __construct( $feature_instance = null ) {
parent::__construct(
'Microsoft Azure',
self::FEATURE_NAME,
'azure_text_to_speech'
);

$this->feature_instance = $feature_instance;

do_action( 'classifai_' . static::ID . '_init', $this );
}

/**
* Register the actions needed.
*/
public function register() {
}

/**
* Render the provider fields.
*/
Expand Down Expand Up @@ -252,7 +240,7 @@ public function connect_to_service( array $args = array() ): array {

if ( is_wp_error( $response ) ) {
add_settings_error(
$this->get_option_name(),
$this->feature_instance->get_option_name(),
'azure-text-to-request-failed',
esc_html__( 'Azure Speech to Text: HTTP request failed.', 'classifai' ),
'error'
Expand All @@ -266,7 +254,7 @@ public function connect_to_service( array $args = array() ): array {
// Return and render error if HTTP response status code is other than 200.
if ( WP_Http::OK !== $http_code ) {
add_settings_error(
$this->get_option_name(),
$this->feature_instance->get_option_name(),
'azure-text-to-speech-auth-failed',
esc_html__( 'Connection to Azure Text to Speech failed.', 'classifai' ),
'error'
Expand Down
12 changes: 0 additions & 12 deletions includes/Classifai/Providers/OpenAI/ChatGPT.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,9 @@ class ChatGPT extends Provider {
* @param \Classifai\Features\Feature $feature_instance The feature instance.
*/
public function __construct( $feature_instance = null ) {
parent::__construct(
'OpenAI ChatGPT',
'ChatGPT',
'openai_chatgpt'
);

$this->feature_instance = $feature_instance;
}

/**
* Register any needed hooks.
*/
public function register() {
}

/**
* Render the provider fields.
*/
Expand Down
13 changes: 0 additions & 13 deletions includes/Classifai/Providers/OpenAI/DallE.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@ class DallE extends Provider {
* @param \Classifai\Features\Feature $feature_instance The feature instance.
*/
public function __construct( $feature_instance = null ) {
parent::__construct(
'OpenAI',
'DALL·E',
'openai_dalle'
);

$this->feature_instance = $feature_instance;
}

Expand Down Expand Up @@ -231,13 +225,6 @@ public function sanitize_settings( array $new_settings ): array {
return $new_settings;
}

/**
* Resets settings for the provider.
*/
public function reset_settings() {
update_option( $this->get_option_name(), $this->get_default_settings() );
}

/**
* Common entry point for all REST endpoints for this provider.
*
Expand Down
9 changes: 1 addition & 8 deletions includes/Classifai/Providers/OpenAI/Embeddings.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,6 @@ class Embeddings extends Provider {
* @param \Classifai\Features\Feature $feature_instance The feature instance.
*/
public function __construct( $feature_instance = null ) {
parent::__construct(
'OpenAI Embeddings',
'Embeddings',
'openai_embeddings',
$feature_instance
);

$this->feature_instance = $feature_instance;
}

Expand Down Expand Up @@ -674,7 +667,7 @@ public function generate_embeddings( int $id = 0, $type = 'post' ) {
return false;
}

$request = new APIRequest( $settings[ static::ID ]['api_key'] ?? '', $this->get_option_name() );
$request = new APIRequest( $settings[ static::ID ]['api_key'] ?? '', $feature->get_option_name() );

/**
* Filter the request body before sending to OpenAI.
Expand Down
6 changes: 0 additions & 6 deletions includes/Classifai/Providers/OpenAI/Moderation.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ class Moderation extends Provider {
* @param \Classifai\Features\Feature $feature_instance The feature instance.
*/
public function __construct( $feature_instance = null ) {
parent::__construct(
'OpenAI Moderation',
'Moderation',
'openai_moderation'
);

$this->feature_instance = $feature_instance;
}

Expand Down
69 changes: 0 additions & 69 deletions includes/Classifai/Providers/OpenAI/OpenAI.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,75 +19,6 @@ trait OpenAI {
*/
protected $model_url = 'https://api.openai.com/v1/models';

/**
* Add our OpenAI API settings field.
*
* @param string $default_api_key Default API key.
*/
protected function setup_api_fields( string $default_api_key = '' ) {
$existing_settings = $this->feature_instance->get_settings();
$description = '';

// Add the settings section.
add_settings_section(
$this->feature_instance->get_option_name(),
$this->provider_service_name,
function () {
printf(
wp_kses(
/* translators: %1$s is replaced with the OpenAI sign up URL */
__( 'Don\'t have an OpenAI account yet? <a title="Sign up for an OpenAI account" href="%1$s">Sign up for one</a> in order to get your API key.', 'classifai' ),
[
'a' => [
'href' => [],
'title' => [],
],
]
),
esc_url( 'https://platform.openai.com/signup' )
);
},
$this->feature_instance->get_option_name()
);

// Determine which other OpenAI provider to look for an API key in.
if ( 'ChatGPT' === $this->provider_service_name ) {
$settings = \Classifai\get_plugin_settings( 'image_processing', 'DALL·E' );
$provider_service_name = 'DALL·E';
} elseif ( 'DALL·E' === $this->provider_service_name ) {
$settings = \Classifai\get_plugin_settings( 'language_processing', 'ChatGPT' );
$provider_service_name = 'ChatGPT';
} else {
$settings = [];
$provider_service_name = '';
}

// If we already have a valid API key from OpenAI, use that as our default.
if ( ! empty( $settings ) && ( isset( $settings['authenticated'] ) && isset( $settings['api_key'] ) && true === $settings['authenticated'] ) ) {
$default_api_key = $settings['api_key'];

if ( empty( $existing_settings ) || empty( $existing_settings['api_key'] ) ) {
/* translators: %1$s: the provider service name */
$description = sprintf( __( 'API key has been prefilled from your %1$s settings.', 'classifai' ), $provider_service_name );
}
}

// Add our API Key setting.
add_settings_field(
'api-key',
esc_html__( 'API Key', 'classifai' ),
[ $this, 'render_input' ],
$this->feature_instance->get_option_name(),
$this->feature_instance->get_option_name(),
[
'label_for' => 'api_key',
'input_type' => 'password',
'default_value' => $default_api_key,
'description' => $description,
]
);
}

/**
* Sanitize the API key, showing an error message if needed.
*
Expand Down
12 changes: 0 additions & 12 deletions includes/Classifai/Providers/OpenAI/Whisper.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,9 @@ class Whisper extends Provider {
* @param \Classifai\Features\Feature $feature_instance The feature instance.
*/
public function __construct( $feature_instance = null ) {
parent::__construct(
'OpenAI Whisper',
'Whisper',
'openai_whisper'
);

$this->feature_instance = $feature_instance;
}

/**
* Register any needed hooks.
*/
public function register() {
}

/**
* Builds the API url.
*
Expand Down
Loading
Loading