<?php

/**
 * Plugin Name:       StockBeacon - Product Waitlists for WooCommerce
 * Description:       Back-in-stock notifications for WooCommerce. Subscribe when out of stock, automatic restock emails.
 * Version:           1.0.0
 * Requires at least: 6.0
 * Requires Plugins: woocommerce
 * Tested up to:      7.0
 * Requires PHP:      8.0
 * Author:            WPVOID
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       stockbeacon-product-waitlists-for-woocommerce
 * Domain Path:       /languages
 * WC requires at least: 8.0
 * WC tested up to:   9.6
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
if ( function_exists( 'stockbeacon_fs' ) ) {
    stockbeacon_fs()->set_basename( false, __FILE__ );
} else {
    define( 'STOCKBEACON_VERSION', '1.0.0' );
    define( 'STOCKBEACON_FILE', __FILE__ );
    define( 'STOCKBEACON_PATH', plugin_dir_path( __FILE__ ) );
    define( 'STOCKBEACON_URL', plugin_dir_url( __FILE__ ) );
    define( 'STOCKBEACON_SLUG', 'stockbeacon-product-waitlists-for-woocommerce' );
    define( 'STOCKBEACON_MENU_SLUG', 'stockbeacon' );
    define( 'STOCKBEACON_PREMIUM_SLUG', 'stockbeacon-product-waitlists-for-woocommerce-pro' );
    if ( !function_exists( 'stockbeacon_fs' ) ) {
        function stockbeacon_fs() {
            global $stockbeacon_fs;
            if ( !isset( $stockbeacon_fs ) ) {
                require_once dirname( __FILE__ ) . '/vendor/freemius/start.php';
                $stockbeacon_fs = fs_dynamic_init( array(
                    'id'               => '32802',
                    'slug'             => STOCKBEACON_SLUG,
                    'premium_slug'     => STOCKBEACON_PREMIUM_SLUG,
                    'type'             => 'plugin',
                    'public_key'       => 'pk_27e06f467bab41f826414655acbb9',
                    'is_premium'       => false,
                    'premium_suffix'   => 'Pro',
                    'has_addons'       => false,
                    'has_paid_plans'   => true,
                    'is_org_compliant' => true,
                    'menu'             => array(
                        'slug'    => STOCKBEACON_MENU_SLUG,
                        'contact' => false,
                        'support' => false,
                        'parent'  => array(
                            'slug' => 'woocommerce',
                        ),
                    ),
                    'is_live'          => true,
                ) );
            }
            return $stockbeacon_fs;
        }

        stockbeacon_fs();
        do_action( 'stockbeacon_fs_loaded' );
    }
    /**
     * Resolve the best local icon path for Freemius filters.
     */
    function stockbeacon_resolve_icon_path(  string $fallback  ) : string {
        $icon_relative_path = 'assets/images/icon.png';
        if ( file_exists( STOCKBEACON_PATH . $icon_relative_path ) ) {
            return STOCKBEACON_PATH . $icon_relative_path;
        }
        $legacy_icon_relative_path = 'icon.png';
        if ( file_exists( STOCKBEACON_PATH . $legacy_icon_relative_path ) ) {
            return STOCKBEACON_PATH . $legacy_icon_relative_path;
        }
        return $fallback;
    }

    /**
     * A paid license in the free WordPress.org package must not unlock behavior.
     * Pro features are only available when the paid package's files are present.
     */
    function stockbeacon_has_premium_package() : bool {
        return class_exists( '\\WPVOID\\StockBeacon\\Analytics\\AnalyticsManager' ) && class_exists( '\\WPVOID\\StockBeacon\\Api\\AnalyticsEndpoints' ) && class_exists( '\\WPVOID\\StockBeacon\\Analytics\\ConversionTracker' ) && class_exists( '\\WPVOID\\StockBeacon\\Frontend\\Shortcode' ) && class_exists( '\\WPVOID\\StockBeacon\\Frontend\\Block' );
    }

    add_filter( 'fs_plugin_icon_' . STOCKBEACON_SLUG, static function ( $icon ) {
        return stockbeacon_resolve_icon_path( (string) $icon );
    } );
    add_filter( 'fs_plugin_icon_' . STOCKBEACON_PREMIUM_SLUG, static function ( $icon ) {
        return stockbeacon_resolve_icon_path( (string) $icon );
    } );
    require_once STOCKBEACON_PATH . 'includes/class-settings.php';
    require_once STOCKBEACON_PATH . 'includes/class-activator.php';
    require_once STOCKBEACON_PATH . 'includes/class-deactivator.php';
    require_once STOCKBEACON_PATH . 'includes/class-plugin.php';
    add_action( 'before_woocommerce_init', static function () {
        if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
            \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
        }
    } );
    register_activation_hook( __FILE__, array(\WPVOID\StockBeacon\Activator::class, 'activate') );
    register_deactivation_hook( __FILE__, array(\WPVOID\StockBeacon\Deactivator::class, 'deactivate') );
    add_action( 'admin_notices', static function () {
        if ( version_compare( PHP_VERSION, '8.0.0', '<' ) ) {
            echo '<div class="notice notice-error"><p>' . esc_html__( 'StockBeacon - Product Waitlists for WooCommerce requires PHP 8.0 or newer.', 'stockbeacon-product-waitlists-for-woocommerce' ) . '</p></div>';
            return;
        }
        if ( !class_exists( 'WooCommerce', false ) ) {
            echo '<div class="notice notice-error"><p>' . esc_html__( 'StockBeacon - Product Waitlists for WooCommerce requires WooCommerce to be active.', 'stockbeacon-product-waitlists-for-woocommerce' ) . '</p></div>';
            return;
        }
    } );
    add_action( 'init', static function () {
        if ( version_compare( PHP_VERSION, '8.0.0', '<' ) || !class_exists( 'WooCommerce', false ) ) {
            return;
        }
        ( new \WPVOID\StockBeacon\Plugin() )->run();
    }, 5 );
    stockbeacon_fs()->add_action( 'after_uninstall', '\\WPVOID\\StockBeacon\\Activator::uninstall_cleanup' );
}