/**
  * Initialize class
  * Hook into WooCommerce actions/filters
  *
  * @since 4.4
  */
 public static function init()
 {
     self::hooks();
     self::$addresses = fetch_business_addresses();
     if (WT_LOG_REQUESTS) {
         self::init_logger();
     }
 }
 /**
  * Constructor: Starts Lookup and hooks into WooCommerce
  *
  * @since 4.2
  */
 public function __construct()
 {
     if (WT_SUBS_ACTIVE && WC_Subscriptions_Cart::cart_contains_subscription()) {
         $this->is_subscription = true;
         // Restore shipping taxes array for orders containing subscriptions
         add_filter('woocommerce_calculated_total', array($this, 'store_shipping_taxes'), 10, 2);
         add_action('woocommerce_cart_updated', array($this, 'restore_shipping_taxes'));
         // Hook for 2.3: woocommerce_after_calculate_totals
         // Set is_renewal flag if subscriptions is calculating the recurring order total
         if (WC_Subscriptions_Cart::get_calculation_type() == 'recurring_total') {
             $this->is_renewal = true;
         } else {
             $this->is_renewal = false;
         }
     }
     $this->cart = WC()->cart;
     $this->taxcloud = TaxCloud();
     $this->addresses = fetch_business_addresses();
     $this->init();
 }
    /**
     * Output origin address select metabox
     *
     * @since 4.2
     * @param (WP_Post) $post post/product being edited
     */
    public static function output_shipping_metabox($post)
    {
        $addresses = fetch_business_addresses();
        echo '<p>Use the box below to search for and add "Shipping Origin Addresses" for this product. These are the locations from which this
		item will be shipped.</p>';
        echo '<p>When an item can be shipped from multiple locations, WooTax will assume that it is sent from the business location in the customer\'s state.</p>';
        // Fetch addresses for this product
        $origin_addresses = fetch_product_origin_addresses($post->ID);
        // Output addresses
        echo '<select class="' . (version_compare(WOOCOMMERCE_VERSION, '2.3', '<') ? 'chosen_select' : 'wc-enhanced-select') . '" name="_wootax_origin_addresses[]" multiple>';
        if (is_array($addresses) && count($addresses) > 0) {
            foreach ($addresses as $key => $address) {
                echo '<option value="' . $key . '"' . (in_array($key, $origin_addresses) ? " selected" : "") . '>' . get_formatted_address($address) . '</option>';
            }
        } else {
            echo '<option value="">There are no addresses to select.</option>';
        }
        echo '</select>';
    }
 /**
  * Maybe update address system from old, single address system to newer multi-address system
  *
  * @since 4.4
  */
 private static function maybe_update_addresses()
 {
     if (get_option('wootax_address1')) {
         // Set new address array
         WC_WooTax::set_option('wootax_addresses', fetch_business_addresses());
         // Delete old options
         delete_option('wootax_address1');
         delete_option('wootax_address2');
         delete_option('wootax_state');
         delete_option('wootax_city');
         delete_option('wootax_zip5');
         delete_option('wootax_zip4');
     }
 }
/**
 * Return business address with given index as an array
 *
 * @since 4.4
 * @param (int) $index the index of the address to return
 * @param (array) $addresses the array of addresses to search for the address at $index
 * @return (array) the address as an associative array
 */
function wootax_get_address($index)
{
    $addresses = fetch_business_addresses();
    $address = array('Address1' => '', 'Address2' => '', 'Country' => '', 'State' => '', 'City' => '', 'Zip5' => '', 'Zip4' => '');
    if (is_array($addresses) && isset($addresses[$index])) {
        $address['Address1'] = $addresses[$index]['address_1'];
        $address['Address2'] = isset($addresses[$index]['address_2']) ? $addresses[$index]['address_2'] : '';
        $address['Country'] = $addresses[$index]['country'];
        $address['State'] = $addresses[$index]['state'];
        $address['City'] = $addresses[$index]['city'];
        $address['Zip5'] = $addresses[$index]['zip5'];
        $address['Zip4'] = $addresses[$index]['zip4'];
    }
    return $address;
}
        /**
         * Output address table
         */
        public function generate_address_table_html($key, $data)
        {
            $woocommerce_path = plugin_dir_url('woocommerce/woocommerce.php');
            ob_start();
            ?>
 		</table>
 		<table id="address_table" class="shippingrows widefat">
			<thead>
				<tr>
					<th><span>Address 1</span> <img class="help_tip" data-tip="Line 1 of your business address." src="<?php 
            echo $woocommerce_path;
            ?>
/assets/images/help.png" height="16" width="16"></th>
					<th><span>Address 2</span> <img class="help_tip" data-tip="Line 2 of your business address." src="<?php 
            echo $woocommerce_path;
            ?>
/assets/images/help.png" height="16" width="16"></th>
					<th><span>City</span> <img class="help_tip" data-tip="The city in which your business operates." src="<?php 
            echo $woocommerce_path;
            ?>
/assets/images/help.png" height="16" width="16"></th>
					<th><span>State</span> <img class="help_tip" data-tip="The state where your business is located." src="<?php 
            echo $woocommerce_path;
            ?>
/assets/images/help.png" height="16" width="16"></th>
					<th><span>ZIP Code</span> <img class="help_tip" data-tip="5 or 9-digit ZIP code of your business address." src="<?php 
            echo $woocommerce_path;
            ?>
/assets/images/help.png" height="16" width="16"></th>
					<th><span>Make Default</span> <img class="help_tip" data-tip="Check this if you want an address to be used as the default 'Shipment Origin Address' for your products. If you only have one business address, it will be used as your default address automatically." src="<?php 
            echo $woocommerce_path;
            ?>
/assets/images/help.png" height="16" width="16"></th>
					<th><span>Remove</span> <img class="help_tip" data-tip="Click the red X to remove a business address. Remember, at least one valid address is required for WooTax to work." src="<?php 
            echo $woocommerce_path;
            ?>
/assets/images/help.png" height="16" width="16"></th> 
				</tr>
			</thead>
			<tfoot>
				<tr>
					<th colspan="7">
						<button class="wp-core-ui button-secondary add-address-row">Add Address</button>
					</th>
				</tr>
			</tfoot>
			<tbody>
			<?php 
            $addresses = fetch_business_addresses();
            for ($i = 0; $i < count($addresses); $i++) {
                $address = $addresses[$i];
                ?>
					<tr>
						<td>
							<input type="text" name="wootax_address1[<?php 
                echo $i;
                ?>
]" class="wootax_address1" value="<?php 
                echo $address['address_1'];
                ?>
" />
						</td>
						<td>
							<input type="text" name="wootax_address2[<?php 
                echo $i;
                ?>
]" class="wootax_address2" value="<?php 
                echo isset($address['address_2']) ? $address['address_2'] : '';
                ?>
