Skip to content

Commit

Permalink
#486 - Handle audio file no longer existing but is part of the post m…
Browse files Browse the repository at this point in the history
…eta still.
  • Loading branch information
Joshua Abenazer committed Jun 20, 2023
1 parent 58efa07 commit a50f567
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
10 changes: 8 additions & 2 deletions includes/Classifai/Admin/SavePostHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,14 @@ public function synthesize_speech( $post_id ) {
$saved_attachment_id = (int) get_post_meta( $post_id, TextToSpeech::AUDIO_ID_KEY, true );

// Don't regenerate the audio file it it already exists and the content hasn't changed.
if ( $saved_attachment_id && ! empty( $content_hash ) && ( md5( $post_content ) === $content_hash ) ) {
return $saved_attachment_id;
if ( $saved_attachment_id ) {

// Check if the audio file exists.
$audio_attachment_url = wp_get_attachment_url( $saved_attachment_id );

if ( $audio_attachment_url && ! empty( $content_hash ) && ( md5( $post_content ) === $content_hash ) ) {
return $saved_attachment_id;
}
}

$voice = $settings['voice'] ?? '';
Expand Down
17 changes: 11 additions & 6 deletions includes/Classifai/Providers/Azure/TextToSpeech.php
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,12 @@ public function render_post_audio_controls( $content ) {
return $content;
}

$audio_attachment_url = wp_get_attachment_url( $audio_attachment_id );

if ( ! $audio_attachment_url ) {
return $content;
}

wp_enqueue_script(
'classifai-post-audio-player-js',
CLASSIFAI_PLUGIN_URL . 'dist/post-audio-controls.js',
Expand All @@ -546,12 +552,11 @@ public function render_post_audio_controls( $content ) {
'all'
);

$audio_timestamp = (int) get_post_meta( $post->ID, self::AUDIO_TIMESTAMP_KEY, true );
$audio_attachment_url = sprintf(
'%1$s?ver=%2$s',
wp_get_attachment_url( $audio_attachment_id ),
filter_var( $audio_timestamp, FILTER_SANITIZE_NUMBER_INT )
);
$audio_timestamp = (int) get_post_meta( $post->ID, self::AUDIO_TIMESTAMP_KEY, true );

if ( $audio_timestamp ) {
$audio_attachment_url = add_query_arg( 'ver', filter_var( $audio_timestamp, FILTER_SANITIZE_NUMBER_INT ), $audio_attachment_url );
}

ob_start();

Expand Down

0 comments on commit a50f567

Please sign in to comment.