Skip to content

Commit 828a08d

Browse files
committed
scope polling JS to TTS feature
1 parent 09cd584 commit 828a08d

4 files changed

Lines changed: 105 additions & 55 deletions

File tree

includes/Classifai/Features/TextToSpeech.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,40 @@ public function feature_setup() {
106106
add_action( 'admin_notices', [ $this, 'show_error_if' ] );
107107
add_action( 'save_post', [ $this, 'save_post_metadata' ], 5 );
108108
add_action( 'wp_ajax_classifai_get_tts_status', [ $this, 'ajax_get_audio_generation_status' ] );
109+
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_admin_assets' ] );
110+
}
111+
112+
/**
113+
* Enqueue the Classic Editor polling script. Loaded in Classic Editor only when the feature is enabled.
114+
*
115+
* @param string $hook_suffix The current admin page.
116+
*/
117+
public function enqueue_admin_assets( string $hook_suffix ) {
118+
if ( 'post.php' !== $hook_suffix && 'post-new.php' !== $hook_suffix ) {
119+
return;
120+
}
121+
122+
if ( ! $this->is_feature_enabled() ) {
123+
return;
124+
}
125+
126+
$screen = get_current_screen();
127+
128+
if ( ! $screen || $screen->is_block_editor() ) {
129+
return;
130+
}
131+
132+
if ( ! in_array( $screen->post_type, $this->get_supported_post_types(), true ) ) {
133+
return;
134+
}
135+
136+
wp_enqueue_script(
137+
'classifai-plugin-classic-text-to-speech',
138+
CLASSIFAI_PLUGIN_URL . 'dist/classifai-plugin-classic-text-to-speech.js',
139+
get_asset_info( 'classifai-plugin-classic-text-to-speech', 'dependencies' ),
140+
get_asset_info( 'classifai-plugin-classic-text-to-speech', 'version' ),
141+
true
142+
);
109143
}
110144

111145
/**
@@ -525,7 +559,7 @@ public function render_audio_generation_ui( \WP_Post $post ) {
525559
public function ajax_get_audio_generation_status() {
526560
check_ajax_referer( 'classifai_tts_status', 'nonce' );
527561

528-
$post_id = isset( $_POST['post_id'] ) ? absint( $_POST['post_id'] ) : 0;
562+
$post_id = isset( $_POST['post_id'] ) ? absint( wp_unslash( $_POST['post_id'] ) ) : 0;
529563

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

src/js/admin.js

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -445,60 +445,6 @@ document.addEventListener( 'DOMContentLoaded', function () {
445445
} );
446446
} )( jQuery );
447447

448-
// Poll admin-ajax to update the Classic Editor TTS meta box once audio generation completes.
449-
( function ( $ ) {
450-
$( function () {
451-
const $status = $( '.classifai-tts-status' );
452-
453-
if ( ! $status.length ) {
454-
return;
455-
}
456-
457-
const postId = parseInt( $status.data( 'post-id' ), 10 );
458-
const nonce = $status.data( 'nonce' );
459-
460-
if ( ! postId || ! nonce ) {
461-
return;
462-
}
463-
464-
const POLL_INTERVAL_MS = 10000;
465-
466-
const renderComplete = ( { error, html } ) => {
467-
if ( error ) {
468-
$status.text( error );
469-
return;
470-
}
471-
472-
if ( html ) {
473-
$status.replaceWith( html );
474-
}
475-
};
476-
477-
const timer = setInterval( () => {
478-
$.ajax( {
479-
url: ajaxurl, // eslint-disable-line no-undef
480-
type: 'POST',
481-
data: {
482-
action: 'classifai_get_tts_status',
483-
nonce,
484-
post_id: postId,
485-
},
486-
} ).done( ( response ) => {
487-
if ( ! response || ! response.success || ! response.data ) {
488-
return;
489-
}
490-
491-
if ( response.data.inProgress ) {
492-
return;
493-
}
494-
495-
clearInterval( timer );
496-
renderComplete( response.data );
497-
} );
498-
}, POLL_INTERVAL_MS );
499-
} );
500-
} )( jQuery );
501-
502448
// Update the Term Cleanup status.
503449
( function ( $ ) {
504450
const statusWrapper = $( '.classifai-term-cleanup-process-status' );
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Poll admin-ajax to update the Classic Editor TTS meta box once audio generation completes.
2+
const init = () => {
3+
const status = document.querySelector( '.classifai-tts-status' );
4+
5+
if ( ! status ) {
6+
return;
7+
}
8+
9+
const postId = parseInt( status.dataset.postId, 10 );
10+
const nonce = status.dataset.nonce;
11+
12+
if ( ! postId || ! nonce ) {
13+
return;
14+
}
15+
16+
const POLL_INTERVAL_MS = 10000;
17+
18+
const renderComplete = ( { error, html } ) => {
19+
if ( error ) {
20+
status.textContent = error;
21+
return;
22+
}
23+
24+
if ( html ) {
25+
status.outerHTML = html;
26+
}
27+
};
28+
29+
const timer = setInterval( async () => {
30+
try {
31+
const body = new URLSearchParams( {
32+
action: 'classifai_get_tts_status',
33+
nonce,
34+
post_id: postId,
35+
} );
36+
37+
const response = await fetch( window.ajaxurl, {
38+
method: 'POST',
39+
credentials: 'same-origin',
40+
body,
41+
} );
42+
43+
if ( ! response.ok ) {
44+
return;
45+
}
46+
47+
const json = await response.json();
48+
49+
if ( ! json || ! json.success || ! json.data ) {
50+
return;
51+
}
52+
53+
if ( json.data.inProgress ) {
54+
return;
55+
}
56+
57+
clearInterval( timer );
58+
renderComplete( json.data );
59+
} catch ( e ) {
60+
// Swallow transient fetch errors and try again next tick.
61+
}
62+
}, POLL_INTERVAL_MS );
63+
};
64+
65+
if ( document.readyState !== 'loading' ) {
66+
init();
67+
} else {
68+
document.addEventListener( 'DOMContentLoaded', init );
69+
}

webpack.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ module.exports = {
2525
'classifai-plugin-classification-pre-publish': './src/js/features/classification/pre-publish-panel.js',
2626
'classifai-plugin-fill': './src/js/features/slot-fill/index.js',
2727
'classifai-plugin-text-to-speech': './src/js/features/text-to-speech/index.js',
28+
'classifai-plugin-classic-text-to-speech': './src/js/features/text-to-speech/classic/index.js',
2829
'classifai-plugin-text-to-speech-frontend': './src/js/features/text-to-speech/frontend/index.js',
2930
'classifai-plugin-content-resizing': './src/js/features/content-resizing/index.js',
3031
'classifai-plugin-title-generation': './src/js/features/title-generation/index.js',

0 commit comments

Comments
 (0)