Skip to content

Commit 67a0165

Browse files
authored
Merge pull request #678 from 10up/upkeep/673
upkeep/673: eliminate dead code
2 parents 7e58b9f + 5b2a414 commit 67a0165

16 files changed

+8
-481
lines changed

includes/Classifai/Helpers.php

-46
Original file line numberDiff line numberDiff line change
@@ -24,52 +24,6 @@ function get_plugin() {
2424
return Plugin::get_instance();
2525
}
2626

27-
/**
28-
* Returns the ClassifAI plugin's stored settings in the WP options.
29-
*
30-
* @param string $service The service to get settings from, defaults to the ServiceManager class.
31-
* @param string $provider The provider service name to get settings from, defaults to the first one found.
32-
* @return array The array of ClassifAi settings.
33-
*/
34-
function get_plugin_settings( string $service = '', string $provider = '' ): array {
35-
$services = Plugin::$instance->services;
36-
if ( empty( $services ) || empty( $services['service_manager'] ) || ! $services['service_manager'] instanceof ServicesManager ) {
37-
return [];
38-
}
39-
40-
/** @var ServicesManager $service_manager Instance of the services manager class. */
41-
$service_manager = $services['service_manager'];
42-
if ( empty( $service ) ) {
43-
return $service_manager->get_settings();
44-
}
45-
46-
if ( ! isset( $service_manager->service_classes[ $service ] ) || ! $service_manager->service_classes[ $service ] instanceof Service ) {
47-
return [];
48-
}
49-
50-
// Ensure we have at least one provider.
51-
$providers = $service_manager->service_classes[ $service ]->provider_classes;
52-
53-
if ( empty( $providers ) ) {
54-
return [];
55-
}
56-
57-
// If we want settings for a specific provider, find the proper provider service.
58-
if ( ! empty( $provider ) ) {
59-
foreach ( $providers as $provider_class ) {
60-
if ( $provider_class->provider_service_name === $provider ) {
61-
return $provider_class->get_settings();
62-
}
63-
}
64-
65-
return [];
66-
}
67-
68-
/** @var Provider $provider An instance or extension of the provider abstract class. */
69-
$provider = $providers[0];
70-
return $provider->get_settings();
71-
}
72-
7327
/**
7428
* Overwrites the ClassifAI plugin's stored settings. Expected format is,
7529
*

includes/Classifai/Providers/Azure/ComputerVision.php

-18
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,9 @@ class ComputerVision extends Provider {
3333
* @param \Classifai\Features\Feature $feature_instance The feature instance.
3434
*/
3535
public function __construct( $feature_instance = null ) {
36-
parent::__construct(
37-
'Microsoft Azure',
38-
'AI Vision',
39-
'computer_vision'
40-
);
41-
4236
$this->feature_instance = $feature_instance;
4337
}
4438

45-
/**
46-
* Resets settings for the ComputerVision provider.
47-
*/
48-
public function reset_settings() {
49-
update_option( $this->get_option_name(), $this->get_default_settings() );
50-
}
51-
5239
/**
5340
* Renders the provider fields.
5441
*/
@@ -665,11 +652,6 @@ protected function prep_api_url( \Classifai\Features\Feature $feature = null ):
665652
return $endpoint;
666653
}
667654

