get_row($wpdb->prepare( "SELECT * FROM {$wpdb->prefix}aieo_dmm_withdrawal_declarations WHERE id = %d", (int) $declaration_id )); if (!$row) { return; } $declared_ts = strtotime($row->declared_at_gmt . ' UTC'); $stamp = wp_date( get_option('date_format') . ', ' . get_option('time_format'), $declared_ts ); $subject = sprintf( __('Withdrawal confirmation — order %s', 'ai-eshop-optimizer'), $order->get_order_number() ); $body = '

' . esc_html__('We confirm that we have received your withdrawal declaration. Keep this email as your receipt.', 'ai-eshop-optimizer') . '

'; $body .= ''; $body .= self::row(__('Order', 'ai-eshop-optimizer'), $order->get_order_number()); $body .= self::row(__('Submitted on', 'ai-eshop-optimizer'), $stamp); $body .= self::row( __('Scope', 'ai-eshop-optimizer'), !empty($row->is_full) ? __('The entire order', 'ai-eshop-optimizer') : __('Selected items only', 'ai-eshop-optimizer') ); $body .= self::row( __('Submitted via', 'ai-eshop-optimizer'), 'guest' === $row->channel ? __('The withdrawal form (verified by email)', 'ai-eshop-optimizer') : __('Your account', 'ai-eshop-optimizer') ); $body .= '
'; $items = isset($context['items']) && is_array($context['items']) ? $context['items'] : array(); if (!empty($items)) { $body .= '

' . esc_html__('Items covered by this withdrawal', 'ai-eshop-optimizer') . '

'; $body .= ''; } if (!empty($row->reason_note)) { $body .= '

' . esc_html__('Your note', 'ai-eshop-optimizer') . ': ' . esc_html($row->reason_note) . '

'; } $body .= !empty($context['cancelled']) ? '

' . esc_html__('Your order has been cancelled. Any amount already paid will be refunded to the original payment method.', 'ai-eshop-optimizer') . '

' : '

' . esc_html__('We are processing your withdrawal and will contact you shortly about returning the items and refunding you.', 'ai-eshop-optimizer') . '

'; $body .= self::account_block($context); if (class_exists('AIEO_Withdrawal_Account')) { $body .= AIEO_Withdrawal_Account::contact_block(); } if (class_exists('AIEO_Withdrawal_Guest')) { AIEO_Withdrawal_Guest::mail($order->get_billing_email(), $subject, $body); } AIEO_Withdrawal::mark_acknowledged($declaration_id); } private static function account_block(array $context) { $account = isset($context['account']) && is_array($context['account']) ? $context['account'] : array(); if (empty($account['created'])) { return ''; } if (!class_exists('AIEO_Withdrawal_Account')) { return ''; } $lines = AIEO_Withdrawal_Account::account_created_lines( (int) ($account['linked'] ?? 1), !empty($account['magic_link']) ); if (empty($lines)) { return ''; } $html = '
'; foreach ($lines as $i => $line) { $html .= '

' . esc_html($line) . '

'; } $html .= '
'; return $html; } public static function send_return_label($declaration_id, $order = null, $has_voucher = true) { if (!class_exists('AIEO_Withdrawal_Returns')) { return; } if (!class_exists('AIEO_Withdrawal_Account')) { return; } if (!class_exists('AIEO_Withdrawal_Guest')) { return; } AIEO_Withdrawal_Guest::in_shop_language(static function () use ($declaration_id, $order, $has_voucher) { self::build_return_label($declaration_id, $order, $has_voucher); }); } private static function build_return_label($declaration_id, $order = null, $has_voucher = true) { if (!class_exists('AIEO_Withdrawal_Returns')) { return; } $row = AIEO_Withdrawal_Returns::declaration((int) $declaration_id); if (!$row) { return; } if (!$order instanceof WC_Order) { $order = wc_get_order((int) $row->order_id); } if (!$order instanceof WC_Order) { return; } $to = (string) $order->get_billing_email(); if (!is_email($to)) { return; } $subject = sprintf( __('Your return — order %s', 'ai-eshop-optimizer'), $order->get_order_number() ); $can_print = class_exists('AIEO_Withdrawal_Returns') && AIEO_Withdrawal_Returns::has_label($row); if (!$can_print) { $body = '

' . esc_html__('We have approved your return. We are arranging the collection and will be in touch with the details shortly.', 'ai-eshop-optimizer') . '

'; if ($has_voucher) { $body .= '

' . esc_html(sprintf( __('Your return reference: %s', 'ai-eshop-optimizer'), (string) $row->return_voucher_no )) . '

'; } if (class_exists('AIEO_Withdrawal_Account')) { $body .= AIEO_Withdrawal_Account::contact_block(); } if (class_exists('AIEO_Withdrawal_Guest')) { AIEO_Withdrawal_Guest::mail($to, $subject, $body); } return; } $url = AIEO_Withdrawal_Account::signed_label_url($row, $order); $body = '

' . esc_html__('Your return label is ready. Print it, attach it to the parcel, and hand it to the courier.', 'ai-eshop-optimizer') . '

'; $body .= '

' . esc_html__('Download the label', 'ai-eshop-optimizer') . '

'; $body .= '

' . esc_html(sprintf( __('Courier: %1$s · Voucher: %2$s', 'ai-eshop-optimizer'), (string) $row->return_carrier, (string) $row->return_voucher_no )) . '

'; $body .= '

