Skip to content

Commit

Permalink
Merge pull request #487 from 10up/feature/486-optimize-calls-to-micro…
Browse files Browse the repository at this point in the history
…soft-azure-text-to-speech-api

#486 - Optimize calls to Microsoft Azure Text to Speech API
  • Loading branch information
dkotter committed Jun 21, 2023
2 parents fa60541 + a50f567 commit 2e6d8c8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
28 changes: 21 additions & 7 deletions includes/Classifai/Admin/SavePostHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,24 @@ public function synthesize_speech( $post_id ) {
);
}

$normalizer = new Normalizer();
$settings = \Classifai\get_plugin_settings( 'language_processing', TextToSpeech::FEATURE_NAME );
$post = get_post( $post_id );
$post_content = $normalizer->normalize_content( $post->post_content, $post->post_title, $post_id );
$normalizer = new Normalizer();
$settings = \Classifai\get_plugin_settings( 'language_processing', TextToSpeech::FEATURE_NAME );
$post = get_post( $post_id );
$post_content = $normalizer->normalize_content( $post->post_content, $post->post_title, $post_id );
$content_hash = get_post_meta( $post_id, TextToSpeech::AUDIO_HASH_KEY, true );
$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 ) {

// 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'] ?? '';
$voice_data = explode( '|', $voice );
$voice_name = '';
Expand Down Expand Up @@ -255,8 +269,6 @@ public function synthesize_speech( $post_id ) {
);
}

$saved_attachment_id = (int) get_post_meta( $post_id, TextToSpeech::AUDIO_ID_KEY, true );

// If audio already exists for this post, delete it.
if ( $saved_attachment_id ) {
wp_delete_attachment( $saved_attachment_id, true );
Expand Down Expand Up @@ -291,7 +303,8 @@ public function synthesize_speech( $post_id ) {
'post_title' => $audio_file_name,
'post_mime_type' => $file_data['type'],
),
$file_data['file']
$file_data['file'],
$post_id
);

// Return error if creation of attachment fails.
Expand All @@ -304,6 +317,7 @@ public function synthesize_speech( $post_id ) {

update_post_meta( $post_id, TextToSpeech::AUDIO_ID_KEY, absint( $attachment_id ) );
update_post_meta( $post_id, TextToSpeech::AUDIO_TIMESTAMP_KEY, time() );
update_post_meta( $post_id, TextToSpeech::AUDIO_HASH_KEY, md5( $post_content ) );

return $attachment_id;
}
Expand Down
25 changes: 19 additions & 6 deletions includes/Classifai/Providers/Azure/TextToSpeech.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ class TextToSpeech extends Provider {
*/
const AUDIO_TIMESTAMP_KEY = '_classifai_post_audio_timestamp';

/**
* Meta key to get/set the audio hash that helps to indicate if there is any need
* for the audio file to be regenerated or not.
*
* @var string
*/
const AUDIO_HASH_KEY = '_classifai_post_audio_hash';

/**
* Azure Text to Speech constructor.
*
Expand Down Expand Up @@ -522,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 @@ -538,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 2e6d8c8

Please sign in to comment.