From 4c9ec0062e20c8d41f7c30a92b23329efd014445 Mon Sep 17 00:00:00 2001 From: Siddharth Thevaril Date: Fri, 2 Feb 2024 00:53:36 +0530 Subject: [PATCH 1/8] eliminate dead code --- .../Providers/Azure/ComputerVision.php | 12 -- includes/Classifai/Providers/Azure/Speech.php | 6 - .../Classifai/Providers/OpenAI/ChatGPT.php | 6 - includes/Classifai/Providers/OpenAI/DallE.php | 7 - .../Classifai/Providers/OpenAI/Embeddings.php | 2 +- .../Classifai/Providers/OpenAI/OpenAI.php | 69 -------- .../Classifai/Providers/OpenAI/Whisper.php | 6 - includes/Classifai/Providers/Provider.php | 149 ------------------ includes/Classifai/Providers/Watson/NLU.php | 57 ------- includes/Classifai/Services/Service.php | 4 +- 10 files changed, 4 insertions(+), 314 deletions(-) diff --git a/includes/Classifai/Providers/Azure/ComputerVision.php b/includes/Classifai/Providers/Azure/ComputerVision.php index 8ae0c33ef..dba91af49 100644 --- a/includes/Classifai/Providers/Azure/ComputerVision.php +++ b/includes/Classifai/Providers/Azure/ComputerVision.php @@ -42,13 +42,6 @@ public function __construct( $feature_instance = null ) { $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. */ @@ -665,11 +658,6 @@ protected function prep_api_url( \Classifai\Features\Feature $feature = null ): return $endpoint; } - /** - * Setup fields - */ - public function setup_fields_sections() {} - /** * Authenticates our credentials. * diff --git a/includes/Classifai/Providers/Azure/Speech.php b/includes/Classifai/Providers/Azure/Speech.php index 5ddacb64e..652a154eb 100644 --- a/includes/Classifai/Providers/Azure/Speech.php +++ b/includes/Classifai/Providers/Azure/Speech.php @@ -55,12 +55,6 @@ public function __construct( $feature_instance = null ) { do_action( 'classifai_' . static::ID . '_init', $this ); } - /** - * Register the actions needed. - */ - public function register() { - } - /** * Render the provider fields. */ diff --git a/includes/Classifai/Providers/OpenAI/ChatGPT.php b/includes/Classifai/Providers/OpenAI/ChatGPT.php index c1d82bf87..2332e1834 100644 --- a/includes/Classifai/Providers/OpenAI/ChatGPT.php +++ b/includes/Classifai/Providers/OpenAI/ChatGPT.php @@ -55,12 +55,6 @@ public function __construct( $feature_instance = null ) { $this->feature_instance = $feature_instance; } - /** - * Register any needed hooks. - */ - public function register() { - } - /** * Render the provider fields. */ diff --git a/includes/Classifai/Providers/OpenAI/DallE.php b/includes/Classifai/Providers/OpenAI/DallE.php index 556247056..6042f646c 100644 --- a/includes/Classifai/Providers/OpenAI/DallE.php +++ b/includes/Classifai/Providers/OpenAI/DallE.php @@ -231,13 +231,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. * diff --git a/includes/Classifai/Providers/OpenAI/Embeddings.php b/includes/Classifai/Providers/OpenAI/Embeddings.php index 13ebdbea6..9edc3a440 100644 --- a/includes/Classifai/Providers/OpenAI/Embeddings.php +++ b/includes/Classifai/Providers/OpenAI/Embeddings.php @@ -674,7 +674,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. diff --git a/includes/Classifai/Providers/OpenAI/OpenAI.php b/includes/Classifai/Providers/OpenAI/OpenAI.php index feeb90aed..26d5dd104 100644 --- a/includes/Classifai/Providers/OpenAI/OpenAI.php +++ b/includes/Classifai/Providers/OpenAI/OpenAI.php @@ -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? Sign up for one 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. * diff --git a/includes/Classifai/Providers/OpenAI/Whisper.php b/includes/Classifai/Providers/OpenAI/Whisper.php index 3e513bbba..ce0ed1c91 100644 --- a/includes/Classifai/Providers/OpenAI/Whisper.php +++ b/includes/Classifai/Providers/OpenAI/Whisper.php @@ -71,12 +71,6 @@ public function __construct( $feature_instance = null ) { $this->feature_instance = $feature_instance; } - /** - * Register any needed hooks. - */ - public function register() { - } - /** * Builds the API url. * diff --git a/includes/Classifai/Providers/Provider.php b/includes/Classifai/Providers/Provider.php index 5eb26664c..f66384d53 100644 --- a/includes/Classifai/Providers/Provider.php +++ b/includes/Classifai/Providers/Provider.php @@ -24,16 +24,6 @@ abstract class Provider { */ public $provider_service_name; - /** - * @var string $option_name Name of the option where the provider settings are stored. - */ - protected $option_name; - - /** - * @var string $service The name of the service this provider belongs to. - */ - protected $service; - /** * Feature instance. * @@ -77,15 +67,6 @@ public function get_settings_section(): string { return $this->option_name; } - /** - * Get the option name. - * - * @return string - */ - public function get_option_name(): string { - return 'classifai_' . $this->option_name; - } - /** * Get provider features. * @@ -95,45 +76,6 @@ public function get_features(): array { return $this->features; } - /** - * Can the Provider be initialized? - * - * @return bool - */ - public function can_register(): bool { - return $this->is_configured(); - } - - /** - * Register the functionality for the Provider. - */ - abstract public function register(); - - /** - * Initialization routine - */ - public function register_admin() { - add_action( 'admin_init', [ $this, 'setup_fields_sections' ] ); - } - - /** - * Helper to get the settings and allow for settings default values. - * - * @param string|bool|mixed $index Optional. Name of the settings option index. - * @return string|array|mixed - */ - public function get_settings( $index = false ) { - $defaults = $this->get_default_settings(); - $settings = get_option( $this->get_option_name(), [] ); - $settings = wp_parse_args( $settings, $defaults ); - - if ( $index && isset( $settings[ $index ] ) ) { - return $settings[ $index ]; - } - - return $settings; - } - /** * Default settings for Provider. * @@ -173,22 +115,6 @@ protected function get_formatted_latest_response( $data ): string { return preg_replace( '/,"/', ', "', wp_json_encode( $data ) ); } - /** - * Returns whether the provider is configured or not. - * - * @return bool - */ - public function is_configured(): bool { - $settings = $this->get_settings(); - - $is_configured = false; - if ( ! empty( $settings ) && ! empty( $settings['authenticated'] ) ) { - $is_configured = true; - } - - return $is_configured; - } - /** * Adds an API key field. * @@ -215,79 +141,4 @@ public function add_api_key_field( array $args = [] ) { ); } - /** - * Determine if the current user has access of the feature - * - * @param string $feature Feature to check. - * @return bool - */ - protected function has_access( string $feature ): bool { - $access_control = new AccessControl( $this, $feature ); - return $access_control->has_access(); - } - - /** - * Determine if the feature is enabled and current user can access the feature - * - * @param string $feature Feature to check. - * @return bool - */ - public function is_feature_enabled( string $feature ): bool { - $is_feature_enabled = false; - $settings = $this->get_settings(); - - // Check if provider is configured, user has access to the feature and the feature is turned on. - if ( - $this->is_configured() && - $this->has_access( $feature ) && - $this->is_enabled( $feature ) - ) { - $is_feature_enabled = true; - } - - /** - * Filter to override permission to a specific classifai feature. - * - * @since 2.4.0 - * @hook classifai_{$this->option_name}_enable_{$feature} - * - * @param {bool} $is_feature_enabled Is the feature enabled? - * @param {array} $settings Current feature settings. - * - * @return {bool} Returns true if the user has access and the feature is enabled, false otherwise. - */ - return apply_filters( "classifai_{$this->option_name}_enable_{$feature}", $is_feature_enabled, $settings ); - } - - /** - * Determine if the feature is turned on. - * - * Note: This function does not check if the user has access to the feature. - * - * - Use `is_feature_enabled()` to check if the user has access to the feature and feature is turned on. - * - Use `has_access()` to check if the user has access to the feature. - * - * @param string $feature Feature to check. - * @return bool - */ - public function is_enabled( string $feature ): bool { - $settings = $this->get_settings(); - $enable_key = 'enable_' . $feature; - - // Check if feature is turned on. - $is_enabled = ( isset( $settings[ $enable_key ] ) && 1 === (int) $settings[ $enable_key ] ); - - /** - * Filter to override a specific classifai feature enabled. - * - * @since 2.5.0 - * @hook classifai_is_{$feature}_enabled - * - * @param {bool} $is_enabled Is the feature enabled? - * @param {array} $settings Current feature settings. - * - * @return {bool} Returns true if the feature is enabled, false otherwise. - */ - return apply_filters( "classifai_is_{$feature}_enabled", $is_enabled, $settings ); - } } diff --git a/includes/Classifai/Providers/Watson/NLU.php b/includes/Classifai/Providers/Watson/NLU.php index 8a87ad5a4..ba3667bd3 100644 --- a/includes/Classifai/Providers/Watson/NLU.php +++ b/includes/Classifai/Providers/Watson/NLU.php @@ -347,14 +347,6 @@ public function get_default_provider_settings(): array { return $common_settings; } - /** - * Resets the settings for the NLU provider. - */ - public function reset_settings() { - $settings = $this->get_default_settings() ?? []; - update_option( $this->get_option_name(), $settings ); - } - /** * Register what we need for the plugin. */ @@ -440,55 +432,6 @@ public function enqueue_admin_assets() { ); } - /** - * Setup fields - */ - public function setup_fields_sections() { - // Add the settings section. - add_settings_section( - $this->get_option_name(), - $this->provider_service_name, - function () { - printf( - wp_kses( - /* translators: %1$s is the link to register for an IBM Cloud account, %2$s is the link to setup the NLU service */ - __( 'Don\'t have an IBM Cloud account yet? Register for one and set up a Natural Language Understanding Resource to get your API key.', 'classifai' ), - [ - 'a' => [ - 'href' => [], - 'title' => [], - ], - ] - ), - esc_url( 'https://cloud.ibm.com/registration' ), - esc_url( 'https://cloud.ibm.com/catalog/services/natural-language-understanding' ) - ); - - $credentials = $this->get_settings( 'credentials' ); - $watson_url = $credentials['watson_url'] ?? ''; - - if ( ! empty( $watson_url ) && strpos( $watson_url, 'watsonplatform.net' ) !== false ) { - echo '

'; - printf( - wp_kses( - /* translators: %s is the link to the IBM Watson documentation */ - __( 'The `watsonplatform.net` endpoint URLs were retired on 26 May 2021. Please update the endpoint url. Check here for details.', 'classifai' ), - [ - 'a' => [ - 'href' => [], - 'title' => [], - ], - ] - ), - esc_url( 'https://cloud.ibm.com/docs/watson?topic=watson-endpoint-change' ) - ); - echo '

'; - } - }, - $this->get_option_name() - ); - } - /** * Check if a username/password is used instead of API key. * diff --git a/includes/Classifai/Services/Service.php b/includes/Classifai/Services/Service.php index 5edf4d043..3723126b8 100644 --- a/includes/Classifai/Services/Service.php +++ b/includes/Classifai/Services/Service.php @@ -106,7 +106,9 @@ public function init() { public function register_providers() { if ( ! empty( $this->provider_classes ) ) { foreach ( $this->provider_classes as $provider ) { - $provider->register(); + if ( method_exists( $provider, 'register' ) ) { + $provider->register(); + } } } } From e91626f0207db7f64e4de75042ee3986045bafcf Mon Sep 17 00:00:00 2001 From: Siddharth Thevaril Date: Fri, 2 Feb 2024 14:20:03 +0530 Subject: [PATCH 2/8] remove dead code --- .../Providers/Watson/SavePostHandler.php | 35 ------------------- 1 file changed, 35 deletions(-) diff --git a/includes/Classifai/Providers/Watson/SavePostHandler.php b/includes/Classifai/Providers/Watson/SavePostHandler.php index 23c00a254..8be5373da 100644 --- a/includes/Classifai/Providers/Watson/SavePostHandler.php +++ b/includes/Classifai/Providers/Watson/SavePostHandler.php @@ -25,41 +25,6 @@ public function register() { add_action( 'admin_post_classifai_classify_post', array( $this, 'classifai_classify_post' ) ); } - /** - * Check to see if we can register this class. - * - * @return bool - */ - public function can_register(): bool { - - $should_register = false; - if ( $this->is_configured() && ( is_admin() || $this->is_rest_route() ) ) { - $should_register = true; - } - - /** - * Filter whether ClassifAI should register this class. - * - * @since 1.8.0 - * @hook classifai_should_register_save_post_handler - * - * @param {bool} $should_register Whether the class should be registered. - * @return {bool} Whether the class should be registered. - */ - $should_register = apply_filters( 'classifai_should_register_save_post_handler', $should_register ); - - return $should_register; - } - - /** - * Check if ClassifAI is properly configured. - * - * @return bool - */ - public function is_configured(): bool { - return ! empty( get_option( 'classifai_configured' ) ) && ! empty( get_option( 'classifai_watson_nlu' )['credentials']['watson_url'] ); - } - /** * Sets the default value for the _classifai_process_content meta key. * From 4a624e368451bb39ea5775447cce53a4891fafd4 Mon Sep 17 00:00:00 2001 From: Siddharth Thevaril Date: Fri, 2 Feb 2024 14:38:55 +0530 Subject: [PATCH 3/8] remove more dead code --- .../Providers/Azure/ComputerVision.php | 6 ------ .../Providers/Azure/Personalizer.php | 6 ------ includes/Classifai/Providers/Azure/Speech.php | 6 ------ .../Classifai/Providers/OpenAI/ChatGPT.php | 6 ------ includes/Classifai/Providers/OpenAI/DallE.php | 6 ------ .../Classifai/Providers/OpenAI/Embeddings.php | 7 ------- .../Classifai/Providers/OpenAI/Whisper.php | 6 ------ includes/Classifai/Providers/Provider.php | 21 ------------------- includes/Classifai/Providers/Watson/NLU.php | 6 ------ includes/Classifai/Services/Service.php | 1 - 10 files changed, 71 deletions(-) diff --git a/includes/Classifai/Providers/Azure/ComputerVision.php b/includes/Classifai/Providers/Azure/ComputerVision.php index dba91af49..ad48c3b5a 100644 --- a/includes/Classifai/Providers/Azure/ComputerVision.php +++ b/includes/Classifai/Providers/Azure/ComputerVision.php @@ -33,12 +33,6 @@ 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; } diff --git a/includes/Classifai/Providers/Azure/Personalizer.php b/includes/Classifai/Providers/Azure/Personalizer.php index e0b63354b..6d50e6d50 100644 --- a/includes/Classifai/Providers/Azure/Personalizer.php +++ b/includes/Classifai/Providers/Azure/Personalizer.php @@ -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' ] ); diff --git a/includes/Classifai/Providers/Azure/Speech.php b/includes/Classifai/Providers/Azure/Speech.php index 652a154eb..98ecac2a6 100644 --- a/includes/Classifai/Providers/Azure/Speech.php +++ b/includes/Classifai/Providers/Azure/Speech.php @@ -44,12 +44,6 @@ 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 ); diff --git a/includes/Classifai/Providers/OpenAI/ChatGPT.php b/includes/Classifai/Providers/OpenAI/ChatGPT.php index 2332e1834..b45f57caf 100644 --- a/includes/Classifai/Providers/OpenAI/ChatGPT.php +++ b/includes/Classifai/Providers/OpenAI/ChatGPT.php @@ -46,12 +46,6 @@ 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; } diff --git a/includes/Classifai/Providers/OpenAI/DallE.php b/includes/Classifai/Providers/OpenAI/DallE.php index 6042f646c..73bae1b7e 100644 --- a/includes/Classifai/Providers/OpenAI/DallE.php +++ b/includes/Classifai/Providers/OpenAI/DallE.php @@ -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; } diff --git a/includes/Classifai/Providers/OpenAI/Embeddings.php b/includes/Classifai/Providers/OpenAI/Embeddings.php index 9edc3a440..bed1e908e 100644 --- a/includes/Classifai/Providers/OpenAI/Embeddings.php +++ b/includes/Classifai/Providers/OpenAI/Embeddings.php @@ -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; } diff --git a/includes/Classifai/Providers/OpenAI/Whisper.php b/includes/Classifai/Providers/OpenAI/Whisper.php index ce0ed1c91..d56b32e61 100644 --- a/includes/Classifai/Providers/OpenAI/Whisper.php +++ b/includes/Classifai/Providers/OpenAI/Whisper.php @@ -62,12 +62,6 @@ 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; } diff --git a/includes/Classifai/Providers/Provider.php b/includes/Classifai/Providers/Provider.php index f66384d53..9fc732995 100644 --- a/includes/Classifai/Providers/Provider.php +++ b/includes/Classifai/Providers/Provider.php @@ -36,18 +36,6 @@ abstract class Provider { */ protected $features = array(); - /** - * Provider constructor. - * - * @param string $provider_name The name of the Provider that will appear in the admin tab - * @param string $provider_service_name The name of the Service. - * @param string $option_name Name of the option where the provider settings are stored. - */ - public function __construct( string $provider_name, string $provider_service_name, string $option_name ) { - $this->provider_name = $provider_name; - $this->provider_service_name = $provider_service_name; - $this->option_name = $option_name; - } /** * Provides the provider name. @@ -58,15 +46,6 @@ public function get_provider_name(): string { return $this->provider_name; } - /** - * Returns the name of the settings section for this provider. - * - * @return string - */ - public function get_settings_section(): string { - return $this->option_name; - } - /** * Get provider features. * diff --git a/includes/Classifai/Providers/Watson/NLU.php b/includes/Classifai/Providers/Watson/NLU.php index ba3667bd3..5142d6adc 100644 --- a/includes/Classifai/Providers/Watson/NLU.php +++ b/includes/Classifai/Providers/Watson/NLU.php @@ -42,12 +42,6 @@ class NLU extends Provider { * @param \Classifai\Features\Feature $feature Feature instance (Optional, only required in admin). */ public function __construct( $feature = null ) { - parent::__construct( - 'IBM Watson', - 'Natural Language Understanding', - 'watson_nlu' - ); - $this->nlu_features = [ 'category' => [ 'feature' => __( 'Category', 'classifai' ), diff --git a/includes/Classifai/Services/Service.php b/includes/Classifai/Services/Service.php index 3723126b8..8738c13d3 100644 --- a/includes/Classifai/Services/Service.php +++ b/includes/Classifai/Services/Service.php @@ -135,7 +135,6 @@ public function get_display_name(): string { * Render the start of a settings page. The rest is added by the providers */ public function render_settings_page() { - $active_tab = $this->provider_classes ? $this->provider_classes[0]->get_settings_section() : ''; $active_tab = isset( $_GET['provider'] ) ? sanitize_text_field( wp_unslash( $_GET['provider'] ) ) : $active_tab; // phpcs:ignore WordPress.Security.NonceVerification.Recommended $base_url = add_query_arg( array( From 54fa81ce06d0c268fbdb4d2f5b75f88f6f21a6bd Mon Sep 17 00:00:00 2001 From: Siddharth Thevaril Date: Fri, 2 Feb 2024 15:04:18 +0530 Subject: [PATCH 4/8] remove more dead code --- includes/Classifai/Helpers.php | 46 ---------------- includes/Classifai/Providers/Provider.php | 55 ------------------- .../Classifai/Providers/Watson/Helpers.php | 2 - includes/Classifai/Services/Service.php | 1 - tests/Classifai/HelpersTest.php | 8 --- 5 files changed, 112 deletions(-) diff --git a/includes/Classifai/Helpers.php b/includes/Classifai/Helpers.php index e91ae6bb1..9e16003d3 100644 --- a/includes/Classifai/Helpers.php +++ b/includes/Classifai/Helpers.php @@ -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, * diff --git a/includes/Classifai/Providers/Provider.php b/includes/Classifai/Providers/Provider.php index 9fc732995..594a9b05a 100644 --- a/includes/Classifai/Providers/Provider.php +++ b/includes/Classifai/Providers/Provider.php @@ -14,16 +14,6 @@ abstract class Provider { */ const ID = ''; - /** - * @var string The display name for the provider, i.e. Azure - */ - public $provider_name; - - /** - * @var string $provider_service_name Formal name of the provider, i.e AI Vision, NLU, Rekongnition. - */ - public $provider_service_name; - /** * Feature instance. * @@ -31,51 +21,6 @@ abstract class Provider { */ protected $feature_instance = null; - /** - * @var array $features Array of features provided by this provider. - */ - protected $features = array(); - - - /** - * Provides the provider name. - * - * @return string - */ - public function get_provider_name(): string { - return $this->provider_name; - } - - /** - * Get provider features. - * - * @return array - */ - public function get_features(): array { - return $this->features; - } - - /** - * Default settings for Provider. - * - * @return array - */ - public function get_default_settings(): array { - return []; - } - - /** - * Common entry point for all REST endpoints for this provider. - * - * @param mixed $item The item we're processing. - * @param string $route_to_call The name of the route we're going to be processing. - * @param array $args Optional arguments to pass to the route. - * @return mixed - */ - public function rest_endpoint_callback( $item, string $route_to_call, array $args = [] ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed - return null; - } - /** * Format the result of most recent request. * diff --git a/includes/Classifai/Providers/Watson/Helpers.php b/includes/Classifai/Providers/Watson/Helpers.php index 86942787a..075f2afc6 100644 --- a/includes/Classifai/Providers/Watson/Helpers.php +++ b/includes/Classifai/Providers/Watson/Helpers.php @@ -7,8 +7,6 @@ use Classifai\Features\Classification; -use function Classifai\get_plugin_settings; - /** * Returns the currently configured Watson API URL. Lookup order is, * diff --git a/includes/Classifai/Services/Service.php b/includes/Classifai/Services/Service.php index 8738c13d3..2fe66e5a5 100644 --- a/includes/Classifai/Services/Service.php +++ b/includes/Classifai/Services/Service.php @@ -135,7 +135,6 @@ public function get_display_name(): string { * Render the start of a settings page. The rest is added by the providers */ public function render_settings_page() { - $active_tab = isset( $_GET['provider'] ) ? sanitize_text_field( wp_unslash( $_GET['provider'] ) ) : $active_tab; // phpcs:ignore WordPress.Security.NonceVerification.Recommended $base_url = add_query_arg( array( 'page' => 'classifai', diff --git a/tests/Classifai/HelpersTest.php b/tests/Classifai/HelpersTest.php index 8b063cb9a..9e161cb77 100644 --- a/tests/Classifai/HelpersTest.php +++ b/tests/Classifai/HelpersTest.php @@ -42,14 +42,6 @@ function test_it_has_a_plugin_instance() { $this->assertInstanceOf( '\Classifai\Plugin', $actual ); } - function test_it_has_plugin_settings() { - $this->markTestSkipped(); - update_option( 'classifai_settings', [ 'post_types' => [ 'foo' ] ] ); - - $actual = get_plugin_settings(); - $this->assertEquals( [ 'foo' ], $actual['post_types'] ); - } - function test_it_has_default_supported_post_types() { $actual = get_supported_post_types(); $this->assertEquals( ['post'], $actual ); From 341aa02194792f79c61d6829c5a688517c0cb345 Mon Sep 17 00:00:00 2001 From: Siddharth Thevaril Date: Fri, 2 Feb 2024 15:07:02 +0530 Subject: [PATCH 5/8] phpcs fixes --- includes/Classifai/Providers/Provider.php | 1 - 1 file changed, 1 deletion(-) diff --git a/includes/Classifai/Providers/Provider.php b/includes/Classifai/Providers/Provider.php index 594a9b05a..1d717441c 100644 --- a/includes/Classifai/Providers/Provider.php +++ b/includes/Classifai/Providers/Provider.php @@ -64,5 +64,4 @@ public function add_api_key_field( array $args = [] ) { ] ); } - } From f2627b59853c9b04ff6efc812ab67b8f4157ab48 Mon Sep 17 00:00:00 2001 From: Siddharth Thevaril Date: Fri, 2 Feb 2024 15:39:08 +0530 Subject: [PATCH 6/8] remove dead code --- includes/Classifai/Providers/OpenAI/Moderation.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/includes/Classifai/Providers/OpenAI/Moderation.php b/includes/Classifai/Providers/OpenAI/Moderation.php index f9d8a98b4..176dcf28f 100644 --- a/includes/Classifai/Providers/OpenAI/Moderation.php +++ b/includes/Classifai/Providers/OpenAI/Moderation.php @@ -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; } From 4176ec4bbc8564c4a3102beb0cbcd4f8cd62eceb Mon Sep 17 00:00:00 2001 From: Siddharth Thevaril Date: Fri, 2 Feb 2024 15:56:56 +0530 Subject: [PATCH 7/8] remove dead code for PHPUnit --- tests/Classifai/Watson/NLUSettingsTest.php | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/tests/Classifai/Watson/NLUSettingsTest.php b/tests/Classifai/Watson/NLUSettingsTest.php index 850e57288..9a311517a 100644 --- a/tests/Classifai/Watson/NLUSettingsTest.php +++ b/tests/Classifai/Watson/NLUSettingsTest.php @@ -36,22 +36,6 @@ function set_up() { $this->provider = new NLU( new Classification() ); } - /** - * Test the option name. - */ - public function test_option_name() { - $this->assertSame( 'classifai_watson_nlu', $this->provider->get_option_name() ); - } - - /** - * Retrieving the options. - */ - public function test_retrieving_options() { - $options = get_option( $this->provider->get_option_name() ); - - $this->assertEquals( $this->settings, $options ); - } - /** * Tests the function providing debug information. */ From 5b2a4146334cdae891f3d74048345248ec783349 Mon Sep 17 00:00:00 2001 From: Darin Kotter Date: Mon, 5 Feb 2024 10:44:05 -0700 Subject: [PATCH 8/8] Fix undefined calls --- includes/Classifai/Providers/Azure/Personalizer.php | 4 ++-- includes/Classifai/Providers/Azure/Speech.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/includes/Classifai/Providers/Azure/Personalizer.php b/includes/Classifai/Providers/Azure/Personalizer.php index 6d50e6d50..09e5d1d6d 100644 --- a/includes/Classifai/Providers/Azure/Personalizer.php +++ b/includes/Classifai/Providers/Azure/Personalizer.php @@ -155,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' diff --git a/includes/Classifai/Providers/Azure/Speech.php b/includes/Classifai/Providers/Azure/Speech.php index 98ecac2a6..670ec195f 100644 --- a/includes/Classifai/Providers/Azure/Speech.php +++ b/includes/Classifai/Providers/Azure/Speech.php @@ -240,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' @@ -254,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'