Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
Wallets_List_Table
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 6
132
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 get_columns
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
20
 get_cached_bitcoin_wallet_object
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 column_status
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 column_balance
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 edit_row_actions
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * Display wallets in use/formerly in use, their status, balance
4 *
5 * @package    brianhenryie/bh-wp-bitcoin-gateway
6 */
7
8namespace BrianHenryIE\WP_Bitcoin_Gateway\Admin;
9
10use BrianHenryIE\WP_Bitcoin_Gateway\API_Interface;
11use BrianHenryIE\WP_Bitcoin_Gateway\API\Addresses\Bitcoin_Wallet;
12use WP_Post;
13
14/**
15 * Hooks into standard WP_List_Table actions and filters.
16 *
17 * @see wp-admin/edit.php?post_type=bh-bitcoin-wallet
18 * @see WP_Posts_List_Table
19 */
20class Wallets_List_Table extends \WP_Posts_List_Table {
21
22    /**
23     * The main plugin functions.
24     *
25     * Not in use here currently.
26     */
27    protected API_Interface $api;
28
29    /**
30     * Constructor
31     *
32     * @see _get_list_table()
33     *
34     * @param array{screen?:\WP_Screen} $args The data passed by WordPress.
35     */
36    public function __construct( $args = array() ) {
37        parent::__construct( $args );
38
39        $post_type_name = $this->screen->post_type;
40
41        /**
42         * Since this object is instantiated because it was defined when registering the post type, it's
43         * extremely unlikely the post type will not exist.
44         *
45         * @var \WP_Post_Type $post_type_object
46         */
47        $post_type_object = get_post_type_object( $post_type_name );
48        $this->api        = $post_type_object->plugin_objects['api'];
49
50        add_filter( 'post_row_actions', array( $this, 'edit_row_actions' ), 10, 2 );
51    }
52
53    /**
54     * Define the custom columns for the post type.
55     * Status|Balance.
56     *
57     * TODO: Only show the wallet column if there is more than one wallet.
58     *
59     * @return array<string, string> Column name : HTML output.
60     */
61    public function get_columns() {
62        $columns = parent::get_columns();
63
64        $new_columns = array();
65        foreach ( $columns as $key => $column ) {
66
67            // Omit the "comments" column.
68            if ( 'comments' === $key ) {
69                continue;
70            }
71
72            // Add remaining columns after the Title column.
73            $new_columns[ $key ] = $column;
74            if ( 'title' === $key ) {
75
76                $new_columns['status']  = 'Status';
77                $new_columns['balance'] = 'Balance';
78            }
79            // The date column will be added last.
80        }
81
82        return $new_columns;
83    }
84
85    /**
86     * Cache each Bitcoin_Wallet object between calls to each `print_{$column}()`.
87     *
88     * @var array<int, Bitcoin_Wallet>
89     */
90    protected array $wallets_cache = array();
91
92    /**
93     * Fill or retrieve from the above cache of Wallet objects.
94     *
95     * @param WP_Post $post The post object for the current row.
96     *
97     * @throws \Exception When the post is not a `bh-bitcoin-wallet` post type.
98     */
99    protected function get_cached_bitcoin_wallet_object( WP_Post $post ): Bitcoin_Wallet {
100        if ( ! isset( $this->wallets_cache[ $post->ID ] ) ) {
101            $this->wallets_cache[ $post->ID ] = new Bitcoin_Wallet( $post->ID );
102        }
103        return $this->wallets_cache[ $post->ID ];
104    }
105
106    /**
107     * Print the status of this wallet.
108     *
109     * One of active|inactive.
110     *
111     * @see Post::register_wallet_post_type()
112     *
113     * @param WP_Post $post The post this row is being rendered for.
114     */
115    public function column_status( WP_Post $post ): void {
116        $bitcoin_wallet = $this->get_cached_bitcoin_wallet_object( $post );
117
118        echo esc_html( $bitcoin_wallet->get_status() );
119    }
120
121    /**
122     * Print the total Bitcoin received by this wallet.
123     *
124     * TODO: Not yet implemented.
125     *
126     * @param WP_Post $post The post this row is being rendered for.
127     */
128    public function column_balance( WP_Post $post ): void {
129        $bitcoin_wallet = $this->get_cached_bitcoin_wallet_object( $post );
130
131        echo esc_html( $bitcoin_wallet->get_balance() ?? 'unknown' );
132    }
133
134    /**
135     * Remove edit and view actions, add an update action.
136     *
137     * TODO: add a click handler to the update action.
138     *
139     * @hooked post_row_actions
140     * @see \WP_Posts_List_Table::handle_row_actions()
141     *
142     * @param array<string,string> $actions Action id : HTML.
143     * @param WP_Post              $post    The post object.
144     *
145     * @return array<string,string>
146     */
147    public function edit_row_actions( array $actions, WP_Post $post ): array {
148
149        if ( Bitcoin_Wallet::POST_TYPE !== $post->post_type ) {
150            return $actions;
151        }
152
153        unset( $actions['edit'] );
154        unset( $actions['inline hide-if-no-js'] ); // "quick edit".
155        unset( $actions['view'] );
156
157        $actions['update_address'] = 'Update';
158
159        return $actions;
160    }
161}