Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
96.97% |
32 / 33 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| Admin_Order_UI | |
96.97% |
32 / 33 |
|
66.67% |
2 / 3 |
7 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| register_address_transactions_meta_box | |
92.86% |
13 / 14 |
|
0.00% |
0 / 1 |
3.00 | |||
| print_address_transactions_metabox | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Add a metabox with the payment details on the admin order page. |
| 4 | * |
| 5 | * @package brianhenryie/bh-wp-bitcoin-gateway |
| 6 | */ |
| 7 | |
| 8 | namespace BrianHenryIE\WP_Bitcoin_Gateway\WooCommerce; |
| 9 | |
| 10 | use DateTime; |
| 11 | use BrianHenryIE\WP_Bitcoin_Gateway\API_Interface; |
| 12 | use Psr\Log\LoggerAwareTrait; |
| 13 | use Psr\Log\LoggerInterface; |
| 14 | use WC_Order; |
| 15 | use WP_Post; |
| 16 | |
| 17 | /** |
| 18 | * Register and print a metabox on the shop_order page, display it only when the order is a Bitcoin order. |
| 19 | */ |
| 20 | class Admin_Order_UI { |
| 21 | use LoggerAwareTrait; |
| 22 | |
| 23 | const TEMPLATE_NAME = 'admin/single-order-ui-bitcoin-details-metabox.php'; |
| 24 | |
| 25 | /** |
| 26 | * Instance of the mail plugin class. |
| 27 | * |
| 28 | * @var API_Interface |
| 29 | */ |
| 30 | protected API_Interface $api; |
| 31 | |
| 32 | /** |
| 33 | * Constructor |
| 34 | * |
| 35 | * @param API_Interface $api Required for order details. |
| 36 | * @param LoggerInterface $logger PSR logger. |
| 37 | */ |
| 38 | public function __construct( API_Interface $api, LoggerInterface $logger ) { |
| 39 | $this->setLogger( $logger ); |
| 40 | $this->api = $api; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Register the Bitcoin order details metabox on shop_order admin edit view. |
| 45 | * |
| 46 | * @hooked add_meta_boxes |
| 47 | * |
| 48 | * @return void |
| 49 | */ |
| 50 | public function register_address_transactions_meta_box(): void { |
| 51 | |
| 52 | global $post; |
| 53 | |
| 54 | if ( 'shop_order' !== $post->post_type ) { |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | $order_id = $post->ID; |
| 59 | |
| 60 | if ( ! $this->api->is_order_has_bitcoin_gateway( $order_id ) ) { |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | add_meta_box( |
| 65 | 'bh-wp-bitcoin-gateway', |
| 66 | 'Bitcoin', |
| 67 | array( $this, 'print_address_transactions_metabox' ), |
| 68 | 'shop_order', |
| 69 | 'normal', |
| 70 | 'core' |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Print a box of information showing the Bitcoin address, amount expcted, paid, transactions, last checked date. |
| 76 | * |
| 77 | * TODO: Display the difference between amount required and amount paid? |
| 78 | * TODO: "Check now" button. |
| 79 | * |
| 80 | * @see Admin_Order_UI::register_address_transactions_meta_box(); |
| 81 | * |
| 82 | * @param WP_Post $post The post this edit page is running for. |
| 83 | */ |
| 84 | public function print_address_transactions_metabox( WP_Post $post ): void { |
| 85 | |
| 86 | $order_id = $post->ID; |
| 87 | |
| 88 | if ( ! $this->api->is_order_has_bitcoin_gateway( $order_id ) ) { |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * This is almost sure to be a valid order object, since this only runs on the order page. |
| 94 | * |
| 95 | * @var WC_Order $order |
| 96 | */ |
| 97 | $order = wc_get_order( $order_id ); |
| 98 | |
| 99 | // Once the order has been paid, no longer poll for new transactions, unless manually pressing refresh. |
| 100 | $refresh = ! $order->is_paid(); |
| 101 | |
| 102 | try { |
| 103 | $template_args = $this->api->get_formatted_order_details( $order, $refresh ); |
| 104 | } catch ( \Exception $exception ) { |
| 105 | $this->logger->warning( |
| 106 | "Failed to get `shop_order:{$order_id}` details for admin order ui metabox template: {$exception->getMessage()}", |
| 107 | array( |
| 108 | 'order_id' => $order_id, |
| 109 | 'exception' => $exception, |
| 110 | ) |
| 111 | ); |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | $template_args['template'] = self::TEMPLATE_NAME; |
| 116 | |
| 117 | wc_get_template( self::TEMPLATE_NAME, $template_args ); |
| 118 | } |
| 119 | } |