Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#538 - Add setting to toggle the default behaviour of audio generation on a post. #539

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion includes/Classifai/Providers/Azure/ComputerVision.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function reset_settings() {
*
* @return array
*/
private function get_default_settings() {
public function get_default_settings() {
return [
'valid' => false,
'url' => '',
Expand Down
2 changes: 1 addition & 1 deletion includes/Classifai/Providers/Azure/Personalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function reset_settings() {
*
* @return array
*/
private function get_default_settings() {
public function get_default_settings() {
return [
'authenticated' => false,
'url' => '',
Expand Down
42 changes: 38 additions & 4 deletions includes/Classifai/Providers/Azure/TextToSpeech.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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' => '',
Expand All @@ -436,6 +463,7 @@ private function get_default_settings() {
'voices' => array(),
'voice' => '',
'authenticated' => false,
'default' => true,
'post-types' => array(),
];
}
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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' );
Expand Down
5 changes: 4 additions & 1 deletion includes/Classifai/Providers/OpenAI/ChatGPT.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down
2 changes: 1 addition & 1 deletion includes/Classifai/Providers/OpenAI/DallE.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '',
Expand Down
2 changes: 1 addition & 1 deletion includes/Classifai/Providers/OpenAI/Embeddings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '',
Expand Down
2 changes: 1 addition & 1 deletion includes/Classifai/Providers/OpenAI/Whisper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '',
Expand Down
9 changes: 8 additions & 1 deletion includes/Classifai/Providers/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand All @@ -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
*
Expand Down
20 changes: 18 additions & 2 deletions tests/Classifai/Providers/Azure/ComputerVisionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
] );
}

/**
Expand Down
Loading