<?php
/**
 * Plugin Name: AddressZen
 * Plugin URI: https://addresszen.com/woocommerce
 * Description: US and International address search and verification for WooCommerce and Wordpress
 * Version: 1.0.1
 * Author: AddressZen
 * Author URI: https://addresszen.com/
 * Developer: AddressZen
 * Developer URI: https://addresszen.com
 * Text Domain: addresszen
 * Domain Path: /languages
 *
 * WC requires at least: 5.0.0
 * WC tested up to: 10.0.4
 *
 * License: MIT
 * License URI: https://github.com/addresszen/woocommerce/blob/master/LICENSE
 */

defined("ABSPATH") or die("No direct script");

//set constants
define("ADDRESSZEN_DIR", plugin_dir_path(__FILE__));
define("ADDRESSZEN_URL", plugin_dir_url(__FILE__));
define("ADDRESSZEN_NAME", "AddressZen");
define("ADDRESSZEN_SLUG", "addresszen");
define("ADDRESSZEN_VERSION", "1.0.0");

//includes
require_once ABSPATH . "wp-admin/includes/plugin.php";

add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'add_action_links');

function add_action_links($actions)
{
  $link = admin_url("admin.php?page=wc-settings&tab=integration&section=addresszen");
  $settings_link = '<a href="' . esc_url($link) . '">' . esc_html__("Settings", "addresszen") . "</a>";
  array_unshift($actions, $settings_link);
  return $actions;
}

if (!class_exists("WC_AddressZen")) {

  add_action('before_woocommerce_init', function() {
    if (class_exists(\Automattic\WooCommerce\Utilities\FeaturesUtil::class)) {
      \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility('custom_order_tables', __FILE__, true);
    }
  });

  class WC_AddressZen
  {
    /**
     * Construct the plugin.
     */
    public function __construct($file)
    {
      add_action("plugins_loaded", [$this, "init"]);
      add_filter("woocommerce_integrations", [$this, "add_integration"]);
    }

    /**
     * Initialize the plugin.
     */
    public function init()
    {
      if (class_exists("WC_Integration")) {
        require_once ADDRESSZEN_DIR .
          "classes" .
          DIRECTORY_SEPARATOR .
          "addresszen.php";
      }
    }

    /**
     * Add a new integration to WooCommerce.
     */
    public function add_integration($integrations)
    {
      $integrations[] = "WC_AddressZen_Integration";
      return $integrations;
    }
  }

  //Init plugin if woocommerce is installed
  $WC_AddressZen_Integration = new WC_AddressZen(__FILE__);
}
