From 53c8c2438f34caa149ed6dd34be08ee40bc1c62f Mon Sep 17 00:00:00 2001 From: faisal-alvi Date: Tue, 24 Oct 2023 14:15:52 +0530 Subject: [PATCH 01/12] classifai_openai_api_request_get hook --- .../Classifai/Providers/OpenAI/APIRequest.php | 32 +++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/includes/Classifai/Providers/OpenAI/APIRequest.php b/includes/Classifai/Providers/OpenAI/APIRequest.php index 333954e2c..c4fd41530 100644 --- a/includes/Classifai/Providers/OpenAI/APIRequest.php +++ b/includes/Classifai/Providers/OpenAI/APIRequest.php @@ -25,13 +25,22 @@ class APIRequest { */ public $api_key; + /** + * The feature name. + * + * @var string + */ + public $feature; + /** * OpenAI APIRequest constructor. * * @param string $api_key OpenAI API key. + * @param string $feature Feature name. */ - public function __construct( string $api_key = '' ) { + public function __construct( string $api_key = '', string $feature = '' ) { $this->api_key = $api_key; + $this->feature = $feature; } /** @@ -43,7 +52,26 @@ public function __construct( string $api_key = '' ) { */ public function get( string $url, array $options = [] ) { $this->add_headers( $options ); - return $this->get_result( wp_remote_get( $url, $options ) ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get + + /** + * Filter the response from OpenAI for a post request. + * + * @since x.x.x + * @hook classifai_openai_api_request_post + * + * @param {string} $url Request URL. + * @param {array} $options Request body options. + * @param {string} $this->feature Feature name. + * + * @return {array} API response. + */ + return apply_filters( + 'classifai_openai_api_request_get', + $this->get_result( wp_remote_get( $url, $options ) ), // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get + $url, + $options, + $this->feature + ); } /** From eba8c8e9b80bdecb8f18b54530cd60f09adec554 Mon Sep 17 00:00:00 2001 From: faisal-alvi Date: Tue, 24 Oct 2023 14:17:19 +0530 Subject: [PATCH 02/12] @hook classifai_openai_api_request_post --- .../Classifai/Providers/OpenAI/APIRequest.php | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/includes/Classifai/Providers/OpenAI/APIRequest.php b/includes/Classifai/Providers/OpenAI/APIRequest.php index c4fd41530..70b0f966b 100644 --- a/includes/Classifai/Providers/OpenAI/APIRequest.php +++ b/includes/Classifai/Providers/OpenAI/APIRequest.php @@ -54,10 +54,10 @@ public function get( string $url, array $options = [] ) { $this->add_headers( $options ); /** - * Filter the response from OpenAI for a post request. + * Filter the response from OpenAI for a get request. * * @since x.x.x - * @hook classifai_openai_api_request_post + * @hook classifai_openai_api_request_get * * @param {string} $url Request URL. * @param {array} $options Request body options. @@ -89,7 +89,26 @@ public function post( string $url = '', array $options = [] ) { ] ); $this->add_headers( $options ); - return $this->get_result( wp_remote_post( $url, $options ) ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get + + /** + * Filter the response from OpenAI for a post request. + * + * @since x.x.x + * @hook classifai_openai_api_request_post + * + * @param {string} $url Request URL. + * @param {array} $options Request body options. + * @param {string} $this->feature Feature name. + * + * @return {array} API response. + */ + return apply_filters( + 'classifai_openai_api_request_post', + $this->get_result( wp_remote_post( $url, $options ) ), // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get + $url, + $options, + $this->feature + ); } /** From c01d3713f4389663e38347c1c7b7a411b103c77e Mon Sep 17 00:00:00 2001 From: faisal-alvi Date: Tue, 24 Oct 2023 14:20:57 +0530 Subject: [PATCH 03/12] @hook classifai_openai_api_request_post_form_options --- .../Classifai/Providers/OpenAI/APIRequest.php | 31 +++++++++++++++---- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/includes/Classifai/Providers/OpenAI/APIRequest.php b/includes/Classifai/Providers/OpenAI/APIRequest.php index 70b0f966b..d2ca1ed23 100644 --- a/includes/Classifai/Providers/OpenAI/APIRequest.php +++ b/includes/Classifai/Providers/OpenAI/APIRequest.php @@ -142,13 +142,32 @@ public function post_form( string $url = '', array $body = [] ) { $payload .= '--' . $boundary . '--'; - $options = [ - 'body' => $payload, - 'headers' => [ - 'Content-Type' => 'multipart/form-data; boundary=' . $boundary, + /** + * Filter the options for the post form request. + * + * @since x.x.x + * @hook classifai_openai_api_request_post_form_options + * + * @param {array} $options The options for the request. + * @param {string} $url The URL for the request. + * @param {array} $body The body of the request. + * @param {string} $this->feature The feature name. + * + * @return {array} The options for the request. + */ + $options = apply_filters( + 'classifai_openai_api_request_post_form_options', + [ + 'body' => $payload, + 'headers' => [ + 'Content-Type' => 'multipart/form-data; boundary=' . $boundary, + ], + 'timeout' => 60, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout ], - 'timeout' => 60, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout - ]; + $url, + $body, + $this->feature + ); $this->add_headers( $options ); From 60ab9007506772e910b81571b7d77bc7fcbf1dcc Mon Sep 17 00:00:00 2001 From: faisal-alvi Date: Tue, 24 Oct 2023 14:43:51 +0530 Subject: [PATCH 04/12] @hook classifai_openai_api_request_post_form --- .../Classifai/Providers/OpenAI/APIRequest.php | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/includes/Classifai/Providers/OpenAI/APIRequest.php b/includes/Classifai/Providers/OpenAI/APIRequest.php index d2ca1ed23..f9ef4b863 100644 --- a/includes/Classifai/Providers/OpenAI/APIRequest.php +++ b/includes/Classifai/Providers/OpenAI/APIRequest.php @@ -171,7 +171,25 @@ public function post_form( string $url = '', array $body = [] ) { $this->add_headers( $options ); - return $this->get_result( wp_remote_post( $url, $options ) ); + /** + * Filter the response from OpenAI for a post form request. + * + * @since x.x.x + * @hook classifai_openai_api_request_post_form + * + * @param {string} $url Request URL. + * @param {array} $options Request body options. + * @param {string} $this->feature Feature name. + * + * @return {array} API response. + */ + return apply_filters( + 'classifai_openai_api_request_post_form', + $this->get_result( wp_remote_post( $url, $options ) ), // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get + $url, + $options, + $this->feature + ); } /** From 8c31e19748033718a0e9599f9c96890d389f398a Mon Sep 17 00:00:00 2001 From: faisal-alvi Date: Tue, 24 Oct 2023 14:48:13 +0530 Subject: [PATCH 05/12] fix hook names --- includes/Classifai/Providers/Azure/Read.php | 2 +- includes/Classifai/Providers/OpenAI/APIRequest.php | 12 ++++++------ includes/Classifai/Providers/OpenAI/ChatGPT.php | 1 + 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/includes/Classifai/Providers/Azure/Read.php b/includes/Classifai/Providers/Azure/Read.php index 57562eabe..c1a5abb62 100644 --- a/includes/Classifai/Providers/Azure/Read.php +++ b/includes/Classifai/Providers/Azure/Read.php @@ -147,7 +147,7 @@ public function read_document() { * Filters the request arguments sent to Read endpoint. * * @since 1.7.0 - * @hook classifai_azure_read_should_process + * @hook classifai_azure_read_request_args * * @param {array} $args Whether to run OCR processing or not. * @param {int} $attachment_id The attachment ID. diff --git a/includes/Classifai/Providers/OpenAI/APIRequest.php b/includes/Classifai/Providers/OpenAI/APIRequest.php index f9ef4b863..5ed08ca87 100644 --- a/includes/Classifai/Providers/OpenAI/APIRequest.php +++ b/includes/Classifai/Providers/OpenAI/APIRequest.php @@ -57,7 +57,7 @@ public function get( string $url, array $options = [] ) { * Filter the response from OpenAI for a get request. * * @since x.x.x - * @hook classifai_openai_api_request_get + * @hook classifai_openai_api_response_get * * @param {string} $url Request URL. * @param {array} $options Request body options. @@ -66,7 +66,7 @@ public function get( string $url, array $options = [] ) { * @return {array} API response. */ return apply_filters( - 'classifai_openai_api_request_get', + 'classifai_openai_api_response_get', $this->get_result( wp_remote_get( $url, $options ) ), // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get $url, $options, @@ -94,7 +94,7 @@ public function post( string $url = '', array $options = [] ) { * Filter the response from OpenAI for a post request. * * @since x.x.x - * @hook classifai_openai_api_request_post + * @hook classifai_openai_api_response_post * * @param {string} $url Request URL. * @param {array} $options Request body options. @@ -103,7 +103,7 @@ public function post( string $url = '', array $options = [] ) { * @return {array} API response. */ return apply_filters( - 'classifai_openai_api_request_post', + 'classifai_openai_api_response_post', $this->get_result( wp_remote_post( $url, $options ) ), // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get $url, $options, @@ -175,7 +175,7 @@ public function post_form( string $url = '', array $body = [] ) { * Filter the response from OpenAI for a post form request. * * @since x.x.x - * @hook classifai_openai_api_request_post_form + * @hook classifai_openai_api_response_post_form * * @param {string} $url Request URL. * @param {array} $options Request body options. @@ -184,7 +184,7 @@ public function post_form( string $url = '', array $body = [] ) { * @return {array} API response. */ return apply_filters( - 'classifai_openai_api_request_post_form', + 'classifai_openai_api_response_post_form', $this->get_result( wp_remote_post( $url, $options ) ), // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get $url, $options, diff --git a/includes/Classifai/Providers/OpenAI/ChatGPT.php b/includes/Classifai/Providers/OpenAI/ChatGPT.php index b307dfc3b..4c5d11369 100644 --- a/includes/Classifai/Providers/OpenAI/ChatGPT.php +++ b/includes/Classifai/Providers/OpenAI/ChatGPT.php @@ -1010,6 +1010,7 @@ public function resize_content( int $post_id, array $args = array() ) { * Filter the resize prompt we will send to ChatGPT. * * @since 2.3.0 + * @hook classifai_chatgpt_' . $args['resize_type'] . '_content_prompt * * @param {string} $prompt Resize prompt we are sending to ChatGPT. Gets added as a system prompt. * @param {int} $post_id ID of post. From bbeb762e127b7ee8f6a2cbf61dc944f164b00ac9 Mon Sep 17 00:00:00 2001 From: faisal-alvi Date: Tue, 24 Oct 2023 14:57:06 +0530 Subject: [PATCH 06/12] @hook classifai_openai_characters_in_token --- includes/Classifai/Providers/OpenAI/Tokenizer.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/includes/Classifai/Providers/OpenAI/Tokenizer.php b/includes/Classifai/Providers/OpenAI/Tokenizer.php index 70663a570..b588b3cc7 100644 --- a/includes/Classifai/Providers/OpenAI/Tokenizer.php +++ b/includes/Classifai/Providers/OpenAI/Tokenizer.php @@ -35,6 +35,18 @@ class Tokenizer { */ public function __construct( $max_tokens ) { $this->max_tokens = $max_tokens; + + /** + * How many characters in one token (roughly) + * + * @since 0.1.0 + * @hook classifai_openai_characters_in_token + * + * @param int $characters_in_token How many characters in one token (roughly) + * + * @return int + */ + $this->characters_in_token = apply_filters( 'classifai_openai_characters_in_token', $this->characters_in_token ); } /** From 8ded044901e7671a181b57a62f19f6bf13443e8c Mon Sep 17 00:00:00 2001 From: faisal-alvi Date: Tue, 24 Oct 2023 15:09:55 +0530 Subject: [PATCH 07/12] @hook classifai_openai_tokens_per_word --- .../Classifai/Providers/OpenAI/Tokenizer.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/includes/Classifai/Providers/OpenAI/Tokenizer.php b/includes/Classifai/Providers/OpenAI/Tokenizer.php index b588b3cc7..06242510d 100644 --- a/includes/Classifai/Providers/OpenAI/Tokenizer.php +++ b/includes/Classifai/Providers/OpenAI/Tokenizer.php @@ -43,10 +43,24 @@ public function __construct( $max_tokens ) { * @hook classifai_openai_characters_in_token * * @param int $characters_in_token How many characters in one token (roughly) + * @param int $max_tokens Maximum tokens the model supports. * * @return int */ - $this->characters_in_token = apply_filters( 'classifai_openai_characters_in_token', $this->characters_in_token ); + $this->characters_in_token = apply_filters( 'classifai_openai_characters_in_token', $this->characters_in_token, $max_tokens ); + + /** + * How many tokens a word will take (roughly) + * + * @since 0.1.0 + * @hook classifai_openai_tokens_per_word + * + * @param int $tokens_per_word How many tokens a word will take (roughly) + * @param int $max_tokens Maximum tokens the model supports. + * + * @return int + */ + $this->tokens_per_word = apply_filters( 'classifai_openai_tokens_per_word', $this->tokens_per_word, $max_tokens ); } /** From 568e388cb2d178e97ea8226b0dbe481f02170d74 Mon Sep 17 00:00:00 2001 From: faisal-alvi Date: Tue, 24 Oct 2023 15:19:43 +0530 Subject: [PATCH 08/12] add second parameter to init'ed objects --- includes/Classifai/Providers/OpenAI/ChatGPT.php | 6 +++--- includes/Classifai/Providers/OpenAI/DallE.php | 2 +- includes/Classifai/Providers/OpenAI/Embeddings.php | 2 +- includes/Classifai/Providers/OpenAI/Whisper/Transcribe.php | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/includes/Classifai/Providers/OpenAI/ChatGPT.php b/includes/Classifai/Providers/OpenAI/ChatGPT.php index 4c5d11369..7110d5bd6 100644 --- a/includes/Classifai/Providers/OpenAI/ChatGPT.php +++ b/includes/Classifai/Providers/OpenAI/ChatGPT.php @@ -796,7 +796,7 @@ public function generate_excerpt( int $post_id = 0, array $args = [] ) { $excerpt_length = absint( $settings['length'] ?? 55 ); - $request = new APIRequest( $settings['api_key'] ?? '' ); + $request = new APIRequest( $settings['api_key'] ?? '', $this->get_option_name() ); $excerpt_prompt = ! empty( $settings['generate_excerpt_prompt'] ) ? esc_textarea( $settings['generate_excerpt_prompt'] ) : $this->generate_excerpt_prompt; @@ -899,7 +899,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['api_key'] ?? '' ); + $request = new APIRequest( $settings['api_key'] ?? '', $this->get_option_name() ); $prompt = ! empty( $settings['generate_title_prompt'] ) ? esc_textarea( $settings['generate_title_prompt'] ) : $this->generate_title_prompt; @@ -998,7 +998,7 @@ public function resize_content( int $post_id, array $args = array() ) { ] ); - $request = new APIRequest( $settings['api_key'] ?? '' ); + $request = new APIRequest( $settings['api_key'] ?? '', $this->get_option_name() ); if ( 'shrink' === $args['resize_type'] ) { $prompt = ! empty( $settings['shrink_content_prompt'] ) ? esc_textarea( $settings['shrink_content_prompt'] ) : $this->shrink_content_prompt; diff --git a/includes/Classifai/Providers/OpenAI/DallE.php b/includes/Classifai/Providers/OpenAI/DallE.php index 7a3b4a070..ab3732e11 100644 --- a/includes/Classifai/Providers/OpenAI/DallE.php +++ b/includes/Classifai/Providers/OpenAI/DallE.php @@ -482,7 +482,7 @@ public function generate_image_callback( string $prompt = '', array $args = [] ) return new WP_Error( 'invalid_param', esc_html__( 'Your image prompt is too long. Please ensure it doesn\'t exceed 1000 characters.', 'classifai' ) ); } - $request = new APIRequest( $settings['api_key'] ?? '' ); + $request = new APIRequest( $settings['api_key'] ?? '', 'generate-image' ); /** * Filter the request body before sending to DALLĀ·E. diff --git a/includes/Classifai/Providers/OpenAI/Embeddings.php b/includes/Classifai/Providers/OpenAI/Embeddings.php index 9b4ae9556..103c1edc3 100644 --- a/includes/Classifai/Providers/OpenAI/Embeddings.php +++ b/includes/Classifai/Providers/OpenAI/Embeddings.php @@ -580,7 +580,7 @@ public function generate_embeddings( int $id = 0, $type = 'post' ) { return false; } - $request = new APIRequest( $settings['api_key'] ?? '' ); + $request = new APIRequest( $settings['api_key'] ?? '', $this->get_option_name() ); /** * Filter the request body before sending to OpenAI. diff --git a/includes/Classifai/Providers/OpenAI/Whisper/Transcribe.php b/includes/Classifai/Providers/OpenAI/Whisper/Transcribe.php index 9faebb731..449585826 100644 --- a/includes/Classifai/Providers/OpenAI/Whisper/Transcribe.php +++ b/includes/Classifai/Providers/OpenAI/Whisper/Transcribe.php @@ -63,7 +63,7 @@ public function process() { return new WP_Error( 'process_error', esc_html__( 'Attachment does not meet processing requirements. Ensure the file type and size meet requirements.', 'classifai' ) ); } - $request = new APIRequest( $this->settings['api_key'] ?? '' ); + $request = new APIRequest( $this->settings['api_key'] ?? '', 'whisper' ); /** * Filter the request body before sending to Whisper. From c469d90e2a6781ed8376a5c9e84cadb6e60b1bfd Mon Sep 17 00:00:00 2001 From: faisal-alvi Date: Wed, 25 Oct 2023 15:51:16 +0530 Subject: [PATCH 09/12] suggestions applied --- .../Classifai/Providers/OpenAI/APIRequest.php | 8 ++++---- .../Classifai/Providers/OpenAI/Tokenizer.php | 16 ++++++++-------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/includes/Classifai/Providers/OpenAI/APIRequest.php b/includes/Classifai/Providers/OpenAI/APIRequest.php index 5ed08ca87..224451373 100644 --- a/includes/Classifai/Providers/OpenAI/APIRequest.php +++ b/includes/Classifai/Providers/OpenAI/APIRequest.php @@ -56,7 +56,7 @@ public function get( string $url, array $options = [] ) { /** * Filter the response from OpenAI for a get request. * - * @since x.x.x + * @since 2.4.0 * @hook classifai_openai_api_response_get * * @param {string} $url Request URL. @@ -93,7 +93,7 @@ public function post( string $url = '', array $options = [] ) { /** * Filter the response from OpenAI for a post request. * - * @since x.x.x + * @since 2.4.0 * @hook classifai_openai_api_response_post * * @param {string} $url Request URL. @@ -145,7 +145,7 @@ public function post_form( string $url = '', array $body = [] ) { /** * Filter the options for the post form request. * - * @since x.x.x + * @since 2.4.0 * @hook classifai_openai_api_request_post_form_options * * @param {array} $options The options for the request. @@ -174,7 +174,7 @@ public function post_form( string $url = '', array $body = [] ) { /** * Filter the response from OpenAI for a post form request. * - * @since x.x.x + * @since 2.4.0 * @hook classifai_openai_api_response_post_form * * @param {string} $url Request URL. diff --git a/includes/Classifai/Providers/OpenAI/Tokenizer.php b/includes/Classifai/Providers/OpenAI/Tokenizer.php index 06242510d..6785fe446 100644 --- a/includes/Classifai/Providers/OpenAI/Tokenizer.php +++ b/includes/Classifai/Providers/OpenAI/Tokenizer.php @@ -39,26 +39,26 @@ public function __construct( $max_tokens ) { /** * How many characters in one token (roughly) * - * @since 0.1.0 + * @since 2.4..0 * @hook classifai_openai_characters_in_token * - * @param int $characters_in_token How many characters in one token (roughly) - * @param int $max_tokens Maximum tokens the model supports. + * @param {int} $characters_in_token How many characters in one token (roughly) + * @param {int} $max_tokens Maximum tokens the model supports. * - * @return int + * @return {int} */ $this->characters_in_token = apply_filters( 'classifai_openai_characters_in_token', $this->characters_in_token, $max_tokens ); /** * How many tokens a word will take (roughly) * - * @since 0.1.0 + * @since 2.4.0 * @hook classifai_openai_tokens_per_word * - * @param int $tokens_per_word How many tokens a word will take (roughly) - * @param int $max_tokens Maximum tokens the model supports. + * @param {int} $tokens_per_word How many tokens a word will take (roughly) + * @param {int} $max_tokens Maximum tokens the model supports. * - * @return int + * @return {int} */ $this->tokens_per_word = apply_filters( 'classifai_openai_tokens_per_word', $this->tokens_per_word, $max_tokens ); } From d60bee06680ccdc70ebf44efe20aa4701d7a5dda Mon Sep 17 00:00:00 2001 From: faisal-alvi Date: Wed, 25 Oct 2023 16:34:09 +0530 Subject: [PATCH 10/12] filter the URL and options for the get request --- .../Classifai/Providers/OpenAI/APIRequest.php | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/includes/Classifai/Providers/OpenAI/APIRequest.php b/includes/Classifai/Providers/OpenAI/APIRequest.php index 224451373..31618aeb8 100644 --- a/includes/Classifai/Providers/OpenAI/APIRequest.php +++ b/includes/Classifai/Providers/OpenAI/APIRequest.php @@ -51,6 +51,34 @@ public function __construct( string $api_key = '', string $feature = '' ) { * @return array|WP_Error */ public function get( string $url, array $options = [] ) { + /** + * Filter the URL for the get request. + * + * @since 2.4.0 + * @hook classifai_openai_api_request_get_url + * + * @param {string} $url The URL for the request. + * @param {array} $options The options for the request. + * @param {string} $this->feature The feature name. + * + * @return {string} The URL for the request. + */ + apply_filters( 'classifai_openai_api_request_get_url', $url, $options, $this->feature ); + + /** + * Filter the options for the get request. + * + * @since 2.4.0 + * @hook classifai_openai_api_request_get_options + * + * @param {array} $options The options for the request. + * @param {string} $url The URL for the request. + * @param {string} $this->feature The feature name. + * + * @return {array} The options for the request. + */ + apply_filters( 'classifai_openai_api_request_get_options', $options, $url, $this->feature ); + $this->add_headers( $options ); /** From 7b71dc89ff41772b87e795ebd822f9aeb7425dff Mon Sep 17 00:00:00 2001 From: faisal-alvi Date: Wed, 25 Oct 2023 16:35:51 +0530 Subject: [PATCH 11/12] filter the url and options for post request --- .../Classifai/Providers/OpenAI/APIRequest.php | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/includes/Classifai/Providers/OpenAI/APIRequest.php b/includes/Classifai/Providers/OpenAI/APIRequest.php index 31618aeb8..150786b2b 100644 --- a/includes/Classifai/Providers/OpenAI/APIRequest.php +++ b/includes/Classifai/Providers/OpenAI/APIRequest.php @@ -116,6 +116,35 @@ public function post( string $url = '', array $options = [] ) { 'timeout' => 60, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout ] ); + + /** + * Filter the URL for the post request. + * + * @since 2.4.0 + * @hook classifai_openai_api_request_post_url + * + * @param {string} $url The URL for the request. + * @param {array} $options The options for the request. + * @param {string} $this->feature The feature name. + * + * @return {string} The URL for the request. + */ + apply_filters( 'classifai_openai_api_request_post_url', $url, $options, $this->feature ); + + /** + * Filter the options for the post request. + * + * @since 2.4.0 + * @hook classifai_openai_api_request_post_options + * + * @param {array} $options The options for the request. + * @param {string} $url The URL for the request. + * @param {string} $this->feature The feature name. + * + * @return {array} The options for the request. + */ + apply_filters( 'classifai_openai_api_request_post_options', $options, $url, $this->feature ); + $this->add_headers( $options ); /** From 94aba7177f3d97a9792059d66c0eb1bd42fb9632 Mon Sep 17 00:00:00 2001 From: faisal-alvi Date: Wed, 25 Oct 2023 16:38:58 +0530 Subject: [PATCH 12/12] filter the URL for the post form request --- .../Classifai/Providers/OpenAI/APIRequest.php | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/includes/Classifai/Providers/OpenAI/APIRequest.php b/includes/Classifai/Providers/OpenAI/APIRequest.php index 150786b2b..5ffb23728 100644 --- a/includes/Classifai/Providers/OpenAI/APIRequest.php +++ b/includes/Classifai/Providers/OpenAI/APIRequest.php @@ -63,7 +63,7 @@ public function get( string $url, array $options = [] ) { * * @return {string} The URL for the request. */ - apply_filters( 'classifai_openai_api_request_get_url', $url, $options, $this->feature ); + $url = apply_filters( 'classifai_openai_api_request_get_url', $url, $options, $this->feature ); /** * Filter the options for the get request. @@ -77,7 +77,7 @@ public function get( string $url, array $options = [] ) { * * @return {array} The options for the request. */ - apply_filters( 'classifai_openai_api_request_get_options', $options, $url, $this->feature ); + $options = apply_filters( 'classifai_openai_api_request_get_options', $options, $url, $this->feature ); $this->add_headers( $options ); @@ -129,7 +129,7 @@ public function post( string $url = '', array $options = [] ) { * * @return {string} The URL for the request. */ - apply_filters( 'classifai_openai_api_request_post_url', $url, $options, $this->feature ); + $url = apply_filters( 'classifai_openai_api_request_post_url', $url, $options, $this->feature ); /** * Filter the options for the post request. @@ -143,7 +143,7 @@ public function post( string $url = '', array $options = [] ) { * * @return {array} The options for the request. */ - apply_filters( 'classifai_openai_api_request_post_options', $options, $url, $this->feature ); + $options = apply_filters( 'classifai_openai_api_request_post_options', $options, $url, $this->feature ); $this->add_headers( $options ); @@ -176,6 +176,19 @@ public function post( string $url = '', array $options = [] ) { * @return array|WP_Error */ public function post_form( string $url = '', array $body = [] ) { + /** + * Filter the URL for the post form request. + * + * @since 2.4.0 + * @hook classifai_openai_api_request_post_form_url + * + * @param {string} $url The URL for the request. + * @param {string} $this->feature The feature name. + * + * @return {string} The URL for the request. + */ + $url = apply_filters( 'classifai_openai_api_request_post_form_url', $url, $this->feature ); + $boundary = wp_generate_password( 24, false ); $payload = '';