__('侧栏/流体区嵌入视频(共享 aksrt-video 播放器)。', 'aksrt'),
'classname' => 'widget_aksrt_video_player',
]
);
}
public function widget($args, $instance): void {
$title = (string) ($instance['title'] ?? '');
$video_url = trim((string) ($instance['video_url'] ?? $instance['url'] ?? ''));
$poster = trim((string) ($instance['poster'] ?? $instance['pic'] ?? ''));
$autoplay = !empty($instance['autoplay']);
$loop = !empty($instance['loop']);
$muted = $autoplay || !empty($instance['muted']);
$hide_controller = !empty($instance['hide_controller']);
$scale = max(20, min(100, (int) ($instance['scale_height'] ?? 56)));
$aspect = (string) ($instance['aspect_ratio'] ?? '');
if ($video_url === '') {
if (current_user_can('manage_options')) {
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo '
' . esc_html__('视频播放器:', 'aksrt') . '' .
esc_html__('请配置视频 URL 或移出该模块', 'aksrt') . '
';
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
return;
}
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
if ($title !== '') {
echo $args['before_title'] . esc_html($title) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
if ($aspect !== '' && str_contains($aspect, ':')) {
$parts = explode(':', $aspect);
if (count($parts) === 2 && (int) $parts[0] > 0) {
$scale = (int) round(((int) $parts[1] / (int) $parts[0]) * 100);
}
}
$embed = $this->get_embed_url($video_url);
$cls = 'aksrt-video-widget relative-h radius8' . ($hide_controller ? ' controller-hide' : '');
echo '';
if ($embed !== '') {
$src = $embed;
if ($autoplay) {
$src = add_query_arg(['autoplay' => '1', 'mute' => '1'], $src);
}
echo '
';
} else {
// Shared player stack (assets/js/video-player.js).
$type = 'mp4';
if (preg_match('/\.m3u8(\?|$)/i', $video_url)) {
$type = 'hls';
} elseif (preg_match('/\.flv(\?|$)/i', $video_url)) {
$type = 'flv';
}
printf(
'
',
esc_url($video_url),
esc_attr($type),
$poster !== '' ? ' data-poster="' . esc_url($poster) . '"' : '',
$autoplay ? ' data-autoplay="true"' : '',
$loop ? ' data-loop="true"' : '',
$muted ? ' data-muted="true"' : ''
);
}
echo '
';
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
private function get_embed_url(string $url): string {
if (preg_match('#(?:youtube\.com/watch\?v=|youtu\.be/)([A-Za-z0-9_-]+)#', $url, $m)) {
return 'https://www.youtube.com/embed/' . rawurlencode($m[1]);
}
if (preg_match('#bilibili\.com/video/(BV[\w]+)#', $url, $m)) {
return 'https://player.bilibili.com/player.html?bvid=' . rawurlencode($m[1]) . '&high_quality=1';
}
if (preg_match('#v\.qq\.com/.+?/([A-Za-z0-9]+)\.html#', $url, $m)) {
return 'https://v.qq.com/txp/iframe/player.html?vid=' . rawurlencode($m[1]);
}
return '';
}
public function form($instance): void {
?>
sanitize_text_field((string) ($new_instance['title'] ?? '')),
'video_url' => esc_url_raw(trim((string) ($new_instance['video_url'] ?? ''))),
'poster' => esc_url_raw(trim((string) ($new_instance['poster'] ?? ''))),
'scale_height' => max(20, min(100, (int) ($new_instance['scale_height'] ?? 56))),
'autoplay' => !empty($new_instance['autoplay']) ? 1 : 0,
'loop' => !empty($new_instance['loop']) ? 1 : 0,
'hide_controller' => !empty($new_instance['hide_controller']) ? 1 : 0,
];
}
}