Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Email
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
2 / 2
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 print_instructions
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2/**
3 * Print payment details in customer emails.
4 *
5 * TODO: Prevent sending the on-hold email immediately, reschedule it for one hour later.
6 *
7 * @package    brianhenryie/bh-wp-bitcoin-gateway
8 */
9
10namespace BrianHenryIE\WP_Bitcoin_Gateway\Integrations\WooCommerce;
11
12use Psr\Log\LoggerAwareTrait;
13use Psr\Log\LoggerInterface;
14use WC_Order;
15
16/**
17 * Load the order details and pass to the email template.
18 */
19class Email {
20    use LoggerAwareTrait;
21
22    const TEMPLATE_NAME = 'emails/email-bitcoin-instructions-status.php';
23
24    /**
25     * Constructor
26     *
27     * @param API_WooCommerce_Interface $api The main plugin functions.
28     * @param LoggerInterface           $logger A PSR logger.
29     */
30    public function __construct(
31        protected API_WooCommerce_Interface $api,
32        LoggerInterface $logger
33    ) {
34        $this->setLogger( $logger );
35    }
36
37    /**
38     * Add payment instructions or payment status (once paid) to the WC emails.
39     *
40     * @hooked woocommerce_email_before_order_table
41     *
42     * @param WC_Order $order The order object the email is being sent for.
43     * @param bool     $sent_to_admin Is this email being sent to an admin, or a customer.
44     * @param bool     $plain_text Is this plain text email ?( !HTML email ).
45     */
46    public function print_instructions( WC_Order $order, bool $sent_to_admin, bool $plain_text = false ): void {
47
48        if ( $sent_to_admin ) {
49            // TODO: Think about what information should be in admin emails.
50            return;
51        }
52
53        if ( ! $this->api->is_bitcoin_gateway( $order->get_payment_method() ) ) {
54            return;
55        }
56
57        /**
58         * There was an error where seemingly the order object being passed to this function is older than the
59         * one saved in `Bitcoin_Gateway::process_payment()` and the meta was not present, so let's refresh.
60         *
61         * @var WC_Order $order
62         */
63        $order = wc_get_order( $order->get_id() );
64
65        try {
66            $template_args = $this->api->get_formatted_order_details( $order );
67        } catch ( \Exception $exception ) {
68            $this->logger->warning(
69                "Failed to get `shop_order:{$order->get_id()}` details for Email template: {$exception->getMessage()}",
70                array(
71                    'order_id'  => $order->get_id(),
72                    'exception' => $exception,
73                )
74            );
75            return;
76        }
77
78        $template_args['template'] = self::TEMPLATE_NAME;
79
80        // TODO: Create a plain text template.
81        wc_get_template( self::TEMPLATE_NAME, $template_args );
82    }
83}