diff --git a/admin/class-rtgodam-transcoder-rest-routes.php b/admin/class-rtgodam-transcoder-rest-routes.php index e48a58da2b..8701bed595 100644 --- a/admin/class-rtgodam-transcoder-rest-routes.php +++ b/admin/class-rtgodam-transcoder-rest-routes.php @@ -669,10 +669,11 @@ public function handle_transcription_callback( WP_REST_Request $request ) { // language. Cache the default caption from the full list instead, // and leave a transcript the user uploaded in WordPress alone. \RTGODAM\Inc\REST_API\Transcription::sync_cached_transcript( (int) $attachment_id, $caption_tracks, false ); - } elseif ( 'upload' !== get_post_meta( $attachment_id, 'rtgodam_transcript_source', true ) ) { + } elseif ( \RTGODAM\Inc\REST_API\Transcription::takes_central_transcript( get_post_meta( $attachment_id, 'rtgodam_transcript_path', true ), get_post_meta( $attachment_id, 'rtgodam_transcript_source', true ) ) ) { // Older Central: only the one transcript path is sent. // The transcript_path parameter is already sanitized by the REST API framework via esc_url_raw sanitize_callback. update_post_meta( $attachment_id, 'rtgodam_transcript_path', $transcript_path ); + update_post_meta( $attachment_id, 'rtgodam_transcript_source', \RTGODAM\Inc\REST_API\Transcription::SOURCE_CENTRAL ); } return new WP_REST_Response( diff --git a/inc/classes/class-seo.php b/inc/classes/class-seo.php index 8165508f2f..1ad90f41ad 100644 --- a/inc/classes/class-seo.php +++ b/inc/classes/class-seo.php @@ -631,7 +631,7 @@ public function get_transcript_excerpt( $attachment_id, $allow_remote = false ) do_action( 'rtgodam_before_attachment_lookup' ); $transcript_path = (string) get_post_meta( $attachment_id, 'rtgodam_transcript_path', true ); $is_deleted = (bool) get_post_meta( $attachment_id, 'rtgodam_transcript_deleted', true ); - $is_upload = 'upload' === get_post_meta( $attachment_id, 'rtgodam_transcript_source', true ); + $source = get_post_meta( $attachment_id, 'rtgodam_transcript_source', true ); $checked_at = (int) get_post_meta( $attachment_id, 'rtgodam_transcript_checked_at', true ); $cache = get_post_meta( $attachment_id, self::TRANSCRIPT_EXCERPT_META_KEY, true ); do_action( 'rtgodam_after_attachment_lookup' ); @@ -643,7 +643,7 @@ public function get_transcript_excerpt( $attachment_id, $allow_remote = false ) // The transcript may be a cached copy of GoDAM Central's default caption, // which can change in Central's own dashboard without telling WordPress. // Re-check it in the background at most every 12 hours. - if ( ! $is_upload && class_exists( '\RTGODAM\Inc\REST_API\Transcription' ) ) { + if ( class_exists( '\RTGODAM\Inc\REST_API\Transcription' ) && \RTGODAM\Inc\REST_API\Transcription::takes_central_transcript( $transcript_path, $source ) ) { \RTGODAM\Inc\REST_API\Transcription::maybe_schedule_refresh( $attachment_id, $checked_at ); } diff --git a/inc/classes/rest-api/class-transcription.php b/inc/classes/rest-api/class-transcription.php index d4f00a81ef..c579435240 100644 --- a/inc/classes/rest-api/class-transcription.php +++ b/inc/classes/rest-api/class-transcription.php @@ -48,7 +48,10 @@ class Transcription extends Base { * the user uploaded in WordPress, rather than a cached copy of GoDAM * Central's default caption. An uploaded file never reaches Central, so the * player plays it ahead of Central's tracks and Central updates never - * overwrite it. Cleared on generate and on delete. + * overwrite it. `central-pending` (SOURCE_PENDING) while a Generate job + * runs, `central` (SOURCE_CENTRAL) once Central's transcript is stored, + * cleared on delete. Unset on transcripts stored before 2.8.0: see + * is_wordpress_upload(). * * @since 2.8.0 * @@ -56,6 +59,26 @@ class Transcription extends Base { */ const SOURCE_META = 'rtgodam_transcript_source'; + /** + * SOURCE_META value for a transcript generated by GoDAM Central. + * + * @since 2.8.0 + * + * @var string + */ + const SOURCE_CENTRAL = 'central'; + + /** + * SOURCE_META value while a Generate job runs. The file stored before it + * (often an upload) is kept and played until Central has a ready default + * caption, so a job that is slow or fails never leaves the video without one. + * + * @since 2.8.0 + * + * @var string + */ + const SOURCE_PENDING = 'central-pending'; + /** * Meta holding when the cached transcript was last checked against GoDAM * Central's caption list (a Unix timestamp), whether or not Central answered. @@ -566,15 +589,17 @@ public function generate_transcription( \WP_REST_Request $request ) { if ( ! empty( $path ) ) { // A (re)generated transcript supersedes any prior delete or upload. delete_post_meta( $attachment_id, self::DELETED_META ); - delete_post_meta( $attachment_id, self::SOURCE_META ); + update_post_meta( $attachment_id, self::SOURCE_META, self::SOURCE_CENTRAL ); update_post_meta( $attachment_id, 'rtgodam_transcript_path', $path ); update_post_meta( $attachment_id, self::STATUS_META, 'Transcribed' ); } elseif ( '' !== $current_status || '' !== $status ) { // A job is genuinely in progress — clear the delete marker and // remember the in-flight status so the editor keeps polling. On a // hard error we leave the delete marker intact. + // Marked pending: the stored file stays until Central reports a ready + // default, which then replaces it (see sync_cached_transcript()). delete_post_meta( $attachment_id, self::DELETED_META ); - delete_post_meta( $attachment_id, self::SOURCE_META ); + update_post_meta( $attachment_id, self::SOURCE_META, self::SOURCE_PENDING ); $track = $current_status ? $current_status : $status; update_post_meta( $attachment_id, self::STATUS_META, $track ); } @@ -816,6 +841,53 @@ private function write_caption( $attachment_id, $method, $body ) { return $response; } + /** + * Whether the attachment's stored transcript is a caption file the user + * uploaded in WordPress, which GoDAM Central does not have. + * + * From 2.8.0 on, {@see self::SOURCE_META} says which it is: `upload`, or + * `central` for Central's transcript. While a Generate job runs + * (`central-pending`), the stored file is kept and played like an upload + * until Central's transcript is ready. Earlier versions left no mark, so an unmarked + * transcript counts as an upload when it is not on Central's CDN: Central + * serves its caption files only from there, so any other address is a file + * the site holds itself. + * + * @since 2.8.0 + * + * @param mixed $path Value of the `rtgodam_transcript_path` meta. + * @param mixed $source Value of the {@see self::SOURCE_META} meta. + * @return bool + */ + public static function is_wordpress_upload( $path, $source ) { + if ( 'upload' === $source ) { + return true; + } + + $path = is_string( $path ) ? $path : ''; + + if ( self::SOURCE_PENDING === $source ) { + return '' !== $path; + } + + return '' === $source && '' !== $path && ! rtgodam_is_transcoder_file_url( $path ); + } + + /** + * Whether Central's caption list may update the stored transcript: it is + * Central's copy, nothing is stored, or a Generate job is waiting for + * Central's transcript. False only for a file uploaded in WordPress. + * + * @since 2.8.0 + * + * @param mixed $path Value of the `rtgodam_transcript_path` meta. + * @param mixed $source Value of the {@see self::SOURCE_META} meta. + * @return bool + */ + public static function takes_central_transcript( $path, $source ) { + return self::SOURCE_PENDING === $source || ! self::is_wordpress_upload( $path, $source ); + } + /** * Point the attachment's cached transcript (`rtgodam_transcript_path`) at * the default caption track Central reports, so the editor's Transcription @@ -857,23 +929,29 @@ public static function sync_cached_transcript( $attachment_id, $captions, $switc do_action( 'rtgodam_before_attachment_lookup' ); } delete_post_meta( $attachment_id, self::DELETED_META ); - if ( 'upload' !== get_post_meta( $attachment_id, self::SOURCE_META, true ) ) { + $cached_path = (string) get_post_meta( $attachment_id, 'rtgodam_transcript_path', true ); + $source = get_post_meta( $attachment_id, self::SOURCE_META, true ); + if ( self::takes_central_transcript( $cached_path, $source ) ) { update_post_meta( $attachment_id, self::CHECKED_META, time() ); - $cached_path = (string) get_post_meta( $attachment_id, 'rtgodam_transcript_path', true ); if ( '' !== $default_path ) { update_post_meta( $attachment_id, 'rtgodam_transcript_path', $default_path ); update_post_meta( $attachment_id, self::STATUS_META, 'Transcribed' ); - } elseif ( '' !== $cached_path && in_array( $file_key( $cached_path ), $listed_files, true ) ) { - // The default has no file yet (an AI transcript still pending, or one - // that failed). Keep the transcript WordPress has while Central still - // lists it, so the Transcription tab and the player don't lose it. - update_post_meta( $attachment_id, self::STATUS_META, 'Transcribed' ); - } else { - delete_post_meta( $attachment_id, 'rtgodam_transcript_path' ); - // Keep an in-flight status (such as "Transcribing") so the tab still - // shows the job in progress; only a finished one goes with the file. - if ( 'Transcribed' === get_post_meta( $attachment_id, self::STATUS_META, true ) ) { - delete_post_meta( $attachment_id, self::STATUS_META ); + update_post_meta( $attachment_id, self::SOURCE_META, self::SOURCE_CENTRAL ); + } elseif ( self::SOURCE_PENDING !== $source ) { + // While Generate runs, or after it failed, the stored file and status + // stay as they are until Central has a ready default caption. + if ( '' !== $cached_path && in_array( $file_key( $cached_path ), $listed_files, true ) ) { + // The default has no file yet (an AI transcript still pending, or one + // that failed). Keep the transcript WordPress has while Central still + // lists it, so the Transcription tab and the player don't lose it. + update_post_meta( $attachment_id, self::STATUS_META, 'Transcribed' ); + } else { + delete_post_meta( $attachment_id, 'rtgodam_transcript_path' ); + // Keep an in-flight status (such as "Transcribing") so the tab still + // shows the job in progress; only a finished one goes with the file. + if ( 'Transcribed' === get_post_meta( $attachment_id, self::STATUS_META, true ) ) { + delete_post_meta( $attachment_id, self::STATUS_META ); + } } } } @@ -906,7 +984,10 @@ public static function refresh_cached_default( $attachment_id ) { /** This action is documented in inc/classes/rest-api/class-transcription.php */ do_action( 'rtgodam_before_attachment_lookup' ); $is_deleted = (bool) get_post_meta( $attachment_id, self::DELETED_META, true ); - $is_upload = 'upload' === get_post_meta( $attachment_id, self::SOURCE_META, true ); + $is_upload = ! self::takes_central_transcript( + get_post_meta( $attachment_id, 'rtgodam_transcript_path', true ), + get_post_meta( $attachment_id, self::SOURCE_META, true ) + ); $job_id = get_post_meta( $attachment_id, 'rtgodam_transcoding_job_id', true ); if ( empty( $job_id ) ) { $job_id = get_post_meta( $attachment_id, '_godam_original_id', true ); diff --git a/inc/helpers/custom-functions.php b/inc/helpers/custom-functions.php index b1a526f1b8..524efd5fa9 100644 --- a/inc/helpers/custom-functions.php +++ b/inc/helpers/custom-functions.php @@ -2172,6 +2172,9 @@ function godam_get_transcript_path( $attachment_id, $job_id = null ) { */ do_action( 'rtgodam_before_attachment_lookup' ); update_post_meta( $attachment_id, 'rtgodam_transcript_path', $transcript_path ); + // Marked as Central's, so only transcripts stored before 2.8.0 need the + // address check in Transcription::is_wordpress_upload(). + update_post_meta( $attachment_id, 'rtgodam_transcript_source', 'central' ); do_action( 'rtgodam_after_attachment_lookup' ); } @@ -3560,6 +3563,11 @@ function rtgodam_get_transcoder_file_hosts() { * A name matches that host and any subdomain of it. Add the CDN domain of a * self-hosted or staging GoDAM Central here, or a domain the CDN is served from. * + * Since 2.8.0 these hosts also tell a caption file uploaded in WordPress apart + * from a copy of Central's caption, for transcripts stored before 2.8.0 (which + * carry no `rtgodam_transcript_source`): a file on any other host is kept as + * an upload and never replaced by Central's caption list. + * * @since 2.7.0 * * @param string[] $hosts Host names, such as `gdcdn.us`. diff --git a/inc/templates/godam-player.php b/inc/templates/godam-player.php index cddabe2ddb..8b876f3e03 100644 --- a/inc/templates/godam-player.php +++ b/inc/templates/godam-player.php @@ -775,6 +775,10 @@ function ( $godam_layer ) { $godam_transcript_url = (string) ( $godam_attachment_data['transcript_path'] ?? '' ); $godam_transcript_deleted = ! empty( $godam_attachment_data['transcript_deleted'] ); $godam_transcript_source = (string) ( $godam_attachment_data['transcript_source'] ?? '' ); +// An upload from before 2.8.0 carries no source mark; recognise it by its address. +if ( \RTGODAM\Inc\REST_API\Transcription::is_wordpress_upload( $godam_transcript_url, $godam_transcript_source ) ) { + $godam_transcript_source = 'upload'; +} $godam_attachment_title = ''; diff --git a/languages/godam.pot b/languages/godam.pot index 09bbc08d5c..15bc005093 100644 --- a/languages/godam.pot +++ b/languages/godam.pot @@ -9,7 +9,7 @@ msgstr "" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" -"POT-Creation-Date: 2026-11-09T12:09:21+00:00\n" +"POT-Creation-Date: 2026-11-10T09:36:25+00:00\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "X-Generator: WP-CLI 2.12.0\n" "X-Domain: godam\n" @@ -11512,26 +11512,26 @@ msgstr "" msgid "Video attachment not found for the provided job ID." msgstr "" -#: admin/class-rtgodam-transcoder-rest-routes.php:681 +#: admin/class-rtgodam-transcoder-rest-routes.php:682 msgid "Transcript path saved successfully." msgstr "" -#: admin/class-rtgodam-transcoder-rest-routes.php:691 +#: admin/class-rtgodam-transcoder-rest-routes.php:692 msgid "Transcription callback received." msgstr "" -#: admin/class-rtgodam-transcoder-rest-routes.php:732 +#: admin/class-rtgodam-transcoder-rest-routes.php:733 #: admin/godam-transcoder-functions.php:607 #: inc/classes/rest-api/class-media-library.php:652 #: inc/classes/rest-api/class-transcoding.php:481 msgid "API key is required." msgstr "" -#: admin/class-rtgodam-transcoder-rest-routes.php:736 +#: admin/class-rtgodam-transcoder-rest-routes.php:737 msgid "API key not configured on the site." msgstr "" -#: admin/class-rtgodam-transcoder-rest-routes.php:740 +#: admin/class-rtgodam-transcoder-rest-routes.php:741 #: inc/classes/rest-api/class-media-library.php:659 #: inc/classes/rest-api/class-settings.php:192 #: inc/classes/rest-api/class-transcoding.php:487 @@ -14023,83 +14023,83 @@ msgstr "" msgid "%1$s (ID %2$d) transcoding request was sent successfully" msgstr "" -#: inc/classes/rest-api/class-transcription.php:161 +#: inc/classes/rest-api/class-transcription.php:184 msgid "The video attachment ID." msgstr "" -#: inc/classes/rest-api/class-transcription.php:182 +#: inc/classes/rest-api/class-transcription.php:205 msgid "Re-read the default caption from GoDAM Central first, so changes made in Central show." msgstr "" -#: inc/classes/rest-api/class-transcription.php:208 +#: inc/classes/rest-api/class-transcription.php:231 msgid "BCP 47 language code to transcribe in, or \"auto\"/\"multi\" for Deepgram detection. Omitted falls back to the org/default language." msgstr "" -#: inc/classes/rest-api/class-transcription.php:214 +#: inc/classes/rest-api/class-transcription.php:237 msgid "Audio stream to transcribe on a multi-audio asset. Omitted transcribes the default track." msgstr "" -#: inc/classes/rest-api/class-transcription.php:234 +#: inc/classes/rest-api/class-transcription.php:257 msgid "URL of the uploaded .vtt / .srt caption file." msgstr "" -#: inc/classes/rest-api/class-transcription.php:309 +#: inc/classes/rest-api/class-transcription.php:332 msgid "BCP 47 language tag for the caption track." msgstr "" -#: inc/classes/rest-api/class-transcription.php:317 +#: inc/classes/rest-api/class-transcription.php:340 msgid "Label shown in the subtitle menu. Unique on the video." msgstr "" -#: inc/classes/rest-api/class-transcription.php:324 +#: inc/classes/rest-api/class-transcription.php:347 msgid "Track kind: subtitles or captions." msgstr "" -#: inc/classes/rest-api/class-transcription.php:330 +#: inc/classes/rest-api/class-transcription.php:353 msgid "Make this the default caption track." msgstr "" -#: inc/classes/rest-api/class-transcription.php:340 +#: inc/classes/rest-api/class-transcription.php:363 msgid "Caption file text (VTT or SRT)." msgstr "" -#: inc/classes/rest-api/class-transcription.php:349 +#: inc/classes/rest-api/class-transcription.php:372 msgid "Format of the uploaded caption text." msgstr "" -#: inc/classes/rest-api/class-transcription.php:367 +#: inc/classes/rest-api/class-transcription.php:390 msgid "Caption track handle returned by the list endpoint." msgstr "" -#: inc/classes/rest-api/class-transcription.php:501 +#: inc/classes/rest-api/class-transcription.php:524 msgid "This video has not been transcoded yet, so it cannot be transcribed." msgstr "" -#: inc/classes/rest-api/class-transcription.php:510 +#: inc/classes/rest-api/class-transcription.php:533 msgid "Connect your GoDAM account to generate transcriptions." msgstr "" -#: inc/classes/rest-api/class-transcription.php:543 +#: inc/classes/rest-api/class-transcription.php:566 msgid "Could not reach the transcription service. Please try again." msgstr "" -#: inc/classes/rest-api/class-transcription.php:609 +#: inc/classes/rest-api/class-transcription.php:634 msgid "Only .vtt or .srt caption files are supported." msgstr "" -#: inc/classes/rest-api/class-transcription.php:1054 +#: inc/classes/rest-api/class-transcription.php:1135 msgid "Connect your GoDAM account to manage captions." msgstr "" -#: inc/classes/rest-api/class-transcription.php:1075 +#: inc/classes/rest-api/class-transcription.php:1156 msgid "This video has not been transcoded yet, so it has no captions." msgstr "" -#: inc/classes/rest-api/class-transcription.php:1107 +#: inc/classes/rest-api/class-transcription.php:1188 msgid "Could not reach the caption service. Please try again." msgstr "" -#: inc/classes/rest-api/class-transcription.php:1128 +#: inc/classes/rest-api/class-transcription.php:1209 msgid "The caption service rejected the request." msgstr "" @@ -14769,23 +14769,23 @@ msgstr "" msgid "s" msgstr "" -#: inc/helpers/custom-functions.php:2474 +#: inc/helpers/custom-functions.php:2477 msgid "Oops! We could not locate your video" msgstr "" -#: inc/helpers/custom-functions.php:2485 +#: inc/helpers/custom-functions.php:2488 msgid "Note: This is a simple image preview. The image and its layers may display differently when added to a page based on theme styles." msgstr "" -#: inc/helpers/custom-functions.php:2487 +#: inc/helpers/custom-functions.php:2490 msgid "Note: This is a simple audio preview. The player may display differently when added to a page based on theme styles." msgstr "" -#: inc/helpers/custom-functions.php:2489 +#: inc/helpers/custom-functions.php:2492 msgid "Note: This is a simple video preview. The video player may display differently when added to a page based on theme styles." msgstr "" -#: inc/helpers/custom-functions.php:2679 +#: inc/helpers/custom-functions.php:2682 msgid "Video not found" msgstr "" diff --git a/tests/php/TranscriptionCaptionRefreshTest.php b/tests/php/TranscriptionCaptionRefreshTest.php index 89f1d2b1bc..f99a5b4bf2 100644 --- a/tests/php/TranscriptionCaptionRefreshTest.php +++ b/tests/php/TranscriptionCaptionRefreshTest.php @@ -31,7 +31,7 @@ protected function setUp(): void { $GLOBALS['rtgodam_stub'] = array( 'post_meta' => array( 'rtgodam_transcoding_job_id' => 'job-1', - 'rtgodam_transcript_path' => 'https://cdn.example/job/en.vtt', + 'rtgodam_transcript_path' => 'https://n8e0ka87m9.gdcdn.us/job/en.vtt', ), 'options' => array( 'rtgodam-api-key' => 'key' ), 'scheduled' => array(), @@ -62,12 +62,12 @@ private function central_default_is_spanish() { 'captions' => array( array( 'language' => 'en', - 'vtt_path' => 'https://cdn.example/job/en.vtt', + 'vtt_path' => 'https://n8e0ka87m9.gdcdn.us/job/en.vtt', 'is_default' => false, ), array( 'language' => 'es', - 'vtt_path' => 'https://cdn.example/job/es.vtt', + 'vtt_path' => 'https://n8e0ka87m9.gdcdn.us/job/es.vtt', 'is_default' => true, ), ), @@ -88,7 +88,7 @@ public function test_follows_a_default_changed_in_central() { $this->assertTrue( Transcription::refresh_cached_default( 7 ) ); $meta = $GLOBALS['rtgodam_stub']['post_meta']; - $this->assertSame( 'https://cdn.example/job/es.vtt', $meta['rtgodam_transcript_path'] ); + $this->assertSame( 'https://n8e0ka87m9.gdcdn.us/job/es.vtt', $meta['rtgodam_transcript_path'] ); $this->assertGreaterThan( time() - 5, $meta[ Transcription::CHECKED_META ] ); $this->assertStringEndsWith( 'godam_core.api.captions.list_captions', $GLOBALS['rtgodam_stub']['posts'][0]['url'] ); } @@ -109,7 +109,21 @@ public function test_leaves_uploads_and_deletes_alone( $key, $value ) { $this->assertFalse( Transcription::refresh_cached_default( 7 ) ); $this->assertSame( array(), $GLOBALS['rtgodam_stub']['posts'] ); - $this->assertSame( 'https://cdn.example/job/en.vtt', $GLOBALS['rtgodam_stub']['post_meta']['rtgodam_transcript_path'] ); + $this->assertSame( 'https://n8e0ka87m9.gdcdn.us/job/en.vtt', $GLOBALS['rtgodam_stub']['post_meta']['rtgodam_transcript_path'] ); + } + + /** + * An upload from before 2.8.0 carries no source mark. Its address is not + * on Central's CDN, so Central is not asked and the file stays. + */ + public function test_leaves_an_unmarked_upload_from_before_2_8_alone() { + $GLOBALS['rtgodam_stub']['post_meta']['rtgodam_transcript_path'] = 'https://site.example/wp-content/uploads/fixed.vtt'; + $this->central_default_is_spanish(); + + $this->assertFalse( Transcription::refresh_cached_default( 7 ) ); + + $this->assertSame( array(), $GLOBALS['rtgodam_stub']['posts'] ); + $this->assertSame( 'https://site.example/wp-content/uploads/fixed.vtt', $GLOBALS['rtgodam_stub']['post_meta']['rtgodam_transcript_path'] ); } /** @@ -154,7 +168,7 @@ public function test_keeps_the_cache_when_the_default_has_no_file() { array( array( 'language' => 'en', - 'vtt_path' => 'https://cdn.example/job/en.vtt', + 'vtt_path' => 'https://n8e0ka87m9.gdcdn.us/job/en.vtt', 'is_default' => false, ), array( @@ -167,7 +181,7 @@ public function test_keeps_the_cache_when_the_default_has_no_file() { ); $this->assertTrue( Transcription::refresh_cached_default( 7 ) ); - $this->assertSame( 'https://cdn.example/job/en.vtt', $GLOBALS['rtgodam_stub']['post_meta']['rtgodam_transcript_path'] ); + $this->assertSame( 'https://n8e0ka87m9.gdcdn.us/job/en.vtt', $GLOBALS['rtgodam_stub']['post_meta']['rtgodam_transcript_path'] ); } /** @@ -176,12 +190,12 @@ public function test_keeps_the_cache_when_the_default_has_no_file() { * transcript is still the same file and stays. */ public function test_keeps_a_signed_cache_when_the_default_has_no_file() { - $GLOBALS['rtgodam_stub']['post_meta']['rtgodam_transcript_path'] = 'https://cdn.example/job/en.vtt?token=A&expires=1'; + $GLOBALS['rtgodam_stub']['post_meta']['rtgodam_transcript_path'] = 'https://n8e0ka87m9.gdcdn.us/job/en.vtt?token=A&expires=1'; $this->central_lists( array( array( 'language' => 'en', - 'vtt_path' => 'https://cdn.example/job/en.vtt?token=B&expires=2', + 'vtt_path' => 'https://n8e0ka87m9.gdcdn.us/job/en.vtt?token=B&expires=2', 'is_default' => false, ), array( @@ -194,7 +208,7 @@ public function test_keeps_a_signed_cache_when_the_default_has_no_file() { ); $this->assertTrue( Transcription::refresh_cached_default( 7 ) ); - $this->assertSame( 'https://cdn.example/job/en.vtt?token=A&expires=1', $GLOBALS['rtgodam_stub']['post_meta']['rtgodam_transcript_path'] ); + $this->assertSame( 'https://n8e0ka87m9.gdcdn.us/job/en.vtt?token=A&expires=1', $GLOBALS['rtgodam_stub']['post_meta']['rtgodam_transcript_path'] ); } /** @@ -250,7 +264,7 @@ public function test_keeps_the_cache_when_central_fails() { }; $this->assertFalse( Transcription::refresh_cached_default( 7 ) ); - $this->assertSame( 'https://cdn.example/job/en.vtt', $GLOBALS['rtgodam_stub']['post_meta']['rtgodam_transcript_path'] ); + $this->assertSame( 'https://n8e0ka87m9.gdcdn.us/job/en.vtt', $GLOBALS['rtgodam_stub']['post_meta']['rtgodam_transcript_path'] ); } /** diff --git a/tests/php/TranscriptionCaptionSyncTest.php b/tests/php/TranscriptionCaptionSyncTest.php index 1fac617eb1..367844eaab 100644 --- a/tests/php/TranscriptionCaptionSyncTest.php +++ b/tests/php/TranscriptionCaptionSyncTest.php @@ -47,13 +47,13 @@ private function captions() { array( 'caption_id' => 'es1', 'language' => 'es', - 'vtt_path' => 'https://cdn.example/job/es.vtt', + 'vtt_path' => 'https://n8e0ka87m9.gdcdn.us/job/es.vtt', 'is_default' => false, ), array( 'caption_id' => 'en1', 'language' => 'en', - 'vtt_path' => 'https://cdn.example/job/en.vtt', + 'vtt_path' => 'https://n8e0ka87m9.gdcdn.us/job/en.vtt', 'is_default' => true, ), ); @@ -65,14 +65,14 @@ private function captions() { */ public function test_caches_the_default_caption() { $GLOBALS['rtgodam_stub']['post_meta'] = array( - 'rtgodam_transcript_path' => 'https://cdn.example/job/old.vtt', + 'rtgodam_transcript_path' => 'https://n8e0ka87m9.gdcdn.us/job/old.vtt', 'rtgodam_transcript_deleted' => '1', ); Transcription::sync_cached_transcript( 7, $this->captions() ); $meta = $GLOBALS['rtgodam_stub']['post_meta']; - $this->assertSame( 'https://cdn.example/job/en.vtt', $meta['rtgodam_transcript_path'] ); + $this->assertSame( 'https://n8e0ka87m9.gdcdn.us/job/en.vtt', $meta['rtgodam_transcript_path'] ); $this->assertSame( 'Transcribed', $meta['rtgodam_transcript_status'] ); $this->assertArrayNotHasKey( 'rtgodam_transcript_deleted', $meta ); } @@ -83,7 +83,7 @@ public function test_caches_the_default_caption() { */ public function test_clears_the_cache_when_no_default_is_ready() { $GLOBALS['rtgodam_stub']['post_meta'] = array( - 'rtgodam_transcript_path' => 'https://cdn.example/job/en.vtt', + 'rtgodam_transcript_path' => 'https://n8e0ka87m9.gdcdn.us/job/en.vtt', 'rtgodam_transcript_status' => 'Transcribed', ); @@ -121,6 +121,76 @@ public function test_keeps_a_wordpress_upload() { ); } + /** + * An upload from before 2.8.0 has no source mark. Its address is not on + * Central's CDN, so it is still kept. + */ + public function test_keeps_an_unmarked_upload_from_before_2_8() { + $GLOBALS['rtgodam_stub']['post_meta'] = array( + 'rtgodam_transcript_path' => 'https://site.example/wp-content/uploads/fixed.vtt', + ); + + Transcription::sync_cached_transcript( 7, $this->captions() ); + + $this->assertSame( + 'https://site.example/wp-content/uploads/fixed.vtt', + $GLOBALS['rtgodam_stub']['post_meta']['rtgodam_transcript_path'] + ); + } + + /** + * After Generate on a video with an old upload, the source is `central`, + * so the finished transcript replaces the upload. + */ + public function test_generate_replaces_an_unmarked_upload() { + $GLOBALS['rtgodam_stub']['post_meta'] = array( + 'rtgodam_transcript_path' => 'https://site.example/wp-content/uploads/fixed.vtt', + 'rtgodam_transcript_source' => 'central', + ); + + Transcription::sync_cached_transcript( 7, $this->captions() ); + + $this->assertSame( + 'https://n8e0ka87m9.gdcdn.us/job/en.vtt', + $GLOBALS['rtgodam_stub']['post_meta']['rtgodam_transcript_path'] + ); + } + + /** + * Which stored transcripts count as uploaded in WordPress. + * + * @dataProvider upload_cases + * + * @param mixed $path Stored transcript path. + * @param mixed $source Stored source mark. + * @param bool $expected Whether it is an upload. + */ + public function test_is_wordpress_upload( $path, $source, $expected ) { + $this->assertSame( $expected, Transcription::is_wordpress_upload( $path, $source ) ); + } + + /** + * Cases for test_is_wordpress_upload. + * + * @return array + */ + public function upload_cases() { + $site = 'https://site.example/wp-content/uploads/2026/10/fixed.vtt'; + $offload = 'https://bucket.s3.amazonaws.com/uploads/fixed.srt'; + $central = 'https://n8e0ka87m9.gdcdn.us/job/en.vtt?token=a&expires=1'; + + return array( + 'marked upload' => array( $central, 'upload', true ), + 'unmarked site file' => array( $site, '', true ), + 'unmarked offloaded file' => array( $offload, '', true ), + 'unmarked Central copy' => array( $central, '', false ), + 'generated, old file still set' => array( $site, 'central', false ), + 'Generate running, file stored' => array( $site, 'central-pending', true ), + 'Generate running, nothing' => array( '', 'central-pending', false ), + 'nothing stored' => array( '', '', false ), + ); + } + /** * Language codes keep their case and may have several subtags, like * Central's own check; detection keywords are lower-cased; junk is dropped. diff --git a/tests/php/TranscriptionGenerateSourceTest.php b/tests/php/TranscriptionGenerateSourceTest.php new file mode 100644 index 0000000000..7271ed533e --- /dev/null +++ b/tests/php/TranscriptionGenerateSourceTest.php @@ -0,0 +1,204 @@ + array( + 'rtgodam_transcoding_job_id' => 'job-1', + 'rtgodam_transcript_path' => self::UPLOAD, + ), + 'options' => array( 'rtgodam-api-key' => 'key' ), + 'scheduled' => array(), + 'posts' => array(), + ); + + $root = dirname( __DIR__, 2 ); + require_once $root . '/tests/stubs/hooks.php'; + require_once $root . '/tests/stubs/video-seo-functions.php'; + require_once $root . '/tests/stubs/engagement-functions.php'; + require_once $root . '/tests/stubs/media-file-functions.php'; + require_once $root . '/tests/stubs/option-functions.php'; + require_once $root . '/tests/stubs/media-library-functions.php'; + require_once $root . '/tests/stubs/class-wp-rest-request.php'; + require_once $root . '/inc/classes/rest-api/class-base.php'; + require_once $root . '/inc/classes/rest-api/class-transcription.php'; + } + + /** + * Make every Central call answer with this message. + * + * @param array $message Frappe `message` payload. + */ + private function central_answers( array $message ) { + $GLOBALS['rtgodam_stub']['post_handler'] = static function () use ( $message ) { + return array( + 'code' => 200, + 'body' => json_encode( array( 'message' => $message ) ), // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode -- test fixture. + ); + }; + } + + /** + * Click Generate Transcription for attachment 7. + */ + private function generate() { + $controller = ( new \ReflectionClass( Transcription::class ) )->newInstanceWithoutConstructor(); + $controller->generate_transcription( new \WP_REST_Request( array( 'attachment_id' => 7 ), 'POST' ) ); + } + + /** + * Central's caption list: one default row, with or without a file. + * + * @param string|null $vtt_path The default's file, or null while it has none. + * @return array + */ + private function default_row( $vtt_path ) { + return array( + array( + 'caption_id' => 'en1', + 'language' => 'en', + 'vtt_path' => $vtt_path, + 'is_default' => true, + ), + ); + } + + /** + * Generate starts a job: the upload stays and still counts as one, so the + * player keeps playing it. + */ + public function test_generate_keeps_the_upload_while_the_job_runs() { + $this->central_answers( + array( + 'success' => true, + 'current_status' => 'Transcribing', + ) + ); + + $this->generate(); + + $meta = $GLOBALS['rtgodam_stub']['post_meta']; + $this->assertSame( Transcription::SOURCE_PENDING, $meta['rtgodam_transcript_source'] ); + $this->assertSame( self::UPLOAD, $meta['rtgodam_transcript_path'] ); + $this->assertTrue( Transcription::is_wordpress_upload( self::UPLOAD, $meta['rtgodam_transcript_source'] ) ); + } + + /** + * Central answers Generate with a finished transcript at once: it replaces + * the upload and is marked as Central's. + */ + public function test_generate_with_an_immediate_transcript_replaces_the_upload() { + $this->central_answers( + array( + 'success' => true, + 'transcript_path' => self::CENTRAL, + ) + ); + + $this->generate(); + + $meta = $GLOBALS['rtgodam_stub']['post_meta']; + $this->assertSame( Transcription::SOURCE_CENTRAL, $meta['rtgodam_transcript_source'] ); + $this->assertSame( self::CENTRAL, $meta['rtgodam_transcript_path'] ); + } + + /** + * While the job runs, or after it failed, Central's default row has no + * file. The upload is not dropped. + * + * @dataProvider unfinished_statuses + * + * @param string $status Central's status for the default row. + */ + public function test_an_unfinished_job_keeps_the_upload( $status ) { + $GLOBALS['rtgodam_stub']['post_meta']['rtgodam_transcript_source'] = Transcription::SOURCE_PENDING; + $captions = $this->default_row( null ); + $captions[0]['status'] = $status; + + Transcription::sync_cached_transcript( 7, $captions ); + + $meta = $GLOBALS['rtgodam_stub']['post_meta']; + $this->assertSame( self::UPLOAD, $meta['rtgodam_transcript_path'] ); + $this->assertSame( Transcription::SOURCE_PENDING, $meta['rtgodam_transcript_source'] ); + } + + /** + * Statuses of a default row with no file. + * + * @return array + */ + public function unfinished_statuses() { + return array( + 'still running' => array( 'Pending' ), + 'failed' => array( 'Failed' ), + ); + } + + /** + * Opening the Transcription tab after the job finished asks Central, and + * the ready default replaces the upload. + */ + public function test_the_finished_transcript_replaces_the_upload() { + $GLOBALS['rtgodam_stub']['post_meta']['rtgodam_transcript_source'] = Transcription::SOURCE_PENDING; + $this->central_answers( + array( + 'success' => true, + 'captions' => $this->default_row( self::CENTRAL ), + ) + ); + + $this->assertTrue( Transcription::refresh_cached_default( 7 ) ); + + $meta = $GLOBALS['rtgodam_stub']['post_meta']; + $this->assertSame( self::CENTRAL, $meta['rtgodam_transcript_path'] ); + $this->assertSame( Transcription::SOURCE_CENTRAL, $meta['rtgodam_transcript_source'] ); + } + + /** + * Central's copy written by the sync is marked, so the address check is + * only needed for transcripts stored before 2.8.0. + */ + public function test_the_sync_marks_central_copies() { + $GLOBALS['rtgodam_stub']['post_meta']['rtgodam_transcript_path'] = 'https://n8e0ka87m9.gdcdn.us/job/old.vtt'; + + Transcription::sync_cached_transcript( 7, $this->default_row( self::CENTRAL ) ); + + $this->assertSame( Transcription::SOURCE_CENTRAL, $GLOBALS['rtgodam_stub']['post_meta']['rtgodam_transcript_source'] ); + } +}