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 8ceeffba1..6be9b1c51 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__( 'Determines if audio generation is on by default for individual items.', '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 { @@ -427,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' => '', @@ -436,6 +463,7 @@ private function get_default_settings() { 'voices' => array(), 'voice' => '', 'authenticated' => false, + 'default' => true, 'post-types' => array(), ]; } @@ -464,9 +492,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 +556,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' ); 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 * 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', + ] ); } /**