<?php

/**
 * Plugin Name: Delivery Date & Timeslot for WooCommerce
 * Plugin URI:  https://dreamfoxmedia.com/plugins/delivery-date-for-woocommerce/
 * Description:     Schedule WooCommerce delivery dates and timeslots at checkout. Set lead times, slot capacity, and delivery windows — full slots hide automatically.
 * Version:     3.0.1
 * Requires at least: 5.0
 * Tested up to: 7.0
 * Requires PHP: 7.4
 * WC requires at least: 6.0.0
 * WC tested up to: 10.7
 * Author:      Dreamfox Media
 * Author URI:  https://dreamfoxmedia.com
 * License:     GPLv3 or later
 * License URI: https://www.gnu.org/licenses/gpl-3.0.html
 * Text Domain: woocommerce-delivery-date
 * Domain Path: /languages
 * Requires Plugins: woocommerce
 *
 * NOTE (wp.org FREE build):
 * - Freemius org-compliance is enabled (is_org_compliant => true).
 * - is_premium is false; no locked/premium functionality is shipped in this zip.
 * - Premium code must NOT be bundled in the .org release. If you keep premium
 *   loaders in bootstrap.php, guard them with my_fs()->is__premium_only().
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
/**
 * Basic plugin constants.
 */
if ( !defined( 'DREAMFOX_WDD_PLUGIN_FILE' ) ) {
    define( 'DREAMFOX_WDD_PLUGIN_FILE', __FILE__ );
}
if ( !defined( 'DREAMFOX_WDD_PLUGIN_DIR' ) ) {
    define( 'DREAMFOX_WDD_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
}
if ( !defined( 'DREAMFOX_WDD_PLUGIN_URL' ) ) {
    define( 'DREAMFOX_WDD_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
}
if ( !defined( 'DREAMFOX_WDD_VERSION' ) ) {
    define( 'DREAMFOX_WDD_VERSION', '2.5.1' );
}
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 );
    }
} );
/**
 * Load textdomain.
 */
add_action( 'init', function () {
    load_plugin_textdomain( 'woocommerce-delivery-date', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
} );
/**
 * Resilient Freemius accessor:
 * - If your project exposes a Freemius function, return it (e.g., dreamfox_delivery_date()).
 * - Otherwise return a safe stub so calls like my_fs()->is__premium_only() won't fatally error.
 */
if ( !function_exists( 'my_fs' ) ) {
    function my_fs() {
        // If your Freemius accessor is a different one in other builds, check them here:
        if ( function_exists( 'dfm_sgppfw_fs' ) ) {
            return dfm_sgppfw_fs();
        }
        if ( function_exists( 'dreamfox_delivery_date' ) ) {
            return dreamfox_delivery_date();
        }
        static $stub = null;
        if ( !$stub ) {
            $stub = new class {
                public function add_filter() {
                    return null;
                }

                public function __call( $name, $args ) {
                    return null;
                }

                public function set_basename() {
                    return null;
                }

            }
;
        }
        return $stub;
    }

}
/**
 * If Freemius is already loaded elsewhere, sync the basename on premium builds.
 * (Harmless on free builds since is__premium_only() returns false.)
 */
if ( function_exists( 'dfm_sgppfw_fs' ) || function_exists( 'dreamfox_delivery_date' ) ) {
}
/**
 * Initialize Freemius ONLY if our accessor doesn’t exist.
 * This block is .org compliant:
 * - is_premium          => false
 * - has_premium_version => true (you may sell elsewhere)
 * - has_paid_plans      => true
 * - is_org_compliant    => true (critical)
 */
if ( !function_exists( 'dreamfox_delivery_date' ) ) {
    function dreamfox_delivery_date() {
        global $dreamfox_delivery_date;
        if ( !isset( $dreamfox_delivery_date ) ) {
            // Enable multisite support for the product.
            if ( !defined( 'WP_FS__PRODUCT_10651_MULTISITE' ) ) {
                define( 'WP_FS__PRODUCT_10651_MULTISITE', true );
            }
            // Include Freemius SDK (shipped by Freemius).
            require_once dirname( __FILE__ ) . '/freemius/start.php';
            $dreamfox_delivery_date = fs_dynamic_init( array(
                'id'               => '10651',
                'slug'             => 'dreamfox-deliverydate',
                'premium_slug'     => 'dreamfox-deliverydate-premium',
                'type'             => 'plugin',
                'public_key'       => 'pk_cbd84d7abd43d4e1d2c4b5850c495',
                'is_premium'       => false,
                'has_paid_plans'   => true,
                'is_org_compliant' => true,
                'menu'             => array(
                    'slug'       => 'woocommerce-delivery-date',
                    'first-path' => 'admin.php?page=woocommerce-delivery-date',
                    'support'    => false,
                    'network'    => true,
                    'parent'     => array(
                        'slug' => 'woocommerce',
                    ),
                ),
                'is_live'          => true,
            ) );
        }
        return $dreamfox_delivery_date;
    }

    // Init Freemius (available to my_fs()) and fire a loaded action.
    dreamfox_delivery_date();
    do_action( 'dreamfox_delivery_date_loaded' );
}
/**
 * === FREE vs PREMIUM Freemius UI filters ===
 * Free   : hide account/contact/pricing menus, enable anonymous, disable branding.
 * Premium: opposite behavior (has no effect in this .org free build because is__premium_only() returns false).
 */
add_action( 'plugins_loaded', function () {
    $fs = my_fs();
    if ( !$fs || !method_exists( $fs, 'add_filter' ) ) {
        return;
    }
    // FREE
    $fs->add_filter( 'show_account', '__return_false' );
    $fs->add_filter( 'show_contact', '__return_false' );
    $fs->add_filter( 'show_pricing', '__return_false' );
    $fs->add_filter( 'is_submenu_visible', '__return_false' );
    $fs->add_filter( 'enable_anonymous', '__return_true' );
    $fs->add_filter( 'disable_freemius_branding', '__return_true' );
}, 20 );
/**
 * Main loader — only run if WooCommerce is active.
 * IMPORTANT for wp.org: Do NOT load or reference premium-only files here.
 * If bootstrap.php internally references premium features, those references
 * must be guarded with `if ( my_fs()->is__premium_only() ) { ... }`.
 */
add_action( 'plugins_loaded', function () {
    // Safe WooCommerce check that works in most contexts.
    $active_plugins = (array) apply_filters( 'active_plugins', get_option( 'active_plugins', array() ) );
    if ( is_multisite() ) {
        $network_actives = array_keys( (array) get_site_option( 'active_sitewide_plugins', array() ) );
        $active_plugins = array_merge( $active_plugins, $network_actives );
    }
    $wc_active = in_array( 'woocommerce/woocommerce.php', $active_plugins, true ) || function_exists( 'is_plugin_active' ) && is_plugin_active( 'woocommerce/woocommerce.php' );
    if ( $wc_active ) {
        // Load your core plugin code (free features only).
        $bootstrap = DREAMFOX_WDD_PLUGIN_DIR . 'bootstrap.php';
        if ( file_exists( $bootstrap ) ) {
            include $bootstrap;
        }
    }
}, 30 );