<?php

/**
 * Plugin Name:          Vanquish Multiple Addresses for WooCommerce
 * Plugin URI:           https://wordpress.org/plugins/vanquish-multiple-addresses-for-woocommerce/
 * Description:          Let customers save and pick from multiple billing and shipping addresses at checkout and in My Account. Premium adds per-product shipping to different addresses, split packages, handling fees, CSV import and more.
 * Version:              1.0.1
 * Requires at least:    5.8
 * Requires PHP:         7.4
 * Requires Plugins:     woocommerce
 * Author:               Vanquish
 * Author URI:           https://vanquishplugins.com/
 * License:              GPL-2.0-or-later
 * License URI:          https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:          vanquish-multiple-addresses-for-woocommerce
 * Domain Path:          /languages
 * WC requires at least: 8.2
 * WC tested up to:      10.9
 *
 *
 * @package Vanquish\MultipleAddresses
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
    // Exit if accessed directly.
}
// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
define( 'VANMUAD_VERSION', '1.0.1' );
define( 'VANMUAD_FILE', __FILE__ );
define( 'VANMUAD_DIR', plugin_dir_path( __FILE__ ) );
define( 'VANMUAD_URL', plugin_dir_url( __FILE__ ) );
define( 'VANMUAD_BASENAME', plugin_basename( __FILE__ ) );
// ---------------------------------------------------------------------------
// Autoloading
// ---------------------------------------------------------------------------
// Composer autoloader (for third-party deps), if present.
if ( file_exists( VANMUAD_DIR . 'vendor/autoload.php' ) ) {
    require_once VANMUAD_DIR . 'vendor/autoload.php';
}
// Lightweight PSR-4 autoloader so the plugin runs even without a Composer install.
// Maps Vanquish\MultipleAddresses\ => includes/.
spl_autoload_register( function ( $class ) {
    $prefix = 'Vanquish\\MultipleAddresses\\';
    $len = strlen( $prefix );
    if ( strncmp( $prefix, $class, $len ) !== 0 ) {
        return;
    }
    $relative = substr( $class, $len );
    $path = VANMUAD_DIR . 'includes/' . str_replace( '\\', '/', $relative ) . '.php';
    if ( file_exists( $path ) ) {
        require_once $path;
    }
} );
// ---------------------------------------------------------------------------
// Freemius
// ---------------------------------------------------------------------------
// Credentials from the Freemius dashboard. They are empty until the product is
// created there; the SDK is then skipped entirely rather than initialised with
// placeholder values, which would break activation. To exercise the premium
// modules before that, define VANMUAD_DEV_PREMIUM in wp-config.php.
// The wp.org gatekeeper below is written INLINE on purpose, never as a
// constant. Freemius strips the line that passes it, so that it never ships
// in the free build — but it can only strip that one line. Behind a constant
// the define() survived, the secret went out in the free package, and
// wordpress.org's pre-commit hook rejected the whole commit (reporting it,
// confusingly, as "the Pro version of the Freemius SDK").
define( 'VANMUAD_FS_ID', '35324' );
define( 'VANMUAD_FS_PUBLIC_KEY', 'pk_236d80f8e6000d958b44be9d0860e' );
if ( !function_exists( 'vanmuad_fs' ) && '' !== VANMUAD_FS_ID && '' !== VANMUAD_FS_PUBLIC_KEY && file_exists( VANMUAD_DIR . 'vendor/freemius/start.php' ) ) {
    /**
     * Freemius SDK accessor.
     *
     * @return Freemius
     */
    function vanmuad_fs() {
        global $vanmuad_fs;
        if ( !isset( $vanmuad_fs ) ) {
            require_once VANMUAD_DIR . 'vendor/freemius/start.php';
            $vanmuad_fs = fs_dynamic_init( array(
                'id'               => VANMUAD_FS_ID,
                'slug'             => 'vanquish-multiple-addresses-for-woocommerce',
                'type'             => 'plugin',
                'public_key'       => VANMUAD_FS_PUBLIC_KEY,
                'is_premium'       => false,
                'premium_suffix'   => 'Premium',
                'has_addons'       => false,
                'has_paid_plans'   => true,
                'is_org_compliant' => true,
                'menu'             => array(
                    'slug'    => 'vanmuad-addresses',
                    'contact' => false,
                ),
                'is_live'          => true,
            ) );
        }
        return $vanmuad_fs;
    }

    vanmuad_fs();
    do_action( 'vanmuad_fs_loaded' );
    // Uninstall cleanup runs on Freemius' after_uninstall hook (the SDK owns the
    // uninstall flow, so there is no uninstall.php file).
    vanmuad_fs()->add_action( 'after_uninstall', 'vanmuad_uninstall_cleanup' );
    // Custom Freemius product icon (opt-in / account / pricing screens). The filter
    // wants a filesystem path, which Freemius turns into a URL. VANMUAD_DIR already
    // ends with a slash, so the path must NOT start with one. The icon ships in the
    // free build too, so it must stay out of the premium-only file list in the header.
    vanmuad_fs()->add_filter( 'plugin_icon', function () {
        return VANMUAD_DIR . 'assets/img/icon-plugin.png';
    } );
}
// ---------------------------------------------------------------------------
// HPOS (High-Performance Order Storage) compatibility
// ---------------------------------------------------------------------------
add_action( 'before_woocommerce_init', function () {
    if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
        \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', VANMUAD_FILE, true );
    }
} );
// ---------------------------------------------------------------------------
// Activation
// ---------------------------------------------------------------------------
register_activation_hook( __FILE__, 'vanmuad_activate' );
/**
 * On activation, flag the rewrite rules for a flush.
 *
 * The My Account "Address book" endpoint is registered on `init`, which has
 * already run by the time this hook fires, so flushing here would not pick it
 * up. Instead the flag is consumed on the next `init` (see
 * Frontend\MyAccountPage::add_endpoint), once the endpoint exists.
 */
