Skip to content
Open
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
23 changes: 20 additions & 3 deletions includes/Classifai/Features/TextToSpeech.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,10 @@ public function add_meta_to_rest_api() {
public function rest_handle_audio( \WP_Post $post, WP_REST_Request $request ) {
$post_id = (int) $request->get_param( 'id' );

if ( ! $this->is_feature_enabled() ) {
if (
! in_array( $post->post_status, $this->get_supported_post_statuses(), true ) ||
! $this->is_feature_enabled()
) {
return;
}

Expand Down Expand Up @@ -614,6 +617,7 @@ function_exists( 'as_has_scheduled_action' ) &&
public function save_post_metadata( int $post_id ) {
if (
! in_array( get_post_type( $post_id ), $this->get_supported_post_types(), true ) ||
! in_array( get_post_status( $post_id ), $this->get_supported_post_statuses(), true ) ||
! $this->is_feature_enabled()
) {
return;
Expand Down Expand Up @@ -961,10 +965,13 @@ protected function get_post_types_select_options(): array {
*/
public function get_feature_default_settings(): array {
return array(
'post_types' => array(
'post_types' => array(
'post' => 'post',
),
'provider' => Speech::ID,
'post_statuses' => array(
'publish' => 'publish',
),
'provider' => Speech::ID,
);
}

Expand All @@ -985,6 +992,16 @@ public function sanitize_default_feature_settings( array $new_settings ): array
}
}

$post_statuses = \Classifai\get_post_statuses_for_language_settings();

foreach ( array_keys( $post_statuses ) as $post_status ) {
if ( ! isset( $new_settings['post_statuses'][ $post_status ] ) ) {
$new_settings['post_statuses'][ $post_status ] = '';
} else {
$new_settings['post_statuses'][ $post_status ] = sanitize_text_field( $new_settings['post_statuses'][ $post_status ] );
}
}

return $new_settings;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { useSelect, useDispatch } from '@wordpress/data';
import { CheckboxControl } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { decodeEntities } from '@wordpress/html-entities';

/**
* Internal dependencies
Expand All @@ -23,36 +24,70 @@ export const TextToSpeechSettings = () => {
select( STORE_NAME ).getFeatureSettings()
);
const { setFeatureSettings } = useDispatch( STORE_NAME );
const { postTypes } = window.classifAISettings;
const { postTypes, postStatuses } = window.classifAISettings;

return (
<SettingsRow
label={ __( 'Allowed post types', 'classifai' ) }
description={ __(
'Choose which post types support this feature.',
'classifai'
) }
className="settings-allowed-post-types"
>
{ Object.keys( postTypes || {} ).map( ( key ) => {
return (
<CheckboxControl
id={ key }
key={ key }
checked={ featureSettings.post_types?.[ key ] === key }
label={ postTypes?.[ key ] }
onChange={ ( value ) => {
setFeatureSettings( {
post_types: {
...featureSettings.post_types,
[ key ]: value ? key : '0',
},
} );
} }
__nextHasNoMarginBottom
/>
);
} ) }
</SettingsRow>
<>
<SettingsRow
label={ __( 'Allowed post statuses', 'classifai' ) }
description={ __(
'Choose which post statuses are allowed to generate audio, e.g. disable this for Draft to avoid generating audio for content that is still being edited.',
'classifai'
) }
className="settings-allowed-post-statuses"
>
{ Object.keys( postStatuses || {} ).map( ( key ) => {
return (
<CheckboxControl
id={ `post_status_${ key }` }
key={ key }
checked={
featureSettings.post_statuses?.[ key ] === key
}
label={ decodeEntities( postStatuses?.[ key ] ) }
onChange={ ( value ) => {
setFeatureSettings( {
post_statuses: {
...featureSettings.post_statuses,
[ key ]: value ? key : '0',
},
} );
} }
__nextHasNoMarginBottom
/>
);
} ) }
</SettingsRow>
<SettingsRow
label={ __( 'Allowed post types', 'classifai' ) }
description={ __(
'Choose which post types support this feature.',
'classifai'
) }
className="settings-allowed-post-types"
>
{ Object.keys( postTypes || {} ).map( ( key ) => {
return (
<CheckboxControl
id={ key }
key={ key }
checked={
featureSettings.post_types?.[ key ] === key
}
label={ postTypes?.[ key ] }
onChange={ ( value ) => {
setFeatureSettings( {
post_types: {
...featureSettings.post_types,
[ key ]: value ? key : '0',
},
} );
} }
__nextHasNoMarginBottom
/>
);
} ) }
</SettingsRow>
</>
);
};