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