Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
107 changes: 85 additions & 22 deletions includes/Classifai/Features/TextToSpeech.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public function feature_setup() {
add_action( 'add_meta_boxes', [ $this, 'add_meta_box' ] );
add_action( 'admin_notices', [ $this, 'show_error_if' ] );
add_action( 'save_post', [ $this, 'save_post_metadata' ], 5 );
add_action( 'wp_ajax_classifai_get_tts_status', [ $this, 'ajax_get_audio_generation_status' ] );
}

/**
Expand Down Expand Up @@ -419,6 +420,36 @@ public function add_meta_box( string $post_type ) {
public function render_meta_box( \WP_Post $post ) {
wp_nonce_field( 'classifai_text_to_speech_meta_action', 'classifai_text_to_speech_meta' );

$is_as_scheduled_job =
function_exists( 'as_has_scheduled_action' ) &&
\as_has_scheduled_action(
'classifai_schedule_text_to_speech_job',
[
'post_id' => (int) $post->ID,
'calling_user_id' => get_current_user_id(),
],
'classifai'
);

if ( $is_as_scheduled_job ) :
?>
<p class="classifai-tts-status" data-post-id="<?php echo esc_attr( (string) $post->ID ); ?>" data-nonce="<?php echo esc_attr( wp_create_nonce( 'classifai_tts_status' ) ); ?>">
<?php esc_html_e( 'Audio generation is in progress…', 'classifai' ); ?>
</p>
<?php
else :
$this->render_audio_generation_ui( $post );
endif;
}

/**
* Render the post-generation UI for the TTS meta box. Used both on initial
* render and via AJAX after a queued job completes so the meta box
* matches a fresh page load.
*
* @param \WP_Post $post WP_Post object.
*/
public function render_audio_generation_ui( \WP_Post $post ) {
$source_url = false;
$audio_id = get_post_meta( $post->ID, self::AUDIO_ID_KEY, true );

Expand All @@ -445,24 +476,7 @@ public function render_meta_box( \WP_Post $post ) {
if ( $post_type ) {
$post_type_label = $post_type->labels->singular_name;
}

$is_as_scheduled_job =
function_exists( 'as_has_scheduled_action' ) &&
\as_has_scheduled_action(
'classifai_schedule_text_to_speech_job',
[
'post_id' => (int) $post->ID,
'calling_user_id' => get_current_user_id(),
],
'classifai'
);

if ( $is_as_scheduled_job ) : ?>
<p>
<?php esc_html_e( 'Audio generation is in progress…', 'classifai' ); ?>
</p>
<?php else : ?>

?>
<p>
<label for="classifai_synthesize_speech">
<input type="checkbox" value="1" id="classifai_synthesize_speech" name="classifai_synthesize_speech" <?php checked( $process_content ); ?> />
Expand All @@ -488,8 +502,6 @@ function_exists( 'as_has_scheduled_action' ) &&
</span>
</p>

<?php endif; ?>

<?php
if ( $source_url ) {
$cache_busting_url = add_query_arg(
Expand All @@ -499,15 +511,66 @@ function_exists( 'as_has_scheduled_action' ) &&
$source_url
);
?>

<p>
<audio id="classifai-audio-preview" controls controlslist="nodownload" src="<?php echo esc_url( $cache_busting_url ); ?>"></audio>
</p>

<?php
}
}

/**
* AJAX handler for the Classic Editor meta box's polling. Returns the
* current audio generation status for the given post id.
*/
public function ajax_get_audio_generation_status() {
check_ajax_referer( 'classifai_tts_status', 'nonce' );

$post_id = isset( $_POST['post_id'] ) ? absint( $_POST['post_id'] ) : 0;
Comment thread
CacheMeOwside marked this conversation as resolved.
Outdated

if ( ! $post_id || ! current_user_can( 'edit_post', $post_id ) ) {
wp_send_json_error( [ 'message' => __( 'Invalid post.', 'classifai' ) ], 403 );
}

$in_progress =
function_exists( 'as_has_scheduled_action' ) &&
\as_has_scheduled_action(
'classifai_schedule_text_to_speech_job',
[
'post_id' => $post_id,
'calling_user_id' => get_current_user_id(),
],
'classifai'
);

$error_message = '';
$raw_error = get_post_meta( $post_id, '_classifai_text_to_speech_error', true );

if ( ! empty( $raw_error ) ) {
$decoded = (array) json_decode( (string) $raw_error );
if ( ! empty( $decoded['message'] ) ) {
$error_message = (string) $decoded['message'];
}
}

$html = '';
if ( ! $in_progress && ! $error_message ) {
$post = get_post( $post_id );
if ( $post ) {
ob_start();
$this->render_audio_generation_ui( $post );
$html = (string) ob_get_clean();
}
}

wp_send_json_success(
[
'inProgress' => (bool) $in_progress,
'error' => $error_message,
'html' => $html,
]
);
}

/**
* Process the meta box save.
*
Expand Down
54 changes: 54 additions & 0 deletions src/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,60 @@ document.addEventListener( 'DOMContentLoaded', function () {
} );
} )( jQuery );

// Poll admin-ajax to update the Classic Editor TTS meta box once audio generation completes.
Comment thread
CacheMeOwside marked this conversation as resolved.
Outdated
( function ( $ ) {
$( function () {
const $status = $( '.classifai-tts-status' );

if ( ! $status.length ) {
return;
}

const postId = parseInt( $status.data( 'post-id' ), 10 );
const nonce = $status.data( 'nonce' );

if ( ! postId || ! nonce ) {
return;
}

const POLL_INTERVAL_MS = 10000;

const renderComplete = ( { error, html } ) => {
if ( error ) {
$status.text( error );
return;
}

if ( html ) {
$status.replaceWith( html );
}
};

const timer = setInterval( () => {
$.ajax( {
url: ajaxurl, // eslint-disable-line no-undef
type: 'POST',
data: {
action: 'classifai_get_tts_status',
nonce,
post_id: postId,
},
} ).done( ( response ) => {
if ( ! response || ! response.success || ! response.data ) {
return;
}

if ( response.data.inProgress ) {
return;
}

clearInterval( timer );
renderComplete( response.data );
} );
}, POLL_INTERVAL_MS );
} );
} )( jQuery );

// Update the Term Cleanup status.
( function ( $ ) {
const statusWrapper = $( '.classifai-term-cleanup-process-status' );
Expand Down
Loading