Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
Addresses_List_Table
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
1 / 1
 woocommerce_gateway_link
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2/**
3 * Hook into the addresses post type list table to add links to the gateways used for those addresses.
4 *
5 * @see \BrianHenryIE\WP_Bitcoin_Gateway\Admin\Addresses_List_Table::column_gateways()
6 *
7 * @package    brianhenryie/bh-wp-bitcoin-gateway
8 */
9
10namespace BrianHenryIE\WP_Bitcoin_Gateway\Integrations\WooCommerce;
11
12use BrianHenryIE\WP_Bitcoin_Gateway\API\Model\Wallet\Bitcoin_Address;
13use BrianHenryIE\WP_Bitcoin_Gateway\API\Model\Wallet\Bitcoin_Wallet;
14use WC_Payment_Gateway;
15use WC_Payment_Gateways;
16
17/**
18 * Link to the gateway recorded to the wallet.
19 */
20class Addresses_List_Table {
21
22    /**
23     * Provide href, text for a link to the gateway used by a wallet.
24     *
25     * `add_filter( 'bh_wp_bitcoin_gateway_gateway_link', 'woocommerce_gateway_link', 10, 5 );`.
26     *
27     * @used-by \BrianHenryIE\WP_Bitcoin_Gateway\Admin\Addresses_List_Table::column_gateways()
28     * @hooked bh_wp_bitcoin_gateway_gateway_link
29     *
30     * @param array{href?:string|non-empty-string,text:string|non-empty-string} $filtered_result Presumably the array with shape, with empty values.
31     * @param string|class-string                                               $integration The recorded integration, that we should check is this us before proceeding.
32     * @param non-empty-string                                                  $gateway_id The id of the gateway being used with the wallet, which we will use to set the href.
33     * @param Bitcoin_Wallet                                                    $_bitcoin_wallet The wallet itself.
34     * @param ?Bitcoin_Address                                                  $_address The payment address, when we're working with a table with changing address each row.
35     * @return array{href?:string,text:string}
36     */
37    public function woocommerce_gateway_link(
38        array $filtered_result,
39        string $integration,
40        string $gateway_id,
41        Bitcoin_Wallet $_bitcoin_wallet,
42        ?Bitcoin_Address $_address = null
43    ): array {
44        if ( WooCommerce_Integration::class !== $integration ) {
45            return $filtered_result;
46        }
47
48        if ( ! isset( WC_Payment_Gateways::instance()->get_available_payment_gateways()[ $gateway_id ] ) ) {
49            return array(
50                'text' => sprintf( 'WooCommerce: %s (unavailable)', $gateway_id ),
51            );
52        }
53
54        /** @var WC_Payment_Gateway $gateway_instance */
55        $gateway_instance = WC_Payment_Gateways::instance()->get_available_payment_gateways()[ $gateway_id ];
56
57        return array(
58            'href' => admin_url( sprintf( 'admin.php?page=wc-settings&tab=checkout&section=%s', $gateway_instance->id ) ),
59            'text' => sprintf( 'WooCommerce: %s', $gateway_instance->title ),
60        );
61    }
62}