" placeholder="(Optional)" />
						</td>
						<td>
							<input type="text" name="wootax_city[<?php 
                echo $i;
                ?>
]" class="wootax_city" value="<?php 
                echo $address['city'];
                ?>
" />
						</td>
						<td>
							<select name="wootax_state[<?php 
                echo $i;
                ?>
]" class="wootax_state">
								<option value="">Select One</option>
								<option value="AL"<?php 
                echo $address['state'] == 'AL' ? ' selected' : '';
                ?>
>Alabama</option> 
								<option value="AK"<?php 
                echo $address['state'] == 'AK' ? ' selected' : '';
                ?>
>Alaska</option> 
								<option value="AZ"<?php 
                echo $address['state'] == 'AZ' ? ' selected' : '';
                ?>
>Arizona</option> 
								<option value="AR"<?php 
                echo $address['state'] == 'AR' ? ' selected' : '';
                ?>
>Arkansas</option> 
								<option value="CA"<?php 
                echo $address['state'] == 'CA' ? ' selected' : '';
                ?>
>California</option> 
								<option value="CO"<?php 
                echo $address['state'] == 'CO' ? ' selected' : '';
                ?>
>Colorado</option> 
								<option value="CT"<?php 
                echo $address['state'] == 'CT' ? ' selected' : '';
                ?>
>Connecticut</option> 
								<option value="DE"<?php 
                echo $address['state'] == 'DE' ? ' selected' : '';
                ?>
>Delaware</option> 
								<option value="DC"<?php 
                echo $address['state'] == 'DC' ? ' selected' : '';
                ?>
>District Of Columbia</option> 
								<option value="FL"<?php 
                echo $address['state'] == 'FL' ? ' selected' : '';
                ?>
>Florida</option> 
								<option value="GA"<?php 
                echo $address['state'] == 'GA' ? ' selected' : '';
                ?>
>Georgia</option> 
								<option value="HI"<?php 
                echo $address['state'] == 'HI' ? ' selected' : '';
                ?>
>Hawaii</option> 
								<option value="ID"<?php 
                echo $address['state'] == 'ID' ? ' selected' : '';
                ?>
>Idaho</option> 
								<option value="IL"<?php 
                echo $address['state'] == 'IL' ? ' selected' : '';
                ?>
>Illinois</option> 
								<option value="IN"<?php 
                echo $address['state'] == 'IN' ? ' selected' : '';
                ?>
>Indiana</option> 
								<option value="IA"<?php 
                echo $address['state'] == 'IA' ? ' selected' : '';
                ?>
>Iowa</option> 
								<option value="KS"<?php 
                echo $address['state'] == 'KS' ? ' selected' : '';
                ?>
>Kansas</option> 
								<option value="KY"<?php 
                echo $address['state'] == 'KY' ? ' selected' : '';
                ?>
>Kentucky</option> 
								<option value="LA"<?php 
                echo $address['state'] == 'LA' ? ' selected' : '';
                ?>
>Louisiana</option> 
								<option value="ME"<?php 
                echo $address['state'] == 'ME' ? ' selected' : '';
                ?>
>Maine</option> 
								<option value="MD"<?php 
                echo $address['state'] == 'MD' ? ' selected' : '';
                ?>
>Maryland</option> 
								<option value="MA"<?php 
                echo $address['state'] == 'MA' ? ' selected' : '';
                ?>
