$units the claim's units: an extension already * used changes what the text says about it * @return array{subject:string, body:string, version:string, to:string, locale:string} */ public function compose(Claim $claim, array $units = []): array { $switched = switch_to_locale(get_locale()); try { return $this->composeInSiteLocale($claim, $units); } finally { if ($switched) { restore_previous_locale(); } } } /** * @param list $units * @return array{subject:string, body:string, version:string, to:string, locale:string} */ private function composeInSiteLocale(Claim $claim, array $units): array { $alreadyExtended = false; foreach ($units as $unit) { $alreadyExtended = $alreadyExtended || $unit->isExtended(); } $order = $this->orders->order($claim->orderId()); $item = null !== $order ? $this->orders->item($order, $claim->orderItemId()) : null; $customSubject = trim((string) get_option(Options::INFORMATIVA_SUBJECT, '')); $customBody = trim((string) get_option(Options::INFORMATIVA_BODY, '')); $subject = '' !== $customSubject ? $customSubject : self::defaultSubject(); $body = '' !== $customBody ? $customBody : self::defaultBody(); $version = '' !== $customBody || '' !== $customSubject ? 'custom-' . substr(hash('sha256', $customSubject . "\n" . $customBody), 0, 12) : self::DEFAULT_VERSION; $values = [ '{customer_name}' => null !== $order ? $this->orders->customerName($order) : '', '{order_number}' => null !== $order ? (string) $order->get_order_number() : (string) $claim->orderId(), '{product_name}' => null !== $item ? (string) $item->get_name() : '', '{reported_on}' => wp_date((string) get_option('date_format'), (int) strtotime($claim->reportedOn()->toString() . ' 12:00:00')) ?: $claim->reportedOn()->toString(), '{shop_name}' => wp_specialchars_decode((string) get_bloginfo('name'), ENT_QUOTES), '{extension_paragraph}' => $alreadyExtended ? self::extensionUsedParagraph() : self::extensionParagraph(), ]; return [ 'subject' => strtr($subject, $values), 'body' => strtr($body, $values), 'version' => $version, 'to' => null !== $order ? (string) $order->get_billing_email() : '', 'locale' => get_locale(), ]; } /** * The subject and the body as one text: what is kept in the history. * * @param array{subject:string, body:string} $mail */ public static function fullText(array $mail): string { return $mail['subject'] . "\n\n" . $mail['body']; } /** * Sends the email. Returns true only when WordPress accepted it; false * or an exception mean the outcome is uncertain (G13): the caller sends * the claim to review and never retries on its own. * * @param array{subject:string, body:string, version:string, to:string, locale:string} $mail */ public function send(Claim $claim, array $mail): bool { if ('' === $mail['to'] || !is_email($mail['to'])) { return false; } $html = wpautop(esc_html($mail['body'])); if (function_exists('WC') && WC()->mailer()) { $mailer = WC()->mailer(); $message = $mailer->wrap_message($mail['subject'], $html); $sent = (bool) $mailer->send($mail['to'], $mail['subject'], $message); } else { $sent = (bool) wp_mail($mail['to'], $mail['subject'], $html, ['Content-Type: text/html; charset=UTF-8']); } if ($sent) { $switched = switch_to_locale(get_locale()); $order = $this->orders->order($claim->orderId()); if (null !== $order) { /* translators: 1: claim number, 2: email address, 3: version of the text, 4: email subject and body */ $order->add_order_note(sprintf(__("Warranty Desk, claim #%1\$d: informativa email to %2\$s accepted for sending by the mail system (text %3\$s). Delivery is not confirmed.\n\n%4\$s", 'abcreate-warranty-desk'), (int) $claim->id(), $mail['to'], $mail['version'], self::fullText($mail))); } if ($switched) { restore_previous_locale(); } } return $sent; } public static function defaultSubject(): string { /* translators: keep the placeholder {order_number} as it is */ return __('Your guarantee request for order {order_number}: repair or replacement', 'abcreate-warranty-desk'); } /** Art. 135-bis c. 2-bis and art. 133 c. 1-bis of the Italian Consumer Code, as amended by D.Lgs. 176/2026. */ public static function defaultBody(): string { /* translators: keep the placeholders in braces as they are; this text informs the consumer as required by art. 135-bis c. 2-bis of the Italian Consumer Code */ return __("Hello {customer_name},\n\nwe have received your report about {product_name} (order {order_number}), made on {reported_on}.\n\nBefore we proceed, as required by article 135-bis, paragraph 2-bis, of the Italian Consumer Code, we inform you that:\n\n1. You can choose whether to have the product repaired or replaced, free of charge, unless the remedy you choose is impossible or would cost us disproportionately more than the other one. The remedy is carried out within a reasonable time and without significant inconvenience to you.\n\n2. {extension_paragraph}\n\nPlease reply to this email with your choice.\n\nKind regards,\n{shop_name}", 'abcreate-warranty-desk'); } /** Art. 133 c. 1-bis: the extension follows a repair carried out as the remedy. */ public static function extensionParagraph(): string { return __('If the product is repaired as the remedy for this lack of conformity, our liability for any lack of conformity of the product is extended by 12 months beyond the original period (article 133, paragraph 1-bis). The extension applies once.', 'abcreate-warranty-desk'); } /** The extension was already given by an earlier repair: it is not given again. */ public static function extensionUsedParagraph(): string { return __('Our liability for this product was already extended by 12 months after an earlier repair (article 133, paragraph 1-bis). The extension applies once, so a new repair does not extend it again.', 'abcreate-warranty-desk'); } }