get_email_attachments($order_id, $items); $attachments = (array) apply_filters('ToDownload_order_email_attachments', $attachments, $order_id, $order, $customer, $items); $order_public_id = Orders::order_public_id($order_id); $ref = $order_public_id !== '' ? substr($order_public_id, -8) : ''; $subject = $ref !== '' ? sprintf( /* translators: %s is a non-sensitive order reference */ __('Your downloads are ready (Order %s)', '2download-connector'), $ref ) : __('Your downloads are ready', '2download-connector'); $subject = (string) apply_filters('ToDownload_order_email_subject', $subject, $order_id, $order, $customer, $items); $html = $this->build_email_html($order_id, $order, $customer, $items); $html = (string) apply_filters('ToDownload_order_email_body_html', $html, $order_id, $order, $customer, $items); $headers = [ 'Content-Type: text/html; charset=UTF-8', ]; $headers = (array) apply_filters('ToDownload_order_email_headers', $headers, $order_id, $order, $customer, $items); $sent = wp_mail($to, $subject, $html, $headers, $attachments); if ($sent) { Orders::append_order_note($order_id, $marker); } } /** * Resend the completed-order email (receipt + download links). * * @return bool true if wp_mail reported success */ public function resend_order_email(int $order_id): bool { $order = Orders::get_order($order_id); if (!$order) { return false; } if (($order['status'] ?? '') !== 'complete') { return false; } $customer = Orders::get_customer((int) ($order['customer_id'] ?? 0)); $to = $customer ? sanitize_email((string) ($customer['email'] ?? '')) : ''; if (!$to || !is_email($to)) { return false; } $items = Orders::get_order_items($order_id); if (empty($items)) { return false; } $attachments = $this->get_email_attachments($order_id, $items); $attachments = (array) apply_filters('ToDownload_order_email_attachments', $attachments, $order_id, $order, $customer, $items); $order_public_id = Orders::order_public_id($order_id); $ref = $order_public_id !== '' ? substr($order_public_id, -8) : ''; $subject = $ref !== '' ? sprintf( /* translators: %s is a non-sensitive order reference */ __('Your downloads are ready (Order %s)', '2download-connector'), $ref ) : __('Your downloads are ready', '2download-connector'); $subject = (string) apply_filters('ToDownload_order_email_subject', $subject, $order_id, $order, $customer, $items); $html = $this->build_email_html($order_id, $order, $customer, $items); $html = (string) apply_filters('ToDownload_order_email_body_html', $html, $order_id, $order, $customer, $items); $headers = [ 'Content-Type: text/html; charset=UTF-8', ]; $headers = (array) apply_filters('ToDownload_order_email_headers', $headers, $order_id, $order, $customer, $items); $sent = wp_mail($to, $subject, $html, $headers, $attachments); Orders::append_order_note($order_id, 'ToDownload_email_resent:downloads'); return (bool) $sent; } private function get_email_attachments(int $order_id, array $items): array { $max_bytes = (int) apply_filters('ToDownload_order_email_attachment_max_bytes', 10 * 1024 * 1024, $order_id, $items); if ($max_bytes < 0) { $max_bytes = 0; } $paths = []; foreach ($items as $it) { $download_id = (int) ($it['download_id'] ?? 0); if (!$download_id) { continue; } // Only attach the actual purchased file (not previews). $file_id = (int) get_post_meta($download_id, '_ToDownload_file_attachment_id', true); if (!$file_id) { continue; } $path = get_attached_file($file_id); if (!$path || !is_string($path)) { continue; } if (!file_exists($path) || !is_readable($path)) { continue; } if ($max_bytes > 0) { $size = @filesize($path); if (is_int($size) && $size > $max_bytes) { continue; } } else { // Max bytes 0 disables attachments entirely. continue; } // Avoid duplicate attachments. $paths[$path] = true; } return array_keys($paths); } private function build_email_html(int $order_id, array $order, ?array $customer, array $items): string { $first = $customer ? (string) ($customer['first_name'] ?? '') : ''; $name = trim($first); $greeting = $name !== '' /* translators: %s = customer name */ ? sprintf(__('Hi %s,', '2download-connector'), esc_html($name)) : esc_html__('Hi,', '2download-connector'); $total = isset($order['total']) ? number_format((float) $order['total'], 2) : '0.00'; $currency = isset($order['currency']) ? (string) $order['currency'] : 'USD'; $lines = []; $lines[] = '
' . $greeting . '
'; $receipt_url = ''; $settings = get_option('ToDownload_settings', []); if (is_array($settings)) { $receipt_url = (string) ($settings['success_url'] ?? ''); } if ($receipt_url === '') { $receipt_url = home_url('/'); } $customer_email = $customer ? sanitize_email((string) ($customer['email'] ?? '')) : ''; if ($customer_email !== '' && is_email($customer_email)) { $order_public_id = Orders::order_public_id((int) $order_id); $receipt_url = add_query_arg([ 'ToDownload_order' => $order_public_id, 'ToDownload_receipt_token' => Token::receipt_token($order_public_id, $customer_email), ], $receipt_url); } $account_url = ''; if ($customer_email !== '' && is_email($customer_email)) { $account_page_id = is_array($settings) ? (int) ($settings['account_page_id'] ?? 0) : 0; if ($account_page_id <= 0) { $account_page_id = (int) get_option('ToDownload_account_page_id'); } $account_base = $account_page_id > 0 ? (string) get_permalink($account_page_id) : ''; if ($account_base === '') { $account_base = home_url('/'); } $email_token = Token::email_lookup_token($customer_email); if ($email_token !== '') { $account_url = (string) add_query_arg([ 'ToDownload_email' => $customer_email, 'ToDownload_email_token' => $email_token, ], $account_base); } } $lines[] = '' . esc_html__('Thanks for your purchase! Your downloads are ready.', '2download-connector') . '
'; $lines[] = '' . esc_html__('Receipt:', '2download-connector') . ' ' . esc_html__('View receipt', '2download-connector') . '
'; if ($account_url !== '') { $lines[] = '' . esc_html__('Account:', '2download-connector') . ' ' . esc_html__('View your purchases', '2download-connector') . '
'; } if (class_exists('ZipArchive')) { $all_link = DownloadDelivery::generate_download_all_link($order_id); $lines[] = '' . esc_html__('Download all (.zip)', '2download-connector') . '
'; } $settings = get_option('ToDownload_settings', []); if (!is_array($settings)) { $settings = []; } $show_license = $this->order_is_seller_membership_purchase($items, $settings); if ($show_license) { Licenses::ensure_for_order($order_id); } $lines[] = '' . esc_html($license) . '') : '';
}
$lines[] = '' . esc_html__('Order reference:', '2download-connector') . ' ' . esc_html($ref_label) . '
';
$lines[] = '' . esc_html__('Total:', '2download-connector') . ' ' . esc_html($currency) . ' ' . esc_html($total) . '
' . esc_html__('Tip: save this email. You can always come back to the receipt link above to re-download.', '2download-connector') . '
'; $lines[] = '' . esc_html__('— 2Download', '2download-connector') . '
'; $lines[] = '