function file($postid, $colabFileUrl) { $waveCargada = getpostmeta($postid,…
- User
function file(colabFileUrl) { post_id, 'waveCargada', true);
- Assistant
function file(colabFileUrl) { post_id, 'waveCargada', true);
ob_start();
?>
Esto va a recibir el post id, y colabfileurl que puede ser un archivo o un audio, necesito que detecte si un archivo o un audio,
si es un audio debería generar un waveform
debe sacar la id del archivo porque se genera asi https://2upra.com/wp-content/uploads/2024/09/AHS-PERC-68.wav y si lo puedes optimizar para que guarde la id en la meta del post porque inicialmente no se genera asi no tiene que buscar la id cada vez que se genera el post creo, audioColab debe ser la id de ese archivo
asi se generan mis wave form
<div id="waveform-<?php echo $post_id; ?>"
class="waveform-container without-image"
postIDWave="<?php echo $post_id; ?>"
data-audio-url="<?php echo site_url('?custom-audio-stream=1&audio_id=' . $audioColab); ?>"
data-wave-cargada="<?php echo $waveCargada ? 'true' : 'false'; ?>">
<div class="waveform-background" style="background-image: url('<?php echo $wave; ?>');"></div>
<div class="waveform-message"></div>
<div class="waveform-loading" style="display: none;">Cargando...</div>
</div>
y si es un archivo, simplemente debe mostrar el nombre del archivo
To achieve the functionality you're describing, we'll first need to determine whether the provided URL is pointing to an audio file. Then, if it's an audio file, we generate a waveform and store the audio file's ID in the post's metadata. If it's a regular file, we'll just display its name. Here's how you could implement this logic:
function file($post_id, $colabFileUrl)
{
// Get the metadata associated with the post ID
$waveCargada = get_post_meta($post_id, 'waveCargada', true);
$audioColab = get_post_meta($post_id, 'audioColab', true);
// Check if the URL is an audio file
$isAudio = false;
$fileParts = pathinfo($colabFileUrl);
if (isset($fileParts['extension'])) {
$audioExtensions = ['mp3', 'wav', 'ogg'];
$isAudio = in_array(strtolower($fileParts['extension']), $audioExtensions);
}
ob_start();
// Generate waveform if it's an audio file
if ($isAudio) {
if (!$audioColab) {
// Assume you have a function to retrieve or set the attachment ID
$audioColab = get_audio_id_from_url($colabFileUrl);
update_post_meta($post_id, 'audioColab', $audioColab);
}
?>
<div id="waveform-<?php echo $post_id; ?>"
class="waveform-container without-image"
postIDWave="<?php echo $post_id; ?>"
data-audio-url="<?php echo site_url('?custom-audio-stream=1&audio_id=' . $audioColab); ?>"
data-wave-cargada="<?php echo $waveCargada ? 'true' : 'false'; ?>">
<div class="waveform-background" style="background-image: url('<?php echo $waveCargada; ?>');"></div>
<div class="waveform-message"></div>
<div class="waveform-loading" style="display: none;">Cargando...</div>
</div>
<?php
} else {
// Simply output the file name if it's not an audio file
echo 'File Name: ' . $fileParts['basename'];
}
return ob_get_clean();
}
// A placeholder function to get attachment ID from URL, implement as needed
function get_audio_id_from_url($url) {
// Your logic to extract the ID from URL, possibly checking attachment post types etc.
return $some_id;
}
File Type Detection: We detect whether the input URL is an audio file by checking its extension against a list of known audio file extensions (mp3, wav, ogg).
Meta Management: If the file is an audio, we try to get its ID from post metadata. If it doesn't exist, you should define logic in get_audio_id_from_url() that obtains or sets the ID based on your system's storage mechanism.
Waveform Handling: If the file is audio, the function generates the waveform HTML with the appropriate data attributes.
File Naming: For non-audio files, it outputs the file name derived from $fileParts.
Waveform Background: Make sure that $waveCargada has the background image URL if needed.
Ensure you have logic implemented in get_audio_id_from_url() to actually handle the retrieval of an attachment ID from a URL, as this code assumes an existing mechanism for that. Adjust according to your WordPress setup and file storage logic.
Not yet included in a published release.