Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion admin/class-rtgodam-transcoder-rest-routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions inc/classes/class-seo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand All @@ -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 );
}

Expand Down
115 changes: 98 additions & 17 deletions inc/classes/rest-api/class-transcription.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,37 @@ 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
*
* @var string
*/
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.
Expand Down Expand Up @@ -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 );
}
Expand Down Expand Up @@ -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 );
Comment thread
subodhr258 marked this conversation as resolved.
Resolved
}

/**
* 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
Expand Down Expand Up @@ -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 );
}
}
}
}
Expand Down Expand Up @@ -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 );
Expand Down
8 changes: 8 additions & 0 deletions inc/helpers/custom-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
}

Expand Down Expand Up @@ -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`.
Expand Down
4 changes: 4 additions & 0 deletions inc/templates/godam-player.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';

Expand Down
Loading
Loading