>Massachusetts</option> 
								<option value="MI"<?php 
                echo $address['state'] == 'MI' ? ' selected' : '';
                ?>
>Michigan</option> 
								<option value="MN"<?php 
                echo $address['state'] == 'MN' ? ' selected' : '';
                ?>
>Minnesota</option> 
								<option value="MS"<?php 
                echo $address['state'] == 'MS' ? ' selected' : '';
                ?>
>Mississippi</option> 
								<option value="MO"<?php 
                echo $address['state'] == 'MO' ? ' selected' : '';
                ?>
>Missouri</option> 
								<option value="MT"<?php 
                echo $address['state'] == 'MT' ? ' selected' : '';
                ?>
>Montana</option> 
								<option value="NE"<?php 
                echo $address['state'] == 'NE' ? ' selected' : '';
                ?>
>Nebraska</option> 
								<option value="NV"<?php 
                echo $address['state'] == 'NV' ? ' selected' : '';
                ?>
>Nevada</option> 
								<option value="NH"<?php 
                echo $address['state'] == 'NH' ? ' selected' : '';
                ?>
>New Hampshire</option> 
								<option value="NJ"<?php 
                echo $address['state'] == 'NJ' ? ' selected' : '';
                ?>
>New Jersey</option> 
								<option value="NM"<?php 
                echo $address['state'] == 'NM' ? ' selected' : '';
                ?>
>New Mexico</option> 
								<option value="NY"<?php 
                echo $address['state'] == 'NY' ? ' selected' : '';
                ?>
>New York</option> 
								<option value="NC"<?php 
                echo $address['state'] == 'NC' ? ' selected' : '';
                ?>
>North Carolina</option> 
								<option value="ND"<?php 
                echo $address['state'] == 'ND' ? ' selected' : '';
                ?>
>North Dakota</option> 
								<option value="OH"<?php 
                echo $address['state'] == 'OH' ? ' selected' : '';
                ?>
>Ohio</option> 
								<option value="OK"<?php 
                echo $address['state'] == 'OK' ? ' selected' : '';
                ?>
>Oklahoma</option> 
								<option value="OR"<?php 
                echo $address['state'] == 'OR' ? ' selected' : '';
                ?>
>Oregon</option> 
								<option value="PA"<?php 
                echo $address['state'] == 'PA' ? ' selected' : '';
                ?>
>Pennsylvania</option> 
								<option value="RI"<?php 
                echo $address['state'] == 'RI' ? ' selected' : '';
                ?>
>Rhode Island</option> 
								<option value="SC"<?php 
                echo $address['state'] == 'SC' ? ' selected' : '';
                ?>
>South Carolina</option> 
								<option value="SD"<?php 
                echo $address['state'] == 'SD' ? ' selected' : '';
                ?>
>South Dakota</option> 
								<option value="TN"<?php 
                echo $address['state'] == 'TN' ? ' selected' : '';
                ?>
>Tennessee</option> 
								<option value="TX"<?php 
                echo $address['state'] == 'TX' ? ' selected' : '';
                ?>
>Texas</option> 
								<option value="UT"<?php 
                echo $address['state'] == 'UT' ? ' selected' : '';
                ?>
>Utah</option> 
								<option value="VT"<?php 
                echo $address['state'] == 'VT' ? ' selected' : '';
                ?>
>Vermont</option> 
								<option value="VA"<?php 
                echo $address['state'] == 'VA' ? ' selected' : '';
                ?>
>Virginia</option> 
								<option value="WA"<?php 
                echo $address['state'] == 'WA' ? ' selected' : '';
                ?>
>Washington</option> 
								<option value="WV"<?php 
                echo $address['state'] == 'WV' ? ' selected' : '';
                ?>
>West Virginia</option> 
								<option value="WI"<?php 
                echo $address['state'] == 'WI' ? ' selected' : '';
                ?>
>Wisconsin</option> 
								<option value="WY"<?php 
                echo $address['state'] == 'WY' ? ' selected' : '';
                ?>
>Wyoming</option>
							</select>
						</td>
						<td>
							<input type="text" name="wootax_zip5[<?php 
                echo $i;
                ?>
]" class="wootax_zip5" value="<?php 
                echo $address['zip5'];
                ?>
" /> - <input type="text" name="wootax_zip4[<?php 
                echo $i;
                ?>
]" value="<?php 
                echo $address['zip4'];
                ?>
" placeholder="(Optional)" class="wootax_zip4" />
						</td>
						<td>
							<input type="radio" name="wootax_default_address" value="<?php 
                echo $i;
                ?>
"<?php 
                echo WC_WooTax::get_option('default_address') == $i || WC_WooTax::get_option('default_address') == '' && $i == 0 ? ' checked' : '';
                ?>
 />
						</td>
						<td>
							<a class="remove_address<?php 
                echo $i == 0 ? ' disabled' : '';
                ?>
">x</a>
						</td>
					</tr>
					<?php 
            }
            ?>
			</tbody>
		</table>
		<table class="form-table">
 		<?php 
            return ob_get_clean();
        }