Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
Activator
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 1
72
0.00% covered (danger)
0.00%
0 / 1
 activate
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 1
72
1<?php
2/**
3 * Fired during plugin activation
4 *
5 * @link       http://example.com
6 * @since      1.0.0
7 *
8 * @package    BH_WC_Address_Validation
9 * @subpackage BH_WC_Address_Validation/includes
10 */
11
12namespace BrianHenryIE\WC_Address_Validation\WP_Includes;
13
14use BrianHenryIE\WC_Address_Validation\API\Settings;
15
16/**
17 * Fired during plugin activation.
18 *
19 * This class defines all code necessary to run during the plugin's activation.
20 *
21 * @since      1.0.0
22 * @package    BH_WC_Address_Validation
23 * @subpackage BH_WC_Address_Validation/includes
24 * @author     Brian Henry <BrianHenryIE@gmail.com>
25 */
26class Activator {
27
28    /**
29     * Checks was a user using woocommerce-usps-address-verification plugin and ports their settings.
30     * // TODO: email is disabled by default, enable it for these users.
31     *
32     * Checks on-hold orders to see if they were previously marked bad-address before a plugin-deactivation,
33     * and if so, checks them again (schedules a cron).
34     *
35     * @see https://wordpress.org/plugins/woocommerce-usps-address-verification/
36     *
37     * Activation hook does not get called on updates!
38     *
39     * @since    1.0.0
40     */
41    public static function activate(): void {
42
43        $earlier_version_usps_username = get_option( 'usps_id' );
44
45        if ( ! empty( $earlier_version_usps_username ) ) {
46            update_option( Settings::USPS_USERNAME_OPTION, $earlier_version_usps_username );
47            delete_option( 'usps_id' );
48        }
49
50        $earlier_version_notification_email_address = get_option( 'notif_email' );
51
52        if ( ! empty( $earlier_version_notification_email_address ) ) {
53
54            $email_settings_option_key = 'woocommerce_bad_address_admin_settings';
55
56            // Extremely unlikely this will already exist.
57            $email_settings = get_option( $email_settings_option_key, array() );
58
59            $email_settings['recipient'] = $earlier_version_notification_email_address;
60
61            update_option( $email_settings_option_key, $email_settings );
62
63            delete_option( 'notif_email' );
64        }
65
66        if ( ! function_exists( 'wc_get_orders' ) ) {
67            return;
68        }
69
70        $orders = wc_get_orders(
71            array(
72                'limit'  => -1,
73                'status' => array( 'on-hold' ),
74            )
75        );
76
77        if ( ! is_array( $orders ) ) {
78            return;
79        }
80
81        $orders_to_check = array();
82
83        foreach ( $orders as $order ) {
84
85            $had_bad_address_status = $order->get_meta( Deactivator::DEACTIVATED_BAD_ADDRESS_META_KEY );
86
87            if ( ! empty( $had_bad_address_status ) ) {
88                $orders_to_check[] = $order->get_id();
89            }
90        }
91
92        if ( ! empty( $orders_to_check ) ) {
93            wp_schedule_single_event( time(), Cron::CHECK_MULTIPLE_ADDRESSES_CRON_JOB, array( $orders_to_check ) );
94        }
95    }
96}