/**
 * Email Template Tags
 *
 * @access      private
 * @since       1.0 
 * @return      string
*/
function edd_email_templage_tags($message, $payment_data, $payment_id)
{
    $user_info = maybe_unserialize($payment_data['user_info']);
    if (isset($user_info['id']) && $user_info['id'] > 0) {
        $user_data = get_userdata($user_info['id']);
        $name = $user_data->display_name;
    } elseif (isset($user_info['first_name'])) {
        $name = $user_info['first_name'];
    } else {
        $name = $user_info['email'];
    }
    $download_list = '<ul>';
    $downloads = maybe_unserialize($payment_data['downloads']);
    if ($downloads) {
        foreach (maybe_unserialize($payment_data['downloads']) as $download) {
            $id = isset($payment_data['cart_details']) ? $download['id'] : $download;
            $download_list .= '<li>' . get_the_title($id) . '<br/>';
            $download_list .= '<ul>';
            $price_id = isset($download['options']['price_id']) ? $download['options']['price_id'] : null;
            $files = edd_get_download_files($id, $price_id);
            if ($files) {
                foreach ($files as $filekey => $file) {
                    $download_list .= '<li>';
                    $file_url = edd_get_download_file_url($payment_data['key'], $payment_data['email'], $filekey, $id);
                    $download_list .= '<a href="' . $file_url . '">' . $file['name'] . '</a>';
                    $download_list .= '</li>';
                }
            }
            $download_list .= '</ul></li>';
        }
    }
    $download_list .= '</ul>';
    $price = edd_currency_filter($payment_data['amount']);
    $gateway = edd_get_gateway_checkout_label(get_post_meta($payment_id, '_edd_payment_gateway', true));
    $receipt_id = $payment_data['key'];
    $message = str_replace('{name}', $name, $message);
    $message = str_replace('{download_list}', $download_list, $message);
    $message = str_replace('{date}', date(get_option('date_format'), strtotime($payment_data['date'])), $message);
    $message = str_replace('{sitename}', get_bloginfo('name'), $message);
    $message = str_replace('{price}', $price, $message);
    $message = str_replace('{payment_method}', $gateway, $message);
    $message = str_replace('{receipt_id}', $receipt_id, $message);
    $message = apply_filters('edd_email_template_tags', $message, $payment_data);
    return $message;
}
</td>
			</tr>
		<?php 
}
?>

		<?php 
if ($edd_receipt_args['payment_method']) {
    ?>
			<tr>
				<td><strong><?php 
    _e('Payment Method', 'easy-digital-downloads');
    ?>
:</strong></td>
				<td><?php 
    echo edd_get_gateway_checkout_label(edd_get_payment_gateway($payment->ID));
    ?>
</td>
			</tr>
		<?php 
}
?>
		<?php 
