/**
 * Email the download link(s) and payment confirmation to the buyer in a
 * customizable Purchase Receipt
 *
 * @since 1.0
 * @param int $payment_id Payment ID
 * @param bool $admin_notice Whether to send the admin email notification or not (default: true)
 * @return void
 */
function edd_email_purchase_receipt($payment_id, $admin_notice = true)
{
    global $edd_options;
    $payment_data = edd_get_payment_meta($payment_id);
    $user_info = maybe_unserialize($payment_data['user_info']);
    $email = edd_get_payment_user_email($payment_id);
    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']) && isset($user_info['last_name'])) {
        $name = $user_info['first_name'] . ' ' . $user_info['last_name'];
    } else {
        $name = $email;
    }
    $message = edd_get_email_body_header();
    $message .= edd_get_email_body_content($payment_id, $payment_data);
    $message .= edd_get_email_body_footer();
    $from_name = isset($edd_options['from_name']) ? $edd_options['from_name'] : get_bloginfo('name');
    $from_email = isset($edd_options['from_email']) ? $edd_options['from_email'] : get_option('admin_email');
    $subject = apply_filters('edd_purchase_subject', isset($edd_options['purchase_subject']) ? trim($edd_options['purchase_subject']) : __('Purchase Receipt', 'edd'), $payment_id);
    $subject = edd_email_template_tags($subject, $payment_data, $payment_id);
    $headers = "From: " . stripslashes_deep(html_entity_decode($from_name, ENT_COMPAT, 'UTF-8')) . " <{$from_email}>\r\n";
    $headers .= "Reply-To: " . $from_email . "\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=utf-8\r\n";
    $headers = apply_filters('edd_receipt_headers', $headers, $payment_id, $payment_data);
    // Allow add-ons to add file attachments
    $attachments = apply_filters('edd_receipt_attachments', array(), $payment_id, $payment_data);
    wp_mail($email, $subject, $message, $headers, $attachments);
    if ($admin_notice && !edd_admin_notices_disabled()) {
        do_action('edd_admin_sale_notice', $payment_id, $payment_data);
    }
}
/**
 * Email Download Purchase Receipt
 *
 * Email the download link(s) and payment confirmation to the buyer.
 *
 * @access      private
 * @since       1.0
 * @return      void
*/
function edd_email_purchase_receipt($payment_id, $admin_notice = true)
{
    global $edd_options;
    $payment_data = edd_get_payment_meta($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']) && isset($user_info['last_name'])) {
        $name = $user_info['first_name'] . ' ' . $user_info['last_name'];
    } else {
        $name = $user_info['email'];
    }
    $message = edd_get_email_body_header();
    $message .= edd_get_email_body_content($payment_id, $payment_data);
    $message .= edd_get_email_body_footer();
    $from_name = isset($edd_options['from_name']) ? $edd_options['from_name'] : get_bloginfo('name');
    $from_email = isset($edd_options['from_email']) ? $edd_options['from_email'] : get_option('admin_email');
    $subject = isset($edd_options['purchase_subject']) && strlen(trim($edd_options['purchase_subject'])) > 0 ? edd_email_template_tags($edd_options['purchase_subject'], $payment_data, $payment_id) : __('Purchase Receipt', 'edd');
    $headers = "From: " . stripslashes_deep(html_entity_decode($from_name, ENT_COMPAT, 'UTF-8')) . " <{$from_email}>\r\n";
    $headers .= "Reply-To: " . $from_email . "\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=utf-8\r\n";
    // allow add-ons to add file attachments
    $attachments = apply_filters('edd_receipt_attachments', array(), $payment_id, $payment_data);
    wp_mail($payment_data['email'], $subject, $message, $headers, $attachments);
    if ($admin_notice) {
        /* send an email notification to the admin */
        $admin_email = edd_get_admin_notice_emails();
        $admin_subject = apply_filters('edd_admin_purchase_notification_subject', __('New download purchase', 'edd'), $payment_id, $payment_data);
        $admin_message = __('Hello', 'edd') . "\n\n" . sprintf(__('A %s purchase has been made', 'edd'), edd_get_label_plural()) . ".\n\n";
        $admin_message .= sprintf(__('%s sold:', 'edd'), edd_get_label_plural()) . "\n\n";
        $download_list = '';
        $downloads = maybe_unserialize($payment_data['downloads']);
        if (is_array($downloads)) {
            foreach ($downloads as $download) {
                $id = isset($payment_data['cart_details']) ? $download['id'] : $download;
                $download_list .= html_entity_decode(get_the_title($id), ENT_COMPAT, 'UTF-8') . "\n";
            }
        }
        $gateway = edd_get_gateway_admin_label(get_post_meta($payment_id, '_edd_payment_gateway', true));
        $admin_message .= $download_list . "\n";
        $admin_message .= __('Purchased by: ', 'edd') . " " . html_entity_decode($name, ENT_COMPAT, 'UTF-8') . "\n";
        $admin_message .= __('Amount: ', 'edd') . " " . html_entity_decode(edd_currency_filter(edd_format_amount($payment_data['amount'])), ENT_COMPAT, 'UTF-8') . "\n\n";
        $admin_message .= __('Payment Method: ', 'edd') . " " . $gateway . "\n\n";
        $admin_message .= __('Thank you', 'edd');
        $admin_message = apply_filters('edd_admin_purchase_notification', $admin_message, $payment_id, $payment_data);
        $admin_headers = apply_filters('edd_admin_purchase_notification_headers', array(), $payment_id, $payment_data);
        $admin_attachments = apply_filters('edd_admin_purchase_notification_attachments', array(), $payment_id, $payment_data);
        wp_mail($admin_email, $admin_subject, $admin_message, $admin_headers, $admin_attachments);
    }
}
/**
 * Email the download link(s) and payment confirmation to the admin accounts for testing.
 *
 * @since 1.5
 * @global $edd_options Array of all the EDD Options
 * @return void
 */
