Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
78.57% covered (warning)
78.57%
33 / 42
87.50% covered (warning)
87.50%
7 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
BH_WP_Bitcoin_Gateway
78.57% covered (warning)
78.57%
33 / 42
87.50% covered (warning)
87.50%
7 / 8
10.98
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 register_hooks
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 set_locale
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 define_plugins_page_hooks
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 define_custom_post_type_hooks
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 define_action_scheduler_hooks
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 define_wp_list_page_ui_hooks
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 define_cli_commands
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * The file that registers the hooks for the plugin.
4 *
5 * @package    brianhenryie/bh-wp-bitcoin-gateway
6 */
7
8namespace BrianHenryIE\WP_Bitcoin_Gateway;
9
10use BrianHenryIE\WP_Bitcoin_Gateway\Action_Scheduler\Background_Jobs_Actions_Interface;
11use BrianHenryIE\WP_Bitcoin_Gateway\Admin\Register_List_Tables;
12use BrianHenryIE\WP_Bitcoin_Gateway\Integrations\WooCommerce\API_WooCommerce;
13use BrianHenryIE\WP_Bitcoin_Gateway\Integrations\WooCommerce\API_WooCommerce_Interface;
14use BrianHenryIE\WP_Bitcoin_Gateway\lucatume\DI52\Container;
15use BrianHenryIE\WP_Bitcoin_Gateway\WP_Includes\Post_BH_Bitcoin_Address;
16use BrianHenryIE\WP_Bitcoin_Gateway\WP_Includes\Post_BH_Bitcoin_Transaction;
17use BrianHenryIE\WP_Bitcoin_Gateway\WP_Includes\Post_BH_Bitcoin_Wallet;
18use BrianHenryIE\WP_Bitcoin_Gateway\Admin\Plugins_Page;
19use BrianHenryIE\WP_Bitcoin_Gateway\WP_Includes\CLI;
20use BrianHenryIE\WP_Bitcoin_Gateway\WP_Includes\I18n;
21use Exception;
22use Psr\Container\ContainerInterface;
23use Psr\Log\LoggerInterface;
24use WP_CLI;
25
26/**
27 * The core plugin class.
28 *
29 * This is used to define internationalization, admin-specific hooks, and
30 * frontend-facing site hooks.
31 */
32class BH_WP_Bitcoin_Gateway {
33
34    /**
35     * @param ContainerInterface $container The DI container.
36     */
37    public function __construct(
38        protected ContainerInterface $container
39    ) {
40    }
41
42    /**
43     * Define the core functionality of the plugin.
44     *
45     * Set the plugin name and the plugin version that can be used throughout the plugin.
46     * Load the dependencies, define the locale, and set the hooks for the admin area and
47     * the frontend-facing side of the site.
48     */
49    public function register_hooks(): void {
50        $this->set_locale();
51
52        $this->define_plugins_page_hooks();
53
54        $this->define_custom_post_type_hooks();
55
56        $this->define_action_scheduler_hooks();
57
58        $this->define_wp_list_page_ui_hooks();
59
60        $this->define_cli_commands();
61    }
62
63    /**
64     * Define the locale for this plugin for internationalization.
65     *
66     * Uses the i18n class in order to set the domain and to register the hook
67     * with WordPress.
68     *
69     * @since    1.0.0
70     */
71    protected function set_locale(): void {
72
73        /** @var I18n $plugin_i18n */
74        $plugin_i18n = $this->container->get( I18n::class );
75
76        add_action( 'init', array( $plugin_i18n, 'load_plugin_textdomain' ) );
77    }
78
79    /**
80     * Hooks to add a "Settings" link on plugins.php.
81     * And a link to an orders filter (where possible).
82     */
83    protected function define_plugins_page_hooks(): void {
84
85        /** @var Plugins_Page $plugins_page */
86        $plugins_page = $this->container->get( Plugins_Page::class );
87
88        /** @var Settings_Interface $settings */
89        $settings        = $this->container->get( Settings_Interface::class );
90        $plugin_basename = $settings->get_plugin_basename();
91
92        add_filter( "plugin_action_links_{$plugin_basename}", array( $plugins_page, 'add_settings_action_link' ) );
93        add_filter( "plugin_action_links_{$plugin_basename}", array( $plugins_page, 'add_orders_action_link' ) );
94
95        add_filter( 'plugin_row_meta', array( $plugins_page, 'split_author_link_into_two_links' ), 10, 2 );
96    }
97
98    /**
99     * Add hooks for defining post types for the wallets and destination addresses.
100     */
101    protected function define_custom_post_type_hooks(): void {
102
103        /** @var Post_BH_Bitcoin_Wallet $wallet */
104        $wallet = $this->container->get( Post_BH_Bitcoin_Wallet::class );
105        add_action( 'init', array( $wallet, 'register_wallet_post_type' ) );
106
107        /** @var Post_BH_Bitcoin_Address $address */
108        $address = $this->container->get( Post_BH_Bitcoin_Address::class );
109        add_action( 'init', array( $address, 'register_address_post_type' ) );
110        add_action( 'parse_query', array( $address, 'add_post_statuses' ) );
111
112        /** @var Post_BH_Bitcoin_Transaction $transaction_post_type */
113        $transaction_post_type = $this->container->get( Post_BH_Bitcoin_Transaction::class );
114        add_action( 'init', array( $transaction_post_type, 'register_transaction_post_type' ) );
115    }
116
117    /**
118     * Handle Action Scheduler invoked actions to generate new addresses and check unpaid orders.
119     */
120    protected function define_action_scheduler_hooks(): void {
121
122        /** @var Background_Jobs_Actions_Interface $background_jobs_actions_handler */
123        $background_jobs_actions_handler = $this->container->get( Background_Jobs_Actions_Interface::class );
124
125        add_action( 'action_scheduler_run_recurring_actions_schedule_hook', array( $background_jobs_actions_handler, 'add_action_scheduler_repeating_actions' ) );
126
127        add_action( Background_Jobs_Actions_Interface::UPDATE_EXCHANGE_RATE_HOOK, array( $background_jobs_actions_handler, 'update_exchange_rate' ) );
128
129        add_action( Background_Jobs_Actions_Interface::RECURRING_ENSURE_UNUSED_ADDRESSES_HOOK, array( $background_jobs_actions_handler, 'ensure_unused_addresses' ) );
130        add_action( Background_Jobs_Actions_Interface::SINGLE_ENSURE_UNUSED_ADDRESSES_HOOK, array( $background_jobs_actions_handler, 'single_ensure_unused_addresses' ) );
131
132        add_action( Background_Jobs_Actions_Interface::GENERATE_NEW_ADDRESSES_HOOK, array( $background_jobs_actions_handler, 'generate_new_addresses' ) );
133
134        add_action( Background_Jobs_Actions_Interface::CHECK_NEW_ADDRESSES_TRANSACTIONS_HOOK, array( $background_jobs_actions_handler, 'check_new_addresses_for_transactions' ) );
135        add_action( Background_Jobs_Actions_Interface::CHECK_ASSIGNED_ADDRESSES_TRANSACTIONS_HOOK, array( $background_jobs_actions_handler, 'check_assigned_addresses_for_transactions' ) );
136    }
137
138    /**
139     * Customize the columns and data shown in the WP_List_Table for bitcoin wallets and bitcoin addresses.
140     */
141    protected function define_wp_list_page_ui_hooks(): void {
142
143        /** @var Register_List_Tables $register_list_tables */
144        $register_list_tables = $this->container->get( Register_List_Tables::class );
145
146        add_filter( 'wp_list_table_class_name', array( $register_list_tables, 'register_bitcoin_address_table' ), 10, 2 );
147        add_filter( 'wp_list_table_class_name', array( $register_list_tables, 'register_bitcoin_wallet_table' ), 10, 2 );
148    }
149
150    /**
151     * Register WP CLI commands.
152     *
153     * `wp bh-bitcoin generate-new-addresses`
154     */
155    protected function define_cli_commands(): void {
156
157        if ( ! class_exists( WP_CLI::class ) ) {
158            return;
159        }
160
161        /**
162         * TODO: isolate integrations' code from core.
163         * We know that this is a {@see Container}.
164         *
165         * @phpstan-ignore method.notFound
166         */
167        $this->container->bind( API_WooCommerce_Interface::class, API_WooCommerce::class );
168
169        /** @var CLI $cli */
170        $cli = $this->container->get( CLI::class );
171
172        try {
173            WP_CLI::add_command( 'bh-bitcoin generate-new-addresses', array( $cli, 'generate_new_addresses' ) );
174            WP_CLI::add_command( 'bh-bitcoin check-transactions', array( $cli, 'check_transactions' ) );
175        } catch ( Exception $e ) {
176            /** @var LoggerInterface $logger */
177            $logger = $this->container->get( LoggerInterface::class );
178            $logger->error( 'Failed to register WP CLI commands: ' . $e->getMessage(), array( 'exception' => $e ) );
179        }
180    }
181}