if ($edd_receipt_args['date']) {
    ?>
		<tr>
			<td><strong><?php 
    _e('Date', 'easy-digital-downloads');
    ?>
:</strong></td>
			<td><?php 
/**
 * Email template tag: payment_method
 * The method of payment used for this purchase
 *
 * @since       1.0.0
 * @param       int $payment_id
 * @return      string gateway
 */
function edd_wallet_email_tag_payment_method($payment_id)
{
    if (get_post_type($payment_id) == 'edd_payment') {
        return edd_get_gateway_checkout_label(edd_get_payment_gateway($payment_id));
    } else {
        return '';
    }
}
/**
 * Email template tag: payment_method
 * The method of payment used for this purchase
 *
 * @param int $payment_id
 *
 * @return string gateway
 */
function edd_email_tag_payment_method($payment_id)
{
    return edd_get_gateway_checkout_label(edd_get_payment_gateway($payment_id));
}
/**
 * Email template tag: payment_method
 * The method of payment used for this purchase
 *
 * @param int $payment_id
 *
 * @return string gateway
 */
function edd_email_tag_payment_method($payment_id)
{
    $payment = new EDD_Payment($payment_id);
    return edd_get_gateway_checkout_label($payment->gateway);
}
/**
 * Email Template Tags
 *
 * @since 1.0
 *
 * @param string $message Message with the template tags
 * @param array $payment_data Payment Data
 * @param int $payment_id Payment ID
 *
 * @return string $message Fully formatted message
 */
function edd_email_template_tags($message, $payment_data, $payment_id)
{
    global $edd_options;
    $has_tags = strpos($message, '{') !== false;
    if (!$has_tags) {
        return $message;
    }
    $user_info = maybe_unserialize($payment_data['user_info']);
    $fullname = '';
    if (isset($user_info['id']) && $user_info['id'] > 0 && isset($user_info['first_name'])) {
        $user_data = get_userdata($user_info['id']);
        $name = $user_info['first_name'];
        $fullname = $user_info['first_name'] . ' ' . $user_info['last_name'];
        $username = $user_data->user_login;
    } elseif (isset($user_info['first_name'])) {
        $name = $user_info['first_name'];
        $fullname = $user_info['first_name'] . ' ' . $user_info['last_name'];
        $username = $user_info['first_name'];
    } else {
        $name = $user_info['email'];
        $username = $user_info['email'];
    }
    $file_urls = '';
    $download_list = '<ul>';
    $cart_items = edd_get_payment_meta_cart_details($payment_id);
    if ($cart_items) {
        $show_names = apply_filters('edd_email_show_names', true);
        foreach ($cart_items as $item) {
            if (edd_use_skus()) {
                $sku = edd_get_download_sku($item['id']);
            }
            $price_id = edd_get_cart_item_price_id($item);
            if ($show_names) {
                $title = get_the_title($item['id']);
                if (!empty($sku)) {
                    $title .= "&nbsp;&ndash;&nbsp;" . __('SKU', 'edd') . ': ' . $sku;
                }
                if ($price_id !== false) {
                    $title .= "&nbsp;&ndash;&nbsp;" . edd_get_price_option_name($item['id'], $price_id);
                }
                $download_list .= '<li>' . apply_filters('edd_email_receipt_download_title', $title, $item['id'], $price_id) . '<br/>';
                $download_list .= '<ul>';
            }
            $files = edd_get_download_files($item['id'], $price_id);
            if ($files) {
                foreach ($files as $filekey => $file) {
                    $download_list .= '<li>';
                    $file_url = edd_get_download_file_url($payment_data['key'], $payment_data['email'], $filekey, $item['id'], $price_id);
                    $download_list .= '<a href="' . esc_url($file_url) . '">' . $file['name'] . '</a>';
                    $download_list .= '</li>';
                    $file_urls .= esc_html($file_url) . '<br/>';
                }
            }
            if ($show_names) {
                $download_list .= '</ul>';
            }
            if ('' != edd_get_product_notes($item['id'])) {
                $download_list .= ' &mdash; <small>' . edd_get_product_notes($item['id']) . '</small>';
            }
            if ($show_names) {
                $download_list .= '</li>';
            }
        }
    }
    $download_list .= '</ul>';
    $subtotal = isset($payment_data['subtotal']) ? $payment_data['subtotal'] : $payment_data['amount'];
    $subtotal = edd_currency_filter(edd_format_amount($subtotal));
    $tax = isset($payment_data['tax']) ? $payment_data['tax'] : 0;
    $tax = edd_currency_filter(edd_format_amount($tax));
    $price = edd_currency_filter(edd_format_amount($payment_data['amount']));
    $gateway = edd_get_gateway_checkout_label(get_post_meta($payment_id, '_edd_payment_gateway', true));
    $receipt_id = $payment_data['key'];
    $message = str_replace('{name}', $name, $message);
    $message = str_replace('{fullname}', $fullname, $message);
    $message = str_replace('{username}', $username, $message);
    $message = str_replace('{download_list}', $download_list, $message);
    $message = str_replace('{file_urls}', $file_urls, $message);
    $message = str_replace('{date}', date_i18n(get_option('date_format'), strtotime($payment_data['date'])), $message);
    $message = str_replace('{sitename}', get_bloginfo('name'), $message);
    $message = str_replace('{subtotal}', $subtotal, $message);
    $message = str_replace('{tax}', $tax, $message);
    $message = str_replace('{price}', $price, $message);
    $message = str_replace('{payment_method}', $gateway, $message);
    $message = str_replace('{receipt_id}', $receipt_id, $message);
    $message = str_replace('{payment_id}', $payment_id, $message);
    $message = str_replace('{receipt_link}', sprintf(__('%1$sView it in your browser.%2$s', 'edd'), '<a href="' . add_query_arg(array('purchase_key' => $receipt_id, 'edd_action' => 'view_receipt'), home_url()) . '">', '</a>'), $message);
    $message = apply_filters('edd_email_template_tags', $message, $payment_data, $payment_id);
    return $message;
}
/**
 * Email Template Tags
 *
 * @param string $message
 * @param array  $payment_data
 * @param int    $payment_id
 *
 * @access private
 * @since 1.0
 * @return string
 */
function edd_email_template_tags($message, $payment_data, $payment_id)
{
    $user_info = maybe_unserialize($payment_data['user_info']);
    $fullname = '';
    if (isset($user_info['id']) && $user_info['id'] > 0 && isset($user_info['first_name'])) {
        $user_data = get_userdata($user_info['id']);
        $name = $user_info['first_name'];
        $fullname = $user_info['first_name'] . ' ' . $user_info['last_name'];
        $username = $user_data->user_login;
    } elseif (isset($user_info['first_name'])) {
        $name = $user_info['first_name'];
        $fullname = $user_info['first_name'] . ' ' . $user_info['last_name'];
        $username = $user_info['first_name'];
    } else {
        $name = $user_info['email'];
        $username = $user_info['email'];
    }
    $file_urls = '';
    $download_list = '<ul>';
    $downloads = edd_get_payment_meta_downloads($payment_id);
    if ($downloads) {
        $show_names = apply_filters('edd_email_show_names', true);
        foreach ($downloads as $download) {
            $id = isset($payment_data['cart_details']) ? $download['id'] : $download;
            if ($show_names) {
                $download_list .= '<li>' . get_the_title($id) . '<br/>';
                $download_list .= '<ul>';
            }
            $price_id = isset($download['options']['price_id']) ? $download['options']['price_id'] : null;
            $files = edd_get_download_files($id, $price_id);
            if ($files) {
                foreach ($files as $filekey => $file) {
                    $download_list .= '<li>';
                    $file_url = edd_get_download_file_url($payment_data['key'], $payment_data['email'], $filekey, $id);
                    $download_list .= '<a href="' . esc_url($file_url) . '">' . $file['name'] . '</a>';
                    $download_list .= '</li>';
                    $file_urls .= esc_html($file_url) . '<br/>';
                }
            }
            if ($show_names) {
                $download_list .= '</ul>';
            }
            if ('' != edd_get_product_notes($id)) {
                $download_list .= ' &mdash; <small>' . edd_get_product_notes($id) . '</small>';
            }
            if ($show_names) {
                $download_list .= '</li>';
            }
        }
    }
    $download_list .= '</ul>';
    $subtotal = isset($payment_data['subtotal']) ? $payment_data['subtotal'] : $payment_data['amount'];
    $subtotal = edd_currency_filter(edd_format_amount($subtotal));
    $tax = isset($payment_data['tax']) ? $payment_data['tax'] : 0;
    $tax = edd_currency_filter(edd_format_amount($tax));
    $price = edd_currency_filter(edd_format_amount($payment_data['amount']));
    $gateway = edd_get_gateway_checkout_label(get_post_meta($payment_id, '_edd_payment_gateway', true));
    $receipt_id = $payment_data['key'];
    $message = str_replace('{name}', $name, $message);
    $message = str_replace('{fullname}', $fullname, $message);
    $message = str_replace('{username}', $username, $message);
    $message = str_replace('{download_list}', $download_list, $message);
    $message = str_replace('{file_urls}', $file_urls, $message);
    $message = str_replace('{date}', date_i18n(get_option('date_format'), strtotime($payment_data['date'])), $message);
    $message = str_replace('{sitename}', get_bloginfo('name'), $message);
    $message = str_replace('{subtotal}', $subtotal, $message);
    $message = str_replace('{tax}', $tax, $message);
    $message = str_replace('{price}', $price, $message);
    $message = str_replace('{payment_method}', $gateway, $message);
    $message = str_replace('{receipt_id}', $receipt_id, $message);
    $message = apply_filters('edd_email_template_tags', $message, $payment_data, $payment_id);
    return $message;
}