From 408f4c708934d1204dd391b4ffecec0d82043304 Mon Sep 17 00:00:00 2001 From: Joshua Abenazer Date: Mon, 17 Jul 2023 23:52:28 +0530 Subject: [PATCH 1/5] #538 - Add setting to toggle the default behaviour of audio generation on a post. --- .../Providers/Azure/TextToSpeech.php | 59 ++++++++++++++++++- 1 file changed, 56 insertions(+), 3 deletions(-) diff --git a/includes/Classifai/Providers/Azure/TextToSpeech.php b/includes/Classifai/Providers/Azure/TextToSpeech.php index 8ceeffba1..5a3a8ff87 100644 --- a/includes/Classifai/Providers/Azure/TextToSpeech.php +++ b/includes/Classifai/Providers/Azure/TextToSpeech.php @@ -194,6 +194,20 @@ public function setup_fields_sections() { ] ); + add_settings_field( + 'default', + esc_html__( 'Enabled by default', 'classifai' ), + [ $this, 'render_input' ], + $this->get_option_name(), + $this->get_option_name(), + [ + 'label_for' => 'default', + 'input_type' => 'checkbox', + 'default_value' => $default_settings['default'], + 'description' => esc_html__( 'Enables the toggle to generate audio on posts by default.', 'classifai' ), + ] + ); + if ( ! empty( $voices_options ) ) { add_settings_field( 'voice', @@ -265,6 +279,19 @@ public function sanitize_settings( $settings ) { } } + $checkbox_settings = [ + 'default', + ]; + + foreach ( $checkbox_settings as $checkbox_setting ) { + + if ( empty( $settings[ $checkbox_setting ] ) || 1 !== (int) $settings[ $checkbox_setting ] ) { + $current_settings[ $checkbox_setting ] = 'no'; + } else { + $current_settings[ $checkbox_setting ] = '1'; + } + } + if ( isset( $settings['voice'] ) && ! empty( $settings['voice'] ) ) { $current_settings['voice'] = sanitize_text_field( $settings['voice'] ); } else { @@ -436,10 +463,30 @@ private function get_default_settings() { 'voices' => array(), 'voice' => '', 'authenticated' => false, + 'default' => true, 'post-types' => array(), ]; } + /** + * 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; + } + /** * Add `classifai_synthesize_speech` to rest API for view/edit. */ @@ -464,9 +511,12 @@ function( $post_type ) { $supported_post_types, 'classifai_synthesize_speech', array( - 'get_callback' => function( $object ) { + 'get_callback' => function( $object ) use ( $settings ) { $process_content = get_post_meta( $object['id'], self::SYNTHESIZE_SPEECH_KEY, true ); - return ( 'no' === $process_content ) ? 'no' : 'yes'; + if ( empty( $process_content ) || ! in_array( $process_content, [ 'no', 'yes'], true ) ) { + $process_content = ( 'no' === $settings['default'] ) ? 'no' : 'yes'; + } + return $process_content; }, 'update_callback' => function ( $value, $object ) { $value = ( 'no' === $value ) ? 'no' : 'yes'; @@ -525,7 +575,10 @@ public function render_meta_box( $post ) { wp_nonce_field( 'classifai_text_to_speech_meta_action', 'classifai_text_to_speech_meta' ); $process_content = get_post_meta( $post->ID, self::SYNTHESIZE_SPEECH_KEY, true ); - $process_content = ( 'no' === $process_content ) ? 'no' : 'yes'; + if ( empty( $process_content ) || ! in_array( $process_content, [ 'no', 'yes'], true ) ) { + $default = $this->get_settings( 'default' ); + $process_content = ( 'no' === $default ) ? 'no' : 'yes'; + } $post_type = get_post_type_object( get_post_type( $post ) ); $post_type_label = esc_html__( 'Post', 'classifai' ); From 0c3ddd842f99ccbd7d1548946f4c9087364d0a87 Mon Sep 17 00:00:00 2001 From: Joshua Abenazer Date: Tue, 18 Jul 2023 00:34:08 +0530 Subject: [PATCH 2/5] PHPCS Updates --- .../Classifai/Providers/Azure/TextToSpeech.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/includes/Classifai/Providers/Azure/TextToSpeech.php b/includes/Classifai/Providers/Azure/TextToSpeech.php index 5a3a8ff87..929960ed0 100644 --- a/includes/Classifai/Providers/Azure/TextToSpeech.php +++ b/includes/Classifai/Providers/Azure/TextToSpeech.php @@ -201,10 +201,10 @@ public function setup_fields_sections() { $this->get_option_name(), $this->get_option_name(), [ - 'label_for' => 'default', - 'input_type' => 'checkbox', - 'default_value' => $default_settings['default'], - 'description' => esc_html__( 'Enables the toggle to generate audio on posts by default.', 'classifai' ), + 'label_for' => 'default', + 'input_type' => 'checkbox', + 'default_value' => $default_settings['default'], + 'description' => esc_html__( 'Enables the toggle to generate audio on posts by default.', 'classifai' ), ] ); @@ -513,8 +513,8 @@ function( $post_type ) { array( 'get_callback' => function( $object ) use ( $settings ) { $process_content = get_post_meta( $object['id'], self::SYNTHESIZE_SPEECH_KEY, true ); - if ( empty( $process_content ) || ! in_array( $process_content, [ 'no', 'yes'], true ) ) { - $process_content = ( 'no' === $settings['default'] ) ? 'no' : 'yes'; + if ( empty( $process_content ) || ! in_array( $process_content, [ 'no', 'yes' ], true ) ) { + $process_content = ( 'no' === $settings['default'] ) ? 'no' : 'yes'; } return $process_content; }, @@ -575,9 +575,9 @@ public function render_meta_box( $post ) { wp_nonce_field( 'classifai_text_to_speech_meta_action', 'classifai_text_to_speech_meta' ); $process_content = get_post_meta( $post->ID, self::SYNTHESIZE_SPEECH_KEY, true ); - if ( empty( $process_content ) || ! in_array( $process_content, [ 'no', 'yes'], true ) ) { + if ( empty( $process_content ) || ! in_array( $process_content, [ 'no', 'yes' ], true ) ) { $default = $this->get_settings( 'default' ); - $process_content = ( 'no' === $default ) ? 'no' : 'yes'; + $process_content = ( 'no' === $default ) ? 'no' : 'yes'; } $post_type = get_post_type_object( get_post_type( $post ) ); From 7512ba5ea6b26c8cafa048a8c518d119697162c3 Mon Sep 17 00:00:00 2001 From: Joshua Abenazer Date: Wed, 19 Jul 2023 09:28:54 +0530 Subject: [PATCH 3/5] Update includes/Classifai/Providers/Azure/TextToSpeech.php Update description for default audio generation toggle setting. Co-authored-by: Darin Kotter --- includes/Classifai/Providers/Azure/TextToSpeech.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/Classifai/Providers/Azure/TextToSpeech.php b/includes/Classifai/Providers/Azure/TextToSpeech.php index 929960ed0..b7383ab8b 100644 --- a/includes/Classifai/Providers/Azure/TextToSpeech.php +++ b/includes/Classifai/Providers/Azure/TextToSpeech.php @@ -204,7 +204,7 @@ public function setup_fields_sections() { 'label_for' => 'default', 'input_type' => 'checkbox', 'default_value' => $default_settings['default'], - 'description' => esc_html__( 'Enables the toggle to generate audio on posts by default.', 'classifai' ), + 'description' => esc_html__( 'Determines if audio generation is on by default for individual items.', 'classifai' ), ] ); From 1d2cf79c9b7859779fc838480ade2a4815f8d332 Mon Sep 17 00:00:00 2001 From: Joshua Abenazer Date: Tue, 25 Jul 2023 11:18:13 +0530 Subject: [PATCH 4/5] #538 - Update access modifiers for get_default_settings method from private to public for all providers. --- .../Providers/Azure/ComputerVision.php | 2 +- .../Providers/Azure/Personalizer.php | 2 +- .../Providers/Azure/TextToSpeech.php | 21 +------------------ .../Classifai/Providers/OpenAI/ChatGPT.php | 5 ++++- includes/Classifai/Providers/OpenAI/DallE.php | 2 +- .../Classifai/Providers/OpenAI/Embeddings.php | 2 +- .../Classifai/Providers/OpenAI/Whisper.php | 2 +- includes/Classifai/Providers/Provider.php | 9 +++++++- 8 files changed, 18 insertions(+), 27 deletions(-) diff --git a/includes/Classifai/Providers/Azure/ComputerVision.php b/includes/Classifai/Providers/Azure/ComputerVision.php index 00ee86901..2a9555610 100644 --- a/includes/Classifai/Providers/Azure/ComputerVision.php +++ b/includes/Classifai/Providers/Azure/ComputerVision.php @@ -62,7 +62,7 @@ public function reset_settings() { * * @return array */ - private function get_default_settings() { + public function get_default_settings() { return [ 'valid' => false, 'url' => '', diff --git a/includes/Classifai/Providers/Azure/Personalizer.php b/includes/Classifai/Providers/Azure/Personalizer.php index e9600cef6..c094e1306 100644 --- a/includes/Classifai/Providers/Azure/Personalizer.php +++ b/includes/Classifai/Providers/Azure/Personalizer.php @@ -62,7 +62,7 @@ public function reset_settings() { * * @return array */ - private function get_default_settings() { + public function get_default_settings() { return [ 'authenticated' => false, 'url' => '', diff --git a/includes/Classifai/Providers/Azure/TextToSpeech.php b/includes/Classifai/Providers/Azure/TextToSpeech.php index b7383ab8b..6be9b1c51 100644 --- a/includes/Classifai/Providers/Azure/TextToSpeech.php +++ b/includes/Classifai/Providers/Azure/TextToSpeech.php @@ -454,7 +454,7 @@ public function get_provider_debug_information( $settings = null, $configured = /** * Returns the default settings. */ - private function get_default_settings() { + public function get_default_settings() { return [ 'credentials' => array( 'url' => '', @@ -468,25 +468,6 @@ private function get_default_settings() { ]; } - /** - * 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; - } - /** * Add `classifai_synthesize_speech` to rest API for view/edit. */ diff --git a/includes/Classifai/Providers/OpenAI/ChatGPT.php b/includes/Classifai/Providers/OpenAI/ChatGPT.php index 97cea4360..098f0dcf9 100644 --- a/includes/Classifai/Providers/OpenAI/ChatGPT.php +++ b/includes/Classifai/Providers/OpenAI/ChatGPT.php @@ -398,7 +398,10 @@ public function reset_settings() { * * @return array */ - private function get_default_settings() { + public function get_default_settings() { + if ( ! function_exists( 'get_editable_roles' ) ) { + require_once( ABSPATH . 'wp-admin/includes/user.php' ); + } $editable_roles = get_editable_roles() ?? []; return [ diff --git a/includes/Classifai/Providers/OpenAI/DallE.php b/includes/Classifai/Providers/OpenAI/DallE.php index 7147d6523..8921211c0 100644 --- a/includes/Classifai/Providers/OpenAI/DallE.php +++ b/includes/Classifai/Providers/OpenAI/DallE.php @@ -374,7 +374,7 @@ public function reset_settings() { * * @return array */ - private function get_default_settings() { + public function get_default_settings() { return [ 'authenticated' => false, 'api_key' => '', diff --git a/includes/Classifai/Providers/OpenAI/Embeddings.php b/includes/Classifai/Providers/OpenAI/Embeddings.php index cefe95b69..158bd2464 100644 --- a/includes/Classifai/Providers/OpenAI/Embeddings.php +++ b/includes/Classifai/Providers/OpenAI/Embeddings.php @@ -282,7 +282,7 @@ public function reset_settings() { * * @return array */ - private function get_default_settings() { + public function get_default_settings() { return [ 'authenticated' => false, 'api_key' => '', diff --git a/includes/Classifai/Providers/OpenAI/Whisper.php b/includes/Classifai/Providers/OpenAI/Whisper.php index 2beb0640f..6ffce1b91 100644 --- a/includes/Classifai/Providers/OpenAI/Whisper.php +++ b/includes/Classifai/Providers/OpenAI/Whisper.php @@ -300,7 +300,7 @@ public function reset_settings() { * * @return array */ - private function get_default_settings() { + public function get_default_settings() { return [ 'authenticated' => false, 'api_key' => '', diff --git a/includes/Classifai/Providers/Provider.php b/includes/Classifai/Providers/Provider.php index 488886b08..f176f2484 100644 --- a/includes/Classifai/Providers/Provider.php +++ b/includes/Classifai/Providers/Provider.php @@ -153,7 +153,7 @@ public function register_settings() { * @return string|array|mixed */ public function get_settings( $index = false ) { - $defaults = []; + $defaults = $this->get_default_settings(); $settings = get_option( $this->get_option_name(), [] ); $settings = wp_parse_args( $settings, $defaults ); @@ -164,6 +164,13 @@ public function get_settings( $index = false ) { return $settings; } + /** + * Returns the default settings. + */ + public function get_default_settings() { + return []; + } + /** * Generic text input field callback * From 8a457ba672d2a21f975dc724472ff4f3686c5b65 Mon Sep 17 00:00:00 2001 From: Joshua Abenazer Date: Tue, 25 Jul 2023 17:26:44 +0530 Subject: [PATCH 5/5] #538 - Update Classifai\Tests\Providers\Azure\ComputerVisionTest::test_no_computer_vision_option_set test. --- .../Providers/Azure/ComputerVisionTest.php | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/tests/Classifai/Providers/Azure/ComputerVisionTest.php b/tests/Classifai/Providers/Azure/ComputerVisionTest.php index b2b59e2e0..c9a752627 100644 --- a/tests/Classifai/Providers/Azure/ComputerVisionTest.php +++ b/tests/Classifai/Providers/Azure/ComputerVisionTest.php @@ -81,14 +81,30 @@ public function test_smart_crop_image() { /** - * Ensure that settings returns empty array of the `classifai_computer_vision` is not set. + * Ensure that settings returns default settings array if the `classifai_computer_vision` is not set. */ public function test_no_computer_vision_option_set() { delete_option( 'classifai_computer_vision' ); $settings = $this->get_computer_vision()->get_settings(); - $this->assertSame( $settings, array() ); + $this->assertSame( $settings, [ + 'valid' => false, + 'url' => '', + 'api_key' => '', + 'enable_image_captions' => array( + 'alt' => 0, + 'caption' => 0, + 'description' => 0, + ), + 'enable_image_tagging' => true, + 'enable_smart_cropping' => false, + 'enable_ocr' => false, + 'enable_read_pdf' => false, + 'caption_threshold' => 75, + 'tag_threshold' => 70, + 'image_tag_taxonomy' => 'classifai-image-tags', + ] ); } /**