function edd_email_test_purchase_receipt()
{
    $from_name = edd_get_option('from_name', wp_specialchars_decode(get_bloginfo('name'), ENT_QUOTES));
    $from_name = apply_filters('edd_purchase_from_name', $from_name, 0, array());
    $from_email = edd_get_option('from_name', get_bloginfo('admin_email'));
    $from_email = apply_filters('edd_purchase_from_address', $from_email, 0, array());
    $subject = edd_get_option('purchase_subject', __('Purchase Receipt', 'edd'));
    $subject = apply_filters('edd_purchase_subject', wp_strip_all_tags($subject), 0);
    $subject = edd_do_email_tags($subject, 0);
    $attachments = apply_filters('edd_receipt_attachments', array(), 0, array());
    $message = edd_do_email_tags(edd_get_email_body_content(0, array()), 0);
    $emails = EDD()->emails;
    $emails->__set('from_name', $from_name);
    $emails->__set('from_email', $from_email);
    $emails->__set('heading', __('Purchase Receipt', 'edd'));
    $headers = apply_filters('edd_receipt_headers', $emails->get_headers(), 0, array());
    $emails->__set('headers', $headers);
    $emails->send(edd_get_admin_notice_emails(), $subject, $message, $attachments);
}
/**
 * Displays the email preview
 *
 * @since 2.1
 * @return void
 */
function edd_display_email_template_preview()
{
    if (empty($_GET['edd_action'])) {
        return;
    }
    if ('preview_email' !== $_GET['edd_action']) {
        return;
    }
    if (!current_user_can('manage_shop_settings')) {
        return;
    }
    EDD()->emails->heading = __('Purchase Receipt', 'edd');
    echo EDD()->emails->build_email(edd_email_preview_template_tags(edd_get_email_body_content(0, array())));
    exit;
}