@@ -48,8 +48,10 @@ class Transcription extends Base {
4848 * the user uploaded in WordPress, rather than a cached copy of GoDAM
4949 * Central's default caption. An uploaded file never reaches Central, so the
5050 * player plays it ahead of Central's tracks and Central updates never
51- * overwrite it. Set to `central` (SOURCE_CENTRAL) on generate, cleared on
52- * delete. Unset on transcripts stored before 2.8.0: see is_wordpress_upload().
51+ * overwrite it. `central-pending` (SOURCE_PENDING) while a Generate job
52+ * runs, `central` (SOURCE_CENTRAL) once Central's transcript is stored,
53+ * cleared on delete. Unset on transcripts stored before 2.8.0: see
54+ * is_wordpress_upload().
5355 *
5456 * @since 2.8.0
5557 *
@@ -66,6 +68,17 @@ class Transcription extends Base {
6668 */
6769 const SOURCE_CENTRAL = 'central ' ;
6870
71+ /**
72+ * SOURCE_META value while a Generate job runs. The file stored before it
73+ * (often an upload) is kept and played until Central has a ready default
74+ * caption, so a job that is slow or fails never leaves the video without one.
75+ *
76+ * @since 2.8.0
77+ *
78+ * @var string
79+ */
80+ const SOURCE_PENDING = 'central-pending ' ;
81+
6982 /**
7083 * Meta holding when the cached transcript was last checked against GoDAM
7184 * Central's caption list (a Unix timestamp), whether or not Central answered.
@@ -583,10 +596,10 @@ public function generate_transcription( \WP_REST_Request $request ) {
583596 // A job is genuinely in progress — clear the delete marker and
584597 // remember the in-flight status so the editor keeps polling. On a
585598 // hard error we leave the delete marker intact.
586- // Marked as Central's, so the finished transcript replaces an upload
587- // still stored from before 2.8.0 (see is_wordpress_upload ()).
599+ // Marked pending: the stored file stays until Central reports a ready
600+ // default, which then replaces it (see sync_cached_transcript ()).
588601 delete_post_meta ( $ attachment_id , self ::DELETED_META );
589- update_post_meta ( $ attachment_id , self ::SOURCE_META , self ::SOURCE_CENTRAL );
602+ update_post_meta ( $ attachment_id , self ::SOURCE_META , self ::SOURCE_PENDING );
590603 $ track = $ current_status ? $ current_status : $ status ;
591604 update_post_meta ( $ attachment_id , self ::STATUS_META , $ track );
592605 }
@@ -833,7 +846,9 @@ private function write_caption( $attachment_id, $method, $body ) {
833846 * uploaded in WordPress, which GoDAM Central does not have.
834847 *
835848 * From 2.8.0 on, {@see self::SOURCE_META} says which it is: `upload`, or
836- * `central` after Generate. Earlier versions left no mark, so an unmarked
849+ * `central` for Central's transcript. While a Generate job runs
850+ * (`central-pending`), the stored file is kept and played like an upload
851+ * until Central's transcript is ready. Earlier versions left no mark, so an unmarked
837852 * transcript counts as an upload when it is not on Central's CDN: Central
838853 * serves its caption files only from there, so any other address is a file
839854 * the site holds itself.
@@ -851,9 +866,28 @@ public static function is_wordpress_upload( $path, $source ) {
851866
852867 $ path = is_string ( $ path ) ? $ path : '' ;
853868
869+ if ( self ::SOURCE_PENDING === $ source ) {
870+ return '' !== $ path ;
871+ }
872+
854873 return '' === $ source && '' !== $ path && ! rtgodam_is_transcoder_file_url ( $ path );
855874 }
856875
876+ /**
877+ * Whether Central's caption list may update the stored transcript: it is
878+ * Central's copy, nothing is stored, or a Generate job is waiting for
879+ * Central's transcript. False only for a file uploaded in WordPress.
880+ *
881+ * @since 2.8.0
882+ *
883+ * @param mixed $path Value of the `rtgodam_transcript_path` meta.
884+ * @param mixed $source Value of the {@see self::SOURCE_META} meta.
885+ * @return bool
886+ */
887+ public static function takes_central_transcript ( $ path , $ source ) {
888+ return self ::SOURCE_PENDING === $ source || ! self ::is_wordpress_upload ( $ path , $ source );
889+ }
890+
857891 /**
858892 * Point the attachment's cached transcript (`rtgodam_transcript_path`) at
859893 * the default caption track Central reports, so the editor's Transcription
@@ -896,22 +930,28 @@ public static function sync_cached_transcript( $attachment_id, $captions, $switc
896930 }
897931 delete_post_meta ( $ attachment_id , self ::DELETED_META );
898932 $ cached_path = (string ) get_post_meta ( $ attachment_id , 'rtgodam_transcript_path ' , true );
899- if ( ! self ::is_wordpress_upload ( $ cached_path , get_post_meta ( $ attachment_id , self ::SOURCE_META , true ) ) ) {
933+ $ source = get_post_meta ( $ attachment_id , self ::SOURCE_META , true );
934+ if ( self ::takes_central_transcript ( $ cached_path , $ source ) ) {
900935 update_post_meta ( $ attachment_id , self ::CHECKED_META , time () );
901936 if ( '' !== $ default_path ) {
902937 update_post_meta ( $ attachment_id , 'rtgodam_transcript_path ' , $ default_path );
903938 update_post_meta ( $ attachment_id , self ::STATUS_META , 'Transcribed ' );
904- } elseif ( '' !== $ cached_path && in_array ( $ file_key ( $ cached_path ), $ listed_files , true ) ) {
905- // The default has no file yet (an AI transcript still pending, or one
906- // that failed). Keep the transcript WordPress has while Central still
907- // lists it, so the Transcription tab and the player don't lose it.
908- update_post_meta ( $ attachment_id , self ::STATUS_META , 'Transcribed ' );
909- } else {
910- delete_post_meta ( $ attachment_id , 'rtgodam_transcript_path ' );
911- // Keep an in-flight status (such as "Transcribing") so the tab still
912- // shows the job in progress; only a finished one goes with the file.
913- if ( 'Transcribed ' === get_post_meta ( $ attachment_id , self ::STATUS_META , true ) ) {
914- delete_post_meta ( $ attachment_id , self ::STATUS_META );
939+ update_post_meta ( $ attachment_id , self ::SOURCE_META , self ::SOURCE_CENTRAL );
940+ } elseif ( self ::SOURCE_PENDING !== $ source ) {
941+ // While Generate runs, or after it failed, the stored file and status
942+ // stay as they are until Central has a ready default caption.
943+ if ( '' !== $ cached_path && in_array ( $ file_key ( $ cached_path ), $ listed_files , true ) ) {
944+ // The default has no file yet (an AI transcript still pending, or one
945+ // that failed). Keep the transcript WordPress has while Central still
946+ // lists it, so the Transcription tab and the player don't lose it.
947+ update_post_meta ( $ attachment_id , self ::STATUS_META , 'Transcribed ' );
948+ } else {
949+ delete_post_meta ( $ attachment_id , 'rtgodam_transcript_path ' );
950+ // Keep an in-flight status (such as "Transcribing") so the tab still
951+ // shows the job in progress; only a finished one goes with the file.
952+ if ( 'Transcribed ' === get_post_meta ( $ attachment_id , self ::STATUS_META , true ) ) {
953+ delete_post_meta ( $ attachment_id , self ::STATUS_META );
954+ }
915955 }
916956 }
917957 }
@@ -944,7 +984,7 @@ public static function refresh_cached_default( $attachment_id ) {
944984 /** This action is documented in inc/classes/rest-api/class-transcription.php */
945985 do_action ( 'rtgodam_before_attachment_lookup ' );
946986 $ is_deleted = (bool ) get_post_meta ( $ attachment_id , self ::DELETED_META , true );
947- $ is_upload = self ::is_wordpress_upload (
987+ $ is_upload = ! self ::takes_central_transcript (
948988 get_post_meta ( $ attachment_id , 'rtgodam_transcript_path ' , true ),
949989 get_post_meta ( $ attachment_id , self ::SOURCE_META , true )
950990 );
0 commit comments