Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
19 / 19 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| Transaction_Formatter | |
100.00% |
19 / 19 |
|
100.00% |
4 / 4 |
6 | |
100.00% |
1 / 1 |
| get_url | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| get_ellipses | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_order_note | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
| get_note_part | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Given a Transaction, return strings to print in HTML. |
| 4 | * |
| 5 | * @package brianhenryie/bh-wp-bitcoin-gateway |
| 6 | */ |
| 7 | |
| 8 | namespace BrianHenryIE\WP_Bitcoin_Gateway\Integrations\WooCommerce; |
| 9 | |
| 10 | use BrianHenryIE\WP_Bitcoin_Gateway\API\Model\Payments\Transaction_Interface; |
| 11 | |
| 12 | /** |
| 13 | * Just a utility class. Not strictly WooCommerce related but not used elsewhere. |
| 14 | */ |
| 15 | class Transaction_Formatter { |
| 16 | |
| 17 | /** |
| 18 | * URL to the transaction details on blockchain.com. |
| 19 | * |
| 20 | * @param Transaction_Interface $transaction The transaction to get URL for. |
| 21 | * @return string The URL to view the transaction. |
| 22 | */ |
| 23 | public static function get_url( Transaction_Interface $transaction ): string { |
| 24 | return sprintf( |
| 25 | 'https://blockchain.com/explorer/transactions/btc/%s', |
| 26 | $transaction->get_txid() |
| 27 | ); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Given a long string, return a short version with ellipses in the middle. |
| 32 | * |
| 33 | * @param Transaction_Interface $transaction The transaction to format. |
| 34 | * @return string The shortened transaction ID with ellipses. |
| 35 | */ |
| 36 | public static function get_ellipses( Transaction_Interface $transaction ): string { |
| 37 | return substr( $transaction->get_txid(), 0, 3 ) . '...' . substr( $transaction->get_txid(), - 3 ); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Generate an order note for newly seen transactions. |
| 42 | * |
| 43 | * @param Transaction_Interface[] $new_order_transactions New transactions to include in the note. |
| 44 | * @return string The HTML formatted order note. |
| 45 | */ |
| 46 | public static function get_order_note( array $new_order_transactions ): string { |
| 47 | |
| 48 | $note = ''; |
| 49 | $plural = count( $new_order_transactions ) === 1 ? '' : 's'; |
| 50 | $note .= "New transaction{$plural} seen: "; |
| 51 | $new_transactions_notes = array(); |
| 52 | foreach ( $new_order_transactions as $new_transaction ) { |
| 53 | $new_transactions_notes[] = self::get_note_part( $new_transaction ); |
| 54 | } |
| 55 | $note .= implode( ',', $new_transactions_notes ) . ".\n\n"; |
| 56 | |
| 57 | return $note; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Get HTML formatted single transaction data for use in a WooCommerce order note. |
| 62 | * |
| 63 | * @param Transaction_Interface $transaction The transaction to format. |
| 64 | * @return string The formatted note part with link and block height. |
| 65 | */ |
| 66 | protected static function get_note_part( Transaction_Interface $transaction ): string { |
| 67 | return sprintf( |
| 68 | '<a href="%s" target="_blank">%s</a>, @%s', |
| 69 | esc_url( self::get_url( $transaction ) ), |
| 70 | self::get_ellipses( $transaction ), |
| 71 | $transaction->get_block_height() ?? 'mempool' |
| 72 | ); |
| 73 | } |
| 74 | } |