668-
/**
669-
* Setup fields
670-
*/
671-
public function setup_fields_sections() {}
672-
673655
/**
674656
* Authenticates our credentials.
675657
*

includes/Classifai/Providers/Azure/Personalizer.php

+2-8
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,6 @@ class Personalizer extends Provider {
3737
* @param \Classifai\Features\Feature $feature_instance The feature instance.
3838
*/
3939
public function __construct( $feature_instance = null ) {
40-
parent::__construct(
41-
'Microsoft Azure',
42-
'AI Personalizer',
43-
'personalizer'
44-
);
45-
4640
$this->feature_instance = $feature_instance;
4741

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

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

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

168162
if ( ! in_array( $code, $registered_settings_errors, true ) ) {
169163
add_settings_error(
170-
$this->get_option_name(),
164+
$this->feature_instance->get_option_name(),
171165
$code,
172166
esc_html( $message ),
173167
'error'

includes/Classifai/Providers/Azure/Speech.php

+2-14
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,11 @@ class Speech extends Provider {
4444
* @param \Classifai\Features\Feature $feature_instance The feature instance.
4545
*/
4646
public function __construct( $feature_instance = null ) {
47-
parent::__construct(
48-
'Microsoft Azure',
49-
self::FEATURE_NAME,
50-
'azure_text_to_speech'
51-
);
52-
5347
$this->feature_instance = $feature_instance;
5448

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

58-
/**
59-
* Register the actions needed.
60-
*/
61-
public function register() {
62-
}
63-
6452
/**
6553
* Render the provider fields.
6654
*/
@@ -252,7 +240,7 @@ public function connect_to_service( array $args = array() ): array {
252240

253241
if ( is_wp_error( $response ) ) {
254242
add_settings_error(
255-
$this->get_option_name(),
243+
$this->feature_instance->get_option_name(),
256244
'azure-text-to-request-failed',
257245
esc_html__( 'Azure Speech to Text: HTTP request failed.', 'classifai' ),
258246
'error'
@@ -266,7 +254,7 @@ public function connect_to_service( array $args = array() ): array {
266254
// Return and render error if HTTP response status code is other than 200.
267255
if ( WP_Http::OK !== $http_code ) {
268256
add_settings_error(
269-
$this->get_option_name(),
257+
$this->feature_instance->get_option_name(),
270258
'azure-text-to-speech-auth-failed',
271259
esc_html__( 'Connection to Azure Text to Speech failed.', 'classifai' ),
272260
'error'

includes/Classifai/Providers/OpenAI/ChatGPT.php

-12
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,9 @@ class ChatGPT extends Provider {
4747
* @param \Classifai\Features\Feature $feature_instance The feature instance.
4848
*/
4949
public function __construct( $feature_instance = null ) {
50-
parent::__construct(
51-
'OpenAI ChatGPT',
52-
'ChatGPT',
53-
'openai_chatgpt'
54-
);
55-
5650
$this->feature_instance = $feature_instance;
5751
}
5852

59-
/**
60-
* Register any needed hooks.
61-
*/
62-
public function register() {
63-
}
64-
6553
/**
6654
* Render the provider fields.
6755
*/

includes/Classifai/Providers/OpenAI/DallE.php

-13
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,6 @@ class DallE extends Provider {
3737
* @param \Classifai\Features\Feature $feature_instance The feature instance.
3838
*/
3939
public function __construct( $feature_instance = null ) {
40-
parent::__construct(
41-
'OpenAI',
42-
'DALL·E',
43-
'openai_dalle'
44-
);
45-
4640
$this->feature_instance = $feature_instance;
4741
}
4842

@@ -231,13 +225,6 @@ public function sanitize_settings( array $new_settings ): array {
231225
return $new_settings;
232226
}
233227

234-
/**
235-
* Resets settings for the provider.
236-
*/
237-
public function reset_settings() {
238-
update_option( $this->get_option_name(), $this->get_default_settings() );
239-
}
240-
241228
/**
242229
* Common entry point for all REST endpoints for this provider.
243230
*

includes/Classifai/Providers/OpenAI/Embeddings.php

+1-8
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,6 @@ class Embeddings extends Provider {
5050
* @param \Classifai\Features\Feature $feature_instance The feature instance.
5151
*/
5252
public function __construct( $feature_instance = null ) {
53-
parent::__construct(
54-
'OpenAI Embeddings',
55-
'Embeddings',
56-
'openai_embeddings',
57-
$feature_instance
58-
);
59-
6053
$this->feature_instance = $feature_instance;
6154
}
6255

@@ -728,7 +721,7 @@ public function generate_embeddings( int $id = 0, $type = 'post' ) {
728721
return false;
729722
}
730723

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

733726
/**
734727
* Filter the request body before sending to OpenAI.

includes/Classifai/Providers/OpenAI/Moderation.php

-6
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,6 @@ class Moderation extends Provider {
2828
* @param \Classifai\Features\Feature $feature_instance The feature instance.
2929
*/
3030
public function __construct( $feature_instance = null ) {
31-
parent::__construct(
32-
'OpenAI Moderation',
33-
'Moderation',
34-
'openai_moderation'
35-
);
36-
3731
$this->feature_instance = $feature_instance;
3832
}
3933

includes/Classifai/Providers/OpenAI/OpenAI.php

-69
Original file line numberDiff line numberDiff line change
@@ -19,75 +19,6 @@ trait OpenAI {
1919
*/
2020
protected $model_url = 'https://api.openai.com/v1/models';
2121

22-
/**
23-
* Add our OpenAI API settings field.
24-
*
25-
* @param string $default_api_key Default API key.
26-
*/
27-
protected function setup_api_fields( string $default_api_key = '' ) {
28-
$existing_settings = $this->feature_instance->get_settings();
29-
$description = '';
30-
31-
// Add the settings section.
32-
add_settings_section(
33-
$this->feature_instance->get_option_name(),
34-
$this->provider_service_name,
35-
function () {
36-
printf(
37-
wp_kses(
38-
/* translators: %1$s is replaced with the OpenAI sign up URL */
39-
__( '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' ),
40-
[
41-
'a' => [
42-
'href' => [],
43-
'title' => [],
44-
],
45-
]
46-
),
47-
esc_url( 'https://platform.openai.com/signup' )
48-
);
49-
},
50-
$this->feature_instance->get_option_name()
51-
);
52-
53-
// Determine which other OpenAI provider to look for an API key in.
54-
if ( 'ChatGPT' === $this->provider_service_name ) {
55-
$settings = \Classifai\get_plugin_settings( 'image_processing', 'DALL·E' );
56-
$provider_service_name = 'DALL·E';
57-
} elseif ( 'DALL·E' === $this->provider_service_name ) {
58-
$settings = \Classifai\get_plugin_settings( 'language_processing', 'ChatGPT' );
59-
$provider_service_name = 'ChatGPT';
60-
} else {
61-
$settings = [];
62-
$provider_service_name = '';
63-
}
64-
65-
// If we already have a valid API key from OpenAI, use that as our default.
66-
if ( ! empty( $settings ) && ( isset( $settings['authenticated'] ) && isset( $settings['api_key'] ) && true === $settings['authenticated'] ) ) {
67-
$default_api_key = $settings['api_key'];
68-
69-
if ( empty( $existing_settings ) || empty( $existing_settings['api_key'] ) ) {
70-
/* translators: %1$s: the provider service name */
71-
$description = sprintf( __( 'API key has been prefilled from your %1$s settings.', 'classifai' ), $provider_service_name );
72-
}
73-
}
74-
75-
// Add our API Key setting.
76-
add_settings_field(
77-
'api-key',
78-
esc_html__( 'API Key', 'classifai' ),
79-
[ $this, 'render_input' ],
80-
$this->feature_instance->get_option_name(),
81-
$this->feature_instance->get_option_name(),
82-
[
83-
'label_for' => 'api_key',
84-
'input_type' => 'password',
85-
'default_value' => $default_api_key,
86-
'description' => $description,
87-
]
88-
);
89-
}
90-
9122
/**
9223
* Sanitize the API key, showing an error message if needed.
9324
*

includes/Classifai/Providers/OpenAI/Whisper.php

-12
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,9 @@ class Whisper extends Provider {
6262
* @param \Classifai\Features\Feature $feature_instance The feature instance.
6363
*/
6464
public function __construct( $feature_instance = null ) {
65-
parent::__construct(
66-
'OpenAI Whisper',
67-
'Whisper',
68-
'openai_whisper'
69-
);
70-
7165
$this->feature_instance = $feature_instance;
7266
}
7367

74-
/**
75-
* Register any needed hooks.
76-
*/
77-
public function register() {
78-
}
79-
8068
/**
8169
* Builds the API url.
8270
*

0 commit comments

Comments
 (0)