[grade=B] v1.9.1: YouTube links auto-render as video cards in beeps, replies, and quotes (click-to-play, nocookie embed)
This commit is contained in:
@@ -87,6 +87,7 @@ class Beep_Quotes {
|
||||
'<span class="beep-quoted-time">' . $time . '</span>' .
|
||||
'</div>' .
|
||||
'<div class="beep-quoted-text">' . $content . '</div>' .
|
||||
beep_render_youtube_card($quoted['content']) .
|
||||
'</div></div>';
|
||||
}
|
||||
|
||||
|
||||
@@ -353,6 +353,7 @@ function beep_render_post($post, $args = []) {
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<div class="beep-text"><?php echo $content; ?></div>
|
||||
<?php echo beep_render_youtube_card($post->post_content); ?>
|
||||
<?php if (!empty($beep_images)) : ?>
|
||||
<div class="beep-media-attachments">
|
||||
<?php echo Beep_Media::render_image_gallery($beep_images); ?>
|
||||
@@ -611,6 +612,7 @@ function beep_render_reply($comment, $all_comments = [], $depth = 0, $post_id =
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<div class="beep-text"><?php echo $content; ?></div>
|
||||
<?php echo beep_render_youtube_card($comment->comment_content); ?>
|
||||
|
||||
<?php if (!empty($reply_images)) : ?>
|
||||
<div class="beep-media-attachments">
|
||||
@@ -710,6 +712,34 @@ function beep_format_content($text) {
|
||||
return $text;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract the first YouTube video ID from a text string, if any.
|
||||
* Supports youtube.com/watch?v=, youtu.be/, shorts/, embed/, live/.
|
||||
* Returns the 11-char video ID or ''.
|
||||
*/
|
||||
function beep_youtube_id_from_text($text) {
|
||||
if (!preg_match_all('#(?<![\w.-])(?:https?://)?(?:www\.|m\.)?(?:youtube\.com/(?:watch\?[^#\s]*v=|shorts/|embed/|live/)|youtu\.be/)([A-Za-z0-9_-]{11})(?![A-Za-z0-9_-])#i', (string) $text, $m)) {
|
||||
return '';
|
||||
}
|
||||
return $m[1][0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Render a click-to-play YouTube card for the first YouTube link in $text.
|
||||
* Thumbnail (hqdefault) + play button; JS swaps in the iframe on click.
|
||||
* Returns '' when no YouTube link is present.
|
||||
*/
|
||||
function beep_render_youtube_card($text) {
|
||||
$vid = beep_youtube_id_from_text($text);
|
||||
if ($vid === '') {
|
||||
return '';
|
||||
}
|
||||
return '<div class="beep-yt-card" data-beep-yt="' . esc_attr($vid) . '" role="button" tabindex="0" aria-label="Play embedded YouTube video">'
|
||||
. '<img class="beep-yt-thumb" src="https://i.ytimg.com/vi/' . esc_attr($vid) . '/hqdefault.jpg" alt="" loading="lazy">'
|
||||
. '<span class="beep-yt-play"><svg viewBox="0 0 24 24" width="28" height="28" fill="currentColor" aria-hidden="true"><path d="M8 5v14l11-7z"/></svg></span>'
|
||||
. '</div>';
|
||||
}
|
||||
|
||||
function beep_relative_time($timestamp) {
|
||||
$now = time();
|
||||
$diff = $now - $timestamp;
|
||||
|
||||
Reference in New Issue
Block a user