Add voice message support: audio upload (MP3/OGG/WAV/WebM/M4A), inline audio player in posts, voice note button + preview in composer

This commit is contained in:
Krystie
2026-05-18 11:57:48 -07:00
parent 07ac9f112d
commit 9f91b0c544
5 changed files with 340 additions and 9 deletions
+95 -1
View File
@@ -217,4 +217,98 @@ class Beep_Media {
delete_post_meta($post_id, self::META_KEY_IMAGES);
delete_post_meta($post_id, self::META_KEY_VIDEO);
}
}
// Voice note support
private static $audio_types = ['audio/mpeg', 'audio/ogg', 'audio/wav', 'audio/webm', 'audio/mp4', 'audio/x-m4a'];
private static $max_audio_size = 20 * 1024 * 1024; // 20 MB
public static function handle_voice_upload($post_id, $file) {
if (!is_array($file) || $file['error'] !== UPLOAD_ERR_OK) {
return new WP_Error('upload_error', 'Upload failed.');
}
if ($file['size'] > self::$max_audio_size) {
return new WP_Error('file_too_large', 'Voice note exceeds 20 MB limit.');
}
$mime = mime_content_type($file['tmp_name']);
if (!in_array($mime, self::$audio_types, true)) {
return new WP_Error('invalid_type', 'Only MP3, OGG, WAV, WebM, and M4A audio are allowed.');
}
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/media.php';
$file_return = wp_handle_upload($file, [
'test_form' => false,
'mimes' => [
'mp3' => 'audio/mpeg',
'ogg' => 'audio/ogg',
'wav' => 'audio/wav',
'webm' => 'audio/webm',
'm4a' => 'audio/mp4',
],
]);
if (isset($file_return['error'])) {
return new WP_Error('upload_error', $file_return['error']);
}
$attachment = [
'post_mime_type' => $file_return['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($file_return['file'])),
'post_content' => '',
'post_status' => 'inherit',
'guid' => $file_return['url'],
];
$attachment_id = wp_insert_attachment($attachment, $file_return['file'], $post_id);
if (is_wp_error($attachment_id)) {
return $attachment_id;
}
$attach_data = wp_generate_attachment_metadata($attachment_id, $file_return['file']);
wp_update_attachment_metadata($attachment_id, $attach_data);
update_post_meta($post_id, 'beep_voice_url', wp_get_attachment_url($attachment_id));
update_post_meta($post_id, 'beep_voice_id', $attachment_id);
return [
'id' => $attachment_id,
'url' => wp_get_attachment_url($attachment_id),
];
}
public static function get_voice_url($post_id) {
return esc_url_raw(get_post_meta($post_id, 'beep_voice_url', true));
}
public static function remove_voice($post_id) {
$id = (int) get_post_meta($post_id, 'beep_voice_id', true);
if ($id) {
wp_delete_attachment($id, true);
}
delete_post_meta($post_id, 'beep_voice_url');
delete_post_meta($post_id, 'beep_voice_id');
}
public static function render_voice_note($post_id) {
$url = self::get_voice_url($post_id);
if (!$url) return '';
$duration = '';
$meta = get_post_meta($post_id, 'beep_voice_id', true);
if ($meta) {
$metadata = wp_get_attachment_metadata($meta);
if ($metadata && isset($metadata['length_formatted'])) {
$duration = $metadata['length_formatted'];
}
}
$dur_html = $duration ? '<span class="beep-voice-duration">' . esc_html($duration) . '</span>' : '';
return '<div class="beep-voice-note">' .
'<audio controls preload="none">' .
'<source src="' . esc_url($url) . '">' .
'Your browser does not support audio playback.' .
'</audio>' .
'<div class="beep-voice-label"><svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor"><path d="M12 14c1.66 0 3-1.34 3-3V5c0-1.66-1.34-3-3-3S9 3.34 9 5v6c0 1.66 1.34 3 3 3zm-1-9c0-.55.45-1 1-1s1 .45 1 1v6c0 .55-.45 1-1 1s-1-.45-1-1V5zm6 6c0 2.76-2.24 5-5 5s-5-2.24-5-5H5c0 3.53 2.61 6.43 6 6.92V21h2v-3.08C16.39 17.43 19 14.53 19 11h-2z"/></svg>Voice note</div>' .
$dur_html .
'</div>';
}
}