ed_at ); $merchant_sent = $this->send_merchant_order_withdrawal_email( $data, $matched_order, $submitted_at ); } catch ( Throwable $e ) { $this->log_email_error( $e ); return false; } if ( ! $customer_sent || ! $merchant_sent ) { $this->log_email_error( sprintf( 'Order withdrawal notification email failed. Customer email sent: %s. Merchant email sent: %s.', $customer_sent ? 'yes' : 'no', $merchant_sent ? 'yes' : 'no' ) ); return false; } return true; } /** * Send the customer order withdrawal acknowledgement email. * * @param array $data Form data. * @param int $submitted_at Unix timestamp for the submission. */ private function send_customer_order_withdrawal_email( array $data, int $submitted_at ): bool { $email = WC()->mailer()->get_emails()['WC_Email_Customer_Order_Withdrawal_Requested'] ?? include WC_ABSPATH . 'includes/emails/class-wc-email-customer-order-withdrawal-requested.php'; if ( ! $email instanceof WC_Email_Customer_Order_Withdrawal_Requested ) { return false; } if ( ! $email->is_enabled() ) { return true; } return $email->trigger( $data, $submitted_at ); } /** * Send the merchant order withdrawal notification email. * * @param array $data Form data. * @param WC_Order|null $matched_order Matched order, if found. * @param int $submitted_at Unix timestamp for the submission. */ private function send_merchant_order_withdrawal_email( array $data, ?WC_Order $matched_order, int $submitted_at ): bool { $email = WC()->mailer()->get_emails()['WC_Email_Order_Withdrawal_Requested'] ?? include WC_ABSPATH . 'includes/emails/class-wc-email-order-withdrawal-requested.php'; if ( ! $email instanceof WC_Email_Order_Withdrawal_Requested ) { return false; } if ( ! $email->is_enabled() ) { return true; } return $email->trigger( $data, $matched_order, $submitted_at, $matched_order instanceof WC_Order && $this->is_order_outside_withdrawal_window( $matched_order ), $matched_order instanceof WC_Order ? $this->get_withdrawal_window_warning_message() : '' ); } /** * Get the order withdrawal email data formatter. */ private function get_email_data_formatter(): OrderWithdrawalEmailDataFormatter { if ( null === $this->email_data_formatter ) { $this->email_data_formatter = new OrderWithdrawalEmailDataFormatter(); } return $this->email_data_formatter; } /** * Log an email failure. * * @param Throwable|string $error Email error. */ private function log_email_error( $error ): void { $message = $error instanceof Throwable ? $error->getMessage() : $error; wc_get_logger()->warning( sprintf( 'Order withdrawal email failed: %s', $message ), array( 'source' => self::LOGGER_SOURCE ) ); } /** * Log an inbox note failure without failing the submission. * * @param Throwable $e Inbox note error. * @param int $order_id Order ID. */ private function log_inbox_note_error( Throwable $e, int $order_id ): void { wc_get_logger()->warning( sprintf( 'Order withdrawal inbox note could not be processed for order %1$d. Error: %2$s', $order_id, $e->getMessage() ), array( 'source' => self::LOGGER_SOURCE ) ); } /** * Log an order note failure without failing the submission. * * @param WC_Order $order Matched order. * @param Throwable|null $e Order note error. */ private function log_order_note_error( WC_Order $order, ?Throwable $e = null ): void { $message = sprintf( 'Order withdrawal note could not be added to order %d.', $order->get_id() ); if ( $e instanceof Throwable ) { $message .= sprintf( ' Error: %s', $e->getMessage() ); } wc_get_logger()->warning( $message, array( 'source' => self::LOGGER_SOURCE ) ); } /** * Log an order meta failure without failing the submission. * * @param WC_Order $order Matched order. * @param Throwable $e Order meta error. */ private function log_order_meta_error( WC_Order $order, Throwable $e ): void { wc_get_logger()->warning( sprintf( 'Order withdrawal meta flag could not be added to order %1$d. Error: %2$s', $order->get_id(), $e->getMessage() ), array( 'source' => self::LOGGER_SOURCE ) ); } }