/**
 * Delete a Playlist from the database
 *
 * @param int $id
 */
function wolf_delete_playlist($id = null)
{
    $songs = array();
    if ($id) {
        $id = intval($id);
        global $wpdb;
        $wolf_jplayer_playlists_table = $wpdb->prefix . 'wolf_jplayer_playlists';
        $wolf_jplayer_table = $wpdb->prefix . 'wolf_jplayer';
        $playlist = $wpdb->get_row("SELECT * FROM {$wolf_jplayer_playlists_table} WHERE id = {$id}");
        $songs = $wpdb->get_results("SELECT * FROM {$wolf_jplayer_table} WHERE playlist_id = {$id}");
        if ($playlist) {
            $wpdb->query("DELETE FROM {$wolf_jplayer_table} WHERE playlist_id = {$id}");
            $wpdb->query("DELETE FROM {$wolf_jplayer_playlists_table} WHERE id = {$id}");
            wolf_jpayer_admin_notices(__('Playlist deleted', 'wolf'), 'updated');
        } else {
            wolf_jpayer_admin_notices(__('The playlist you are trying to delete does not exist.', 'wolf'), 'error');
        }
    }
    // end if id
}
/**
 * Manage song
 *
 * @param int $playlist_id
 * @param int $sing_id
 */
function wolf_manage_song($playlist_id = 0, $song_id = null)
{
    global $wpdb;
    $wolf_jplayer_table = $wpdb->prefix . 'wolf_jplayer';
    /* If no errors
    	----------------------------------------------*/
    if (!wolf_error($song_id)) {
        /* All good, proceed  */
        $name = sanitize_text_field($_POST['song_name']);
        $artist = sanitize_text_field($_POST['artist']);
        $itunes = esc_url($_POST['itunes']);
        $amazon = esc_url($_POST['amazon']);
        $buy = esc_url($_POST['buy']);
        $free = !empty($_POST['free']) ? sanitize_title($_POST['free']) : null;
        $mp3_url = esc_url($_POST['mp3']);
        $ogg_url = isset($_POST['ogg']) ? esc_url($_POST['ogg']) : null;
        $poster = esc_url($_POST['poster']);
        if (!$song_id) {
            /* Insertion */
            $data = array('name' => $name, 'mp3' => $mp3_url, 'ogg' => $ogg_url, 'poster' => $poster, 'artist' => $artist, 'itunes' => $itunes, 'amazon' => $amazon, 'buy' => $buy, 'free' => $free, 'position' => 0, 'playlist_id' => $playlist_id);
            $format = array('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%d');
            if ($wpdb->insert($wolf_jplayer_table, $data, $format)) {
                $notice_type = 'updated';
                $confirm = __('Your song has been added to your playlist.', 'wolf');
            } else {
                $notice_type = 'error';
                $confirm = __('Your song could not be added to your database.', 'wolf');
            }
            wolf_jpayer_admin_notices($confirm, $notice_type);
        } else {
            // if update
            /* Update */
            $data = array('name' => $name, 'ogg' => $ogg_url, 'mp3' => $mp3_url, 'poster' => $poster, 'artist' => $artist, 'itunes' => $itunes, 'amazon' => $amazon, 'buy' => $buy, 'free' => $free);
            $format = array('%s');
            $conditions = array('id' => $song_id);
            $wpdb->update($wolf_jplayer_table, $data, $conditions, $format, array('%d'));
            $notice_type = 'updated';
            $confirm = __('Your song has been updated.', 'wolf');
            wolf_jpayer_admin_notices($confirm, $notice_type);
        }
        // end if song id
    }
    // end if error
}