Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
57.14% covered (warning)
57.14%
24 / 42
50.00% covered (danger)
50.00%
4 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
BH_WC_Address_Validation
57.14% covered (warning)
57.14%
24 / 42
50.00% covered (danger)
50.00%
4 / 8
15.38
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 set_locale
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 define_admin_hooks
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 define_plugin_installer_hooks
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 define_woocommerce_hooks
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
 define_woocommerce_order_hooks
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 define_cron_hooks
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 define_cli_commands
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * The file that defines the core plugin class
4 *
5 * A class definition that includes attributes and functions used across both the
6 * woocommerce-facing side of the site and the admin area.
7 *
8 * @link       http://example.com
9 * @since      1.0.0
10 *
11 * @package    BH_WC_Address_Validation
12 * @subpackage BH_WC_Address_Validation/includes
13 */
14
15namespace BrianHenryIE\WC_Address_Validation;
16
17use BrianHenryIE\WC_Address_Validation\Admin\Plugin_Installer_Skin;
18use BrianHenryIE\WC_Address_Validation\Psr\Container\ContainerInterface;
19use BrianHenryIE\WC_Address_Validation\Admin\Plugins_Page;
20use BrianHenryIE\WC_Address_Validation\WooCommerce\Email\Emails;
21use BrianHenryIE\WC_Address_Validation\WooCommerce\Order;
22use BrianHenryIE\WC_Address_Validation\WooCommerce\Order_Status;
23use BrianHenryIE\WC_Address_Validation\WooCommerce\Shipping_Settings_Page;
24use BrianHenryIE\WC_Address_Validation\WP_Includes\CLI;
25use BrianHenryIE\WC_Address_Validation\WP_Includes\Cron;
26use BrianHenryIE\WC_Address_Validation\WP_Includes\I18n;
27use WP_CLI;
28
29/**
30 * The core plugin class.
31 *
32 * This is used to define internationalization, admin-specific hooks, and
33 * woocommerce-facing site hooks.
34 *
35 * Also maintains the unique identifier of this plugin as well as the current
36 * version of the plugin.
37 *
38 * @since      1.0.0
39 * @package    BH_WC_Address_Validation
40 * @subpackage BH_WC_Address_Validation/includes
41 * @author     Brian Henry <BrianHenryIE@gmail.com>
42 */
43class BH_WC_Address_Validation {
44
45    protected ContainerInterface $container;
46
47    /**
48     * Define the core functionality of the plugin.
49     *
50     * Set the plugin name and the plugin version that can be used throughout the plugin.
51     * Load the dependencies, define the locale, and set the hooks for the admin area and
52     * the woocommerce-facing side of the site.
53     *
54     * @since    1.0.0
55     */
56    public function __construct( ContainerInterface $container ) {
57
58        $this->container = $container;
59
60        $this->set_locale();
61
62        $this->define_admin_hooks();
63        $this->define_plugin_installer_hooks();
64        $this->define_woocommerce_hooks();
65        $this->define_woocommerce_order_hooks();
66        $this->define_cron_hooks();
67        $this->define_cli_commands();
68    }
69
70    /**
71     * Define the locale for this plugin for internationalization.
72     *
73     * Uses the i18n class in order to set the domain and to register the hook
74     * with WordPress.
75     *
76     * @since    1.0.0
77     */
78    protected function set_locale(): void {
79
80        $plugin_i18n = $this->container->get( I18n::class );
81
82        add_action( 'init', array( $plugin_i18n, 'load_plugin_textdomain' ) );
83    }
84
85    /**
86     * Register all of the hooks related to the admin area functionality
87     * of the plugin.
88     *
89     * @since    1.0.0
90     */
91    protected function define_admin_hooks(): void {
92
93        $plugins_page = $this->container->get( Plugins_Page::class );
94
95        $settings        = $this->container->get( Settings_Interface::class );
96        $plugin_basename = $settings->get_plugin_basename();
97
98        add_filter( 'plugin_action_links_' . $plugin_basename, array( $plugins_page, 'action_links' ) );
99        add_filter( 'plugin_row_meta', array( $plugins_page, 'row_meta' ), 20, 4 );
100    }
101
102    protected function define_plugin_installer_hooks(): void {
103        $plugin_installer_skin = $this->container->get( Plugin_Installer_Skin::class );
104        add_filter( 'install_plugin_overwrite_comparison', array( $plugin_installer_skin, 'add_changelog_entry_to_upgrade_screen' ), 10, 3 );
105    }
106
107    /**
108     *
109     */
110    protected function define_woocommerce_hooks(): void {
111
112        $shipping_settings_page = $this->container->get( Shipping_Settings_Page::class );
113        add_filter( 'woocommerce_get_sections_shipping', array( $shipping_settings_page, 'address_validation_section' ), 10, 1 );
114        add_filter( 'woocommerce_get_settings_shipping', array( $shipping_settings_page, 'address_validation_settings' ), 10, 2 );
115
116        /**
117         * The Order_Status class defines one new order status, wc-bad-address.
118         */
119        $order_status = $this->container->get( Order_Status::class );
120        add_action( 'woocommerce_init', array( $order_status, 'register_status' ) );
121        add_filter( 'wc_order_statuses', array( $order_status, 'add_order_status_to_woocommerce' ) );
122        add_filter( 'woocommerce_order_is_paid_statuses', array( $order_status, 'add_to_paid_status_list' ) );
123        add_filter( 'woocommerce_reports_order_statuses', array( $order_status, 'add_to_reports_status_list' ) );
124
125        $woocommerce_email = $this->container->get( Emails::class );
126        add_filter( 'woocommerce_email_classes', array( $woocommerce_email, 'register_email' ), 10, 1 );
127    }
128
129    /**
130     *
131     */
132    protected function define_woocommerce_order_hooks(): void {
133        $woocommerce_order = $this->container->get( Order::class );
134
135        add_action( 'woocommerce_order_status_changed', array( $woocommerce_order, 'check_address_on_single_order_processing' ), 10, 3 );
136        add_action( 'admin_action_mark_processing', array( $woocommerce_order, 'check_address_on_bulk_order_processing' ) );
137
138        add_filter( 'woocommerce_order_actions', array( $woocommerce_order, 'add_admin_ui_order_action' ) );
139        add_action( 'woocommerce_order_action_bh_wc_address_validate', array( $woocommerce_order, 'check_address_on_admin_order_action' ) );
140
141        add_action( 'woocommerce_admin_order_data_after_shipping_address', array( $woocommerce_order, 'print_link_to_usps_tools_zip_lookup' ) );
142        add_action( 'woocommerce_admin_order_data_after_shipping_address', array( $woocommerce_order, 'add_previous_address_to_order' ) );
143    }
144
145    /**
146     * Register the cron hook so it can run.
147     *
148     * @since    1.0.0
149     */
150    protected function define_cron_hooks(): void {
151        $cron = $this->container->get( Cron::class );
152
153        add_action( Cron::CHECK_SINGLE_ADDRESS_CRON_JOB, array( $cron, 'check_address_for_single_order' ) );
154        add_action( Cron::CHECK_MULTIPLE_ADDRESSES_CRON_JOB, array( $cron, 'check_address_for_multiple_orders' ) );
155        add_action( Cron::RECHECK_BAD_ADDRESSES_CRON_JOB, array( $cron, 'recheck_bad_address_orders' ) );
156
157        add_action( 'plugins_loaded', array( $cron, 'add_cron_jon' ) );
158    }
159
160    protected function define_cli_commands(): void {
161
162        if ( class_exists( WP_CLI::class ) ) {
163            CLI::$api = $this->container->get( API_Interface::class );
164            // e.g. `vendor/bin/wp address_validation check_order 123`.
165            WP_CLI::add_command( 'address_validation', CLI::class );
166        }
167    }
168}