' . esc_html__('It is also on your order page in your account, any time you need it again.', 'ai-eshop-optimizer') . '

'; $party = AIEO_Withdrawal_Returns::policy(); if ('recipient' === $party['charge_party']) { $body .= '

' . esc_html__('The return costs you nothing — we are paying the courier.', 'ai-eshop-optimizer') . '

'; } if (class_exists('AIEO_Withdrawal_Guest')) { AIEO_Withdrawal_Guest::mail($to, $subject, $body); } } public static function add_store_email($emails) { if (!is_array($emails) || !class_exists('WC_Email')) { return $emails; } if (!class_exists('AIEO_Email_Withdrawal_Request')) { $file = __DIR__ . '/class-aieo-email-withdrawal-request.php'; if (!file_exists($file)) { return $emails; } require_once $file; } if (class_exists('AIEO_Email_Withdrawal_Request')) { $emails['AIEO_Email_Withdrawal_Request'] = new AIEO_Email_Withdrawal_Request(); } return $emails; } public static function send_rejection($row, $order = null) { if (!class_exists('AIEO_Withdrawal_Guest')) { return false; } $to = ''; if ($order instanceof WC_Order) { $to = (string) $order->get_billing_email(); } if (!is_email($to) && !empty($row->billing_email)) { $to = (string) $row->billing_email; } if (!is_email($to)) { return false; } $sent = false; AIEO_Withdrawal_Guest::in_shop_language(static function () use ($row, $order, $to, &$sent) { $grounds = array(); if (class_exists('AIEO_Withdrawal_Rejection_Reasons')) { $ids = json_decode((string) ($row->rejection_reasons ?? ''), true); $grounds = AIEO_Withdrawal_Rejection_Reasons::labels_for_customer(is_array($ids) ? $ids : array()); } $order_no = ($order instanceof WC_Order) ? $order->get_order_number() : (string) ($row->order_id ?? ''); $subject = sprintf( __('About your withdrawal request for order %s', 'ai-eshop-optimizer'), $order_no ); $body = '

' . esc_html__('We have looked at your withdrawal request and we are not able to accept it.', 'ai-eshop-optimizer') . '

'; if ($grounds) { $body .= '

' . esc_html__('This is because:', 'ai-eshop-optimizer') . '

'; } if (!empty($row->rejection_note)) { $body .= '

' . esc_html((string) $row->rejection_note) . '

'; } $body .= '

' . esc_html__('If you think this is wrong, reply to this email and we will look at it again.', 'ai-eshop-optimizer') . '

'; if (class_exists('AIEO_Withdrawal_Account')) { $body .= AIEO_Withdrawal_Account::contact_block(); } AIEO_Withdrawal_Guest::mail($to, $subject, $body); $sent = true; }); return $sent; } public static function send_store_notification($declaration_id, $order, array $context) { if (!$order instanceof WC_Order) { return; } if (!empty($context['cancelled'])) { return; } if (!function_exists('WC') || !WC()->mailer()) { return; } if (!class_exists('AIEO_Withdrawal_Guest')) { return; } AIEO_Withdrawal_Guest::in_shop_language(static function () use ($declaration_id, $order, $context) { try { $emails = WC()->mailer()->get_emails(); if (isset($emails['AIEO_Email_Withdrawal_Request'])) { $emails['AIEO_Email_Withdrawal_Request']->trigger((int) $declaration_id, $order, $context); } } catch (Throwable $e) { } }); } public static function append_to_dispatch_email($body, $order = null) { if (!AIEO_Withdrawal::is_enabled()) { return $body; } if (!$order instanceof WC_Order) { return $body; } return rtrim((string) $body) . "\n\n" . "----\n" . self::notice_intro() . "\n" . self::notice_label() . ': ' . self::notice_url($order) . "\n"; } private static function notice_intro() { $settings = AIEO_Withdrawal::settings(); $intro = trim((string) $settings['email_intro']); return $intro !== '' ? $intro : __('You may withdraw from this order within 14 days of receiving it, without giving a reason.', 'ai-eshop-optimizer'); } private static function notice_label() { return __('Press here to withdraw', 'ai-eshop-optimizer'); } private static function notice_url($order) { return class_exists('AIEO_Withdrawal_Guest') ? AIEO_Withdrawal_Guest::form_url_for_order($order) : home_url('/'); } private static function row($label, $value) { return '' . '' . esc_html($label) . '' . '' . esc_html($value) . '' . ''; } public static function inject_notice($order, $sent_to_admin = false, $plain_text = false, $email = null) { if ($sent_to_admin) { return; } if (!AIEO_Withdrawal::is_enabled()) { return; } if (!$order instanceof WC_Order) { return; } $id = ($email && isset($email->id)) ? $email->id : ''; $wanted = apply_filters('aieo_withdrawal_notice_emails', array( 'customer_processing_order', 'customer_on_hold_order', 'customer_completed_order', 'customer_pos_completed_order', 'customer_invoice', )); if ($id !== '' && !in_array($id, (array) $wanted, true)) { return; } $intro = self::notice_intro(); $url = self::notice_url($order); $label = self::notice_label(); if ($plain_text) { echo "\n\n" . esc_html($intro) . "\n" . esc_html($label) . ': ' . esc_url($url) . "\n"; return; } echo '
'; echo '

' . esc_html($intro) . '

'; echo '

' . esc_html($label) . '

'; echo '
'; } } }