From a765c73a9b3886c3d1f78222facdd9a772c6b4d3 Mon Sep 17 00:00:00 2001 From: Siddharth Thevaril Date: Tue, 7 Nov 2023 19:26:38 +0530 Subject: [PATCH] fix OpenAI trait --- .../Classifai/Features/ContentResizing.php | 5 +++- .../Classifai/Features/ExcerptGeneration.php | 5 +++- .../Classifai/Features/TitleGeneration.php | 21 +++------------ .../Classifai/Providers/OpenAI/ChatGPT.php | 13 +++------ .../Classifai/Providers/OpenAI/OpenAI.php | 27 ++++++++++--------- includes/Classifai/Providers/Provider.php | 6 +++++ 6 files changed, 35 insertions(+), 42 deletions(-) diff --git a/includes/Classifai/Features/ContentResizing.php b/includes/Classifai/Features/ContentResizing.php index 0b3e04805..95491637b 100644 --- a/includes/Classifai/Features/ContentResizing.php +++ b/includes/Classifai/Features/ContentResizing.php @@ -134,6 +134,7 @@ public function get_default_settings() { ChatGPT::ID => [ 'api_key' => '', 'number_of_suggestions' => 1, + 'authenticated' => false, 'condense_text_prompt' => array( array( 'title' => esc_html__( 'Decrease the content length no more than 2 to 4 sentences.', 'classifai' ), @@ -179,7 +180,9 @@ public function sanitize_settings( $settings ) { } if ( isset( $settings[ ChatGPT::ID ] ) ) { - $new_settings[ ChatGPT::ID ]['api_key'] = $chat_gpt->sanitize_api_key( $settings ); + $api_key_settings = $chat_gpt->sanitize_api_key_settings( $settings ); + $new_settings[ ChatGPT::ID ]['api_key'] = $api_key_settings[ ChatGPT::ID ]['api_key']; + $new_settings[ ChatGPT::ID ]['authenticated'] = $api_key_settings[ ChatGPT::ID ]['authenticated']; $new_settings[ ChatGPT::ID ]['number_of_suggestions'] = $chat_gpt->sanitize_number_of_responses_field( 'number_of_suggestions', $settings ); $new_settings[ ChatGPT::ID ]['condense_text_prompt'] = $chat_gpt->sanitize_prompts( 'condense_text_prompt', $settings ); $new_settings[ ChatGPT::ID ]['expand_text_prompt'] = $chat_gpt->sanitize_prompts( 'expand_text_prompt', $settings ); diff --git a/includes/Classifai/Features/ExcerptGeneration.php b/includes/Classifai/Features/ExcerptGeneration.php index f45233864..ca077dc48 100644 --- a/includes/Classifai/Features/ExcerptGeneration.php +++ b/includes/Classifai/Features/ExcerptGeneration.php @@ -134,6 +134,7 @@ public function get_default_settings() { 'provider' => \Classifai\Providers\OpenAI\ChatGPT::ID, ChatGPT::ID => [ 'api_key' => '', + 'authenticated' => false, 'generate_excerpt_prompt' => array( array( 'title' => esc_html__( 'Default', 'classifai' ), @@ -174,7 +175,9 @@ public function sanitize_settings( $settings ) { } if ( isset( $settings[ ChatGPT::ID ] ) ) { - $new_settings[ ChatGPT::ID ]['api_key'] = $chat_gpt->sanitize_api_key( $settings ); + $api_key_settings = $chat_gpt->sanitize_api_key_settings( $settings ); + $new_settings[ ChatGPT::ID ]['api_key'] = $api_key_settings[ ChatGPT::ID ]['api_key']; + $new_settings[ ChatGPT::ID ]['authenticated'] = $api_key_settings[ ChatGPT::ID ]['authenticated']; $new_settings[ ChatGPT::ID ]['generate_excerpt_prompt'] = $chat_gpt->sanitize_prompts( 'generate_excerpt_prompt', $settings ); } diff --git a/includes/Classifai/Features/TitleGeneration.php b/includes/Classifai/Features/TitleGeneration.php index 19cd7f856..fd21343a2 100644 --- a/includes/Classifai/Features/TitleGeneration.php +++ b/includes/Classifai/Features/TitleGeneration.php @@ -61,22 +61,6 @@ public function setup_fields_sections() { ] ); - add_settings_field( - 'length', - esc_html__( 'Excerpt length', 'classifai' ), - [ $this, 'render_input' ], - $this->get_option_name(), - $this->get_option_name() . '_section', - [ - 'label_for' => 'length', - 'input_type' => 'number', - 'min' => 1, - 'step' => 1, - 'default_value' => $default_settings['length'], - 'description' => __( 'How many words should the excerpt be? Note that the final result may not exactly match this. In testing, ChatGPT tended to exceed this number by 10-15 words.', 'classifai' ), - ] - ); - add_settings_field( 'provider', esc_html__( 'Select a provider', 'classifai' ), @@ -142,6 +126,7 @@ public function get_default_settings() { ChatGPT::ID => [ 'api_key' => '', 'number_of_titles' => 1, + 'authenticated' => false, 'generate_title_prompt' => array( array( 'title' => esc_html__( 'ClassifAI default', 'classifai' ), @@ -176,7 +161,9 @@ public function sanitize_settings( $settings ) { } if ( isset( $settings[ ChatGPT::ID ] ) ) { - $new_settings[ ChatGPT::ID ]['api_key'] = $chat_gpt->sanitize_api_key( $settings ); + $api_key_settings = $chat_gpt->sanitize_api_key_settings( $settings ); + $new_settings[ ChatGPT::ID ]['api_key'] = $api_key_settings[ ChatGPT::ID ]['api_key']; + $new_settings[ ChatGPT::ID ]['authenticated'] = $api_key_settings[ ChatGPT::ID ]['authenticated']; $new_settings[ ChatGPT::ID ]['number_of_titles'] = $chat_gpt->sanitize_number_of_responses_field( 'number_of_titles', $settings ); $new_settings[ ChatGPT::ID ]['generate_title_prompt'] = $chat_gpt->sanitize_prompts( 'generate_title_prompt', $settings ); } diff --git a/includes/Classifai/Providers/OpenAI/ChatGPT.php b/includes/Classifai/Providers/OpenAI/ChatGPT.php index 339185b4d..439bc7d30 100644 --- a/includes/Classifai/Providers/OpenAI/ChatGPT.php +++ b/includes/Classifai/Providers/OpenAI/ChatGPT.php @@ -68,13 +68,6 @@ class ChatGPT extends Provider { */ protected $grow_content_prompt = 'Increase the content length no more than 2 to 4 sentences.'; - /** - * Feature instance. - * - * @var \Classifai\Features\Feature - */ - protected $feature_instance = null; - /** * OpenAI ChatGPT constructor. * @@ -590,7 +583,7 @@ public function generate_excerpt( int $post_id = 0, array $args = [] ) { $excerpt_length = absint( $settings['length'] ?? 55 ); - $request = new APIRequest( $settings[ static::ID ]['api_key'] ?? '', $this->get_option_name() ); + $request = new APIRequest( $settings[ static::ID ]['api_key'] ?? '', $feature->get_option_name() ); $excerpt_prompt = esc_textarea( $this->get_default_prompt( $settings[ static::ID ]['generate_excerpt_prompt'] ) ?? $this->generate_excerpt_prompt ); @@ -694,7 +687,7 @@ public function generate_titles( int $post_id = 0, array $args = [] ) { return new WP_Error( 'not_enabled', esc_html__( 'Title generation is disabled or OpenAI authentication failed. Please check your settings.', 'classifai' ) ); } - $request = new APIRequest( $settings[ static::ID ]['api_key'] ?? '', $this->get_option_name() ); + $request = new APIRequest( $settings[ static::ID ]['api_key'] ?? '', $feature->get_option_name() ); $prompt = esc_textarea( $this->get_default_prompt( $settings[ static::ID ]['generate_title_prompt'] ) ?? $this->generate_title_prompt ); @@ -795,7 +788,7 @@ public function resize_content( int $post_id, array $args = array() ) { ] ); - $request = new APIRequest( $settings[ static::ID ]['api_key'] ?? '', $this->get_option_name() ); + $request = new APIRequest( $settings[ static::ID ]['api_key'] ?? '', $feature->get_option_name() ); if ( 'shrink' === $args['resize_type'] ) { $prompt = esc_textarea( $this->get_default_prompt( $settings[ static::ID ]['shrink_content_prompt'] ) ?? $this->shrink_content_prompt ); diff --git a/includes/Classifai/Providers/OpenAI/OpenAI.php b/includes/Classifai/Providers/OpenAI/OpenAI.php index 149542263..df8b48190 100644 --- a/includes/Classifai/Providers/OpenAI/OpenAI.php +++ b/includes/Classifai/Providers/OpenAI/OpenAI.php @@ -25,12 +25,12 @@ trait OpenAI { * @param string $default_api_key Default API key. */ protected function setup_api_fields( string $default_api_key = '' ) { - $existing_settings = $this->get_settings(); + $existing_settings = $this->feature_instance->get_settings(); $description = ''; // Add the settings section. add_settings_section( - $this->get_option_name(), + $this->feature_instance->get_option_name(), $this->provider_service_name, function() { printf( @@ -47,7 +47,7 @@ function() { esc_url( 'https://platform.openai.com/signup' ) ); }, - $this->get_option_name() + $this->feature_instance->get_option_name() ); // Determine which other OpenAI provider to look for an API key in. @@ -77,8 +77,8 @@ function() { 'api-key', esc_html__( 'API Key', 'classifai' ), [ $this, 'render_input' ], - $this->get_option_name(), - $this->get_option_name(), + $this->feature_instance->get_option_name(), + $this->feature_instance->get_option_name(), [ 'label_for' => 'api_key', 'input_type' => 'password', @@ -95,17 +95,18 @@ function() { * @param array $old_settings Existing settings, if any. * @return array */ - protected function sanitize_api_key_settings( array $new_settings = [], array $old_settings = [] ) { - $authenticated = $this->authenticate_credentials( $old_settings['api_key'] ?? '' ); + public function sanitize_api_key_settings( array $old_settings = [] ) { + $new_settings = $this->feature_instance->get_settings(); + $authenticated = $this->authenticate_credentials( $old_settings[ static::ID ]['api_key'] ?? '' ); if ( is_wp_error( $authenticated ) ) { - $new_settings['authenticated'] = false; - $error_message = $authenticated->get_error_message(); + $new_settings[ static::ID ]['authenticated'] = false; + $error_message = $authenticated->get_error_message(); // For response code 429, credentials are valid but rate limit is reached. if ( 429 === (int) $authenticated->get_error_code() ) { - $new_settings['authenticated'] = true; - $error_message = str_replace( 'plan and billing details', 'plan and billing details', $error_message ); + $new_settings[ static::ID ]['authenticated'] = true; + $error_message = str_replace( 'plan and billing details', 'plan and billing details', $error_message ); } else { $error_message = str_replace( 'https://platform.openai.com/account/api-keys', 'https://platform.openai.com/account/api-keys', $error_message ); } @@ -117,10 +118,10 @@ protected function sanitize_api_key_settings( array $new_settings = [], array $o 'error' ); } else { - $new_settings['authenticated'] = true; + $new_settings[ static::ID ]['authenticated'] = true; } - $new_settings['api_key'] = sanitize_text_field( $old_settings['api_key'] ?? '' ); + $new_settings[ static::ID ]['api_key'] = sanitize_text_field( $old_settings[ static::ID ]['api_key'] ?? '' ); return $new_settings; } diff --git a/includes/Classifai/Providers/Provider.php b/includes/Classifai/Providers/Provider.php index 2eea97137..d36d43df8 100644 --- a/includes/Classifai/Providers/Provider.php +++ b/includes/Classifai/Providers/Provider.php @@ -34,6 +34,12 @@ abstract class Provider { */ public $onboarding_options; + /** + * Feature instance. + * + * @var \Classifai\Features\Feature + */ + protected $feature_instance = null; /** * Provider constructor.