function vanmuad_activate() {
    update_option( 'vanmuad_flush_rewrite', 1 );
}

// ---------------------------------------------------------------------------
// Bootstrap
// ---------------------------------------------------------------------------
add_action( 'plugins_loaded', 'vanmuad_bootstrap' );
/**
 * Boot the plugin once all plugins are loaded.
 */
function vanmuad_bootstrap() {
    if ( !class_exists( 'WooCommerce' ) ) {
        add_action( 'admin_notices', 'vanmuad_notice_missing_woocommerce' );
        return;
    }
    \Vanquish\MultipleAddresses\Plugin::instance()->run();
}

/**
 * Admin notice shown when WooCommerce is not active.
 */
function vanmuad_notice_missing_woocommerce() {
    echo '<div class="notice notice-error"><p>';
    echo esc_html__( 'Vanquish Multiple Addresses for WooCommerce requires WooCommerce to be installed and active.', 'vanquish-multiple-addresses-for-woocommerce' );
    echo '</p></div>';
}

/**
 * Remove the plugin's own leftovers on uninstall (Freemius after_uninstall hook).
 *
 * Nothing the merchant or their customers created is deleted here, and that is a
 * decision rather than an omission:
 *
 *   - The SETTINGS are kept. A configured shop can represent an afternoon of work
 *     — excluded categories, fee ranges, per-language labels — and uninstalling
 *     to reinstall, or to test something, is common. Losing all of it silently is
 *     not a reasonable price for that. "Reset settings" on the settings page is
 *     the deliberate way to clear them.
 *   - The saved ADDRESSES (user meta), and the legacy book the plugin may have
 *     converted from, belong to the customers. Deleting them would be
 *     unrecoverable, and they are the merchant's only copy if they ever go back
 *     to the old plugin.
 *
 * What does go is the plugin's own internal marker. There are no cron events to
 * clear, and the import batches are transients that expire within the hour.
 */
function vanmuad_uninstall_cleanup() {
    delete_option( 'vanmuad_flush_rewrite' );
}
