<?php

/**
 * Plugin Name:       Datery – Delivery Date for WooCommerce
 * Plugin URI:        https://easydeliverydate.com
 * Description:       Let customers pick a delivery date at checkout. Lightweight, locale-safe, no jQuery UI. The date is saved to the order and shown in admin, emails and My Account.
 * Version:           1.0.1
 * Author:            Le Huy Dat
 * Author URI:        https://easydeliverydate.com/about
 * Text Domain:       datery-delivery-date-for-woocommerce
 * Domain Path:       /languages
 * Requires at least: 5.8
 * Requires PHP:      7.2
 * Requires Plugins:  woocommerce
 * WC requires at least: 6.0
 * WC tested up to:   10.8
 * License:           GPL-2.0-or-later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 *
 * @package Datery
 */
defined( 'ABSPATH' ) || exit;
define( 'DATERY_VERSION', '1.0.1' );
define( 'DATERY_FILE', __FILE__ );
define( 'DATERY_PATH', plugin_dir_path( __FILE__ ) );
define( 'DATERY_URL', plugin_dir_url( __FILE__ ) );
define( 'DATERY_META_KEY', '_datery_delivery_date' );
/**
 * Load private Freemius deployment keys.
 *
 * `freemius-keys.php` is gitignored and defines the secret `DATERY_FS_GATEKEEPER`
 * used only when Freemius generates the wp.org build. The plugin runs fine without
 * it (e.g. on a fresh clone), so its absence is intentionally non-fatal.
 * See `freemius-keys.sample.php` for the template.
 */
if ( file_exists( DATERY_PATH . 'freemius-keys.php' ) ) {
    require_once DATERY_PATH . 'freemius-keys.php';
}
/**
 * Freemius SDK bootstrap.
 *
 * Handles licensing, secure checkout, automatic updates and the free/premium
 * code separation. The premium build keeps `is_premium => true`; Freemius'
 * deployment automatically generates the free build with it flipped to false.
 */
if ( function_exists( 'datery_fs' ) ) {
    // The Pro build already loaded the SDK; point it at this file so Freemius can
    // auto-deactivate the free version when the Pro version is activated.
    datery_fs()->set_basename( false, __FILE__ );
} else {
    // DO NOT REMOVE THIS INNER IF — it is essential for the function_exists()
    // check above to work with Freemius' free/premium auto-deactivation.
    if ( !function_exists( 'datery_fs' ) ) {
        // Create a helper function for easy SDK access.
        function datery_fs() {
            global $datery_fs;
            if ( !isset( $datery_fs ) ) {
                // Include Freemius SDK.
                require_once DATERY_PATH . 'vendor/freemius/start.php';
                $datery_fs = fs_dynamic_init( array(
                    'id'               => '32336',
                    'slug'             => 'datery-delivery-date-for-woocommerce',
                    'premium_slug'     => 'easy-delivery-date-for-woocommerce-premium',
                    'type'             => 'plugin',
                    'public_key'       => 'pk_831af7240947eba27bae44b6c7b3b',
                    'is_premium'       => false,
                    'premium_suffix'   => 'Pro',
                    'has_addons'       => false,
                    'has_paid_plans'   => true,
                    'is_org_compliant' => true,
                    'menu'             => array(
                        'support' => false,
                    ),
                    'is_live'          => true,
                ) );
            }
            return $datery_fs;
        }

    }
    // Init Freemius.
    datery_fs();
    // Signal that SDK was initiated.
    do_action( 'datery_fs_loaded' );
}
/**
 * Declare HPOS (High-Performance Order Storage) compatibility.
 * Without this, WooCommerce flags the plugin as incompatible on modern stores.
 */
add_action( 'before_woocommerce_init', function () {
    if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
        \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', DATERY_FILE, true );
    }
} );
/**
 * Boot the plugin once all plugins are loaded so we can safely check for WooCommerce.
 */
add_action( 'plugins_loaded', 'datery_bootstrap' );
function datery_bootstrap() {
    // Translations load automatically: WordPress.org serves them by slug, and core
    // just-in-time loading handles bundled .mo files, so no load_plugin_textdomain() call is needed.
    // Hard guard: do nothing (and never fatal) if WooCommerce is missing.
    if ( !class_exists( 'WooCommerce' ) ) {
        add_action( 'admin_notices', 'datery_missing_wc_notice' );
        return;
    }
    require_once DATERY_PATH . 'includes/class-datery-settings.php';
    require_once DATERY_PATH . 'includes/class-datery-dates.php';
    require_once DATERY_PATH . 'includes/class-datery-checkout.php';
    require_once DATERY_PATH . 'includes/class-datery-order.php';
    require_once DATERY_PATH . 'includes/class-datery-admin.php';
    require_once DATERY_PATH . 'includes/class-datery-blocks.php';
    Datery_Settings::instance();
    Datery_Checkout::instance();
    Datery_Order::instance();
    Datery_Admin::instance();
    Datery_Blocks::instance();
    // Pro layer. Loaded only when the customer has an active Pro license.
    // Freemius strips the loader (its name ends in __premium_only) and the whole
    // includes/pro__premium_only/ folder out of the generated free build.
    if ( datery_fs()->can_use_premium_code() ) {
        datery_load_pro__premium_only();
    }
}

function datery_missing_wc_notice() {
    echo '<div class="notice notice-error"><p>';
    echo esc_html__( 'Datery requires WooCommerce to be installed and active.', 'datery-delivery-date-for-woocommerce' );
    echo '</p></div>';
}

/**
 * Set sane defaults on activation.
 */
register_activation_hook( __FILE__, function () {
    $defaults = array(
        'field_label'   => __( 'Delivery date', 'datery-delivery-date-for-woocommerce' ),
        'required'      => 'yes',
        'min_lead_days' => 1,
        'max_days'      => 30,
        'cutoff_time'   => '',
    );
    if ( false === get_option( 'datery_settings' ) ) {
        add_option( 'datery_settings', $defaults );
    }
} );