Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
88.41% |
61 / 69 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| Post_BH_Bitcoin_Address | |
88.41% |
61 / 69 |
|
33.33% |
1 / 3 |
5.04 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| register_address_post_type | |
100.00% |
61 / 61 |
|
100.00% |
1 / 1 |
1 | |||
| add_post_statuses | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Add a custom post type for Bitcoin address. |
| 4 | * Will have statuses 'unused', 'used', 'assigned'. |
| 5 | * Will have postmeta for: |
| 6 | * * its derive path |
| 7 | * * which order it is for |
| 8 | * * its transactions |
| 9 | * * its total received amount |
| 10 | * Its parent will be its xpub. |
| 11 | * |
| 12 | * WP_List_Table can show all addresses and their orders and amount received and last activity date. |
| 13 | * |
| 14 | * @see \BrianHenryIE\WP_Bitcoin_Gateway\Admin\Addresses_List_Table |
| 15 | * |
| 16 | * @package brianhenryie/bh-wp-bitcoin-gateway |
| 17 | */ |
| 18 | |
| 19 | namespace BrianHenryIE\WP_Bitcoin_Gateway\WP_Includes; |
| 20 | |
| 21 | use BrianHenryIE\WP_Bitcoin_Gateway\Admin\Addresses_List_Table; |
| 22 | use BrianHenryIE\WP_Bitcoin_Gateway\API\Model\Wallet\Bitcoin_Address_Status; |
| 23 | use BrianHenryIE\WP_Bitcoin_Gateway\API\Model\Wallet\Bitcoin_Address_WP_Post_Interface; |
| 24 | use BrianHenryIE\WP_Bitcoin_Gateway\API\Repositories\Bitcoin_Address_Repository; |
| 25 | use BrianHenryIE\WP_Bitcoin_Gateway\API\Repositories\Bitcoin_Wallet_Repository; |
| 26 | use BrianHenryIE\WP_Bitcoin_Gateway\API_Interface; |
| 27 | use WP_Query; |
| 28 | |
| 29 | /** |
| 30 | * Register the custom post types with WordPress. |
| 31 | * |
| 32 | * @see _get_list_table() |
| 33 | * |
| 34 | * @see register_post_type() |
| 35 | * @see register_post_status() |
| 36 | * |
| 37 | * @see Addresses_List_Table |
| 38 | * @see wp-admin/edit.php?post_type=bh-bitcoin-address |
| 39 | * |
| 40 | * @see Bitcoin_Address_WP_Post_Interface |
| 41 | * |
| 42 | * @phpstan-import-type Address_List_Table_Dependencies_Array from Addresses_List_Table |
| 43 | */ |
| 44 | class Post_BH_Bitcoin_Address { |
| 45 | |
| 46 | /** |
| 47 | * Dependencies to pass to the WP_Post_Type object for use by the list table. |
| 48 | * |
| 49 | * Stored on the post type args and accessible via: |
| 50 | * `get_post_type_object( 'bh-bitcoin-address' )->dependencies` |
| 51 | * |
| 52 | * @see Addresses_List_Table::__construct() Consumer of these dependencies. |
| 53 | * @see self::get_dependencies_schema() For the expected array structure. |
| 54 | * |
| 55 | * @var array&Address_List_Table_Dependencies_Array $dependencies |
| 56 | */ |
| 57 | protected array $dependencies; |
| 58 | |
| 59 | /** |
| 60 | * Constructor |
| 61 | * |
| 62 | * @param API_Interface $api The main plugin API for address operations (transactions, etc.). |
| 63 | * @param Bitcoin_Address_Repository $bitcoin_address_repository To get the address details for the admin list table. |
| 64 | * @param Bitcoin_Wallet_Repository $bitcoin_wallet_repository To get the wallet integration details in the list table view. |
| 65 | */ |
| 66 | public function __construct( |
| 67 | API_Interface $api, |
| 68 | Bitcoin_Address_Repository $bitcoin_address_repository, |
| 69 | Bitcoin_Wallet_Repository $bitcoin_wallet_repository, |
| 70 | ) { |
| 71 | $this->dependencies = array( |
| 72 | 'api' => $api, |
| 73 | 'bitcoin_address_repository' => $bitcoin_address_repository, |
| 74 | 'bitcoin_wallet_repository' => $bitcoin_wallet_repository, |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Registers the bh-bitcoin-address post type and its statuses. |
| 80 | * |
| 81 | * @hooked init |
| 82 | */ |
| 83 | public function register_address_post_type(): void { |
| 84 | |
| 85 | $labels = array( |
| 86 | 'name' => _x( 'Bitcoin Addresses', 'post type general name', 'bh-wp-bitcoin-gateway' ), |
| 87 | 'singular_name' => _x( 'Bitcoin Address', 'post type singular name', 'bh-wp-bitcoin-gateway' ), |
| 88 | 'menu_name' => 'Bitcoin Addresses', |
| 89 | ); |
| 90 | $args = array( |
| 91 | 'labels' => $labels, |
| 92 | 'description' => 'Addresses used with WooCommerce Bitcoin gateways.', |
| 93 | 'public' => true, |
| 94 | 'menu_position' => 8, |
| 95 | 'supports' => array( 'title', 'thumbnail', 'excerpt', 'comments' ), |
| 96 | 'has_archive' => false, |
| 97 | 'show_in_menu' => false, |
| 98 | 'show_in_rest' => false, // TODO: change to `true` after ensuring correct authorization. |
| 99 | /** |
| 100 | * Dependencies passed to the list table via the WP_Post_Type object. |
| 101 | * |
| 102 | * @see Addresses_List_Table::__construct() Where these are consumed. |
| 103 | */ |
| 104 | 'dependencies' => $this->dependencies, |
| 105 | ); |
| 106 | register_post_type( Bitcoin_Address_WP_Post_Interface::POST_TYPE, $args ); |
| 107 | |
| 108 | register_post_status( |
| 109 | Bitcoin_Address_Status::UNKNOWN->value, |
| 110 | array( |
| 111 | 'post_type' => array( Bitcoin_Address_WP_Post_Interface::POST_TYPE ), |
| 112 | 'label' => _x( 'Unknown', 'post' ), |
| 113 | 'public' => true, |
| 114 | 'show_in_admin_all_list' => true, |
| 115 | 'show_in_admin_status_list' => true, |
| 116 | /* translators: %s is the number of Bitcoin addresses whose status is unknown. */ |
| 117 | 'label_count' => _n_noop( 'Unknown <span class="count">(%s)</span>', 'Unknown <span class="count">(%s)</span>' ), |
| 118 | ) |
| 119 | ); |
| 120 | |
| 121 | register_post_status( |
| 122 | Bitcoin_Address_Status::UNUSED->value, |
| 123 | array( |
| 124 | 'post_type' => array( Bitcoin_Address_WP_Post_Interface::POST_TYPE ), |
| 125 | 'label' => _x( 'Unused', 'post' ), |
| 126 | 'public' => true, |
| 127 | 'show_in_admin_all_list' => true, |
| 128 | 'show_in_admin_status_list' => true, |
| 129 | /* translators: %s is the number of Bitcoin addresses that have yet to be used. */ |
| 130 | 'label_count' => _n_noop( 'Unused <span class="count">(%s)</span>', 'Unused <span class="count">(%s)</span>' ), |
| 131 | ) |
| 132 | ); |
| 133 | |
| 134 | register_post_status( |
| 135 | Bitcoin_Address_Status::ASSIGNED->value, |
| 136 | array( |
| 137 | 'post_type' => array( Bitcoin_Address_WP_Post_Interface::POST_TYPE ), |
| 138 | 'label' => _x( 'Assigned', 'post' ), |
| 139 | 'public' => true, |
| 140 | 'show_in_admin_all_list' => true, |
| 141 | 'show_in_admin_status_list' => true, |
| 142 | /* translators: %s is the number of Bitcoin addresses that have been assigned. */ |
| 143 | 'label_count' => _n_noop( 'Assigned <span class="count">(%s)</span>', 'Assigned <span class="count">(%s)</span>' ), |
| 144 | ) |
| 145 | ); |
| 146 | |
| 147 | register_post_status( |
| 148 | Bitcoin_Address_Status::USED->value, |
| 149 | array( |
| 150 | 'post_type' => array( Bitcoin_Address_WP_Post_Interface::POST_TYPE ), |
| 151 | 'label' => _x( 'Used', 'post' ), |
| 152 | 'public' => true, |
| 153 | 'show_in_admin_all_list' => true, |
| 154 | 'show_in_admin_status_list' => true, |
| 155 | /* translators: %s is the number of Bitcoin addresses that have been used. */ |
| 156 | 'label_count' => _n_noop( 'Used <span class="count">(%s)</span>', 'Used <span class="count">(%s)</span>' ), |
| 157 | ) |
| 158 | ); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * If the query is for bh-bitcoin-address posts, set post_status to all statuses, unless another is specified. |
| 163 | * |
| 164 | * Otherwise, `get_posts()` and the REST API return no posts. |
| 165 | * |
| 166 | * @see get_posts() |
| 167 | * @hooked parse_query |
| 168 | * @see WP_Query::get_posts() |
| 169 | * |
| 170 | * @param WP_Query $query The WP_Query instance (passed by reference). |
| 171 | */ |
| 172 | public function add_post_statuses( WP_Query $query ): void { |
| 173 | |
| 174 | if ( 'bh-bitcoin-address' === ( $query->query['post_type'] ?? false ) |
| 175 | && 'publish' === ( $query->query['post_status'] ?? false ) |
| 176 | ) { |
| 177 | $query->query_vars['post_status'] = 'all'; |
| 178 | } |
| 179 | } |
| 180 | } |