' . esc_html__("Settings", "addresszen") . ""; 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"]); add_action("woocommerce_blocks_loaded", [$this, "init_blocks"]); // Ensure scripts are enqueued early - Integration may load too late add_action("wp_enqueue_scripts", [$this, "maybe_load_scripts"]); } /** * Load frontend scripts if enabled. */ public function maybe_load_scripts() { $settings = get_option("woocommerce_addresszen_settings", []); if (isset($settings["addresszen_enabled"]) && $settings["addresszen_enabled"] === "yes") { // Check if this is blocks checkout - skip if so (handled by blocks integration) if (function_exists('has_block') && function_exists('wc_get_page_id')) { $checkout_page_id = wc_get_page_id('checkout'); if ($checkout_page_id && is_page($checkout_page_id)) { $post = get_post($checkout_page_id); if ($post && has_block('woocommerce/checkout', $post)) { return; // Skip - blocks integration handles this } } } // Enqueue styles wp_enqueue_style( "addresszen-autocomplete-style", ADDRESSZEN_URL . "css/addresszen.css", [], ADDRESSZEN_VERSION ); // Enqueue scripts with config $config = $this->get_frontend_config($settings); wp_enqueue_script( "addresszen-bindings", ADDRESSZEN_URL . "js/woocommerce.min.js", [], ADDRESSZEN_VERSION, true ); wp_add_inline_script("addresszen-bindings", "window.idpcConfig = " . wp_json_encode($config) . ";", "before"); } } /** * Get frontend config from settings. */ private function get_frontend_config($settings) { $to_bool = function($val) { return $val === "yes"; }; return [ "enabled" => $to_bool($settings["addresszen_enabled"] ?? ""), "publicKey" => $settings["addresszen_api_key"] ?? "", "apiKey" => $settings["addresszen_api_key"] ?? "", "autocomplete" => $to_bool($settings["addresszen_autocomplete"] ?? ""), "watchCountry" => $to_bool($settings["addresszen_watch_country"] ?? ""), "separateFinder" => $to_bool($settings["addresszen_separate_finder"] ?? ""), "hideAddressFields" => $to_bool($settings["addresszen_hide_address_fields"] ?? ""), "allowManualEntry" => $to_bool($settings["addresszen_allow_manual_entry"] ?? ""), "removeOrganisation" => $to_bool($settings["addresszen_populate_organisation"] ?? ""), "populateCounty" => $to_bool($settings["addresszen_populate_county"] ?? ""), "autocompleteOverride" => json_decode($settings["addresszen_autocomplete_override"] ?? "{}", true) ?: [], "usZip5Only" => $to_bool($settings["addresszen_us_zip5_only"] ?? "yes"), ]; } /** * 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; } /** * Initialize WooCommerce Blocks integration. */ public function init_blocks() { require_once ADDRESSZEN_DIR . "classes" . DIRECTORY_SEPARATOR . "class-addresszen-blocks.php"; WC_AddressZen_Blocks::init(); } } //Init plugin if woocommerce is installed $WC_AddressZen_Integration = new WC_AddressZen(__FILE__); }