Skip to content
Open
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
195 changes: 195 additions & 0 deletions inc/classes/class-embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ class Embed {
*/
const LLMS_AV_PROG_AUTO_PLAY_OPTION = 'llms_av_prog_auto_play';

/**
* Attachment IDs found for GoDAM job IDs during this request; 0 when none.
*
* @since n.e.x.t
*
* @var array<string, int>
*/
private $attachment_ids_by_job = array();

/**
* Construct method.
*/
Expand All @@ -45,6 +54,7 @@ protected function __construct() {
*/
public function setup_hooks() {
add_action( 'init', array( $this, 'register_oembed_provider' ) );
add_action( 'init', array( $this, 'register_local_embed_handler' ) );
add_filter( 'pre_oembed_result', array( $this, 'pre_oembed_result' ), 10, 2 );
}

Expand Down Expand Up @@ -80,6 +90,15 @@ public function pre_oembed_result( $data, string $url ) {
return null;
}

// A video that's in this site's media library plays in the site's own
// player, so it doesn't depend on GoDAM's oEmbed API, which only
// embeds videos shared publicly.
$local_html = $this->get_local_embed_html( $url );

if ( '' !== $local_html ) {
return $local_html;
}

$embed_url = RTGODAM_API_BASE . '/api/method/godam_core.api.oembed.get_oembed';

$response = wp_remote_post(
Expand Down Expand Up @@ -119,6 +138,182 @@ public function pre_oembed_result( $data, string $url ) {
return null;
}

/**
* Register an embed handler for GoDAM share URLs of videos in the media library.
*
* WordPress runs embed handlers before its oEmbed cache when it embeds a
* URL in post content (and when ACF's oEmbed field falls back to it), so
* the player renders, with its scripts, on every view. The oEmbed HTML
* cache would keep the markup but skip the scripts.
*
* @since n.e.x.t
*
* @return void
*/
public function register_local_embed_handler() {
$host = wp_parse_url( RTGODAM_API_BASE, PHP_URL_HOST );

if ( ! is_string( $host ) || '' === $host ) {
return;
}

wp_embed_register_handler(
'godam-local-video',
'#^https?://' . preg_quote( $host, '#' ) . '/web/video/[A-Za-z0-9_-]+/?(?:[?\#].*)?$#i',
array( $this, 'render_local_embed' ),
9
);
}

/**
* Embed handler callback: render a GoDAM share URL with the site's own player.
*
* @since n.e.x.t
*
* @param array $matches The regex matches.
* @param array $attr Embed attributes.
* @param string $url The URL being embedded.
*
* @return string|false The player HTML, or false to let WordPress try oEmbed.
*/
public function render_local_embed( $matches, $attr, $url ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed -- signature set by wp_embed_register_handler().
$html = $this->get_local_embed_html( (string) $url );

return '' === $html ? false : $html;
}

/**
* Render a GoDAM share URL with the site's own player, when its video is in the media library.
*
* The share URL is RTGODAM_API_BASE/web/video/<job ID>. The Vimeo
* migration writes these into ACF fields; they can also be pasted into
* content.
*
* @since n.e.x.t
*
* @param string $url GoDAM share URL.
*
* @return string The player HTML, or '' when the URL isn't a share URL of a video in the media library.
*/
public function get_local_embed_html( string $url ): string {
if ( ! $this->is_godam_url( $url ) ) {
return '';
}

$job_id = self::get_share_url_job_id( $url );

if ( '' === $job_id ) {
return '';
}

$attachment_id = $this->find_attachment_id_by_job_id( $job_id );

if ( $attachment_id <= 0 ) {
return '';
}

/**
* Filters whether a GoDAM share URL renders with the site's own player.
*
* Return false to embed the video through GoDAM's oEmbed API instead,
* as LifterLMS lessons do: their player integration talks to the
* embedded iframe.
*
* @since n.e.x.t
*
* @param bool $render_locally Whether to render with the site's own player. Default true.
* @param string $url The GoDAM share URL.
* @param int $attachment_id ID of the video in the media library.
*/
if ( ! apply_filters( 'rtgodam_render_share_url_locally', true, $url, $attachment_id ) ) {
return '';
}

return (string) do_shortcode( sprintf( '[godam_video id="%d"]', $attachment_id ) );
}

/**
* Read the GoDAM job ID from a share URL.
*
* @since n.e.x.t
*
* @param string $url GoDAM share URL, RTGODAM_API_BASE/web/video/<job ID>.
*
* @return string The job ID, or '' when the URL has none.
*/
public static function get_share_url_job_id( string $url ): string {
$path = wp_parse_url( $url, PHP_URL_PATH );

if ( is_string( $path ) && preg_match( '#^/web/video/([A-Za-z0-9_-]+)/?$#', $path, $matches ) ) {
return $matches[1];
}

return '';
}

/**
* Find the media library attachment for a GoDAM job ID.
*
* Matches transcoded uploads (`rtgodam_transcoding_job_id`) and media
* imported from GoDAM, including migrated Vimeo videos
* (`_godam_original_id`).
*
* @since n.e.x.t
*
* @param string $job_id GoDAM job ID.
*
* @return int Attachment ID, or 0 when none.
*/
private function find_attachment_id_by_job_id( string $job_id ): int {
if ( isset( $this->attachment_ids_by_job[ $job_id ] ) ) {
return $this->attachment_ids_by_job[ $job_id ];
}

global $wpdb;

if ( ! is_object( $wpdb ) ) {
return 0;
}

/**
* Fires before looking the attachment up, so integrations that
* centralize media on another site can switch context first.
*
* @since 2.2.0
*/
do_action( 'rtgodam_before_attachment_lookup' );

try {
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- cached per request in $attachment_ids_by_job.
$attachment_id = (int) $wpdb->get_var(
$wpdb->prepare(
"SELECT pm.post_id
FROM {$wpdb->postmeta} pm
INNER JOIN {$wpdb->posts} p ON p.ID = pm.post_id
WHERE pm.meta_key IN ('rtgodam_transcoding_job_id', '_godam_original_id')
AND pm.meta_value = %s
AND p.post_type = 'attachment'
AND p.post_status <> 'trash'
ORDER BY pm.post_id ASC
LIMIT 1",
$job_id
)
);
} finally {
/**
* Fires after the lookup, so integrations can restore the
* context switched in `rtgodam_before_attachment_lookup`.
*
* @since 2.2.0
*/
do_action( 'rtgodam_after_attachment_lookup' );
}

$this->attachment_ids_by_job[ $job_id ] = $attachment_id;

return $attachment_id;
}

/**
* Check whether a URL is on GoDAM's own host.
*
Expand Down
23 changes: 23 additions & 0 deletions inc/classes/lifter-lms/class-lifter-lms.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,29 @@ private function setup_hooks() {
add_filter( 'godam_player_block_attributes', array( $this, 'modify_godam_player_block_attributes' ) );

add_filter( 'lifterlms_available_integrations', array( $this, 'add_godam_integration' ) );

add_filter( 'rtgodam_render_share_url_locally', array( $this, 'keep_share_url_embeds_in_lifterlms' ) );
}

/**
* Keep GoDAM share URLs embedded through GoDAM's oEmbed API on LifterLMS pages.
*
* The LifterLMS Advanced Videos integration tracks progress and autoplays
* the next lesson by talking to the embedded GoDAM iframe, so a share URL
* on a LifterLMS page is not swapped for the site's own player.
*
* @since n.e.x.t
*
* @param bool $render_locally Whether to render the share URL with the site's own player.
*
* @return bool
*/
public function keep_share_url_embeds_in_lifterlms( $render_locally ) {
if ( $this->is_lifterlms_active() && $this->is_lifterlms_advanced_video_active() && $this->is_lifterlms_content() ) {
return false;
}

return $render_locally;
}

/**
Expand Down
Loading
Loading